[PATCH 1/6] httpd: Last-Modified optimization

Sergey Ponomarev stokito at gmail.com
Sat Jan 9 21:09:46 UTC 2021


The date_str variable used for formatting both Date and Last-Modified headers.
Now it only used for Date header and Last-Modified is stored in globals.last_mod_date near to ETag

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

diff --git a/networking/httpd.c b/networking/httpd.c
index 3cad28921..5a3de2043 100644
--- a/networking/httpd.c
+++ b/networking/httpd.c
@@ -314,6 +314,9 @@ static const char DEFAULT_PATH_HTTPD_CONF[] ALIGN1 = "/etc";
 static const char HTTPD_CONF[] ALIGN1 = "httpd.conf";
 static const char HTTP_200[] ALIGN1 = "HTTP/1.1 200 OK\r\n";
 static const char index_html[] ALIGN1 = "index.html";
+#if ENABLE_FEATURE_HTTPD_DATE || ENABLE_FEATURE_HTTPD_LAST_MODIFIED
+	static const char RFC1123FMT[] ALIGN1 = "%a, %d %b %Y %H:%M:%S GMT";
+#endif
 
 typedef struct has_next_ptr {
 	struct has_next_ptr *next;
@@ -492,6 +495,11 @@ struct globals {
 #if ENABLE_FEATURE_HTTPD_ETAG
 	char etag[sizeof("'%llx-%llx'") + 2 * sizeof(long long)*3];
 #endif
+#if ENABLE_FEATURE_HTTPD_LAST_MODIFIED
+	/* Last-Modified header.
+	 * Fixed size 29-byte string. Example: Sun, 06 Nov 1994 08:49:37 GMT */
+	char last_mod_date[40]; /* using a bit larger buffer to paranoia reasons */
+#endif
 #if ENABLE_FEATURE_HTTPD_ERROR_PAGES
 	const char *http_error_page[ARRAY_SIZE(http_response_type)];
 #endif
@@ -1070,8 +1078,7 @@ 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";
+#if ENABLE_FEATURE_HTTPD_DATE
 	/* 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;
@@ -1172,9 +1179,6 @@ static void send_headers(unsigned responseNum)
 #endif
 
 	if (file_size != -1) {    /* file */
-#if ENABLE_FEATURE_HTTPD_LAST_MODIFIED
-		strftime(date_str, sizeof(date_str), RFC1123FMT, gmtime_r(&last_mod, &tm));
-#endif
 #if ENABLE_FEATURE_HTTPD_RANGES
 		if (responseNum == HTTP_PARTIAL_CONTENT) {
 			len += sprintf(iobuf + len,
@@ -1234,7 +1238,7 @@ static void send_headers(unsigned responseNum)
 	 */
 			"Content-Length: %"OFF_FMT"u\r\n",
 #if ENABLE_FEATURE_HTTPD_LAST_MODIFIED
-				date_str,
+				G.last_mod_date,
 #endif
 #if ENABLE_FEATURE_HTTPD_ETAG
 				G.etag,
@@ -1734,6 +1738,9 @@ static NOINLINE void send_file_and_exit(const char *url, int what)
 	char *suffix;
 	int fd;
 	ssize_t count;
+#if ENABLE_FEATURE_HTTPD_LAST_MODIFIED
+	struct tm tm;
+#endif
 
 	if (content_gzip) {
 		/* does <url>.gz exist? Then use it instead */
@@ -1763,6 +1770,10 @@ 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);
-- 
2.27.0



More information about the busybox mailing list