Refactor guess_mime_type()

This commit is contained in:
Sergey Lyubka 2022-06-29 10:28:37 +01:00
parent e8f050c194
commit 93a6bc501e
2 changed files with 80 additions and 76 deletions

View File

@ -1758,12 +1758,10 @@ static void static_cb(struct mg_connection *c, int ev, void *ev_data,
(void) ev_data;
}
static struct mg_str guess_content_type(struct mg_str path, const char *extra) {
struct mg_str k, v, s = mg_str(extra);
size_t i = 0;
// Known mime types. Keep it outside guess_content_type() function, since
// some environments don't like it defined there.
// clang-format off
struct mg_str tab[] = {
static struct mg_str s_known_types[] = {
MG_C_STR("html"), MG_C_STR("text/html; charset=utf-8"),
MG_C_STR("htm"), MG_C_STR("text/html; charset=utf-8"),
MG_C_STR("css"), MG_C_STR("text/css; charset=utf-8"),
@ -1798,6 +1796,10 @@ static struct mg_str guess_content_type(struct mg_str path, const char *extra) {
};
// clang-format on
static struct mg_str guess_content_type(struct mg_str path, const char *extra) {
struct mg_str k, v, s = mg_str(extra);
size_t i = 0;
// Shrink path to its extension only
while (i < path.len && path.ptr[path.len - i - 1] != '.') i++;
path.ptr += path.len - i;
@ -1809,8 +1811,8 @@ static struct mg_str guess_content_type(struct mg_str path, const char *extra) {
}
// Process built-in mime types
for (i = 0; tab[i].ptr != NULL; i += 2) {
if (mg_strcmp(path, tab[i]) == 0) return tab[i + 1];
for (i = 0; s_known_types[i].ptr != NULL; i += 2) {
if (mg_strcmp(path, s_known_types[i]) == 0) return s_known_types[i + 1];
}
return mg_str("text/plain; charset=utf-8");

View File

@ -354,12 +354,10 @@ static void static_cb(struct mg_connection *c, int ev, void *ev_data,
(void) ev_data;
}
static struct mg_str guess_content_type(struct mg_str path, const char *extra) {
struct mg_str k, v, s = mg_str(extra);
size_t i = 0;
// Known mime types. Keep it outside guess_content_type() function, since
// some environments don't like it defined there.
// clang-format off
struct mg_str tab[] = {
static struct mg_str s_known_types[] = {
MG_C_STR("html"), MG_C_STR("text/html; charset=utf-8"),
MG_C_STR("htm"), MG_C_STR("text/html; charset=utf-8"),
MG_C_STR("css"), MG_C_STR("text/css; charset=utf-8"),
@ -394,6 +392,10 @@ static struct mg_str guess_content_type(struct mg_str path, const char *extra) {
};
// clang-format on
static struct mg_str guess_content_type(struct mg_str path, const char *extra) {
struct mg_str k, v, s = mg_str(extra);
size_t i = 0;
// Shrink path to its extension only
while (i < path.len && path.ptr[path.len - i - 1] != '.') i++;
path.ptr += path.len - i;
@ -405,8 +407,8 @@ static struct mg_str guess_content_type(struct mg_str path, const char *extra) {
}
// Process built-in mime types
for (i = 0; tab[i].ptr != NULL; i += 2) {
if (mg_strcmp(path, tab[i]) == 0) return tab[i + 1];
for (i = 0; s_known_types[i].ptr != NULL; i += 2) {
if (mg_strcmp(path, s_known_types[i]) == 0) return s_known_types[i + 1];
}
return mg_str("text/plain; charset=utf-8");