[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    [PATCH 4/6] httpd: Add Last-Modified only if resource returned with 200
From:       Sergey Ponomarev <stokito () gmail ! com>
Date:       2021-01-09 21:09:49
Message-ID: 20210109210951.200757-5-stokito () gmail ! com
[Download RAW message or body]

When matched by ETag then we anyway should send 304 with ETag.
This is because If-None-Match may have few ETags and client needs to know which one \
exactly matched. But we don't have to send Last-Modified on 304 because it will be \
always the same as client have cached.

On 304 skip to send headers:
* Content-Type
* Accept-Ranges: bytes
* Last-Modified
* ETag

All those headers will be stored by client and in browsers JavaScript will anyway \
return correct document.lastModified and document.contentType

Signed-off-by: Sergey Ponomarev <stokito@gmail.com>
---
 networking/httpd.c | 60 ++++++++++++++++++++++++----------------------
 1 file changed, 32 insertions(+), 28 deletions(-)

diff --git a/networking/httpd.c b/networking/httpd.c
index 93db24f25..72c1ce937 100644
--- a/networking/httpd.c
+++ b/networking/httpd.c
@@ -1132,7 +1132,7 @@ static void send_headers(unsigned responseNum)
 		);
 	}
 
-	if (responseNum != HTTP_OK || found_mime_type) {
+	if ((responseNum != HTTP_NOT_MODIFIED) && (responseNum != HTTP_OK || \
found_mime_type)) {  len += sprintf(iobuf + len,
 				"Content-type: %s\r\n",
 				/* if it's error message, then it's HTML */
@@ -1222,32 +1222,38 @@ static void send_headers(unsigned responseNum)
 // (NB: standards do not define "Transfer-Length:" _header_,
 // transfer-length above is just a concept).
 
-		len += sprintf(iobuf + len,
-#if ENABLE_FEATURE_HTTPD_RANGES
-			"Accept-Ranges: bytes\r\n"
-#endif
-#if ENABLE_FEATURE_HTTPD_LAST_MODIFIED
-			"Last-Modified: %s\r\n"
-#endif
-#if ENABLE_FEATURE_HTTPD_ETAG
-			"ETag: %s\r\n"
-#endif
-
-	/* 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.
-	 * Theoretically we should not send it if page is compressed,
-	 * but de-facto standard is to send it (see comment below).
-	 */
-			"Content-Length: %"OFF_FMT"u\r\n",
-#if ENABLE_FEATURE_HTTPD_LAST_MODIFIED
+		if (responseNum == HTTP_NOT_MODIFIED) {
+			#if ENABLE_FEATURE_HTTPD_ETAG
+			len += sprintf(iobuf + len, "ETag: %s\r\n", G.etag);
+			#endif
+		} else {
+			len += sprintf(iobuf + len,
+				#if ENABLE_FEATURE_HTTPD_RANGES
+				"Accept-Ranges: bytes\r\n"
+				#endif
+				#if ENABLE_FEATURE_HTTPD_LAST_MODIFIED
+				"Last-Modified: %s\r\n"
+	 			#endif
+				#if ENABLE_FEATURE_HTTPD_ETAG
+				"ETag: %s\r\n"
+				#endif
+
+				/* Because of 4.4 (5), we can forgot sending of "Content-Length"
+				 * since we close connection afterwards, but it helps clients
+				 * to e.g. estimate download times, show progress bars etc.
+				 * Theoretically we should not send it if page is compressed,
+				 * but de-facto standard is to send it (see comment below).
+				 */
+				"Content-Length: %"OFF_FMT"u\r\n",
+				#if ENABLE_FEATURE_HTTPD_LAST_MODIFIED
 				G.last_mod_date,
-#endif
-#if ENABLE_FEATURE_HTTPD_ETAG
+				#endif
+				#if ENABLE_FEATURE_HTTPD_ETAG
 				G.etag,
-#endif
+				#endif
 				file_size
-		);
+			);
+		}
 	}
 
 	/* This should be "Transfer-Encoding", not "Content-Encoding":
@@ -1773,10 +1779,6 @@ static NOINLINE void send_file_and_exit(const char *url, int \
what)  send_headers_and_exit(HTTP_NOT_FOUND);
 		log_and_exit();
 	}
-#if ENABLE_FEATURE_HTTPD_LAST_MODIFIED
-	/* Generate Last-Modified header */
-	strftime(G.last_mod_date, sizeof(G.last_mod_date), RFC1123FMT, gmtime_r(&last_mod, \
                &tm));
-#endif
 #if ENABLE_FEATURE_HTTPD_ETAG
 	/* ETag is "hex(last_mod)-hex(file_size)" e.g. "5e132e20-417" */
 	sprintf(G.etag, "\"%llx-%llx\"", (unsigned long long)last_mod, (unsigned long \
long)file_size); @@ -1791,6 +1793,8 @@ static NOINLINE void send_file_and_exit(const \
char *url, int what)  }
 #endif
 #if ENABLE_FEATURE_HTTPD_LAST_MODIFIED
+	/* Generate Last-Modified header */
+	strftime(G.last_mod_date, sizeof(G.last_mod_date), RFC1123FMT, gmtime_r(&last_mod, \
&tm));  #if ENABLE_FEATURE_HTTPD_ETAG
 	/* if ETag present but wasn't matched then no sense to match by If-Modified-Since \
*/  if (!G.if_none_match) {
-- 
2.27.0

_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic