#include "httpd.js.h" #include "file.js.h" #include "http.h" #include "log.h" #include "mem.h" #include "ssb.db.h" #include "ssb.ebt.h" #include "ssb.h" #include "task.h" #include "tls.h" #include "tlscontext.js.h" #include "trace.h" #include "util.js.h" #include "version.h" #include "ow-crypt.h" #include "picohttpparser.h" #include "sodium/crypto_sign.h" #include "sodium/utils.h" #include #include #include #include #include #if !defined(__APPLE__) && !defined(__OpenBSD__) && !defined(_WIN32) #include #endif #define CYAN "\e[1;36m" #define MAGENTA "\e[1;35m" #define YELLOW "\e[1;33m" #define RESET "\e[0m" const int64_t k_refresh_interval = 1ULL * 7 * 24 * 60 * 60 * 1000; static JSValue _authenticate_jwt(tf_ssb_t* ssb, JSContext* context, const char* jwt); static JSValue _httpd_websocket_upgrade(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv); static bool _is_name_valid(const char* name); static const char* _make_session_jwt(JSContext* context, tf_ssb_t* ssb, const char* name); static const char* _make_set_session_cookie_header(tf_http_request_t* request, const char* session_cookie); const char** _form_data_decode(const char* data, int length); const char* _form_data_get(const char** form_data, const char* key); static JSClassID _httpd_request_class_id; typedef struct _http_user_data_t { char redirect[1024]; } http_user_data_t; typedef struct _http_handler_data_t { JSContext* context; JSValue callback; } http_handler_data_t; static JSValue _httpd_response_write_head(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) { JS_SetPropertyStr(context, this_val, "response_status", JS_DupValue(context, argv[0])); JS_SetPropertyStr(context, this_val, "response_headers", JS_DupValue(context, argv[1])); return JS_UNDEFINED; } static int _object_to_headers(JSContext* context, JSValue object, const char** headers, int headers_length) { int count = 0; JSPropertyEnum* ptab = NULL; uint32_t plen = 0; JS_GetOwnPropertyNames(context, &ptab, &plen, object, JS_GPN_STRING_MASK); for (; count < (int)plen && count < headers_length / 2; ++count) { JSPropertyDescriptor desc; JSValue key_value = JS_NULL; if (JS_GetOwnProperty(context, &desc, object, ptab[count].atom) == 1) { key_value = desc.value; JS_FreeValue(context, desc.setter); JS_FreeValue(context, desc.getter); } headers[count * 2 + 0] = JS_AtomToCString(context, ptab[count].atom); headers[count * 2 + 1] = JS_ToCString(context, key_value); JS_FreeValue(context, key_value); } for (uint32_t i = 0; i < plen; ++i) { JS_FreeAtom(context, ptab[i].atom); } js_free(context, ptab); return count; } static JSValue _httpd_response_end(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) { tf_http_request_t* request = JS_GetOpaque(this_val, _httpd_request_class_id); size_t length = 0; const char* cstring = NULL; const void* data = NULL; JSValue buffer = JS_UNDEFINED; if (JS_IsString(argv[0])) { cstring = JS_ToCStringLen(context, &length, argv[0]); data = cstring; } else if ((data = tf_util_try_get_array_buffer(context, &length, argv[0])) != 0) { } else { size_t offset; size_t size; size_t element_size; buffer = tf_util_try_get_typed_array_buffer(context, argv[0], &offset, &size, &element_size); if (!JS_IsException(buffer)) { data = tf_util_try_get_array_buffer(context, &length, buffer); } } JSValue response_status = JS_GetPropertyStr(context, this_val, "response_status"); int status = 0; JS_ToInt32(context, &status, response_status); JS_FreeValue(context, response_status); const char* headers[64] = { 0 }; JSValue response_headers = JS_GetPropertyStr(context, this_val, "response_headers"); int headers_count = _object_to_headers(context, response_headers, headers, tf_countof(headers)); JS_FreeValue(context, response_headers); tf_http_respond(request, status, headers, headers_count, data, length); for (int i = 0; i < headers_count * 2; i++) { JS_FreeCString(context, headers[i]); } JS_FreeValue(context, buffer); if (cstring) { JS_FreeCString(context, cstring); } return JS_UNDEFINED; } static JSValue _httpd_response_send(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) { tf_http_request_t* request = JS_GetOpaque(this_val, _httpd_request_class_id); int opcode = 0x1; JS_ToInt32(context, &opcode, argv[1]); size_t length = 0; const char* message = JS_ToCStringLen(context, &length, argv[0]); tf_http_request_websocket_send(request, opcode, message, length); JS_FreeCString(context, message); return JS_UNDEFINED; } static void _httpd_websocket_close_callback(tf_http_request_t* request) { JSContext* context = request->context; JSValue response_object = JS_MKPTR(JS_TAG_OBJECT, request->user_data); JSValue on_close = JS_GetPropertyStr(context, response_object, "onClose"); JSValue response = JS_Call(context, on_close, JS_UNDEFINED, 0, NULL); tf_util_report_error(context, response); JS_FreeValue(context, response); JS_FreeValue(context, on_close); JS_SetPropertyStr(context, response_object, "onMessage", JS_UNDEFINED); JS_SetPropertyStr(context, response_object, "onClose", JS_UNDEFINED); JS_FreeValue(context, response_object); } static void _httpd_message_callback(tf_http_request_t* request, int op_code, const void* data, size_t size) { JSContext* context = request->context; JSValue response_object = JS_MKPTR(JS_TAG_OBJECT, request->user_data); JSValue on_message = JS_GetPropertyStr(context, response_object, "onMessage"); JSValue event = JS_NewObject(context); JS_SetPropertyStr(context, event, "opCode", JS_NewInt32(context, op_code)); JS_SetPropertyStr(context, event, "data", JS_NewStringLen(context, data, size)); JSValue response = JS_Call(context, on_message, JS_UNDEFINED, 1, &event); tf_util_report_error(context, response); JS_FreeValue(context, response); JS_FreeValue(context, event); JS_FreeValue(context, on_message); } static JSValue _httpd_make_response_object(JSContext* context, tf_http_request_t* request) { JSValue response_object = JS_NewObjectClass(context, _httpd_request_class_id); JS_SetOpaque(response_object, request); JS_SetPropertyStr(context, response_object, "writeHead", JS_NewCFunction(context, _httpd_response_write_head, "writeHead", 2)); JS_SetPropertyStr(context, response_object, "end", JS_NewCFunction(context, _httpd_response_end, "end", 1)); JS_SetPropertyStr(context, response_object, "send", JS_NewCFunction(context, _httpd_response_send, "send", 2)); JS_SetPropertyStr(context, response_object, "upgrade", JS_NewCFunction(context, _httpd_websocket_upgrade, "upgrade", 2)); return response_object; } static bool _httpd_redirect(tf_http_request_t* request) { if (request->is_tls) { return false; } http_user_data_t* user_data = tf_http_get_user_data(request->http); if (!user_data || !*user_data->redirect) { return false; } char redirect[1024]; snprintf(redirect, sizeof(redirect), "%s%s", user_data->redirect, request->path); tf_http_respond(request, 303, (const char*[]) { "Location", redirect }, 1, NULL, 0); return true; } static JSValue _httpd_websocket_upgrade(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) { tf_http_request_t* request = JS_GetOpaque(this_val, _httpd_request_class_id); tf_http_request_ref(request); const char* header_connection = tf_http_request_get_header(request, "connection"); const char* header_upgrade = tf_http_request_get_header(request, "upgrade"); const char* header_sec_websocket_key = tf_http_request_get_header(request, "sec-websocket-key"); if (header_connection && header_upgrade && header_sec_websocket_key && strstr(header_connection, "Upgrade") && strcasecmp(header_upgrade, "websocket") == 0) { static const char* k_magic = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; size_t key_length = strlen(header_sec_websocket_key); size_t size = key_length + 36; uint8_t* key_magic = alloca(size); memcpy(key_magic, header_sec_websocket_key, key_length); memcpy(key_magic + key_length, k_magic, 36); uint8_t digest[20]; SHA1(key_magic, size, digest); char key[41] = { 0 }; tf_base64_encode(digest, sizeof(digest), key, sizeof(key)); const char* headers[64] = { 0 }; int headers_count = 0; headers[headers_count * 2 + 0] = "Upgrade"; headers[headers_count * 2 + 1] = "websocket"; headers_count++; headers[headers_count * 2 + 0] = "Connection"; headers[headers_count * 2 + 1] = "Upgrade"; headers_count++; headers[headers_count * 2 + 0] = "Sec-WebSocket-Accept"; headers[headers_count * 2 + 1] = key; headers_count++; tf_ssb_t* ssb = tf_task_get_ssb(tf_task_get(context)); const char* session = tf_http_get_cookie(tf_http_request_get_header(request, "cookie"), "session"); JSValue jwt = _authenticate_jwt(ssb, context, session); tf_free((void*)session); JSValue name = !JS_IsUndefined(jwt) ? JS_GetPropertyStr(context, jwt, "name") : JS_UNDEFINED; const char* name_string = !JS_IsUndefined(name) ? JS_ToCString(context, name) : NULL; const char* session_token = _make_session_jwt(tf_ssb_get_context(ssb), ssb, name_string); const char* cookie = _make_set_session_cookie_header(request, session_token); tf_free((void*)session_token); JS_FreeCString(context, name_string); JS_FreeValue(context, name); JS_FreeValue(context, jwt); headers[headers_count * 2 + 0] = "Set-Cookie"; headers[headers_count * 2 + 1] = cookie ? cookie : ""; headers_count++; bool send_version = !tf_http_request_get_header(request, "sec-websocket-version") || strcmp(tf_http_request_get_header(request, "sec-websocket-version"), "13") != 0; if (send_version) { headers[headers_count * 2 + 0] = "Sec-WebSocket-Accept"; headers[headers_count * 2 + 1] = key; headers_count++; } int js_headers_count = _object_to_headers(context, argv[1], headers + headers_count * 2, tf_countof(headers) - headers_count * 2); headers_count += js_headers_count; tf_http_request_websocket_upgrade(request); tf_http_respond(request, 101, headers, headers_count, NULL, 0); for (int i = headers_count - js_headers_count; i < headers_count * 2; i++) { JS_FreeCString(context, headers[i * 2 + 0]); JS_FreeCString(context, headers[i * 2 + 1]); } tf_free((void*)cookie); request->on_message = _httpd_message_callback; request->on_close = _httpd_websocket_close_callback; request->context = context; request->user_data = JS_VALUE_GET_PTR(JS_DupValue(context, this_val)); } else { tf_http_respond(request, 400, NULL, 0, NULL, 0); } tf_http_request_unref(request); return JS_UNDEFINED; } typedef struct _httpd_listener_t { tf_tls_context_t* tls; } httpd_listener_t; static void _httpd_listener_cleanup(void* user_data) { httpd_listener_t* listener = user_data; if (listener->tls) { tf_tls_context_destroy(listener->tls); } tf_free(listener); } typedef struct _auth_query_work_t { const char* settings; JSValue entry; JSValue result; JSValue promise[2]; } auth_query_work_t; static void _httpd_auth_query_work(tf_ssb_t* ssb, void* user_data) { auth_query_work_t* work = user_data; work->settings = tf_ssb_db_get_property(ssb, "core", "settings"); } static void _httpd_auth_query_after_work(tf_ssb_t* ssb, int status, void* user_data) { auth_query_work_t* work = user_data; JSContext* context = tf_ssb_get_context(ssb); JSValue name = JS_GetPropertyStr(context, work->entry, "name"); const char* name_string = JS_ToCString(context, name); JSValue settings_value = work->settings ? JS_ParseJSON(context, work->settings, strlen(work->settings), NULL) : JS_UNDEFINED; JSValue out_permissions = JS_NewObject(context); JS_SetPropertyStr(context, work->result, "permissions", out_permissions); JSValue permissions = !JS_IsUndefined(settings_value) ? JS_GetPropertyStr(context, settings_value, "permissions") : JS_UNDEFINED; JSValue user_permissions = !JS_IsUndefined(permissions) ? JS_GetPropertyStr(context, permissions, name_string) : JS_UNDEFINED; int length = !JS_IsUndefined(user_permissions) ? tf_util_get_length(context, user_permissions) : 0; for (int i = 0; i < length; i++) { JSValue permission = JS_GetPropertyUint32(context, user_permissions, i); const char* permission_string = JS_ToCString(context, permission); JS_SetPropertyStr(context, out_permissions, permission_string, JS_TRUE); JS_FreeCString(context, permission_string); JS_FreeValue(context, permission); } JSValue error = JS_Call(context, work->promise[0], JS_UNDEFINED, 1, &work->result); JS_FreeValue(context, work->result); tf_util_report_error(context, error); JS_FreeValue(context, error); JS_FreeValue(context, work->promise[0]); JS_FreeValue(context, work->promise[1]); JS_FreeValue(context, user_permissions); JS_FreeValue(context, permissions); JS_FreeValue(context, settings_value); tf_free((void*)work->settings); JS_FreeCString(context, name_string); JS_FreeValue(context, name); tf_free(work); } static JSValue _httpd_auth_query(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) { tf_task_t* task = tf_task_get(context); tf_ssb_t* ssb = tf_task_get_ssb(task); JSValue headers = argv[0]; if (JS_IsUndefined(headers)) { return JS_UNDEFINED; } JSValue cookie = JS_GetPropertyStr(context, headers, "cookie"); const char* cookie_string = JS_ToCString(context, cookie); const char* session = tf_http_get_cookie(cookie_string, "session"); JSValue entry = _authenticate_jwt(ssb, context, session); tf_free((void*)session); JS_FreeCString(context, cookie_string); JS_FreeValue(context, cookie); JSValue result = JS_UNDEFINED; if (!JS_IsUndefined(entry)) { JSValue value = JS_NewObject(context); JS_SetPropertyStr(context, value, "session", entry); auth_query_work_t* work = tf_malloc(sizeof(auth_query_work_t)); *work = (auth_query_work_t) { .entry = entry, .result = value, }; result = JS_NewPromiseCapability(context, work->promise); tf_ssb_run_work(ssb, _httpd_auth_query_work, _httpd_auth_query_after_work, work); } return result; } typedef struct _magic_bytes_t { const char* type; uint8_t bytes[12]; uint8_t ignore[12]; } magic_bytes_t; static bool _magic_bytes_match(const magic_bytes_t* magic, const uint8_t* actual, size_t size) { if (size < sizeof(magic->bytes)) { return false; } int length = (int)tf_min(sizeof(magic->bytes), size); for (int i = 0; i < length; i++) { if ((magic->bytes[i] & ~magic->ignore[i]) != (actual[i] & ~magic->ignore[i])) { return false; } } return true; } static const char* _httpd_mime_type_from_magic_bytes(const uint8_t* bytes, size_t size) { const char* type = "application/binary"; if (bytes) { const magic_bytes_t k_magic_bytes[] = { { .type = "image/jpeg", .bytes = { 0xff, 0xd8, 0xff, 0xdb }, }, { .type = "image/jpeg", .bytes = { 0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, 0x01 }, }, { .type = "image/jpeg", .bytes = { 0xff, 0xd8, 0xff, 0xee }, }, { .type = "image/jpeg", .bytes = { 0xff, 0xd8, 0xff, 0xe1, 0x00, 0x00, 0x45, 0x78, 0x69, 0x66, 0x00, 0x00 }, .ignore = { 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, }, { .type = "image/png", .bytes = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a }, }, { .type = "image/gif", .bytes = { 0x47, 0x49, 0x46, 0x38, 0x37, 0x61 }, }, { .type = "image/gif", .bytes = { 0x47, 0x49, 0x46, 0x38, 0x39, 0x61 }, }, { .type = "image/webp", .bytes = { 0x52, 0x49, 0x46, 0x46, 0x00, 0x00, 0x00, 0x00, 0x57, 0x45, 0x42, 0x50 }, .ignore = { 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 }, }, { .type = "image/svg+xml", .bytes = { 0x3c, 0x73, 0x76, 0x67 }, }, { .type = "audio/mpeg", .bytes = { 0x00, 0x00, 0x00, 0x00, 0x66, 0x74, 0x79, 0x70, 0x6d, 0x70, 0x34, 0x32 }, .ignore = { 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, }, { .type = "video/mp4", .bytes = { 0x00, 0x00, 0x00, 0x00, 0x66, 0x74, 0x79, 0x70, 0x69, 0x73, 0x6f, 0x6d }, .ignore = { 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, }, { .type = "video/mp4", .bytes = { 0x00, 0x00, 0x00, 0x00, 0x66, 0x74, 0x79, 0x70, 0x6d, 0x70, 0x34, 0x32 }, .ignore = { 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, }, { .type = "audio/midi", .bytes = { 0x4d, 0x54, 0x68, 0x64 }, }, }; for (int i = 0; i < tf_countof(k_magic_bytes); i++) { if (_magic_bytes_match(&k_magic_bytes[i], bytes, size)) { type = k_magic_bytes[i].type; break; } } } return type; } static const char* _ext_to_content_type(const char* ext, bool use_fallback) { if (ext) { typedef struct _ext_type_t { const char* ext; const char* type; } ext_type_t; const ext_type_t k_types[] = { { .ext = ".html", .type = "text/html; charset=UTF-8" }, { .ext = ".js", .type = "text/javascript; charset=UTF-8" }, { .ext = ".mjs", .type = "text/javascript; charset=UTF-8" }, { .ext = ".css", .type = "text/css; charset=UTF-8" }, { .ext = ".png", .type = "image/png" }, { .ext = ".json", .type = "application/json" }, { .ext = ".map", .type = "application/json" }, { .ext = ".svg", .type = "image/svg+xml" }, }; for (int i = 0; i < tf_countof(k_types); i++) { if (strcmp(ext, k_types[i].ext) == 0) { return k_types[i].type; } } } return use_fallback ? "application/binary" : NULL; } static void _httpd_request_finalizer(JSRuntime* runtime, JSValue value) { tf_http_request_t* request = JS_GetOpaque(value, _httpd_request_class_id); tf_http_request_unref(request); } static void _httpd_endpoint_trace(tf_http_request_t* request) { if (_httpd_redirect(request)) { return; } tf_task_t* task = request->user_data; tf_trace_t* trace = tf_task_get_trace(task); char* json = tf_trace_export(trace); const char* headers[] = { "Content-Type", "application/json; charset=utf-8", "Access-Control-Allow-Origin", "*", }; tf_http_respond(request, 200, headers, tf_countof(headers) / 2, json, json ? strlen(json) : 0); tf_free(json); } static void _httpd_endpoint_mem(tf_http_request_t* request) { if (_httpd_redirect(request)) { return; } char* response = NULL; size_t length = 0; int count = 0; tf_mem_allocation_t* alloc = tf_mem_summarize_allocations(&count); for (int i = 0; i < count; i++) { const char* stack = tf_util_backtrace_to_string(alloc[i].frames, alloc[i].frames_count); int line = snprintf(NULL, 0, "%zd bytes in %d allocations\n%s\n\n", alloc[i].size, alloc[i].count, stack); response = tf_resize_vec(response, length + line); snprintf(response + length, line, "%zd bytes in %d allocations\n%s\n\n", alloc[i].size, alloc[i].count, stack); length += line - 1; tf_free((void*)stack); } tf_free(alloc); const char* headers[] = { "Content-Type", "text/plain; charset=utf-8", "Access-Control-Allow-Origin", "*", }; tf_http_respond(request, 200, headers, tf_countof(headers) / 2, response, length); tf_free(response); } static const char* _after(const char* text, const char* prefix) { size_t prefix_length = strlen(prefix); if (text && strncmp(text, prefix, prefix_length) == 0) { return text + prefix_length; } return NULL; } static double _time_spec_to_double(const uv_timespec_t* time_spec) { return (double)time_spec->tv_sec + (double)(time_spec->tv_nsec) / 1e9; } typedef struct _http_file_t { tf_http_request_t* request; char etag[512]; } http_file_t; static bool _ends_with(const char* a, const char* suffix) { if (!a || !suffix) { return false; } size_t alen = strlen(a); size_t suffixlen = strlen(suffix); return alen >= suffixlen && strcmp(a + alen - suffixlen, suffix) == 0; } static void _httpd_endpoint_static_read(tf_task_t* task, const char* path, int result, const void* data, void* user_data) { http_file_t* file = user_data; tf_http_request_t* request = file->request; if (result >= 0) { if (strcmp(path, "core/tfrpc.js") == 0 || _ends_with(path, "core/tfrpc.js")) { const char* content_type = _ext_to_content_type(strrchr(path, '.'), true); const char* headers[] = { "Content-Type", content_type, "etag", file->etag, "Access-Control-Allow-Origin", "null", }; tf_http_respond(request, 200, headers, tf_countof(headers) / 2, data, result); } else { const char* content_type = _ext_to_content_type(strrchr(path, '.'), true); const char* headers[] = { "Content-Type", content_type, "etag", file->etag, }; tf_http_respond(request, 200, headers, tf_countof(headers) / 2, data, result); } } else { const char* k_payload = tf_http_status_text(404); tf_http_respond(request, 404, NULL, 0, k_payload, strlen(k_payload)); } tf_http_request_unref(request); tf_free(file); } static void _httpd_endpoint_static_stat(tf_task_t* task, const char* path, int result, const uv_stat_t* stat, void* user_data) { tf_http_request_t* request = user_data; const char* match = tf_http_request_get_header(request, "if-none-match"); if (result != 0) { const char* k_payload = tf_http_status_text(404); tf_http_respond(request, 404, NULL, 0, k_payload, strlen(k_payload)); tf_http_request_unref(request); } else { char etag[512]; snprintf(etag, sizeof(etag), "\"%f_%zd\"", _time_spec_to_double(&stat->st_mtim), (size_t)stat->st_size); if (match && strcmp(match, etag) == 0) { tf_http_respond(request, 304, NULL, 0, NULL, 0); tf_http_request_unref(request); } else { http_file_t* file = tf_malloc(sizeof(http_file_t)); *file = (http_file_t) { .request = request }; static_assert(sizeof(file->etag) == sizeof(etag), "Size mismatch"); memcpy(file->etag, etag, sizeof(etag)); tf_file_read(task, path, _httpd_endpoint_static_read, file); } } } static void _httpd_endpoint_static(tf_http_request_t* request) { if (request->path && strncmp(request->path, "/.well-known/", strlen("/.well-known/")) && _httpd_redirect(request)) { return; } const char* k_static_files[] = { "index.html", "client.js", "tildefriends.svg", "jszip.min.js", "style.css", "tfrpc.js", "w3.css", }; const char* k_map[][2] = { { "/static/", "core/" }, { "/lit/", "deps/lit/" }, { "/codemirror/", "deps/codemirror/" }, { "/prettier/", "deps/prettier/" }, { "/speedscope/", "deps/speedscope/" }, { "/.well-known/", "data/global/.well-known/" }, }; bool is_core = false; const char* after = NULL; const char* file_path = NULL; for (int i = 0; i < tf_countof(k_map) && !after; i++) { const char* next_after = _after(request->path, k_map[i][0]); if (next_after) { after = next_after; file_path = k_map[i][1]; is_core = after && i == 0; } } if ((!after || !*after) && request->path[strlen(request->path) - 1] == '/') { after = "index.html"; if (!file_path) { file_path = "core/"; is_core = true; } } if (!after || strstr(after, "..")) { const char* k_payload = tf_http_status_text(404); tf_http_respond(request, 404, NULL, 0, k_payload, strlen(k_payload)); return; } if (is_core) { bool found = false; for (int i = 0; i < tf_countof(k_static_files); i++) { if (strcmp(after, k_static_files[i]) == 0) { found = true; break; } } if (!found) { const char* k_payload = tf_http_status_text(404); tf_http_respond(request, 404, NULL, 0, k_payload, strlen(k_payload)); return; } } tf_task_t* task = request->user_data; const char* root_path = tf_task_get_root_path(task); size_t size = (root_path ? strlen(root_path) + 1 : 0) + strlen(file_path) + strlen(after) + 1; char* path = alloca(size); snprintf(path, size, "%s%s%s%s", root_path ? root_path : "", root_path ? "/" : "", file_path, after); tf_http_request_ref(request); tf_file_stat(task, path, _httpd_endpoint_static_stat, request); } static void _httpd_endpoint_add_slash(tf_http_request_t* request) { const char* host = tf_http_request_get_header(request, "x-forwarded-host"); if (!host) { host = tf_http_request_get_header(request, "host"); } char url[1024]; snprintf(url, sizeof(url), "%s%s%s/", request->is_tls ? "https://" : "http://", host, request->path); const char* headers[] = { "Location", url, }; tf_http_respond(request, 303, headers, tf_countof(headers) / 2, "", 0); } typedef struct _user_app_t { const char* user; const char* app; } user_app_t; static user_app_t* _parse_user_app_from_path(const char* path, const char* expected_suffix) { if (!path || path[0] != '/' || path[1] != '~') { return NULL; } size_t length = strlen(path); size_t suffix_length = expected_suffix ? strlen(expected_suffix) : 0; if (length < suffix_length || strcmp(path + length - suffix_length, expected_suffix) != 0) { return NULL; } const char* slash = strchr(path + 2, '/'); if (!slash) { return NULL; } const char* user = path + 2; size_t user_length = (size_t)(slash - user); const char* app = slash + 1; size_t app_length = (size_t)(length - suffix_length - user_length - 3); user_app_t* result = tf_malloc(sizeof(user_app_t) + user_length + 1 + app_length + 1); *result = (user_app_t) { .user = (char*)(result + 1), .app = (char*)(result + 1) + user_length + 1, }; memcpy((char*)result->user, user, user_length); ((char*)result->user)[user_length] = '\0'; memcpy((char*)result->app, app, app_length); ((char*)result->app)[app_length] = '\0'; if (!_is_name_valid(result->user) || !_is_name_valid(result->app)) { tf_free(result); result = NULL; } return result; } typedef struct _app_blob_t { tf_http_request_t* request; bool found; bool not_modified; bool use_handler; bool use_static; void* data; size_t size; char app_blob_id[k_blob_id_len]; const char* file; user_app_t* user_app; char etag[256]; } app_blob_t; static void _httpd_endpoint_app_blob_work(tf_ssb_t* ssb, void* user_data) { app_blob_t* data = user_data; tf_http_request_t* request = data->request; if (request->path[0] == '/' && request->path[1] == '~') { const char* last_slash = strchr(request->path + 1, '/'); if (last_slash) { last_slash = strchr(last_slash + 1, '/'); } data->user_app = last_slash ? _parse_user_app_from_path(request->path, last_slash) : NULL; if (data->user_app) { size_t path_length = strlen("path:") + strlen(data->user_app->app) + 1; char* app_path = tf_malloc(path_length); snprintf(app_path, path_length, "path:%s", data->user_app->app); const char* value = tf_ssb_db_get_property(ssb, data->user_app->user, app_path); tf_string_set(data->app_blob_id, sizeof(data->app_blob_id), value); tf_free(app_path); tf_free((void*)value); data->file = last_slash + 1; } } else if (request->path[0] == '/' && request->path[1] == '&') { const char* end = strstr(request->path, ".sha256/"); if (end) { snprintf(data->app_blob_id, sizeof(data->app_blob_id), "%.*s", (int)(end + strlen(".sha256") - request->path - 1), request->path + 1); data->file = end + strlen(".sha256/"); } } char* app_blob = NULL; size_t app_blob_size = 0; if (*data->app_blob_id && tf_ssb_db_blob_get(ssb, data->app_blob_id, (uint8_t**)&app_blob, &app_blob_size)) { JSMallocFunctions funcs = { 0 }; tf_get_js_malloc_functions(&funcs); JSRuntime* runtime = JS_NewRuntime2(&funcs, NULL); JSContext* context = JS_NewContext(runtime); JSValue app_object = JS_ParseJSON(context, app_blob, app_blob_size, NULL); JSValue files = JS_GetPropertyStr(context, app_object, "files"); JSValue blob_id = JS_GetPropertyStr(context, files, data->file); if (JS_IsUndefined(blob_id)) { blob_id = JS_GetPropertyStr(context, files, "handler.js"); if (!JS_IsUndefined(blob_id)) { data->use_handler = true; } } else { const char* blob_id_str = JS_ToCString(context, blob_id); if (blob_id_str) { snprintf(data->etag, sizeof(data->etag), "\"%s\"", blob_id_str); const char* match = tf_http_request_get_header(data->request, "if-none-match"); if (match && strcmp(match, data->etag) == 0) { data->not_modified = true; } else { data->found = tf_ssb_db_blob_get(ssb, blob_id_str, (uint8_t**)&data->data, &data->size); } } JS_FreeCString(context, blob_id_str); } JS_FreeValue(context, blob_id); JS_FreeValue(context, files); JS_FreeValue(context, app_object); JS_FreeContext(context); JS_FreeRuntime(runtime); tf_free(app_blob); } } static void _httpd_call_app_handler(tf_ssb_t* ssb, tf_http_request_t* request, const char* app_blob_id, const char* path, const char* package_owner, const char* app) { JSContext* context = tf_ssb_get_context(ssb); JSValue global = JS_GetGlobalObject(context); JSValue exports = JS_GetPropertyStr(context, global, "exports"); JSValue call_app_handler = JS_GetPropertyStr(context, exports, "callAppHandler"); JSValue response = _httpd_make_response_object(context, request); tf_http_request_ref(request); JSValue handler_blob_id = JS_NewString(context, app_blob_id); JSValue path_value = JS_NewString(context, path); JSValue package_owner_value = JS_NewString(context, package_owner); JSValue app_value = JS_NewString(context, app); JSValue query_value = request->query ? JS_NewString(context, request->query) : JS_UNDEFINED; JSValue headers = JS_NewObject(context); for (int i = 0; i < request->headers_count; i++) { char name[256] = ""; snprintf(name, sizeof(name), "%.*s", (int)request->headers[i].name_len, request->headers[i].name); JS_SetPropertyStr(context, headers, name, JS_NewStringLen(context, request->headers[i].value, request->headers[i].value_len)); } JSValue args[] = { response, handler_blob_id, path_value, query_value, headers, package_owner_value, app_value, }; JSValue result = JS_Call(context, call_app_handler, JS_NULL, tf_countof(args), args); tf_util_report_error(context, result); JS_FreeValue(context, result); JS_FreeValue(context, headers); JS_FreeValue(context, query_value); JS_FreeValue(context, app_value); JS_FreeValue(context, package_owner_value); JS_FreeValue(context, handler_blob_id); JS_FreeValue(context, path_value); JS_FreeValue(context, response); JS_FreeValue(context, call_app_handler); JS_FreeValue(context, exports); JS_FreeValue(context, global); } static void _httpd_endpoint_app_blob_after_work(tf_ssb_t* ssb, int status, void* user_data) { app_blob_t* data = user_data; if (data->not_modified) { tf_http_respond(data->request, 304, NULL, 0, NULL, 0); } else if (data->use_static) { _httpd_endpoint_static(data->request); } else if (data->use_handler) { _httpd_call_app_handler(ssb, data->request, data->app_blob_id, data->file, data->user_app->user, data->user_app->app); } else if (data->found) { const char* mime_type = _ext_to_content_type(strrchr(data->request->path, '.'), false); if (!mime_type) { mime_type = _httpd_mime_type_from_magic_bytes(data->data, data->size); } const char* headers[] = { "Access-Control-Allow-Origin", "*", "Content-Security-Policy", "sandbox allow-downloads allow-top-navigation-by-user-activation", "Content-Type", mime_type ? mime_type : "application/binary", "etag", data->etag, }; tf_http_respond(data->request, 200, headers, tf_countof(headers) / 2, data->data, data->size); } tf_free(data->user_app); tf_free(data->data); tf_http_request_unref(data->request); tf_free(data); } static void _httpd_endpoint_app_blob(tf_http_request_t* request) { tf_http_request_ref(request); tf_task_t* task = request->user_data; tf_ssb_t* ssb = tf_task_get_ssb(task); app_blob_t* data = tf_malloc(sizeof(app_blob_t)); *data = (app_blob_t) { .request = request }; tf_ssb_run_work(ssb, _httpd_endpoint_app_blob_work, _httpd_endpoint_app_blob_after_work, data); } static bool _has_property(JSContext* context, JSValue object, const char* name) { JSAtom atom = JS_NewAtom(context, name); bool result = JS_HasProperty(context, object, atom) > 0; JS_FreeAtom(context, atom); return result; } static void _httpd_endpoint_app_index_work(tf_ssb_t* ssb, void* user_data) { app_blob_t* data = user_data; data->use_static = true; user_app_t* user_app = data->user_app; size_t app_path_length = strlen("path:") + strlen(user_app->app) + 1; char* app_path = tf_malloc(app_path_length); snprintf(app_path, app_path_length, "path:%s", user_app->app); const char* app_blob_id = tf_ssb_db_get_property(ssb, user_app->user, app_path); tf_free(app_path); uint8_t* app_blob = NULL; size_t app_blob_size = 0; if (tf_ssb_db_blob_get(ssb, app_blob_id, &app_blob, &app_blob_size)) { JSMallocFunctions funcs = { 0 }; tf_get_js_malloc_functions(&funcs); JSRuntime* runtime = JS_NewRuntime2(&funcs, NULL); JSContext* context = JS_NewContext(runtime); JSValue app = JS_ParseJSON(context, (const char*)app_blob, app_blob_size, NULL); JSValue files = JS_GetPropertyStr(context, app, "files"); if (!_has_property(context, files, "app.js")) { JSValue index = JS_GetPropertyStr(context, files, "index.html"); if (JS_IsString(index)) { const char* index_string = JS_ToCString(context, index); tf_ssb_db_blob_get(ssb, index_string, (uint8_t**)&data->data, &data->size); JS_FreeCString(context, index_string); } JS_FreeValue(context, index); } JS_FreeValue(context, files); JS_FreeValue(context, app); JS_FreeContext(context); JS_FreeRuntime(runtime); tf_free(app_blob); } tf_free((void*)app_blob_id); } static char* _replace(const char* original, size_t size, const char* find, const char* replace, size_t* out_size) { char* pos = strstr(original, find); if (!pos) { return tf_strdup(original); } size_t replace_length = strlen(replace); size_t find_length = strlen(find); size_t new_size = size + replace_length - find_length; char* buffer = tf_malloc(new_size); memcpy(buffer, original, pos - original); memcpy(buffer + (pos - original), replace, replace_length); memcpy(buffer + (pos - original) + replace_length, pos + find_length, size - (pos - original) - find_length); *out_size = new_size; return buffer; } static char* _append_raw(char* document, size_t* current_size, const char* data, size_t size) { document = tf_resize_vec(document, *current_size + size); memcpy(document + *current_size, data, size); document[*current_size + size] = '\0'; *current_size += size; return document; } static char* _append_encoded(char* document, const char* data, size_t size, size_t* out_size) { size_t current_size = strlen(document); int accum = 0; for (int i = 0; (size_t)i < size; i++) { switch (data[i]) { case '"': if (i > accum) { document = _append_raw(document, ¤t_size, data + accum, i - accum); } document = _append_raw(document, ¤t_size, """, strlen(""")); accum = i + 1; break; case '\'': if (i > accum) { document = _append_raw(document, ¤t_size, data + accum, i - accum); } document = _append_raw(document, ¤t_size, "'", strlen("'")); accum = i + 1; break; case '<': if (i > accum) { document = _append_raw(document, ¤t_size, data + accum, i - accum); } document = _append_raw(document, ¤t_size, "<", strlen("<")); accum = i + 1; break; case '>': if (i > accum) { document = _append_raw(document, ¤t_size, data + accum, i - accum); } document = _append_raw(document, ¤t_size, ">", strlen(">")); accum = i + 1; break; case '&': if (i > accum) { document = _append_raw(document, ¤t_size, data + accum, i - accum); } document = _append_raw(document, ¤t_size, "&", strlen("&")); accum = i + 1; break; default: break; } } *out_size = current_size; return document; } static void _httpd_endpoint_app_index_file_read(tf_task_t* task, const char* path, int result, const void* data, void* user_data) { app_blob_t* state = user_data; if (result > 0) { char* replacement = tf_strdup("