Skip to content

Commit 19e87b1

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 19e87b1

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
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: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,15 +199,34 @@ 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+
<<<<<<< HEAD
209210
String etag = String(request->_tempFile.size());
210211
if (_last_modified.length() && _last_modified == request->header(F("If-Modified-Since"))) {
212+
=======
213+
time_t lw = request->_tempFile.getLastWrite(); // get last file mod time (if supported by FS)
214+
if (lw) {
215+
_last_modified.clear();
216+
_last_modified.reserve(30); // need 'Fri, 27 Jan 2023 15:50:27 GMT'
217+
char *t = ctime(&lw); // ctime 'Thu Jan 26 17:42:48 2023'
218+
_last_modified.concat(t, 3); // day of week
219+
_last_modified.concat((char)0x2c); // comma
220+
_last_modified.concat(t+7, 3); // day
221+
_last_modified.concat(t+3, 4); // month
222+
_last_modified.concat(t+19, 5); // year
223+
_last_modified.concat(t+10, 9); // time
224+
_last_modified.concat(" GMT");
225+
_last_modified.setCharAt(29, 0); // null terminate
226+
}
227+
String etag(request->_tempFile.size());
228+
if (_last_modified.length() && _last_modified == request->header("If-Modified-Since")) {
229+
>>>>>>> 68b21d2 (set real "Last-Modified" header based on file's LastWrite time)
211230
request->_tempFile.close();
212231
request->send(304); // Not modified
213232
} else if (_cache_control.length() && request->hasHeader(F("If-None-Match")) && request->header(F("If-None-Match")).equals(etag)) {

0 commit comments

Comments
 (0)