[PATCH 3/4] httpd: Support caching via 'ETag:' header

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


If server returned ETag in response then next time client (browser) will add it to request into If-None-Match header. Then httpd will check if file wasn't modified and if not return 304 Not Modified status code. The ETag value is itself the last modification date in unix epoch.

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

diff --git a/networking/httpd.c b/networking/httpd.c
index 97b61fb77..50a837229 100644
--- a/networking/httpd.c
+++ b/networking/httpd.c
@@ -214,6 +214,14 @@
 //config:	help
 //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_CACHE
+//config:	bool "Support caching via 'ETag:' header"
+//config:	default y
+//config:	depends on HTTPD
+//config:	help
+//config:	Enable caching Conditional GET requests via "ETag" and If-None-Match" headers.
+//config:	The ETag value is simply file's last modification date so not completely reliable.
 
 //applet:IF_HTTPD(APPLET(httpd, BB_DIR_USR_SBIN, BB_SUID_DROP))
 
@@ -310,6 +318,7 @@ enum {
 	HTTP_OK = 200,
 	HTTP_PARTIAL_CONTENT = 206,
 	HTTP_MOVED_TEMPORARILY = 302,
+	HTTP_NOT_MODIFIED = 304,
 	HTTP_BAD_REQUEST = 400,       /* malformed syntax */
 	HTTP_UNAUTHORIZED = 401, /* authentication needed, respond with auth hdr */
 	HTTP_NOT_FOUND = 404,
@@ -327,7 +336,6 @@ enum {
 	HTTP_NO_CONTENT = 204,
 	HTTP_MULTIPLE_CHOICES = 300,
 	HTTP_MOVED_PERMANENTLY = 301,
-	HTTP_NOT_MODIFIED = 304,
 	HTTP_PAYMENT_REQUIRED = 402,
 	HTTP_BAD_GATEWAY = 502,
 	HTTP_SERVICE_UNAVAILABLE = 503, /* overload, maintenance */
@@ -340,6 +348,9 @@ static const uint16_t http_response_type[] ALIGN2 = {
 	HTTP_PARTIAL_CONTENT,
 #endif
 	HTTP_MOVED_TEMPORARILY,
+#if ENABLE_FEATURE_HTTPD_CACHE
+	HTTP_NOT_MODIFIED,
+#endif
 	HTTP_REQUEST_TIMEOUT,
 	HTTP_NOT_IMPLEMENTED,
 #if ENABLE_FEATURE_HTTPD_BASIC_AUTH
@@ -356,7 +367,6 @@ static const uint16_t http_response_type[] ALIGN2 = {
 	HTTP_NO_CONTENT,
 	HTTP_MULTIPLE_CHOICES,
 	HTTP_MOVED_PERMANENTLY,
-	HTTP_NOT_MODIFIED,
 	HTTP_BAD_GATEWAY,
 	HTTP_SERVICE_UNAVAILABLE,
 #endif
@@ -371,6 +381,9 @@ static const struct {
 	{ "Partial Content", NULL },
 #endif
 	{ "Found", NULL },
+#if ENABLE_FEATURE_HTTPD_CACHE
+	{ "Not Modified" },
+#endif
 	{ "Request Timeout", "No request appeared within 60 seconds" },
 	{ "Not Implemented", "The requested method is not recognized" },
 #if ENABLE_FEATURE_HTTPD_BASIC_AUTH
@@ -387,7 +400,6 @@ static const struct {
 	{ "No Content" },
 	{ "Multiple Choices" },
 	{ "Moved Permanently" },
-	{ "Not Modified" },
 	{ "Bad Gateway", "" },
 	{ "Service Unavailable", "" },
 #endif
@@ -400,7 +412,10 @@ struct globals {
 	/* client can handle gzip / we are going to send gzip */
 	smallint content_gzip;
 #endif
+#if ENABLE_FEATURE_HTTPD_CACHE
+	time_t modified_since;
 	time_t last_mod;
+#endif
 	char *rmt_ip_str;       /* for $REMOTE_ADDR and $REMOTE_PORT */
 	const char *bind_addr_or_port;
 
@@ -457,7 +472,10 @@ struct globals {
 #define index_page        (G.index_page       )
 #define found_mime_type   (G.found_mime_type  )
 #define found_moved_temporarily (G.found_moved_temporarily)
+#if ENABLE_FEATURE_HTTPD_CACHE
+# define modified_since   (G.modified_since   )
 #define last_mod          (G.last_mod         )
+#endif
 #define ip_a_d            (G.ip_a_d           )
 #define g_realm           (G.g_realm          )
 #define remoteuser        (G.remoteuser       )
@@ -486,6 +504,7 @@ enum {
 	setup_common_bufsiz(); \
 	SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
 	IF_FEATURE_HTTPD_BASIC_AUTH(g_realm = "Web Server Authentication";) \
+	IF_FEATURE_HTTPD_CACHE(modified_since = 0;) \
 	IF_FEATURE_HTTPD_RANGES(range_start = -1;) \
 	bind_addr_or_port = "80"; \
 	index_page = index_html; \
@@ -1166,8 +1185,10 @@ static void send_headers(unsigned responseNum)
 #if ENABLE_FEATURE_HTTPD_RANGES
 			"Accept-Ranges: bytes\r\n"
 #endif
+#if ENABLE_FEATURE_HTTPD_CACHE
 			/* Instead of Last-Modified formatted date send it as a plain unix timestamp via ETag */
 			"ETag: %ld\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.
@@ -1175,7 +1196,9 @@ static void send_headers(unsigned responseNum)
 	 * but de-facto standard is to send it (see comment below).
 	 */
 			"Content-Length: %"OFF_FMT"u\r\n",
+#if ENABLE_FEATURE_HTTPD_CACHE
 				last_mod,
+#endif
 				file_size
 		);
 	}
@@ -1681,13 +1704,16 @@ static NOINLINE void send_file_and_exit(const char *url, int what)
 			struct stat sb;
 			fstat(fd, &sb);
 			file_size = sb.st_size;
+#if ENABLE_FEATURE_HTTPD_CACHE
 			last_mod = sb.st_mtime;
+#endif
 		} else {
 			IF_FEATURE_HTTPD_GZIP(content_gzip = 0;)
 			fd = open(url, O_RDONLY);
 		}
 	} else {
 		fd = open(url, O_RDONLY);
+		/* file_size and last_mod are already populated */
 	}
 	if (fd < 0) {
 		if (DEBUG)
@@ -1699,6 +1725,14 @@ 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_CACHE
+	if (DEBUG)
+		bb_perror_msg("Not modified '%s'", url);
+	if (modified_since == last_mod) {
+		send_headers_and_exit(HTTP_NOT_MODIFIED);
+		log_and_exit();
+	}
+#endif
 	/* If you want to know about EPIPE below
 	 * (happens if you abort downloads from local httpd): */
 	signal(SIGPIPE, SIG_IGN);
@@ -2334,7 +2368,9 @@ static void handle_incoming_and_exit(const len_and_sockaddr *fromAddr)
 #endif
 		if (!found_moved_temporarily) {
 			file_size = sb.st_size;
+#if ENABLE_FEATURE_HTTPD_CACHE
 			last_mod = sb.st_mtime;
+#endif
 		}
 	}
 #if ENABLE_FEATURE_HTTPD_CGI
@@ -2438,6 +2474,13 @@ static void handle_incoming_and_exit(const len_and_sockaddr *fromAddr)
 			continue;
 		}
 #endif
+#if ENABLE_FEATURE_HTTPD_CACHE
+		if (STRNCASECMP(iobuf, "If-None-Match:") == 0) {
+			char *s = skip_whitespace(iobuf + sizeof("If-None-Match:")-1);
+			modified_since = strtoul(s, &s, 10);
+			continue;
+		}
+#endif
 #if ENABLE_FEATURE_HTTPD_CGI
 		if (cgi_type != CGI_NONE) {
 			bool ct = (STRNCASECMP(iobuf, "Content-Type:") == 0);
-- 
2.25.1



More information about the busybox mailing list