Add extra headers param to mg_http_serve_file

This commit is contained in:
cpq 2021-01-07 12:48:20 +00:00
parent 03ab9e4e0d
commit b7a79a556a
7 changed files with 14 additions and 13 deletions

View File

@ -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.

View File

@ -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");

View File

@ -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");

View File

@ -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);
}

View File

@ -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);

View File

@ -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);
}

View File

@ -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);