Add struct mg_http_serve_opts::extra_headers

This commit is contained in:
cpq 2021-03-10 07:56:14 +00:00
parent f326cbedbe
commit 6366c832b0
7 changed files with 29 additions and 20 deletions

View File

@ -688,6 +688,7 @@ Write a chunk of data in chunked encoding format.
struct mg_http_serve_opts { struct mg_http_serve_opts {
const char *root_dir; // Web root directory, must be non-NULL const char *root_dir; // Web root directory, must be non-NULL
const char *ssi_pattern; // SSI filename pattern, e.g. #.shtml const char *ssi_pattern; // SSI filename pattern, e.g. #.shtml
const char *extra_headers; // Extra HTTP headers to add in responses
}; };
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,
const struct mg_http_serve_opts *opts); const struct mg_http_serve_opts *opts);

View File

@ -11,7 +11,7 @@ static const char *s_ssi_pattern = "#.shtml";
static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { 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_serve_opts opts = {s_root_dir, s_ssi_pattern}; struct mg_http_serve_opts opts = {s_root_dir, s_ssi_pattern, NULL};
mg_http_serve_dir(c, ev_data, &opts); mg_http_serve_dir(c, ev_data, &opts);
} }
(void) fn_data; (void) fn_data;

View File

@ -970,7 +970,7 @@ static void printdirentry(struct mg_connection *c, const char *name,
} }
static void listdir(struct mg_connection *c, struct mg_http_message *hm, static void listdir(struct mg_connection *c, struct mg_http_message *hm,
char *dir) { struct mg_http_serve_opts *opts, char *dir) {
char path[MG_PATH_MAX], *p = &dir[strlen(dir) - 1], tmp[10]; char path[MG_PATH_MAX], *p = &dir[strlen(dir) - 1], tmp[10];
struct dirent *dp; struct dirent *dp;
DIR *dirp; DIR *dirp;
@ -1001,10 +1001,12 @@ static void listdir(struct mg_connection *c, struct mg_http_message *hm,
while (p > dir && *p != '/') *p-- = '\0'; while (p > dir && *p != '/') *p-- = '\0';
if ((dirp = (opendir(dir))) != NULL) { if ((dirp = (opendir(dir))) != NULL) {
size_t off, n; size_t off, n;
mg_printf(c, "%s\r\n", mg_printf(c,
"HTTP/1.1 200 OK\r\n" "HTTP/1.1 200 OK\r\n"
"Content-Type: text/html; charset=utf-8\r\n" "Content-Type: text/html; charset=utf-8\r\n"
"Content-Length: \r\n"); "%s"
"Content-Length: \r\n\r\n",
opts->extra_headers == NULL ? "" : opts->extra_headers);
off = c->send.len; // Start of body off = c->send.len; // Start of body
mg_printf(c, mg_printf(c,
"<!DOCTYPE html><html><head><title>Index of %.*s</title>%s%s" "<!DOCTYPE html><html><head><title>Index of %.*s</title>%s%s"
@ -1099,7 +1101,7 @@ void mg_http_serve_dir(struct mg_connection *c, struct mg_http_message *hm,
#endif #endif
if (is_index && fp == NULL) { if (is_index && fp == NULL) {
#if MG_ENABLE_DIRECTORY_LISTING #if MG_ENABLE_DIRECTORY_LISTING
listdir(c, hm, t2); listdir(c, hm, opts, t2);
#else #else
mg_http_reply(c, 403, "", "%s", "Directory listing not supported"); mg_http_reply(c, 403, "", "%s", "Directory listing not supported");
#endif #endif
@ -1111,7 +1113,8 @@ void mg_http_serve_dir(struct mg_connection *c, struct mg_http_message *hm,
mg_http_serve_ssi(c, t1, t2); mg_http_serve_ssi(c, t1, t2);
#endif #endif
} else { } else {
mg_http_serve_file(c, hm, t2, guess_content_type(t2), NULL); mg_http_serve_file(c, hm, t2, guess_content_type(t2),
opts->extra_headers);
} }
if (fp != NULL) fclose(fp); if (fp != NULL) fclose(fp);
} }

View File

@ -690,6 +690,7 @@ struct mg_http_message {
struct mg_http_serve_opts { struct mg_http_serve_opts {
const char *root_dir; // Web root directory, must be non-NULL const char *root_dir; // Web root directory, must be non-NULL
const char *ssi_pattern; // SSI filename pattern, e.g. #.shtml const char *ssi_pattern; // SSI filename pattern, e.g. #.shtml
const char *extra_headers; // Extra HTTP headers to add in responses
}; };
int mg_http_parse(const char *s, size_t len, struct mg_http_message *); int mg_http_parse(const char *s, size_t len, struct mg_http_message *);

View File

@ -551,7 +551,7 @@ static void printdirentry(struct mg_connection *c, const char *name,
} }
static void listdir(struct mg_connection *c, struct mg_http_message *hm, static void listdir(struct mg_connection *c, struct mg_http_message *hm,
char *dir) { struct mg_http_serve_opts *opts, char *dir) {
char path[MG_PATH_MAX], *p = &dir[strlen(dir) - 1], tmp[10]; char path[MG_PATH_MAX], *p = &dir[strlen(dir) - 1], tmp[10];
struct dirent *dp; struct dirent *dp;
DIR *dirp; DIR *dirp;
@ -582,10 +582,12 @@ static void listdir(struct mg_connection *c, struct mg_http_message *hm,
while (p > dir && *p != '/') *p-- = '\0'; while (p > dir && *p != '/') *p-- = '\0';
if ((dirp = (opendir(dir))) != NULL) { if ((dirp = (opendir(dir))) != NULL) {
size_t off, n; size_t off, n;
mg_printf(c, "%s\r\n", mg_printf(c,
"HTTP/1.1 200 OK\r\n" "HTTP/1.1 200 OK\r\n"
"Content-Type: text/html; charset=utf-8\r\n" "Content-Type: text/html; charset=utf-8\r\n"
"Content-Length: \r\n"); "%s"
"Content-Length: \r\n\r\n",
opts->extra_headers == NULL ? "" : opts->extra_headers);
off = c->send.len; // Start of body off = c->send.len; // Start of body
mg_printf(c, mg_printf(c,
"<!DOCTYPE html><html><head><title>Index of %.*s</title>%s%s" "<!DOCTYPE html><html><head><title>Index of %.*s</title>%s%s"
@ -680,7 +682,7 @@ void mg_http_serve_dir(struct mg_connection *c, struct mg_http_message *hm,
#endif #endif
if (is_index && fp == NULL) { if (is_index && fp == NULL) {
#if MG_ENABLE_DIRECTORY_LISTING #if MG_ENABLE_DIRECTORY_LISTING
listdir(c, hm, t2); listdir(c, hm, opts, t2);
#else #else
mg_http_reply(c, 403, "", "%s", "Directory listing not supported"); mg_http_reply(c, 403, "", "%s", "Directory listing not supported");
#endif #endif
@ -692,7 +694,8 @@ void mg_http_serve_dir(struct mg_connection *c, struct mg_http_message *hm,
mg_http_serve_ssi(c, t1, t2); mg_http_serve_ssi(c, t1, t2);
#endif #endif
} else { } else {
mg_http_serve_file(c, hm, t2, guess_content_type(t2), NULL); mg_http_serve_file(c, hm, t2, guess_content_type(t2),
opts->extra_headers);
} }
if (fp != NULL) fclose(fp); if (fp != NULL) fclose(fp);
} }

View File

@ -25,6 +25,7 @@ struct mg_http_message {
struct mg_http_serve_opts { struct mg_http_serve_opts {
const char *root_dir; // Web root directory, must be non-NULL const char *root_dir; // Web root directory, must be non-NULL
const char *ssi_pattern; // SSI filename pattern, e.g. #.shtml const char *ssi_pattern; // SSI filename pattern, e.g. #.shtml
const char *extra_headers; // Extra HTTP headers to add in responses
}; };
int mg_http_parse(const char *s, size_t len, struct mg_http_message *); int mg_http_parse(const char *s, size_t len, struct mg_http_message *);

View File

@ -337,7 +337,7 @@ static void eh1(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
} else if (mg_http_match_uri(hm, "/bar")) { } else if (mg_http_match_uri(hm, "/bar")) {
mg_http_reply(c, 404, "", "not found"); mg_http_reply(c, 404, "", "not found");
} else if (mg_http_match_uri(hm, "/badroot")) { } else if (mg_http_match_uri(hm, "/badroot")) {
struct mg_http_serve_opts opts = {"/BAAADDD!", NULL}; struct mg_http_serve_opts opts = {"/BAAADDD!", NULL, NULL};
mg_http_serve_dir(c, hm, &opts); mg_http_serve_dir(c, hm, &opts);
} else if (mg_http_match_uri(hm, "/creds")) { } else if (mg_http_match_uri(hm, "/creds")) {
char user[100], pass[100]; char user[100], pass[100];
@ -346,10 +346,10 @@ static void eh1(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
} else if (mg_http_match_uri(hm, "/upload")) { } else if (mg_http_match_uri(hm, "/upload")) {
mg_http_upload(c, hm, "."); mg_http_upload(c, hm, ".");
} else if (mg_http_match_uri(hm, "/test/")) { } else if (mg_http_match_uri(hm, "/test/")) {
struct mg_http_serve_opts opts = {".", NULL}; struct mg_http_serve_opts opts = {".", NULL, "A: B\r\nC: D\r\n"};
mg_http_serve_dir(c, hm, &opts); mg_http_serve_dir(c, hm, &opts);
} else { } else {
struct mg_http_serve_opts opts = {"./test/data", "#.shtml"}; struct mg_http_serve_opts opts = {"./test/data", "#.shtml", "C: D\r\n"};
mg_http_serve_dir(c, hm, &opts); mg_http_serve_dir(c, hm, &opts);
} }
} else if (ev == MG_EV_WS_MSG) { } else if (ev == MG_EV_WS_MSG) {