mirror of
https://github.com/cesanta/mongoose.git
synced 2025-07-29 17:06:14 +08:00
Add extra headers param to mg_http_serve_file
This commit is contained in:
parent
03ab9e4e0d
commit
b7a79a556a
@ -646,7 +646,8 @@ enable SSI, set a `-DMG_ENABLE_SSI=1` build flag.
|
|||||||
|
|
||||||
```c
|
```c
|
||||||
void mg_http_serve_file(struct mg_connection *, struct mg_http_message *hm,
|
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.
|
Serve static 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"
|
"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");
|
"Content-Type: multipart/x-mixed-replace; boundary=--foo\r\n\r\n");
|
||||||
} else if (mg_http_match_uri(hm, "/api/log/static")) {
|
} 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")) {
|
} else if (mg_http_match_uri(hm, "/api/log/live")) {
|
||||||
c->label[0] = 'L'; // Mark that connection as live log listener
|
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");
|
mg_printf(c, "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");
|
||||||
|
@ -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) {
|
if (ev == MG_EV_HTTP_MSG) {
|
||||||
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
||||||
if (mg_http_match_uri(hm, "/api/log/static")) {
|
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")) {
|
} else if (mg_http_match_uri(hm, "/api/log/live")) {
|
||||||
c->label[0] = 'L'; // Mark that connection as live log listener
|
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");
|
mg_printf(c, "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");
|
||||||
|
@ -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,
|
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 mg_str *inm = mg_http_get_header(hm, "If-None-Match");
|
||||||
struct stat st;
|
struct stat st;
|
||||||
FILE *fp = fopen(path, "rb");
|
FILE *fp = fopen(path, "rb");
|
||||||
@ -791,8 +791,8 @@ void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm,
|
|||||||
} else {
|
} else {
|
||||||
mg_printf(c,
|
mg_printf(c,
|
||||||
"HTTP/1.1 200 OK\r\nContent-Type: %s\r\n"
|
"HTTP/1.1 200 OK\r\nContent-Type: %s\r\n"
|
||||||
"Etag: %s\r\nContent-Length: %lu\r\n\r\n",
|
"Etag: %s\r\nContent-Length: %lu\r\n%s\r\n",
|
||||||
mime, etag, (unsigned long) st.st_size);
|
mime, etag, (unsigned long) st.st_size, hdrs ? hdrs : "");
|
||||||
if (mg_vcasecmp(&hm->method, "HEAD") == 0) {
|
if (mg_vcasecmp(&hm->method, "HEAD") == 0) {
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
} else {
|
} 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);
|
mg_http_serve_ssi(c, root, real);
|
||||||
#endif
|
#endif
|
||||||
} else {
|
} 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);
|
if (fp != NULL) fclose(fp);
|
||||||
}
|
}
|
||||||
|
@ -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,
|
void mg_http_serve_dir(struct mg_connection *, struct mg_http_message *hm,
|
||||||
struct mg_http_serve_opts *);
|
struct mg_http_serve_opts *);
|
||||||
void mg_http_serve_file(struct mg_connection *, struct mg_http_message *,
|
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,
|
void mg_http_reply(struct mg_connection *, int status_code, const char *headers,
|
||||||
const char *body_fmt, ...);
|
const char *body_fmt, ...);
|
||||||
struct mg_str *mg_http_get_header(struct mg_http_message *, const char *name);
|
struct mg_str *mg_http_get_header(struct mg_http_message *, const char *name);
|
||||||
|
@ -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,
|
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 mg_str *inm = mg_http_get_header(hm, "If-None-Match");
|
||||||
struct stat st;
|
struct stat st;
|
||||||
FILE *fp = fopen(path, "rb");
|
FILE *fp = fopen(path, "rb");
|
||||||
@ -377,8 +377,8 @@ void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm,
|
|||||||
} else {
|
} else {
|
||||||
mg_printf(c,
|
mg_printf(c,
|
||||||
"HTTP/1.1 200 OK\r\nContent-Type: %s\r\n"
|
"HTTP/1.1 200 OK\r\nContent-Type: %s\r\n"
|
||||||
"Etag: %s\r\nContent-Length: %lu\r\n\r\n",
|
"Etag: %s\r\nContent-Length: %lu\r\n%s\r\n",
|
||||||
mime, etag, (unsigned long) st.st_size);
|
mime, etag, (unsigned long) st.st_size, hdrs ? hdrs : "");
|
||||||
if (mg_vcasecmp(&hm->method, "HEAD") == 0) {
|
if (mg_vcasecmp(&hm->method, "HEAD") == 0) {
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
} else {
|
} 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);
|
mg_http_serve_ssi(c, root, real);
|
||||||
#endif
|
#endif
|
||||||
} else {
|
} 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);
|
if (fp != NULL) fclose(fp);
|
||||||
}
|
}
|
||||||
|
@ -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,
|
void mg_http_serve_dir(struct mg_connection *, struct mg_http_message *hm,
|
||||||
struct mg_http_serve_opts *);
|
struct mg_http_serve_opts *);
|
||||||
void mg_http_serve_file(struct mg_connection *, struct mg_http_message *,
|
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,
|
void mg_http_reply(struct mg_connection *, int status_code, const char *headers,
|
||||||
const char *body_fmt, ...);
|
const char *body_fmt, ...);
|
||||||
struct mg_str *mg_http_get_header(struct mg_http_message *, const char *name);
|
struct mg_str *mg_http_get_header(struct mg_http_message *, const char *name);
|
||||||
|
Loading…
Reference in New Issue
Block a user