http: Fix some headers.

This commit is contained in:
Cory McWilliams 2024-10-16 19:26:26 -04:00
parent 72def5ae6d
commit 97661e2ca2

View File

@ -557,14 +557,11 @@ static bool _magic_bytes_match(const magic_bytes_t* magic, const uint8_t* actual
return true; 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; const char* type = "application/binary";
size_t size = 0;
uint8_t* bytes = tf_util_try_get_array_buffer(context, &size, argv[0]);
if (bytes) if (bytes)
{ {
const magic_bytes_t k_magic_bytes[] = { const magic_bytes_t k_magic_bytes[] = {
{ {
.type = "image/jpeg", .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)) 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; 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) 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[] = { const char* headers[] = {
"Content-Security-Policy", "sandbox allow-downloads allow-top-navigation-by-user-activation", "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, filename ? "Content-Disposition" : NULL, filename ? content_disposition : NULL,
}; };
int count = filename ? tf_countof(headers) / 2 : (tf_countof(headers) / 2 - 1); int count = filename ? tf_countof(headers) / 2 : (tf_countof(headers) / 2 - 1);