Skip to content

Commit

Permalink
rollback time handling code style from c++ to C and 8266
Browse files Browse the repository at this point in the history
  • Loading branch information
vortigont authored and mathieucarbou committed Jan 28, 2024
1 parent 52c0db9 commit 84f4572
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
3 changes: 1 addition & 2 deletions src/WebHandlerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

#include "stddef.h"
#include <time.h>
#include <ctime>

class AsyncStaticWebHandler: public AsyncWebHandler {
using File = fs::File;
Expand Down Expand Up @@ -56,7 +55,7 @@ class AsyncStaticWebHandler: public AsyncWebHandler {
AsyncStaticWebHandler& setDefaultFile(const char* filename);
AsyncStaticWebHandler& setCacheControl(const char* cache_control);
AsyncStaticWebHandler& setLastModified(const char* last_modified);
AsyncStaticWebHandler& setLastModified(const std::tm* last_modified);
AsyncStaticWebHandler& setLastModified(struct tm* last_modified);
#ifdef ESP8266
AsyncStaticWebHandler& setLastModified(time_t last_modified);
AsyncStaticWebHandler& setLastModified(); //sets to current time. Make sure sntp is runing and time is updated
Expand Down
17 changes: 10 additions & 7 deletions src/WebHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,14 @@ AsyncStaticWebHandler& AsyncStaticWebHandler::setLastModified(const char* last_m
return *this;
}

AsyncStaticWebHandler& AsyncStaticWebHandler::setLastModified(const std::tm* last_modified){
constexpr size_t buffsize = sizeof("Fri, 27 Jan 2023 15:50:27 GMT"); // a format for LM header
char result[buffsize];
std::strftime(result, buffsize, "%a, %d %b %Y %H:%M:%S GMT", last_modified);
return setLastModified(static_cast<const char *>(result));
AsyncStaticWebHandler& AsyncStaticWebHandler::setLastModified(struct tm* last_modified){
auto formatP = PSTR("%a, %d %b %Y %H:%M:%S %Z");
char format[strlen_P(formatP) + 1];
strcpy_P(format, formatP);

char result[30];
strftime(result, sizeof(result), format, last_modified);
return setLastModified((const char *)result);
}

#ifdef ESP8266
Expand Down Expand Up @@ -195,15 +198,15 @@ uint8_t AsyncStaticWebHandler::_countBits(const uint8_t value) const
void AsyncStaticWebHandler::handleRequest(AsyncWebServerRequest *request)
{
// Get the filename from request->_tempObject and free it
String filename((char*)request->_tempObject);
String filename = String((char*)request->_tempObject);
free(request->_tempObject);
request->_tempObject = NULL;
if((_username.length() && _password.length()) && !request->authenticate(_username.c_str(), _password.c_str()))
return request->requestAuthentication();

if (request->_tempFile == true) {
time_t lw = request->_tempFile.getLastWrite(); // get last file mod time (if supported by FS)
if (lw) setLastModified(std::gmtime(&lw));
if (lw) setLastModified(gmtime(&lw));
String etag(lw ? lw : request->_tempFile.size()); // set etag to lastmod timestamp if available, otherwise to size
if (_last_modified.length() && _last_modified == request->header(F("If-Modified-Since"))) {
request->_tempFile.close();
Expand Down

0 comments on commit 84f4572

Please sign in to comment.