aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Ponomarev <stokito@gmail.com>2020-08-09 01:23:33 +0300
committerDenys Vlasenko <vda.linux@googlemail.com>2020-08-15 23:23:45 +0200
commitb6efac31d864cffdf618744fd8a6fddbdee3eb4b (patch)
tree4c2f5e1201761a1e7e19db7c774d3f970f8a2d96
parent68f75bb9cecbed3a1bed29219de77373fcba7a88 (diff)
downloadbusybox-w32-b6efac31d864cffdf618744fd8a6fddbdee3eb4b.tar.gz
busybox-w32-b6efac31d864cffdf618744fd8a6fddbdee3eb4b.tar.bz2
busybox-w32-b6efac31d864cffdf618744fd8a6fddbdee3eb4b.zip
httpd: Don't add Last-Modified header to response
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@gmail.com> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--networking/httpd.c31
1 files changed, 25 insertions, 6 deletions
diff --git a/networking/httpd.c b/networking/httpd.c
index a1f841aa8..94f7297ad 100644
--- a/networking/httpd.c
+++ b/networking/httpd.c
@@ -215,6 +215,16 @@
215//config: Makes httpd send files using GZIP content encoding if the 215//config: Makes httpd send files using GZIP content encoding if the
216//config: client supports it and a pre-compressed <file>.gz exists. 216//config: client supports it and a pre-compressed <file>.gz exists.
217//config: 217//config:
218//config:config FEATURE_HTTPD_LAST_MODIFIED
219//config: bool "Add Last-Modified header to response"
220//config: default y
221//config: depends on HTTPD
222//config: help
223//config: The Last-Modified header is used for cache validation.
224//config: The client sends last seen mtime to server in If-Modified-Since.
225//config: Both headers MUST be an RFC 1123 formatted, which is hard to parse.
226//config: Use ETag header instead.
227//config:
218//config:config FEATURE_HTTPD_DATE 228//config:config FEATURE_HTTPD_DATE
219//config: bool "Add Date header to response" 229//config: bool "Add Date header to response"
220//config: default y 230//config: default y
@@ -1046,11 +1056,12 @@ static void log_and_exit(void)
1046 */ 1056 */
1047static void send_headers(unsigned responseNum) 1057static void send_headers(unsigned responseNum)
1048{ 1058{
1059#if ENABLE_FEATURE_HTTPD_DATE || ENABLE_FEATURE_HTTPD_LAST_MODIFIED
1049 static const char RFC1123FMT[] ALIGN1 = "%a, %d %b %Y %H:%M:%S GMT"; 1060 static const char RFC1123FMT[] ALIGN1 = "%a, %d %b %Y %H:%M:%S GMT";
1050 /* Fixed size 29-byte string. Example: Sun, 06 Nov 1994 08:49:37 GMT */ 1061 /* Fixed size 29-byte string. Example: Sun, 06 Nov 1994 08:49:37 GMT */
1051 char date_str[40]; /* using a bit larger buffer to paranoia reasons */ 1062 char date_str[40]; /* using a bit larger buffer to paranoia reasons */
1052
1053 struct tm tm; 1063 struct tm tm;
1064#endif
1054 const char *responseString = ""; 1065 const char *responseString = "";
1055 const char *infoString = NULL; 1066 const char *infoString = NULL;
1056#if ENABLE_FEATURE_HTTPD_ERROR_PAGES 1067#if ENABLE_FEATURE_HTTPD_ERROR_PAGES
@@ -1058,7 +1069,6 @@ static void send_headers(unsigned responseNum)
1058#endif 1069#endif
1059 unsigned len; 1070 unsigned len;
1060 unsigned i; 1071 unsigned i;
1061 time_t timer = time(NULL);
1062 1072
1063 for (i = 0; i < ARRAY_SIZE(http_response_type); i++) { 1073 for (i = 0; i < ARRAY_SIZE(http_response_type); i++) {
1064 if (http_response_type[i] == responseNum) { 1074 if (http_response_type[i] == responseNum) {
@@ -1079,11 +1089,13 @@ static void send_headers(unsigned responseNum)
1079 * always fit into those kbytes. 1089 * always fit into those kbytes.
1080 */ 1090 */
1081 1091
1092 {
1082#if ENABLE_FEATURE_HTTPD_DATE 1093#if ENABLE_FEATURE_HTTPD_DATE
1083 strftime(date_str, sizeof(date_str), RFC1123FMT, gmtime_r(&timer, &tm)); 1094 time_t timer = time(NULL);
1084 /* ^^^ using gmtime_r() instead of gmtime() to not use static data */ 1095 strftime(date_str, sizeof(date_str), RFC1123FMT, gmtime_r(&timer, &tm));
1096 /* ^^^ using gmtime_r() instead of gmtime() to not use static data */
1085#endif 1097#endif
1086 len = sprintf(iobuf, 1098 len = sprintf(iobuf,
1087 "HTTP/1.1 %u %s\r\n" 1099 "HTTP/1.1 %u %s\r\n"
1088#if ENABLE_FEATURE_HTTPD_DATE 1100#if ENABLE_FEATURE_HTTPD_DATE
1089 "Date: %s\r\n" 1101 "Date: %s\r\n"
@@ -1093,7 +1105,8 @@ static void send_headers(unsigned responseNum)
1093#if ENABLE_FEATURE_HTTPD_DATE 1105#if ENABLE_FEATURE_HTTPD_DATE
1094 ,date_str 1106 ,date_str
1095#endif 1107#endif
1096 ); 1108 );
1109 }
1097 1110
1098 if (responseNum != HTTP_OK || found_mime_type) { 1111 if (responseNum != HTTP_OK || found_mime_type) {
1099 len += sprintf(iobuf + len, 1112 len += sprintf(iobuf + len,
@@ -1145,7 +1158,9 @@ static void send_headers(unsigned responseNum)
1145#endif 1158#endif
1146 1159
1147 if (file_size != -1) { /* file */ 1160 if (file_size != -1) { /* file */
1161#if ENABLE_FEATURE_HTTPD_LAST_MODIFIED
1148 strftime(date_str, sizeof(date_str), RFC1123FMT, gmtime_r(&last_mod, &tm)); 1162 strftime(date_str, sizeof(date_str), RFC1123FMT, gmtime_r(&last_mod, &tm));
1163#endif
1149#if ENABLE_FEATURE_HTTPD_RANGES 1164#if ENABLE_FEATURE_HTTPD_RANGES
1150 if (responseNum == HTTP_PARTIAL_CONTENT) { 1165 if (responseNum == HTTP_PARTIAL_CONTENT) {
1151 len += sprintf(iobuf + len, 1166 len += sprintf(iobuf + len,
@@ -1190,7 +1205,9 @@ static void send_headers(unsigned responseNum)
1190#if ENABLE_FEATURE_HTTPD_RANGES 1205#if ENABLE_FEATURE_HTTPD_RANGES
1191 "Accept-Ranges: bytes\r\n" 1206 "Accept-Ranges: bytes\r\n"
1192#endif 1207#endif
1208#if ENABLE_FEATURE_HTTPD_LAST_MODIFIED
1193 "Last-Modified: %s\r\n" 1209 "Last-Modified: %s\r\n"
1210#endif
1194 /* Because of 4.4 (5), we can forgo sending of "Content-Length" 1211 /* Because of 4.4 (5), we can forgo sending of "Content-Length"
1195 * since we close connection afterwards, but it helps clients 1212 * since we close connection afterwards, but it helps clients
1196 * to e.g. estimate download times, show progress bars etc. 1213 * to e.g. estimate download times, show progress bars etc.
@@ -1198,7 +1215,9 @@ static void send_headers(unsigned responseNum)
1198 * but de-facto standard is to send it (see comment below). 1215 * but de-facto standard is to send it (see comment below).
1199 */ 1216 */
1200 "Content-Length: %"OFF_FMT"u\r\n", 1217 "Content-Length: %"OFF_FMT"u\r\n",
1218#if ENABLE_FEATURE_HTTPD_LAST_MODIFIED
1201 date_str, 1219 date_str,
1220#endif
1202 file_size 1221 file_size
1203 ); 1222 );
1204 } 1223 }