aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins
diff options
context:
space:
mode:
authorDave Barach <dave@barachs.net>2022-12-07 14:19:15 -0500
committerFlorin Coras <florin.coras@gmail.com>2022-12-08 15:40:02 +0000
commitc0a2527a83dde2c23dad2bb0d28ef4cf64da6c6c (patch)
tree5da971021d700a99651f876dc8e85d3eb7fe1d29 /src/plugins
parent919fdad6bc54d0ed6e2ac842770e9e77c5cbc0a6 (diff)
http_static: derive mime type from file extensions
Type: improvement Signed-off-by: Dave Barach <dave@barachs.net> Change-Id: I0f087477e257f5119d7d6182d19f8796773a1f19
Diffstat (limited to 'src/plugins')
-rw-r--r--src/plugins/http/http.c2
-rw-r--r--src/plugins/http/http.h86
-rw-r--r--src/plugins/http_static/http_static.h5
-rw-r--r--src/plugins/http_static/static_server.c47
4 files changed, 132 insertions, 8 deletions
diff --git a/src/plugins/http/http.c b/src/plugins/http/http.c
index 27801e8c34f..d25123a757d 100644
--- a/src/plugins/http/http.c
+++ b/src/plugins/http/http.c
@@ -37,7 +37,7 @@ const char *http_status_code_str[] = {
};
const char *http_content_type_str[] = {
-#define _(s, str) str,
+#define _(s, ext, str) str,
foreach_http_content_type
#undef _
};
diff --git a/src/plugins/http/http.h b/src/plugins/http/http.h
index b11e0da580d..50203dcd2f9 100644
--- a/src/plugins/http/http.h
+++ b/src/plugins/http/http.h
@@ -80,15 +80,89 @@ typedef enum http_msg_type_
} http_msg_type_t;
#define foreach_http_content_type \
- _ (TEXT_HTML, "text/html") \
- _ (TEXT_CSS, "text/css") \
- _ (TEXT_JS, "text/javascript") \
- _ (APP_JSON, "application/json") \
- _ (APP_OCTET_STREAM, "application/octet-stream")
+ _ (APP_7Z, ".7z", "application / x - 7z - compressed") \
+ _ (APP_DOC, ".doc", "application / msword") \
+ _ (APP_DOCX, ".docx", \
+ "application / vnd.openxmlformats - " \
+ "officedocument.wordprocessingml.document") \
+ _ (APP_EPUB, ".epub", "application / epub + zip") \
+ _ (APP_FONT, ".eot", "application / vnd.ms - fontobject") \
+ _ (APP_JAR, ".jar", "application / java - archive") \
+ _ (APP_JSON, ".json", "application / json") \
+ _ (APP_JSON_LD, ".jsonld", "application / ld + json") \
+ _ (APP_MPKG, ".mpkg", "application / vnd.apple.installer + xml") \
+ _ (APP_ODP, ".odp", "application / vnd.oasis.opendocument.presentation") \
+ _ (APP_ODS, ".ods", "application / vnd.oasis.opendocument.spreadsheet") \
+ _ (APP_ODT, ".odt", "application / vnd.oasis.opendocument.text") \
+ _ (APP_OGX, ".ogx", "application / ogg") \
+ _ (APP_PDF, ".pdf", "application / pdf") \
+ _ (APP_PHP, ".php", "application / x - httpd - php") \
+ _ (APP_PPT, ".ppt", "application / vnd.ms - powerpoint") \
+ _ (APP_PPTX, ".pptx", "application / vnd.ms - powerpoint") \
+ _ (APP_RAR, ".rar", "application / vnd.rar") \
+ _ (APP_RTF, ".rtf", "application / rtf") \
+ _ (APP_SH, ".sh", "application / x - sh") \
+ _ (APP_TAR, ".tar", "application / x - tar") \
+ _ (APP_VSD, ".vsd", "application / vnd.visio") \
+ _ (APP_XHTML, ".xhtml", "application / xhtml + xml") \
+ _ (APP_XLS, ".xls", "application / vnd.ms - excel") \
+ _ (APP_XML, ".xml", "application / xml") \
+ _ (APP_XSLX, ".xlsx", \
+ "application / vnd.openxmlformats - officedocument.spreadsheetml.sheet") \
+ _ (APP_XUL, ".xul", "application / vnd.mozilla.xul + xml") \
+ _ (APP_ZIP, ".zip", "application / zip") \
+ _ (AUDIO_AAC, ".aac", "audio / aac") \
+ _ (AUDIO_CD, ".cda", "application / x - cdf") \
+ _ (AUDIO_WAV, ".wav", "audio / wav") \
+ _ (AUDIO_WEBA, ".weba", "audio / webm") \
+ _ (AUDO_MIDI, ".midi", "audio / midi") \
+ _ (AUDO_MID, ".mid", "audo / midi") \
+ _ (AUDO_MP3, ".mp3", "audio / mpeg") \
+ _ (AUDO_OGA, ".oga", "audio / ogg") \
+ _ (AUDO_OPUS, ".opus", "audio / opus") \
+ _ (APP_OCTET_STREAM, ".bin", "application / octet - stream") \
+ _ (BZIP2, ".bz2", "application / x - bzip2") \
+ _ (BZIP, ".bz", "application / x - bzip") \
+ _ (FONT_OTF, ".otf", "font / otf") \
+ _ (FONT_TTF, ".ttf", "font / ttf") \
+ _ (FONT_WOFF2, ".woff2", "font / woff2") \
+ _ (FONT_WOFF, ".woff", "font / woff") \
+ _ (GZIP, ".gz", "application / gzip") \
+ _ (IMAGE_AVIF, ".avif", "image / avif") \
+ _ (IMAGE_BMP, ".bmp", "image / bmp") \
+ _ (IMAGE_GIF, ".gif", "image / gif") \
+ _ (IMAGE_ICON, ".ico", "image / vnd.microsoft.icon") \
+ _ (IMAGE_JPEG, ".jpeg", "image / jpeg") \
+ _ (IMAGE_JPG, ".jpg", "image / jpeg") \
+ _ (IMAGE_PNG, ".png", "image / png") \
+ _ (IMAGE_SVG, ".svg", "image / svg + xml") \
+ _ (IMAGE_TIFF, ".tiff", "image / tiff") \
+ _ (IMAGE_TIF, ".tif", "image / tiff") \
+ _ (IMAGE_WEBP, ".webp", "image / webp") \
+ _ (SCRIPT_CSH, ".csh", "application / x - csh") \
+ _ (TEXT_ABIWORD, ".abw", "application / x - abiword") \
+ _ (TEXT_ARCHIVE, ".arc", "application / x - freearc") \
+ _ (TEXT_AZW, ".azw", "application / vnd.amazon.ebook") \
+ _ (TEXT_CALENDAR, ".ics", "text / calendar") \
+ _ (TEXT_CSS, ".css", "text / css") \
+ _ (TEXT_CSV, ".csv", "text / csv") \
+ _ (TEXT_HTM, ".htm", "text / html") \
+ _ (TEXT_HTML, ".html", "text / html") \
+ _ (TEXT_JS, ".js", "text / javascript") \
+ _ (TEXT_MJS, ".mjs", "text / javascript") \
+ _ (TEXT_PLAIN, ".txt", "text / plain") \
+ _ (VIDEO_3GP2, ".3g2", "video / 3gpp2") \
+ _ (VIDEO_3GP, ".3gp", "video / 3gpp") \
+ _ (VIDEO_AVI, ".avi", "video / x - msvideo") \
+ _ (VIDEO_MP4, ".mp4", "video / mp4") \
+ _ (VIDEO_MPEG, ".mpeg", "video / mpeg") \
+ _ (VIDEO_OGG, ".ogv", "video / ogg") \
+ _ (VIDEO_TS, ".ts", "video / mp2t") \
+ _ (VIDEO_WEBM, ".webm", "video / webm")
typedef enum http_content_type_
{
-#define _(s, str) HTTP_CONTENT_##s,
+#define _(s, ext, str) HTTP_CONTENT_##s,
foreach_http_content_type
#undef _
} http_content_type_t;
diff --git a/src/plugins/http_static/http_static.h b/src/plugins/http_static/http_static.h
index 5ea1a6eab8f..2850d356b74 100644
--- a/src/plugins/http_static/http_static.h
+++ b/src/plugins/http_static/http_static.h
@@ -50,6 +50,8 @@ typedef struct
int free_data;
/** File cache pool index */
u32 cache_pool_index;
+ /** Content type, e.g. text, text/javascript, etc. */
+ http_content_type_t content_type;
} hss_session_t;
typedef struct hss_session_handle_
@@ -150,6 +152,9 @@ typedef struct
u8 enable_url_handlers;
/** Max cache size before LRU occurs */
u64 cache_size;
+
+ /** hash table of file extensions to mime types string indices */
+ uword *mime_type_indices_by_file_extensions;
} hss_main_t;
extern hss_main_t hss_main;
diff --git a/src/plugins/http_static/static_server.c b/src/plugins/http_static/static_server.c
index a76cb87a2ce..4d0373a31b6 100644
--- a/src/plugins/http_static/static_server.c
+++ b/src/plugins/http_static/static_server.c
@@ -89,7 +89,7 @@ start_send_data (hss_session_t *hs, http_status_code_t status)
msg.type = HTTP_MSG_REPLY;
msg.code = status;
- msg.content_type = HTTP_CONTENT_TEXT_HTML;
+ msg.content_type = hs->content_type;
msg.data.len = hs->data_len;
if (hs->data_len > hss_main.use_ptr_thresh)
@@ -145,6 +145,33 @@ hss_session_send_data (hss_url_handler_args_t *args)
start_send_data (hs, args->sc);
}
+static http_content_type_t
+content_type_from_request (u8 *request)
+{
+ u8 *ext;
+ uword *p;
+ /* default to text/html */
+ http_content_type_t rv = HTTP_CONTENT_TEXT_HTML;
+
+ ASSERT (vec_len (request) > 0);
+
+ ext = request + vec_len (request) - 1;
+
+ while (ext > request && ext[0] != '.')
+ ext--;
+
+ if (ext == request)
+ return rv;
+
+ p = hash_get_mem (hss_main.mime_type_indices_by_file_extensions, ext);
+
+ if (p == 0)
+ return rv;
+
+ rv = p[0];
+ return rv;
+}
+
static int
try_url_handler (hss_main_t *hsm, hss_session_t *hs, http_req_method_t rt,
u8 *request)
@@ -152,11 +179,14 @@ try_url_handler (hss_main_t *hsm, hss_session_t *hs, http_req_method_t rt,
http_status_code_t sc = HTTP_STATUS_OK;
hss_url_handler_args_t args = {};
uword *p, *url_table;
+ http_content_type_t type;
int rv;
if (!hsm->enable_url_handlers || !request)
return -1;
+ type = content_type_from_request (request);
+
/* Look for built-in GET / POST handlers */
url_table =
(rt == HTTP_REQ_GET) ? hsm->get_url_handlers : hsm->post_url_handlers;
@@ -193,6 +223,7 @@ try_url_handler (hss_main_t *hsm, hss_session_t *hs, http_req_method_t rt,
hs->data = args.data;
hs->data_len = args.data_len;
hs->free_data = args.free_vec_data;
+ hs->content_type = type;
start_send_data (hs, sc);
@@ -284,11 +315,14 @@ try_file_handler (hss_main_t *hsm, hss_session_t *hs, http_req_method_t rt,
http_status_code_t sc = HTTP_STATUS_OK;
u8 *path;
u32 ce_index;
+ http_content_type_t type;
/* Feature not enabled */
if (!hsm->www_root)
return -1;
+ type = content_type_from_request (request);
+
/*
* Construct the file to open
* Browsers are capable of sporadically including a leading '/'
@@ -331,6 +365,7 @@ try_file_handler (hss_main_t *hsm, hss_session_t *hs, http_req_method_t rt,
done:
+ hs->content_type = type;
start_send_data (hs, sc);
if (!hs->data)
hss_session_disconnect_transport (hs);
@@ -908,6 +943,16 @@ hss_main_init (vlib_main_t *vm)
hsm->app_index = ~0;
hsm->vlib_main = vm;
+ /* Set up file extension to mime type index map */
+ hsm->mime_type_indices_by_file_extensions =
+ hash_create_string (0, sizeof (uword));
+
+#define _(def, ext, str) \
+ hash_set_mem (hsm->mime_type_indices_by_file_extensions, ext, \
+ HTTP_CONTENT_##def);
+ foreach_http_content_type;
+#undef _
+
return 0;
}