[PATCH 2/4] httpd: send ETag header instead of Last-Modified

Sergey Ponomarev stokito at gmail.com
Thu Jul 9 22:42:17 UTC 2020


The Last-Modified header is used for caching. The client (browser) will send back the received date to server via If-Modified-Since request header.
But both headers MUST be an RFC 1123 formatted string.
And the formatting consumes resources on request parsing and response generation.
Instead the last mod date can be passed via ETag header as a raw unix timestamp.
This simplifies logic and the only downside is that in JavaScript the document.lastModified will return null which is not a big deal.

Signed-off-by: Sergey Ponomarev <stokito at gmail.com>
---
 networking/httpd.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)

diff --git a/networking/httpd.c b/networking/httpd.c
index ff25b4c5b..97b61fb77 100644
--- a/networking/httpd.c
+++ b/networking/httpd.c
@@ -1038,11 +1038,6 @@ static void log_and_exit(void)
  */
 static void send_headers(unsigned responseNum)
 {
-	static const char RFC1123FMT[] ALIGN1 = "%a, %d %b %Y %H:%M:%S GMT";
-	/* Fixed size 29-byte string. Example: Sun, 06 Nov 1994 08:49:37 GMT */
-	char date_str[40]; /* using a bit larger buffer to paranoia reasons */
-
-	struct tm tm;
 	const char *responseString = "";
 	const char *infoString = NULL;
 #if ENABLE_FEATURE_HTTPD_ERROR_PAGES
@@ -1127,7 +1122,6 @@ static void send_headers(unsigned responseNum)
 #endif
 
 	if (file_size != -1) {    /* file */
-		strftime(date_str, sizeof(date_str), RFC1123FMT, gmtime_r(&last_mod, &tm));
 #if ENABLE_FEATURE_HTTPD_RANGES
 		if (responseNum == HTTP_PARTIAL_CONTENT) {
 			len += sprintf(iobuf + len,
@@ -1172,7 +1166,8 @@ static void send_headers(unsigned responseNum)
 #if ENABLE_FEATURE_HTTPD_RANGES
 			"Accept-Ranges: bytes\r\n"
 #endif
-			"Last-Modified: %s\r\n"
+			/* Instead of Last-Modified formatted date send it as a plain unix timestamp via ETag */
+			"ETag: %ld\r\n"
 	/* Because of 4.4 (5), we can forgo sending of "Content-Length"
 	 * since we close connection afterwards, but it helps clients
 	 * to e.g. estimate download times, show progress bars etc.
@@ -1180,7 +1175,7 @@ static void send_headers(unsigned responseNum)
 	 * but de-facto standard is to send it (see comment below).
 	 */
 			"Content-Length: %"OFF_FMT"u\r\n",
-				date_str,
+				last_mod,
 				file_size
 		);
 	}
-- 
2.25.1



More information about the busybox mailing list