Commit 39215f69 authored by Sergey Lyubka's avatar Sergey Lyubka

Exported mg_get_builtin_mime_type()

parent df7da95c
...@@ -1707,58 +1707,76 @@ static const struct { ...@@ -1707,58 +1707,76 @@ static const struct {
const char *extension; const char *extension;
size_t ext_len; size_t ext_len;
const char *mime_type; const char *mime_type;
size_t mime_type_len;
} builtin_mime_types[] = { } builtin_mime_types[] = {
{".html", 5, "text/html", 9}, {".html", 5, "text/html"},
{".htm", 4, "text/html", 9}, {".htm", 4, "text/html"},
{".shtm", 5, "text/html", 9}, {".shtm", 5, "text/html"},
{".shtml", 6, "text/html", 9}, {".shtml", 6, "text/html"},
{".css", 4, "text/css", 8}, {".css", 4, "text/css"},
{".js", 3, "application/x-javascript", 24}, {".js", 3, "application/x-javascript"},
{".ico", 4, "image/x-icon", 12}, {".ico", 4, "image/x-icon"},
{".gif", 4, "image/gif", 9}, {".gif", 4, "image/gif"},
{".jpg", 4, "image/jpeg", 10}, {".jpg", 4, "image/jpeg"},
{".jpeg", 5, "image/jpeg", 10}, {".jpeg", 5, "image/jpeg"},
{".png", 4, "image/png", 9}, {".png", 4, "image/png"},
{".svg", 4, "image/svg+xml", 13}, {".svg", 4, "image/svg+xml"},
{".torrent", 8, "application/x-bittorrent", 24}, {".txt", 4, "text/plain"},
{".wav", 4, "audio/x-wav", 11}, {".torrent", 8, "application/x-bittorrent"},
{".mp3", 4, "audio/x-mp3", 11}, {".wav", 4, "audio/x-wav"},
{".mid", 4, "audio/mid", 9}, {".mp3", 4, "audio/x-mp3"},
{".m3u", 4, "audio/x-mpegurl", 15}, {".mid", 4, "audio/mid"},
{".ram", 4, "audio/x-pn-realaudio", 20}, {".m3u", 4, "audio/x-mpegurl"},
{".xml", 4, "text/xml", 8}, {".ram", 4, "audio/x-pn-realaudio"},
{".xslt", 5, "application/xml", 15}, {".xml", 4, "text/xml"},
{".ra", 3, "audio/x-pn-realaudio", 20}, {".xslt", 5, "application/xml"},
{".doc", 4, "application/msword", 19}, {".ra", 3, "audio/x-pn-realaudio"},
{".exe", 4, "application/octet-stream", 24}, {".doc", 4, "application/msword"},
{".zip", 4, "application/x-zip-compressed", 28}, {".exe", 4, "application/octet-stream"},
{".xls", 4, "application/excel", 17}, {".zip", 4, "application/x-zip-compressed"},
{".tgz", 4, "application/x-tar-gz", 20}, {".xls", 4, "application/excel"},
{".tar", 4, "application/x-tar", 17}, {".tgz", 4, "application/x-tar-gz"},
{".gz", 3, "application/x-gunzip", 20}, {".tar", 4, "application/x-tar"},
{".arj", 4, "application/x-arj-compressed", 28}, {".gz", 3, "application/x-gunzip"},
{".rar", 4, "application/x-arj-compressed", 28}, {".arj", 4, "application/x-arj-compressed"},
{".rtf", 4, "application/rtf", 15}, {".rar", 4, "application/x-arj-compressed"},
{".pdf", 4, "application/pdf", 15}, {".rtf", 4, "application/rtf"},
{".swf", 4, "application/x-shockwave-flash",29}, {".pdf", 4, "application/pdf"},
{".mpg", 4, "video/mpeg", 10}, {".swf", 4, "application/x-shockwave-flash"},
{".mpeg", 5, "video/mpeg", 10}, {".mpg", 4, "video/mpeg"},
{".mp4", 4, "video/mp4", 9}, {".webm", 5, "video/webm"},
{".m4v", 4, "video/x-m4v", 11}, {".mpeg", 5, "video/mpeg"},
{".asf", 4, "video/x-ms-asf", 14}, {".mp4", 4, "video/mp4"},
{".avi", 4, "video/x-msvideo", 15}, {".m4v", 4, "video/x-m4v"},
{".bmp", 4, "image/bmp", 9}, {".asf", 4, "video/x-ms-asf"},
{NULL, 0, NULL, 0} {".avi", 4, "video/x-msvideo"},
{".bmp", 4, "image/bmp"},
{NULL, 0, NULL}
}; };
const char *mg_get_builtin_mime_type(const char *path) {
const char *ext;
size_t i, path_len;
path_len = strlen(path);
for (i = 0; builtin_mime_types[i].extension != NULL; i++) {
ext = path + (path_len - builtin_mime_types[i].ext_len);
if (path_len > builtin_mime_types[i].ext_len &&
mg_strcasecmp(ext, builtin_mime_types[i].extension) == 0) {
return builtin_mime_types[i].mime_type;
}
}
return "text/plain";
}
// Look at the "path" extension and figure what mime type it has. // Look at the "path" extension and figure what mime type it has.
// Store mime type in the vector. // Store mime type in the vector.
static void get_mime_type(struct mg_context *ctx, const char *path, static void get_mime_type(struct mg_context *ctx, const char *path,
struct vec *vec) { struct vec *vec) {
struct vec ext_vec, mime_vec; struct vec ext_vec, mime_vec;
const char *list, *ext; const char *list, *ext;
size_t i, path_len; size_t path_len;
path_len = strlen(path); path_len = strlen(path);
...@@ -1774,20 +1792,8 @@ static void get_mime_type(struct mg_context *ctx, const char *path, ...@@ -1774,20 +1792,8 @@ static void get_mime_type(struct mg_context *ctx, const char *path,
} }
} }
// Now scan built-in mime types vec->ptr = mg_get_builtin_mime_type(path);
for (i = 0; builtin_mime_types[i].extension != NULL; i++) { vec->len = strlen(vec->ptr);
ext = path + (path_len - builtin_mime_types[i].ext_len);
if (path_len > builtin_mime_types[i].ext_len &&
mg_strcasecmp(ext, builtin_mime_types[i].extension) == 0) {
vec->ptr = builtin_mime_types[i].mime_type;
vec->len = builtin_mime_types[i].mime_type_len;
return;
}
}
// Nothing found. Fall back to "text/plain"
vec->ptr = "text/plain";
vec->len = 10;
} }
#ifndef HAVE_MD5 #ifndef HAVE_MD5
......
...@@ -250,6 +250,11 @@ typedef void * (*mg_thread_func_t)(void *); ...@@ -250,6 +250,11 @@ typedef void * (*mg_thread_func_t)(void *);
int mg_start_thread(mg_thread_func_t f, void *p); int mg_start_thread(mg_thread_func_t f, void *p);
// Return builtin mime type for the given file name.
// For unrecognized extensions, "text/plain" is returned.
const char *mg_get_builtin_mime_type(const char *file_name);
// Platform independent string utility functions. // Platform independent string utility functions.
void mg_strlcpy(char *dst, const char *src, size_t n); void mg_strlcpy(char *dst, const char *src, size_t n);
int mg_strncasecmp(const char *s1, const char *s2, size_t len); int mg_strncasecmp(const char *s1, const char *s2, size_t len);
......
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