diff --git a/docs/README.md b/docs/README.md index e9b66025..9f982c28 100644 --- a/docs/README.md +++ b/docs/README.md @@ -646,7 +646,8 @@ enable SSI, set a `-DMG_ENABLE_SSI=1` build flag. ```c void mg_http_serve_file(struct mg_connection *, struct mg_http_message *hm, - const char *path, const char *mimetype); + const char *path, const char *mimetype, + const char *extra_headers); ``` Serve static file. diff --git a/examples/complete/main.c b/examples/complete/main.c index 8ccfbb22..28b5250e 100644 --- a/examples/complete/main.c +++ b/examples/complete/main.c @@ -119,7 +119,7 @@ static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { "Pragma: no-cache\r\nExpires: Thu, 01 Dec 1994 16:00:00 GMT\r\n" "Content-Type: multipart/x-mixed-replace; boundary=--foo\r\n\r\n"); } else if (mg_http_match_uri(hm, "/api/log/static")) { - mg_http_serve_file(c, hm, "log.txt", "text/plain"); + mg_http_serve_file(c, hm, "log.txt", "text/plain", ""); } else if (mg_http_match_uri(hm, "/api/log/live")) { c->label[0] = 'L'; // Mark that connection as live log listener mg_printf(c, "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"); diff --git a/examples/live-log/main.c b/examples/live-log/main.c index 75679e29..073fab8e 100644 --- a/examples/live-log/main.c +++ b/examples/live-log/main.c @@ -11,7 +11,7 @@ static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; if (mg_http_match_uri(hm, "/api/log/static")) { - mg_http_serve_file(c, hm, "log.txt", "text/plain"); + mg_http_serve_file(c, hm, "log.txt", "text/plain", ""); } else if (mg_http_match_uri(hm, "/api/log/live")) { c->label[0] = 'L'; // Mark that connection as live log listener mg_printf(c, "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"); diff --git a/mongoose.c b/mongoose.c index 34eba8fb..53f58c69 100644 --- a/mongoose.c +++ b/mongoose.c @@ -777,7 +777,7 @@ static const char *guess_content_type(const char *filename) { } void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm, - const char *path, const char *mime) { + const char *path, const char *mime, const char *hdrs) { struct mg_str *inm = mg_http_get_header(hm, "If-None-Match"); struct stat st; FILE *fp = fopen(path, "rb"); @@ -791,8 +791,8 @@ void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm, } else { mg_printf(c, "HTTP/1.1 200 OK\r\nContent-Type: %s\r\n" - "Etag: %s\r\nContent-Length: %lu\r\n\r\n", - mime, etag, (unsigned long) st.st_size); + "Etag: %s\r\nContent-Length: %lu\r\n%s\r\n", + mime, etag, (unsigned long) st.st_size, hdrs ? hdrs : ""); if (mg_vcasecmp(&hm->method, "HEAD") == 0) { fclose(fp); } else { @@ -1062,7 +1062,7 @@ void mg_http_serve_dir(struct mg_connection *c, struct mg_http_message *hm, mg_http_serve_ssi(c, root, real); #endif } else { - mg_http_serve_file(c, hm, real, guess_content_type(real)); + mg_http_serve_file(c, hm, real, guess_content_type(real), NULL); } if (fp != NULL) fclose(fp); } diff --git a/mongoose.h b/mongoose.h index a6624844..18e541e7 100644 --- a/mongoose.h +++ b/mongoose.h @@ -686,7 +686,7 @@ struct mg_connection *mg_http_connect(struct mg_mgr *, const char *url, void mg_http_serve_dir(struct mg_connection *, struct mg_http_message *hm, struct mg_http_serve_opts *); void mg_http_serve_file(struct mg_connection *, struct mg_http_message *, - const char *, const char *mime); + const char *, const char *mime, const char *headers); void mg_http_reply(struct mg_connection *, int status_code, const char *headers, const char *body_fmt, ...); struct mg_str *mg_http_get_header(struct mg_http_message *, const char *name); diff --git a/src/http.c b/src/http.c index 7cb22835..2081c320 100644 --- a/src/http.c +++ b/src/http.c @@ -363,7 +363,7 @@ static const char *guess_content_type(const char *filename) { } void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm, - const char *path, const char *mime) { + const char *path, const char *mime, const char *hdrs) { struct mg_str *inm = mg_http_get_header(hm, "If-None-Match"); struct stat st; FILE *fp = fopen(path, "rb"); @@ -377,8 +377,8 @@ void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm, } else { mg_printf(c, "HTTP/1.1 200 OK\r\nContent-Type: %s\r\n" - "Etag: %s\r\nContent-Length: %lu\r\n\r\n", - mime, etag, (unsigned long) st.st_size); + "Etag: %s\r\nContent-Length: %lu\r\n%s\r\n", + mime, etag, (unsigned long) st.st_size, hdrs ? hdrs : ""); if (mg_vcasecmp(&hm->method, "HEAD") == 0) { fclose(fp); } else { @@ -648,7 +648,7 @@ void mg_http_serve_dir(struct mg_connection *c, struct mg_http_message *hm, mg_http_serve_ssi(c, root, real); #endif } else { - mg_http_serve_file(c, hm, real, guess_content_type(real)); + mg_http_serve_file(c, hm, real, guess_content_type(real), NULL); } if (fp != NULL) fclose(fp); } diff --git a/src/http.h b/src/http.h index 2406af6a..890367e7 100644 --- a/src/http.h +++ b/src/http.h @@ -36,7 +36,7 @@ struct mg_connection *mg_http_connect(struct mg_mgr *, const char *url, void mg_http_serve_dir(struct mg_connection *, struct mg_http_message *hm, struct mg_http_serve_opts *); void mg_http_serve_file(struct mg_connection *, struct mg_http_message *, - const char *, const char *mime); + const char *, const char *mime, const char *headers); void mg_http_reply(struct mg_connection *, int status_code, const char *headers, const char *body_fmt, ...); struct mg_str *mg_http_get_header(struct mg_http_message *, const char *name);