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 committed Jan 27, 2024
1 parent 3355186 commit 43c5f55
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 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
15 changes: 9 additions & 6 deletions src/WebHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,13 @@ AsyncStaticWebHandler& AsyncStaticWebHandler::setLastModified(const char* last_m
}

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));
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 43c5f55

Please sign in to comment.