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;
// clang-format off
struct mg_str tab[] = {
// Known mime types. Keep it outside guess_content_type() function, since
// some environments don't like it defined there.
// clang-format off
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"),
@ -1795,8 +1793,12 @@ static struct mg_str guess_content_type(struct mg_str path, const char *extra) {
MG_C_STR("zip"), MG_C_STR("application/zip"),
MG_C_STR("3gp"), MG_C_STR("video/3gpp"),
{0, 0},
};
// clang-format on
};
// 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++;
@ -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;
// clang-format off
struct mg_str tab[] = {
// Known mime types. Keep it outside guess_content_type() function, since
// some environments don't like it defined there.
// clang-format off
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"),
@ -391,8 +389,12 @@ static struct mg_str guess_content_type(struct mg_str path, const char *extra) {
MG_C_STR("zip"), MG_C_STR("application/zip"),
MG_C_STR("3gp"), MG_C_STR("video/3gpp"),
{0, 0},
};
// clang-format on
};
// 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++;
@ -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");