From 84f45727e7a78c385b7bc5d16fea5a46125b0b76 Mon Sep 17 00:00:00 2001 From: Emil Muratov Date: Sat, 27 Jan 2024 17:58:40 +0900 Subject: [PATCH] rollback time handling code style from c++ to C and 8266 --- src/WebHandlerImpl.h | 3 +-- src/WebHandlers.cpp | 17 ++++++++++------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/WebHandlerImpl.h b/src/WebHandlerImpl.h index 7114bfdcc..9b7ba1b04 100644 --- a/src/WebHandlerImpl.h +++ b/src/WebHandlerImpl.h @@ -28,7 +28,6 @@ #include "stddef.h" #include -#include class AsyncStaticWebHandler: public AsyncWebHandler { using File = fs::File; @@ -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 diff --git a/src/WebHandlers.cpp b/src/WebHandlers.cpp index 7d28ddcc1..0b55b0ddf 100644 --- a/src/WebHandlers.cpp +++ b/src/WebHandlers.cpp @@ -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(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 @@ -195,7 +198,7 @@ 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())) @@ -203,7 +206,7 @@ void AsyncStaticWebHandler::handleRequest(AsyncWebServerRequest *request) 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();