[PATCH 3/4] httpd: Don't add Last-Modified header to response

Sergey Ponomarev stokito at gmail.com
Sat Aug 8 22:23:33 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 we can use ETag header.
This simplifies logic and the only downside is that in JavaScript the document.lastModified will return null.

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

diff --git a/networking/httpd.c b/networking/httpd.c
index 7a429d2b5..1cea33ddd 100644
--- a/networking/httpd.c
+++ b/networking/httpd.c
@@ -215,6 +215,16 @@
 //config:	Makes httpd send files using GZIP content encoding if the
 //config:	client supports it and a pre-compressed <file>.gz exists.
 //config:
+//config:config FEATURE_HTTPD_LAST_MODIFIED
+//config:	bool "Add Last-Modified header to response"
+//config:	default y
+//config:	depends on HTTPD
+//config:	help
+//config:	The Last-Modified header is used for cache validation.
+//config:	The client will send back the received date to server via If-Modified-Since.
+//config:	But both headers MUST be an RFC 1123 formatted string and it's hard to parse.
+//config:	Use ETag header instead.
+//config:
 //config:config FEATURE_HTTPD_DATE
 //config:	bool "Add Date header to response"
 //config:	default y
@@ -1046,11 +1056,14 @@ static void log_and_exit(void)
  */
 static void send_headers(unsigned responseNum)
 {
+#if ENABLE_FEATURE_HTTPD_DATE || ENABLE_FEATURE_HTTPD_LAST_MODIFIED
 	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;
+	time_t timer = time(NULL);
+#endif
 	const char *responseString = "";
 	const char *infoString = NULL;
 #if ENABLE_FEATURE_HTTPD_ERROR_PAGES
@@ -1058,7 +1071,6 @@ static void send_headers(unsigned responseNum)
 #endif
 	unsigned len;
 	unsigned i;
-	time_t timer = time(NULL);
 
 	for (i = 0; i < ARRAY_SIZE(http_response_type); i++) {
 		if (http_response_type[i] == responseNum) {
@@ -1141,7 +1153,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,
@@ -1186,7 +1197,6 @@ static void send_headers(unsigned responseNum)
 #if ENABLE_FEATURE_HTTPD_RANGES
 			"Accept-Ranges: bytes\r\n"
 #endif
-			"Last-Modified: %s\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.
@@ -1194,9 +1204,13 @@ 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,
 				file_size
 		);
+
+#if ENABLE_FEATURE_HTTPD_DATE || ENABLE_FEATURE_HTTPD_LAST_MODIFIED
+		strftime(date_str, sizeof(date_str), RFC1123FMT, gmtime_r(&last_mod, &tm));
+		len += sprintf(iobuf + len, "Last-Modified: %s\r\n", date_str);
+#endif
 	}
 
 	/* This should be "Transfer-Encoding", not "Content-Encoding":
-- 
2.25.1



More information about the busybox mailing list