Skip to content

Commit ebde6d7

Browse files
committed
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". Signed-off-by: Emil Muratov <[email protected]>
1 parent 2d7fdbb commit ebde6d7

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -876,8 +876,11 @@ handler->setCacheControl("max-age=30");
876876
```
877877
878878
### Specifying Date-Modified header
879-
It is possible to specify Date-Modified header to enable the server to return Not-Modified (304) response for requests
880-
with "If-Modified-Since" header with the same value, instead of responding with the actual file content.
879+
Sever sets "Last-Modified" header automatically if FS driver supports file modification timestamps (LittleFS does).
880+
Server returns "Not-Modified" (304) response for requests with "If-Modified-Since" header with _the same_ value as file's mod date, instead of responding with the actual file content. It does not perform date calculations checking if File's mod date is newer or later than in "If-Modified-Since" header.
881+
882+
For FS not supporting file timestamps (like deprecated SPIFFS) it is possible to specify Date-Modified header manually.
883+
881884
```cpp
882885
// Update the date modified string every time files are updated
883886
server.serveStatic("/", SPIFFS, "/www/").setLastModified("Mon, 20 Jun 2016 14:00:00 GMT");

src/WebHandlers.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,15 +199,29 @@ uint8_t AsyncStaticWebHandler::_countBits(const uint8_t value) const
199199
void AsyncStaticWebHandler::handleRequest(AsyncWebServerRequest *request)
200200
{
201201
// Get the filename from request->_tempObject and free it
202-
String filename = String((char*)request->_tempObject);
202+
String filename((char*)request->_tempObject);
203203
free(request->_tempObject);
204204
request->_tempObject = NULL;
205205
if((_username.length() && _password.length()) && !request->authenticate(_username.c_str(), _password.c_str()))
206206
return request->requestAuthentication();
207207

208208
if (request->_tempFile == true) {
209-
String etag = String(request->_tempFile.size());
210-
if (_last_modified.length() && _last_modified == request->header(F("If-Modified-Since"))) {
209+
time_t lw = request->_tempFile.getLastWrite(); // get last file mod time (if supported by FS)
210+
if (lw) {
211+
_last_modified.clear();
212+
_last_modified.reserve(30); // need 'Fri, 27 Jan 2023 15:50:27 GMT'
213+
char *t = ctime(&lw); // ctime 'Thu Jan 26 17:42:48 2023'
214+
_last_modified.concat(t, 3); // day of week
215+
_last_modified.concat((char)0x2c); // comma
216+
_last_modified.concat(t+7, 3); // day
217+
_last_modified.concat(t+3, 4); // month
218+
_last_modified.concat(t+19, 5); // year
219+
_last_modified.concat(t+10, 9); // time
220+
_last_modified.concat(" GMT");
221+
_last_modified.setCharAt(29, 0); // null terminate
222+
}
223+
String etag(request->_tempFile.size());
224+
if (_last_modified.length() && _last_modified == request->header("If-Modified-Since")) {
211225
request->_tempFile.close();
212226
request->send(304); // Not modified
213227
} else if (_cache_control.length() && request->hasHeader(F("If-None-Match")) && request->header(F("If-None-Match")).equals(etag)) {

0 commit comments

Comments
 (0)