Skip to content

Commit

Permalink
serve_file: Support 'Content-Encoding: gzip'
Browse files Browse the repository at this point in the history
Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
  • Loading branch information
zhaojh329 committed Jul 2, 2020
1 parent e9666e8 commit 2b8fcfe
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 2b8fcfe

Please sign in to comment.