[PATCH] httpd: support Content-Encoding for CGI scripts

Assaf Gordon assafgordon at gmail.com
Wed Mar 27 18:24:22 UTC 2019


Pass the HTTP_CONTENT_ENCODING header to CGI script (if received from
the client). For example, this can indicate the submitted POST content
is gzipped.

With this patch busybox's httpd server can be used as a frontend
to git's git-http-backend CGI program ('git fetch' sometimes sends
compressed data). Without this patch, extra steps are needed to detect
whether the input is gzipped or not, see:
https://www.openwall.com/lists/musl/2019/03/27/3 .

bloat-check:

function                                             old     new   delta
handle_incoming_and_exit                            2844    2912     +68
.rodata                                             7562    7602     +40
send_cgi_and_exit                                    982    1006     +24
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/0 up/down: 132/0)             Total: 132
bytes
   text    data     bss     dec     hex filename
  83728    3174    1544   88446   1597e busybox_old
  83860    3174    1544   88578   15a02 busybox_unstripped

Signed-off-by: Assaf Gordon <assafgordon at gmail.com>
---
 networking/httpd.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/networking/httpd.c b/networking/httpd.c
index b52526a78..1a22cd23a 100644
--- a/networking/httpd.c
+++ b/networking/httpd.c
@@ -416,6 +416,7 @@ struct globals {
 	IF_FEATURE_HTTPD_CGI(char *user_agent;)
 	IF_FEATURE_HTTPD_CGI(char *host;)
 	IF_FEATURE_HTTPD_CGI(char *http_accept;)
+	IF_FEATURE_HTTPD_CGI(char *content_encoding;)
 	IF_FEATURE_HTTPD_CGI(char *http_accept_language;)
 
 	off_t file_size;        /* -1 - unknown */
@@ -1536,6 +1537,8 @@ static void send_cgi_and_exit(
 		setenv1("HTTP_ACCEPT", G.http_accept);
 	if (G.http_accept_language)
 		setenv1("HTTP_ACCEPT_LANGUAGE", G.http_accept_language);
+	if (G.content_encoding)
+		setenv1("HTTP_CONTENT_ENCODING", G.content_encoding);
 	if (post_len)
 		putenv(xasprintf("CONTENT_LENGTH=%u", post_len));
 	if (cookie)
@@ -2323,6 +2326,9 @@ static void handle_incoming_and_exit(const len_and_sockaddr *fromAddr)
 			} else if (STRNCASECMP(iobuf, "Accept-Language:") == 0) {
 				if (!G.http_accept_language)
 					G.http_accept_language = xstrdup(skip_whitespace(iobuf + sizeof("Accept-Language:")-1));
+			} else if (STRNCASECMP(iobuf, "Content-Encoding:") == 0) {
+				if (!G.content_encoding)
+					G.content_encoding = xstrdup(skip_whitespace(iobuf + sizeof("Content-Encoding:")-1));
 			}
 #endif
 #if ENABLE_FEATURE_HTTPD_BASIC_AUTH
-- 
2.11.0



More information about the busybox mailing list