httpd: Update to HTTP/1.1

HTTP v1.1 was released in 1999 year and it's time to update BB HTTPD.
Browsers may behave badly with HTTP/1.0
E.g. Chrome does not send the If-None-Match header with ETag.

Signed-off-by: Sergey Ponomarev <stokito@gmail.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Sergey Ponomarev 2020-08-09 01:23:31 +03:00 committed by Denys Vlasenko
parent 1a5d6fcbb5
commit b414cdf5b4

View File

@ -269,7 +269,7 @@
static const char DEFAULT_PATH_HTTPD_CONF[] ALIGN1 = "/etc"; static const char DEFAULT_PATH_HTTPD_CONF[] ALIGN1 = "/etc";
static const char HTTPD_CONF[] ALIGN1 = "httpd.conf"; static const char HTTPD_CONF[] ALIGN1 = "httpd.conf";
static const char HTTP_200[] ALIGN1 = "HTTP/1.0 200 OK\r\n"; static const char HTTP_200[] ALIGN1 = "HTTP/1.1 200 OK\r\n";
static const char index_html[] ALIGN1 = "index.html"; static const char index_html[] ALIGN1 = "index.html";
typedef struct has_next_ptr { typedef struct has_next_ptr {
@ -1074,7 +1074,7 @@ static void send_headers(unsigned responseNum)
strftime(date_str, sizeof(date_str), RFC1123FMT, gmtime_r(&timer, &tm)); strftime(date_str, sizeof(date_str), RFC1123FMT, gmtime_r(&timer, &tm));
/* ^^^ using gmtime_r() instead of gmtime() to not use static data */ /* ^^^ using gmtime_r() instead of gmtime() to not use static data */
len = sprintf(iobuf, len = sprintf(iobuf,
"HTTP/1.0 %u %s\r\n" "HTTP/1.1 %u %s\r\n"
"Date: %s\r\n" "Date: %s\r\n"
"Connection: close\r\n", "Connection: close\r\n",
responseNum, responseString, responseNum, responseString,
@ -1099,7 +1099,7 @@ static void send_headers(unsigned responseNum)
#endif #endif
if (responseNum == HTTP_MOVED_TEMPORARILY) { if (responseNum == HTTP_MOVED_TEMPORARILY) {
/* Responding to "GET /dir" with /* Responding to "GET /dir" with
* "HTTP/1.0 302 Found" "Location: /dir/" * "HTTP/1.1 302 Found" "Location: /dir/"
* - IOW, asking them to repeat with a slash. * - IOW, asking them to repeat with a slash.
* Here, overflow IS possible, can't use sprintf: * Here, overflow IS possible, can't use sprintf:
* mkdir test * mkdir test
@ -1409,7 +1409,7 @@ static NOINLINE void cgi_io_loop_and_exit(int fromCgi_rd, int toCgi_wr, int post
count = safe_read(fromCgi_rd, rbuf + out_cnt, IOBUF_SIZE - 8); count = safe_read(fromCgi_rd, rbuf + out_cnt, IOBUF_SIZE - 8);
if (count <= 0) { if (count <= 0) {
/* eof (or error) and there was no "HTTP", /* eof (or error) and there was no "HTTP",
* send "HTTP/1.0 200 OK\r\n", then send received data */ * send "HTTP/1.1 200 OK\r\n", then send received data */
if (out_cnt) { if (out_cnt) {
full_write(STDOUT_FILENO, HTTP_200, sizeof(HTTP_200)-1); full_write(STDOUT_FILENO, HTTP_200, sizeof(HTTP_200)-1);
full_write(STDOUT_FILENO, rbuf, out_cnt); full_write(STDOUT_FILENO, rbuf, out_cnt);
@ -1420,10 +1420,10 @@ static NOINLINE void cgi_io_loop_and_exit(int fromCgi_rd, int toCgi_wr, int post
count = 0; count = 0;
/* "Status" header format is: "Status: 302 Redirected\r\n" */ /* "Status" header format is: "Status: 302 Redirected\r\n" */
if (out_cnt >= 8 && memcmp(rbuf, "Status: ", 8) == 0) { if (out_cnt >= 8 && memcmp(rbuf, "Status: ", 8) == 0) {
/* send "HTTP/1.0 " */ /* send "HTTP/1.1 " */
if (full_write(STDOUT_FILENO, HTTP_200, 9) != 9) if (full_write(STDOUT_FILENO, HTTP_200, 9) != 9)
break; break;
/* skip "Status: " (including space, sending "HTTP/1.0 NNN" is wrong) */ /* skip "Status: " (including space, sending "HTTP/1.1 NNN" is wrong) */
rbuf += 8; rbuf += 8;
count = out_cnt - 8; count = out_cnt - 8;
out_cnt = -1; /* buffering off */ out_cnt = -1; /* buffering off */
@ -1439,7 +1439,7 @@ static NOINLINE void cgi_io_loop_and_exit(int fromCgi_rd, int toCgi_wr, int post
full_write(s, "Content-type: text/plain\r\n\r\n", 28); full_write(s, "Content-type: text/plain\r\n\r\n", 28);
} }
* Counter-example of valid CGI without Content-type: * Counter-example of valid CGI without Content-type:
* echo -en "HTTP/1.0 302 Found\r\n" * echo -en "HTTP/1.1 302 Found\r\n"
* echo -en "Location: http://www.busybox.net\r\n" * echo -en "Location: http://www.busybox.net\r\n"
* echo -en "\r\n" * echo -en "\r\n"
*/ */
@ -1546,7 +1546,7 @@ static void send_cgi_and_exit(
/* (Older versions of bbox seem to do some decoding) */ /* (Older versions of bbox seem to do some decoding) */
setenv1("QUERY_STRING", g_query); setenv1("QUERY_STRING", g_query);
putenv((char*)"SERVER_SOFTWARE=busybox httpd/"BB_VER); putenv((char*)"SERVER_SOFTWARE=busybox httpd/"BB_VER);
putenv((char*)"SERVER_PROTOCOL=HTTP/1.0"); putenv((char*)"SERVER_PROTOCOL=HTTP/1.1");
putenv((char*)"GATEWAY_INTERFACE=CGI/1.1"); putenv((char*)"GATEWAY_INTERFACE=CGI/1.1");
/* Having _separate_ variables for IP and port defeats /* Having _separate_ variables for IP and port defeats
* the purpose of having socket abstraction. Which "port" * the purpose of having socket abstraction. Which "port"