Commit 45f66120 authored by Andrea Guzzo's avatar Andrea Guzzo

allow to define a default mime-type to use when lookup in the internal table fails

instead of always returning 'text/plain'

To allow setting a different default mime-type the mg_get_mime_type() api has been
extended to require an mg_server parameter.

Note that using a static global as default mime type wouldn't have allowed us to
control it via a configuration option because there is not global initialization phase
and configuration options are provided (and specific) to each server instance.
This means that different servers can be configured to use a different default mime-type
(which is also a desired behaviour in many scenarios) when lookup fails.
parent 2392156f
...@@ -220,6 +220,7 @@ enum { ...@@ -220,6 +220,7 @@ enum {
CGI_PATTERN, DAV_AUTH_FILE, DOCUMENT_ROOT, ENABLE_DIRECTORY_LISTING, CGI_PATTERN, DAV_AUTH_FILE, DOCUMENT_ROOT, ENABLE_DIRECTORY_LISTING,
#endif #endif
EXTRA_MIME_TYPES, EXTRA_MIME_TYPES,
DEFAULT_MIME_TYPE,
#ifndef MONGOOSE_NO_FILESYSTEM #ifndef MONGOOSE_NO_FILESYSTEM
GLOBAL_AUTH_FILE, GLOBAL_AUTH_FILE,
#endif #endif
...@@ -249,6 +250,7 @@ static const char *static_config_options[] = { ...@@ -249,6 +250,7 @@ static const char *static_config_options[] = {
"enable_directory_listing", "yes", "enable_directory_listing", "yes",
#endif #endif
"extra_mime_types", NULL, "extra_mime_types", NULL,
"default_mime_type", "text/plain",
#ifndef MONGOOSE_NO_FILESYSTEM #ifndef MONGOOSE_NO_FILESYSTEM
"global_auth_file", NULL, "global_auth_file", NULL,
#endif #endif
...@@ -1993,7 +1995,7 @@ static void write_to_socket(struct connection *conn) { ...@@ -1993,7 +1995,7 @@ static void write_to_socket(struct connection *conn) {
} }
} }
const char *mg_get_mime_type(const char *path) { const char *mg_get_mime_type(struct mg_server *server, const char *path) {
const char *ext; const char *ext;
size_t i, path_len; size_t i, path_len;
...@@ -2007,7 +2009,7 @@ const char *mg_get_mime_type(const char *path) { ...@@ -2007,7 +2009,7 @@ const char *mg_get_mime_type(const char *path) {
} }
} }
return "text/plain"; return server->config_options[DEFAULT_MIME_TYPE];
} }
static struct uri_handler *find_uri_handler(struct mg_server *server, static struct uri_handler *find_uri_handler(struct mg_server *server,
...@@ -2093,7 +2095,7 @@ static void get_mime_type(const struct mg_server *server, const char *path, ...@@ -2093,7 +2095,7 @@ static void get_mime_type(const struct mg_server *server, const char *path,
} }
} }
vec->ptr = mg_get_mime_type(path); vec->ptr = mg_get_mime_type((struct mg_server *)server, path);
vec->len = strlen(vec->ptr); vec->len = strlen(vec->ptr);
} }
......
...@@ -86,7 +86,7 @@ int mg_printf(struct mg_connection *conn, const char *fmt, ...); ...@@ -86,7 +86,7 @@ int mg_printf(struct mg_connection *conn, const char *fmt, ...);
const char *mg_get_header(const struct mg_connection *, const char *name); const char *mg_get_header(const struct mg_connection *, const char *name);
const char *mg_get_mime_type(const char *file_name); const char *mg_get_mime_type(struct mg_server*, const char *file_name);
int mg_get_var(const struct mg_connection *conn, const char *var_name, int mg_get_var(const struct mg_connection *conn, const char *var_name,
char *buf, size_t buf_len); char *buf, size_t buf_len);
int mg_parse_header(const char *hdr, const char *var_name, char *buf, size_t); int mg_parse_header(const char *hdr, const char *var_name, char *buf, size_t);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment