Skip to content
This repository was archived by the owner on Jan 21, 2025. It is now read-only.

Commit 52c0db9

Browse files
vortigontmathieucarbou
authored andcommitted
set real "Last-Modified" header based on file's LastWrite time
Get file's LastWrite timestamp for file handlers (if supported by FS driver) and construct proper "Last-Modified" header. Works fine for LittleFS. If not supported by FS than fallback to previous implementation with manual value for "Last-Modified". an example code to serve static files from LittleFS with IMS revalidation ``` // serve all static files from LittleFS root / server.serveStatic("/", LittleFS, "/") .setDefaultFile("index.html") .setCacheControl("must-revalidate"); // revalidate based on etag/IMS headers ``` Signed-off-by: Emil Muratov <[email protected]>
1 parent 4873863 commit 52c0db9

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

src/WebHandlerImpl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include "stddef.h"
3030
#include <time.h>
31+
#include <ctime>
3132

3233
class AsyncStaticWebHandler: public AsyncWebHandler {
3334
using File = fs::File;
@@ -55,7 +56,7 @@ class AsyncStaticWebHandler: public AsyncWebHandler {
5556
AsyncStaticWebHandler& setDefaultFile(const char* filename);
5657
AsyncStaticWebHandler& setCacheControl(const char* cache_control);
5758
AsyncStaticWebHandler& setLastModified(const char* last_modified);
58-
AsyncStaticWebHandler& setLastModified(struct tm* last_modified);
59+
AsyncStaticWebHandler& setLastModified(const std::tm* last_modified);
5960
#ifdef ESP8266
6061
AsyncStaticWebHandler& setLastModified(time_t last_modified);
6162
AsyncStaticWebHandler& setLastModified(); //sets to current time. Make sure sntp is runing and time is updated

src/WebHandlers.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,15 @@ AsyncStaticWebHandler& AsyncStaticWebHandler::setCacheControl(const char* cache_
5858
}
5959

6060
AsyncStaticWebHandler& AsyncStaticWebHandler::setLastModified(const char* last_modified){
61-
_last_modified = String(last_modified);
61+
_last_modified = last_modified;
6262
return *this;
6363
}
6464

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

7572
#ifdef ESP8266
@@ -198,14 +195,16 @@ uint8_t AsyncStaticWebHandler::_countBits(const uint8_t value) const
198195
void AsyncStaticWebHandler::handleRequest(AsyncWebServerRequest *request)
199196
{
200197
// Get the filename from request->_tempObject and free it
201-
String filename = String((char*)request->_tempObject);
198+
String filename((char*)request->_tempObject);
202199
free(request->_tempObject);
203200
request->_tempObject = NULL;
204201
if((_username.length() && _password.length()) && !request->authenticate(_username.c_str(), _password.c_str()))
205202
return request->requestAuthentication();
206203

207204
if (request->_tempFile == true) {
208-
String etag = String(request->_tempFile.size());
205+
time_t lw = request->_tempFile.getLastWrite(); // get last file mod time (if supported by FS)
206+
if (lw) setLastModified(std::gmtime(&lw));
207+
String etag(lw ? lw : request->_tempFile.size()); // set etag to lastmod timestamp if available, otherwise to size
209208
if (_last_modified.length() && _last_modified == request->header(F("If-Modified-Since"))) {
210209
request->_tempFile.close();
211210
request->send(304); // Not modified

0 commit comments

Comments
 (0)