diff --git a/src/httpd.js.c b/src/httpd.js.c index 6d29090e..597c9bcc 100644 --- a/src/httpd.js.c +++ b/src/httpd.js.c @@ -557,14 +557,11 @@ static bool _magic_bytes_match(const magic_bytes_t* magic, const uint8_t* actual return true; } -static JSValue _httpd_mime_type_from_magic_bytes(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) +static const char* _httpd_mime_type_from_magic_bytes_internal(const uint8_t* bytes, size_t size) { - JSValue result = JS_UNDEFINED; - size_t size = 0; - uint8_t* bytes = tf_util_try_get_array_buffer(context, &size, argv[0]); + const char* type = "application/binary"; if (bytes) { - const magic_bytes_t k_magic_bytes[] = { { .type = "image/jpeg", @@ -629,12 +626,19 @@ static JSValue _httpd_mime_type_from_magic_bytes(JSContext* context, JSValueCons { if (_magic_bytes_match(&k_magic_bytes[i], bytes, size)) { - result = JS_NewString(context, k_magic_bytes[i].type); + type = k_magic_bytes[i].type; break; } } } - return result; + return type; +} + +static JSValue _httpd_mime_type_from_magic_bytes(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) +{ + size_t size = 0; + uint8_t* bytes = tf_util_try_get_array_buffer(context, &size, argv[0]); + return JS_NewString(context, _httpd_mime_type_from_magic_bytes_internal(bytes, size)); } static const char* _ext_to_content_type(const char* ext, bool use_fallback) @@ -1046,6 +1050,7 @@ static void _httpd_endpoint_view_after_work(tf_ssb_t* ssb, int status, void* use } const char* headers[] = { "Content-Security-Policy", "sandbox allow-downloads allow-top-navigation-by-user-activation", + "Content-Type", view->data ? _httpd_mime_type_from_magic_bytes_internal(view->data, view->size) : "text/plain", filename ? "Content-Disposition" : NULL, filename ? content_disposition : NULL, }; int count = filename ? tf_countof(headers) / 2 : (tf_countof(headers) / 2 - 1);