diff --git a/core/core.js b/core/core.js index 043b74fc..f767c44b 100644 --- a/core/core.js +++ b/core/core.js @@ -812,45 +812,6 @@ async function getProcessBlob(blobId, key, options) { return process; } -/** - * TODOC - * @param {*} response - * @param {*} data - * @param {*} type - * @param {*} headers - * @param {*} status_code - */ -function sendData(response, data, type, headers, status_code) { - if (data) { - response.writeHead( - status_code ?? 200, - Object.assign( - { - 'Content-Type': - type || - httpd.mime_type_from_magic_bytes(data) || - 'application/binary', - 'Content-Length': data.byteLength, - }, - headers || {} - ) - ); - response.end(data); - } else { - response.writeHead( - status_code ?? 404, - Object.assign( - { - 'Content-Type': 'text/plain; charset=utf-8', - 'Content-Length': 'File not found'.length, - }, - headers || {} - ) - ); - response.end('File not found'); - } -} - ssb.addEventListener('message', function () { broadcastEvent('onMessage', [...arguments]); }); diff --git a/src/httpd.js.c b/src/httpd.js.c index 62ccc5ee..64592b21 100644 --- a/src/httpd.js.c +++ b/src/httpd.js.c @@ -560,7 +560,7 @@ static bool _magic_bytes_match(const magic_bytes_t* magic, const uint8_t* actual return true; } -static const char* _httpd_mime_type_from_magic_bytes_internal(const uint8_t* bytes, size_t size) +static const char* _httpd_mime_type_from_magic_bytes(const uint8_t* bytes, size_t size) { const char* type = "application/binary"; if (bytes) @@ -637,13 +637,6 @@ static const char* _httpd_mime_type_from_magic_bytes_internal(const uint8_t* byt 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) { if (ext) @@ -676,14 +669,6 @@ static const char* _ext_to_content_type(const char* ext, bool use_fallback) return use_fallback ? "application/binary" : NULL; } -static JSValue _httpd_mime_type_from_extension(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) -{ - const char* name = JS_ToCString(context, argv[0]); - const char* type = _ext_to_content_type(strrchr(name, '.'), false); - JS_FreeCString(context, name); - return type ? JS_NewString(context, type) : JS_UNDEFINED; -} - static void _httpd_finalizer(JSRuntime* runtime, JSValue value) { tf_http_t* http = JS_GetOpaque(value, _httpd_class_id); @@ -1130,7 +1115,7 @@ static void _httpd_endpoint_app_blob_after_work(tf_ssb_t* ssb, int status, void* const char* mime_type = _ext_to_content_type(strrchr(data->request->path, '.'), false); if (!mime_type) { - mime_type = _httpd_mime_type_from_magic_bytes_internal(data->data, data->size); + mime_type = _httpd_mime_type_from_magic_bytes(data->data, data->size); } const char* headers[] = { "Access-Control-Allow-Origin", @@ -1240,7 +1225,7 @@ static void _httpd_endpoint_view_after_work(tf_ssb_t* ssb, int status, void* use "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", + view->data ? _httpd_mime_type_from_magic_bytes(view->data, view->size) : "text/plain", filename ? "Content-Disposition" : NULL, filename ? content_disposition : NULL, }; @@ -2264,13 +2249,10 @@ void tf_httpd_register(JSContext* context) tf_http_add_handler(http, "/login/logout", _httpd_endpoint_logout, NULL, task); tf_http_add_handler(http, "/login", _httpd_endpoint_login, NULL, task); - JS_SetPropertyStr(context, httpd, "handlers", JS_NewObject(context)); JS_SetPropertyStr(context, httpd, "all", JS_NewCFunction(context, _httpd_endpoint_all, "all", 2)); JS_SetPropertyStr(context, httpd, "start", JS_NewCFunction(context, _httpd_endpoint_start, "start", 2)); JS_SetPropertyStr(context, httpd, "set_http_redirect", JS_NewCFunction(context, _httpd_set_http_redirect, "set_http_redirect", 1)); JS_SetPropertyStr(context, httpd, "auth_query", JS_NewCFunction(context, _httpd_auth_query, "auth_query", 1)); - JS_SetPropertyStr(context, httpd, "mime_type_from_magic_bytes", JS_NewCFunction(context, _httpd_mime_type_from_magic_bytes, "mime_type_from_magic_bytes", 1)); - JS_SetPropertyStr(context, httpd, "mime_type_from_extension", JS_NewCFunction(context, _httpd_mime_type_from_extension, "mime_type_from_extension", 1)); JS_SetPropertyStr(context, global, "httpd", httpd); JS_FreeValue(context, global); }