From 2b8fcfec8972c1a9f0d10ed3aa29f9d4bb438ffc Mon Sep 17 00:00:00 2001 From: Jianhui Zhao Date: Thu, 2 Jul 2020 23:37:54 +0800 Subject: [PATCH] serve_file: Support 'Content-Encoding: gzip' Signed-off-by: Jianhui Zhao --- src/file.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/file.c b/src/file.c index 3e4c73f..8e465b1 100644 --- a/src/file.c +++ b/src/file.c @@ -120,6 +120,18 @@ static bool file_if_unmodified_since(struct uh_connection *conn, struct stat *s) return true; } +static void file_if_gzip(struct uh_connection *conn, const char *path) +{ + const char *hdr = conn->get_header(conn, "Accept-Encoding"); + const char *extn = rindex(path, '.'); + + if (!hdr || !strstr(hdr, "gzip")) + return; + + if (extn && !strcmp(extn, ".gz")) + conn->printf(conn, "Content-Encoding: gzip\r\n"); +} + void serve_file(struct uh_connection *conn, const char *docroot, const char *index_page) { const char *path = conn->get_path(conn); @@ -184,7 +196,11 @@ void serve_file(struct uh_connection *conn, const char *docroot, const char *ind file_response_ok_hdrs(conn, &st); conn->printf(conn, "Content-Type: %s\r\n", file_mime_lookup(path)); - conn->printf(conn, "Content-Length: %" PRIu64 "\r\n\r\n", st.st_size); + conn->printf(conn, "Content-Length: %" PRIu64 "\r\n", st.st_size); + + file_if_gzip(conn, path); + + conn->printf(conn, "\r\n"); if (conn->get_method(conn) == HTTP_HEAD) return;