2023-12-20 19:56:16 -05:00
|
|
|
#include "httpd.js.h"
|
|
|
|
|
2024-02-08 20:21:57 -05:00
|
|
|
#include "file.js.h"
|
2023-12-20 20:27:57 -05:00
|
|
|
#include "http.h"
|
2024-02-13 18:07:36 -05:00
|
|
|
#include "log.h"
|
2023-12-20 20:27:57 -05:00
|
|
|
#include "mem.h"
|
|
|
|
#include "task.h"
|
2023-12-30 13:59:02 -05:00
|
|
|
#include "tlscontext.js.h"
|
2024-01-02 10:43:17 -05:00
|
|
|
#include "trace.h"
|
2023-12-20 20:27:57 -05:00
|
|
|
#include "util.js.h"
|
|
|
|
|
2023-12-20 21:04:20 -05:00
|
|
|
#include "picohttpparser.h"
|
|
|
|
|
2024-02-08 20:21:57 -05:00
|
|
|
#include <assert.h>
|
2023-12-20 21:06:17 -05:00
|
|
|
#include <stdlib.h>
|
2023-12-24 12:43:33 -05:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <openssl/sha.h>
|
2023-12-20 21:06:17 -05:00
|
|
|
|
2023-12-20 21:04:20 -05:00
|
|
|
#if !defined(__APPLE__) && !defined(__OpenBSD__) && !defined(_WIN32)
|
|
|
|
#include <alloca.h>
|
|
|
|
#endif
|
|
|
|
|
2024-01-02 10:43:17 -05:00
|
|
|
#define tf_countof(a) ((int)(sizeof((a)) / sizeof(*(a))))
|
|
|
|
|
2024-01-25 13:00:23 -05:00
|
|
|
static JSValue _httpd_websocket_upgrade(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
|
|
|
|
2023-12-20 20:27:57 -05:00
|
|
|
static JSClassID _httpd_class_id;
|
2023-12-21 12:45:06 -05:00
|
|
|
static JSClassID _httpd_request_class_id;
|
2023-12-20 20:27:57 -05:00
|
|
|
|
2024-01-02 10:02:47 -05:00
|
|
|
typedef struct _http_user_data_t
|
|
|
|
{
|
|
|
|
char redirect[1024];
|
|
|
|
} http_user_data_t;
|
|
|
|
|
2023-12-20 20:27:57 -05:00
|
|
|
typedef struct _http_handler_data_t
|
|
|
|
{
|
|
|
|
JSContext* context;
|
|
|
|
JSValue callback;
|
|
|
|
} http_handler_data_t;
|
|
|
|
|
2023-12-20 21:04:20 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-01-25 13:00:23 -05:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
JSValue key = JS_AtomToString(context, ptab[count].atom);
|
|
|
|
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_ToCString(context, key);
|
|
|
|
headers[count * 2 + 1] = JS_ToCString(context, key_value);
|
2024-01-27 13:26:01 -05:00
|
|
|
JS_FreeValue(context, key);
|
|
|
|
JS_FreeValue(context, key_value);
|
2024-01-25 13:00:23 -05:00
|
|
|
}
|
|
|
|
for (uint32_t i = 0; i < plen; ++i)
|
|
|
|
{
|
|
|
|
JS_FreeAtom(context, ptab[i].atom);
|
|
|
|
}
|
|
|
|
js_free(context, ptab);
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2023-12-20 21:04:20 -05:00
|
|
|
static JSValue _httpd_response_end(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2023-12-21 12:45:06 -05:00
|
|
|
tf_http_request_t* request = JS_GetOpaque(this_val, _httpd_request_class_id);
|
2023-12-20 21:04:20 -05:00
|
|
|
size_t length = 0;
|
|
|
|
const void* data = NULL;
|
2023-12-30 22:09:32 -05:00
|
|
|
JSValue buffer = JS_UNDEFINED;
|
2023-12-20 21:04:20 -05:00
|
|
|
if (JS_IsString(argv[0]))
|
|
|
|
{
|
|
|
|
data = JS_ToCStringLen(context, &length, argv[0]);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
|
2024-01-25 13:00:23 -05:00
|
|
|
const char* headers[64] = { 0 };
|
2023-12-20 21:04:20 -05:00
|
|
|
JSValue response_headers = JS_GetPropertyStr(context, this_val, "response_headers");
|
2024-01-25 13:00:23 -05:00
|
|
|
int headers_count = _object_to_headers(context, response_headers, headers, tf_countof(headers));
|
2024-01-26 20:25:30 -05:00
|
|
|
JS_FreeValue(context, response_headers);
|
2023-12-20 21:04:20 -05:00
|
|
|
|
2024-01-25 13:00:23 -05:00
|
|
|
tf_http_respond(request, status, headers, headers_count, data, length);
|
2023-12-20 21:04:20 -05:00
|
|
|
|
2024-01-25 13:00:23 -05:00
|
|
|
for (int i = 0; i < headers_count * 2; i++)
|
2023-12-20 21:04:20 -05:00
|
|
|
{
|
|
|
|
JS_FreeCString(context, headers[i]);
|
|
|
|
}
|
|
|
|
JS_FreeValue(context, buffer);
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
2023-12-25 17:53:05 -05:00
|
|
|
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]);
|
|
|
|
uint64_t length = 0;
|
|
|
|
size_t length_size = 0;
|
|
|
|
const char* message = JS_ToCStringLen(context, &length_size, argv[0]);
|
|
|
|
length = length_size;
|
|
|
|
uint8_t* copy = tf_malloc(length + 16);
|
|
|
|
bool fin = true;
|
|
|
|
size_t header = 1;
|
|
|
|
copy[0] = (fin ? (1 << 7) : 0) | (opcode & 0xf);
|
|
|
|
if (length < 126)
|
|
|
|
{
|
|
|
|
copy[1] = length;
|
2023-12-25 18:39:16 -05:00
|
|
|
header += 1;
|
2023-12-25 17:53:05 -05:00
|
|
|
}
|
|
|
|
else if (length < (1 << 16))
|
|
|
|
{
|
|
|
|
copy[1] = 126;
|
|
|
|
copy[2] = (length >> 8) & 0xff;
|
|
|
|
copy[3] = (length >> 0) & 0xff;
|
2023-12-25 18:39:16 -05:00
|
|
|
header += 3;
|
2023-12-25 17:53:05 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
uint32_t high = (length >> 32) & 0xffffffff;
|
|
|
|
uint32_t low = (length >> 0) & 0xffffffff;
|
|
|
|
copy[1] = 127;
|
|
|
|
copy[2] = (high >> 24) & 0xff;
|
|
|
|
copy[3] = (high >> 16) & 0xff;
|
|
|
|
copy[4] = (high >> 8) & 0xff;
|
|
|
|
copy[5] = (high >> 0) & 0xff;
|
|
|
|
copy[6] = (low >> 24) & 0xff;
|
|
|
|
copy[7] = (low >> 16) & 0xff;
|
|
|
|
copy[8] = (low >> 8) & 0xff;
|
|
|
|
copy[9] = (low >> 0) & 0xff;
|
2023-12-25 18:39:16 -05:00
|
|
|
header += 9;
|
2023-12-25 17:53:05 -05:00
|
|
|
}
|
|
|
|
memcpy(copy + header, message, length);
|
|
|
|
tf_http_request_send(request, copy, header + length);
|
|
|
|
tf_free(copy);
|
|
|
|
JS_FreeCString(context, message);
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
2024-01-27 10:45:51 -05:00
|
|
|
static void _httpd_websocket_close_callback(tf_http_request_t* request)
|
2024-01-26 21:36:08 -05:00
|
|
|
{
|
|
|
|
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);
|
2024-01-27 10:45:51 -05:00
|
|
|
JS_SetPropertyStr(context, response_object, "onMessage", JS_UNDEFINED);
|
|
|
|
JS_SetPropertyStr(context, response_object, "onClose", JS_UNDEFINED);
|
|
|
|
JS_FreeValue(context, response_object);
|
2024-01-26 21:36:08 -05:00
|
|
|
}
|
|
|
|
|
2023-12-29 12:45:07 -05:00
|
|
|
static void _httpd_message_callback(tf_http_request_t* request, int op_code, const void* data, size_t size)
|
2023-12-25 18:39:16 -05:00
|
|
|
{
|
|
|
|
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);
|
2023-12-29 12:45:07 -05:00
|
|
|
JS_SetPropertyStr(context, event, "opCode", JS_NewInt32(context, op_code));
|
|
|
|
JS_SetPropertyStr(context, event, "data", JS_NewStringLen(context, data, size));
|
2023-12-25 18:39:16 -05:00
|
|
|
JSValue response = JS_Call(context, on_message, JS_UNDEFINED, 1, &event);
|
|
|
|
tf_util_report_error(context, response);
|
2024-01-09 12:22:39 -05:00
|
|
|
JS_FreeValue(context, response);
|
2023-12-25 18:39:16 -05:00
|
|
|
JS_FreeValue(context, event);
|
|
|
|
JS_FreeValue(context, on_message);
|
|
|
|
}
|
|
|
|
|
2023-12-30 11:29:16 -05:00
|
|
|
static void _httpd_callback_internal(tf_http_request_t* request, bool is_websocket)
|
2023-12-20 20:27:57 -05:00
|
|
|
{
|
|
|
|
http_handler_data_t* data = request->user_data;
|
2023-12-20 21:04:20 -05:00
|
|
|
JSContext* context = data->context;
|
|
|
|
JSValue request_object = JS_NewObject(context);
|
2023-12-23 14:52:59 -05:00
|
|
|
JS_SetPropertyStr(context, request_object, "method", JS_NewString(context, request->method));
|
2023-12-20 21:04:20 -05:00
|
|
|
JS_SetPropertyStr(context, request_object, "uri", JS_NewString(context, request->path));
|
|
|
|
JSValue headers = JS_NewObject(context);
|
|
|
|
for (int i = 0; i < request->headers_count; i++)
|
|
|
|
{
|
|
|
|
JS_SetPropertyStr(context, headers, request->headers[i].name, JS_NewString(context, request->headers[i].value));
|
|
|
|
}
|
2023-12-21 12:45:06 -05:00
|
|
|
JS_SetPropertyStr(context, request_object, "headers", headers);
|
2023-12-23 14:52:59 -05:00
|
|
|
if (request->query)
|
|
|
|
{
|
|
|
|
JS_SetPropertyStr(context, request_object, "query", JS_NewString(context, request->query));
|
|
|
|
}
|
|
|
|
if (request->body)
|
|
|
|
{
|
|
|
|
JS_SetPropertyStr(context, request_object, "body", tf_util_new_uint8_array(context, request->body, request->content_length));
|
|
|
|
}
|
2023-12-20 21:04:20 -05:00
|
|
|
|
|
|
|
JSValue client = JS_NewObject(context);
|
2024-01-18 19:48:42 -05:00
|
|
|
JS_SetPropertyStr(context, client, "tls", request->is_tls ? JS_TRUE : JS_FALSE);
|
2023-12-20 21:04:20 -05:00
|
|
|
JS_SetPropertyStr(context, request_object, "client", client);
|
|
|
|
|
2023-12-21 12:45:06 -05:00
|
|
|
JSValue response_object = JS_NewObjectClass(context, _httpd_request_class_id);
|
2024-01-27 10:45:51 -05:00
|
|
|
/* The ref is owned by the JS object and will be released by the finalizer. */
|
2023-12-21 12:45:06 -05:00
|
|
|
tf_http_request_ref(request);
|
2023-12-20 21:04:20 -05:00
|
|
|
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));
|
2023-12-25 17:53:05 -05:00
|
|
|
JS_SetPropertyStr(context, response_object, "send", JS_NewCFunction(context, _httpd_response_send, "send", 2));
|
2024-01-25 13:00:23 -05:00
|
|
|
JS_SetPropertyStr(context, response_object, "upgrade", JS_NewCFunction(context, _httpd_websocket_upgrade, "upgrade", 2));
|
2023-12-20 21:04:20 -05:00
|
|
|
JSValue args[] =
|
|
|
|
{
|
|
|
|
request_object,
|
|
|
|
response_object,
|
|
|
|
};
|
|
|
|
JSValue response = JS_Call(context, data->callback, JS_UNDEFINED, 2, args);
|
|
|
|
tf_util_report_error(context, response);
|
|
|
|
JS_FreeValue(context, request_object);
|
|
|
|
JS_FreeValue(context, response);
|
2024-01-25 13:00:23 -05:00
|
|
|
JS_FreeValue(context, response_object);
|
2023-12-30 11:29:16 -05:00
|
|
|
}
|
|
|
|
|
2024-01-02 10:02:47 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-12-30 11:29:16 -05:00
|
|
|
static void _httpd_callback(tf_http_request_t* request)
|
|
|
|
{
|
2024-01-02 10:02:47 -05:00
|
|
|
if (_httpd_redirect(request))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-12-30 11:29:16 -05:00
|
|
|
_httpd_callback_internal(request, false);
|
|
|
|
}
|
|
|
|
|
2024-01-25 13:00:23 -05:00
|
|
|
static JSValue _httpd_websocket_upgrade(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
2023-12-30 11:29:16 -05:00
|
|
|
{
|
2024-01-25 13:00:23 -05:00
|
|
|
tf_http_request_t* request = JS_GetOpaque(this_val, _httpd_request_class_id);
|
|
|
|
tf_http_request_ref(request);
|
2023-12-30 11:29:16 -05:00
|
|
|
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));
|
2024-01-25 13:00:23 -05:00
|
|
|
|
|
|
|
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++;
|
|
|
|
|
2023-12-30 11:29:16 -05:00
|
|
|
bool send_version =
|
|
|
|
!tf_http_request_get_header(request, "sec-websocket-version") ||
|
|
|
|
strcmp(tf_http_request_get_header(request, "sec-websocket-version"), "13") != 0;
|
2024-01-25 13:00:23 -05:00
|
|
|
if (send_version)
|
|
|
|
{
|
|
|
|
headers[headers_count * 2 + 0] = "Sec-WebSocket-Accept";
|
|
|
|
headers[headers_count * 2 + 1] = key;
|
|
|
|
headers_count++;
|
|
|
|
}
|
2024-01-27 13:26:01 -05:00
|
|
|
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;
|
2024-01-25 13:00:23 -05:00
|
|
|
|
2023-12-30 11:29:16 -05:00
|
|
|
tf_http_request_websocket_upgrade(request);
|
2024-01-25 13:00:23 -05:00
|
|
|
tf_http_respond(request, 101, headers, headers_count, NULL, 0);
|
|
|
|
|
2024-01-27 13:26:01 -05:00
|
|
|
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]);
|
|
|
|
}
|
|
|
|
|
2024-01-25 13:00:23 -05:00
|
|
|
request->on_message = _httpd_message_callback;
|
2024-01-27 10:45:51 -05:00
|
|
|
request->on_close = _httpd_websocket_close_callback;
|
2024-01-25 13:00:23 -05:00
|
|
|
request->context = context;
|
|
|
|
request->user_data = JS_VALUE_GET_PTR(JS_DupValue(context, this_val));
|
2023-12-30 11:29:16 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tf_http_respond(request, 400, NULL, 0, NULL, 0);
|
|
|
|
}
|
2024-01-27 10:45:51 -05:00
|
|
|
tf_http_request_unref(request);
|
2023-12-30 11:29:16 -05:00
|
|
|
|
2024-01-25 13:00:23 -05:00
|
|
|
return JS_UNDEFINED;
|
2023-12-20 20:27:57 -05:00
|
|
|
}
|
2023-12-20 19:56:16 -05:00
|
|
|
|
2024-01-27 12:11:24 -05:00
|
|
|
static void _httpd_cleanup_callback(void* user_data)
|
|
|
|
{
|
|
|
|
http_handler_data_t* data = user_data;
|
|
|
|
JS_FreeValue(data->context, data->callback);
|
|
|
|
tf_free(data);
|
|
|
|
}
|
|
|
|
|
2024-01-02 21:14:17 -05:00
|
|
|
static JSValue _httpd_endpoint_all(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
2023-12-20 19:56:16 -05:00
|
|
|
{
|
2023-12-20 20:27:57 -05:00
|
|
|
tf_http_t* http = JS_GetOpaque(this_val, _httpd_class_id);
|
|
|
|
const char* pattern = JS_ToCString(context, argv[0]);
|
|
|
|
http_handler_data_t* data = tf_malloc(sizeof(http_handler_data_t));
|
|
|
|
*data = (http_handler_data_t) { .context = context, .callback = JS_DupValue(context, argv[1]) };
|
2024-01-27 12:11:24 -05:00
|
|
|
tf_http_add_handler(http, pattern, _httpd_callback, _httpd_cleanup_callback, data);
|
2023-12-20 20:27:57 -05:00
|
|
|
JS_FreeCString(context, pattern);
|
2023-12-20 19:56:16 -05:00
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
2024-01-27 12:27:56 -05:00
|
|
|
typedef struct _httpd_listener_t
|
|
|
|
{
|
|
|
|
JSContext* context;
|
|
|
|
JSValue tls;
|
|
|
|
} httpd_listener_t;
|
|
|
|
|
|
|
|
static void _httpd_listener_cleanup(void* user_data)
|
|
|
|
{
|
|
|
|
httpd_listener_t* listener = user_data;
|
|
|
|
JS_FreeValue(listener->context, listener->tls);
|
|
|
|
tf_free(listener);
|
|
|
|
}
|
|
|
|
|
2024-01-02 21:14:17 -05:00
|
|
|
static JSValue _httpd_endpoint_start(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
2023-12-20 19:56:16 -05:00
|
|
|
{
|
2023-12-20 20:27:57 -05:00
|
|
|
tf_http_t* http = JS_GetOpaque(this_val, _httpd_class_id);
|
2023-12-29 12:45:07 -05:00
|
|
|
int port = 0;
|
|
|
|
JS_ToInt32(context, &port, argv[0]);
|
2024-01-27 12:27:56 -05:00
|
|
|
|
|
|
|
httpd_listener_t* listener = tf_malloc(sizeof(httpd_listener_t));
|
|
|
|
*listener = (httpd_listener_t) { .context = context, .tls = JS_DupValue(context, argv[1]) };
|
|
|
|
tf_tls_context_t* tls = tf_tls_context_get(listener->tls);
|
|
|
|
int assigned_port = tf_http_listen(http, port, tls, _httpd_listener_cleanup, listener);
|
2023-12-30 14:47:36 -05:00
|
|
|
return JS_NewInt32(context, assigned_port);
|
2023-12-20 19:56:16 -05:00
|
|
|
}
|
|
|
|
|
2024-01-02 10:02:47 -05:00
|
|
|
static void _httpd_free_user_data(void* user_data)
|
|
|
|
{
|
|
|
|
tf_free(user_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSValue _httpd_set_http_redirect(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
tf_http_t* http = JS_GetOpaque(this_val, _httpd_class_id);
|
|
|
|
http_user_data_t* user_data = tf_http_get_user_data(http);
|
|
|
|
if (!user_data)
|
|
|
|
{
|
|
|
|
user_data = tf_malloc(sizeof(http_user_data_t));
|
|
|
|
memset(user_data, 0, sizeof(http_user_data_t));
|
|
|
|
tf_http_set_user_data(http, user_data, _httpd_free_user_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* redirect = JS_ToCString(context, argv[0]);
|
|
|
|
snprintf(user_data->redirect, sizeof(user_data->redirect), "%s", redirect ? redirect : "");
|
|
|
|
JS_FreeCString(context, redirect);
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
2024-01-02 21:14:17 -05:00
|
|
|
static void _httpd_finalizer(JSRuntime* runtime, JSValue value)
|
2023-12-20 20:27:57 -05:00
|
|
|
{
|
|
|
|
tf_http_t* http = JS_GetOpaque(value, _httpd_class_id);
|
|
|
|
tf_http_destroy(http);
|
|
|
|
}
|
|
|
|
|
2024-01-02 21:14:17 -05:00
|
|
|
static void _httpd_request_finalizer(JSRuntime* runtime, JSValue value)
|
2023-12-21 12:45:06 -05:00
|
|
|
{
|
|
|
|
tf_http_request_t* request = JS_GetOpaque(value, _httpd_request_class_id);
|
2024-01-27 10:45:51 -05:00
|
|
|
tf_http_request_unref(request);
|
2023-12-21 12:45:06 -05:00
|
|
|
}
|
|
|
|
|
2024-01-02 10:43:17 -05:00
|
|
|
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);
|
2024-01-27 16:29:06 -05:00
|
|
|
response = tf_resize_vec(response, length + line);
|
2024-01-02 10:43:17 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2024-01-02 18:26:42 -05:00
|
|
|
static void _httpd_endpoint_disconnections(tf_http_request_t* request)
|
|
|
|
{
|
|
|
|
if (_httpd_redirect(request))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tf_task_t* task = request->user_data;
|
|
|
|
char* response = tf_task_get_disconnections(task);
|
|
|
|
const char* headers[] =
|
|
|
|
{
|
|
|
|
"Content-Type", "application/json; charset=utf-8",
|
|
|
|
"Access-Control-Allow-Origin", "*",
|
|
|
|
};
|
|
|
|
tf_http_respond(request, 200, headers, tf_countof(headers) / 2, response, response ? strlen(response) : 0);
|
|
|
|
tf_free(response);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _httpd_endpoint_hitches(tf_http_request_t* request)
|
|
|
|
{
|
|
|
|
if (_httpd_redirect(request))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tf_task_t* task = request->user_data;
|
|
|
|
char* response = tf_task_get_hitches(task);
|
|
|
|
const char* headers[] =
|
|
|
|
{
|
|
|
|
"Content-Type", "application/json; charset=utf-8",
|
|
|
|
"Access-Control-Allow-Origin", "*",
|
|
|
|
};
|
|
|
|
tf_http_respond(request, 200, headers, tf_countof(headers) / 2, response, response ? strlen(response) : 0);
|
|
|
|
tf_free(response);
|
|
|
|
}
|
|
|
|
|
2024-02-08 20:21:57 -05:00
|
|
|
static const char* _after(const char* text, const char* prefix)
|
|
|
|
{
|
|
|
|
size_t prefix_length = strlen(prefix);
|
|
|
|
if (strncmp(text, prefix, prefix_length) == 0)
|
|
|
|
{
|
|
|
|
return text + prefix_length;
|
|
|
|
}
|
2024-02-09 22:14:02 -05:00
|
|
|
return NULL;
|
2024-02-08 20:21:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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 const char* _ext_to_content_type(const char* ext)
|
|
|
|
{
|
|
|
|
if (ext)
|
|
|
|
{
|
|
|
|
if (strcmp(ext, ".js") == 0)
|
|
|
|
{
|
|
|
|
return "text/javascript; charset=UTF-8";
|
|
|
|
}
|
|
|
|
if (strcmp(ext, ".css") == 0)
|
|
|
|
{
|
|
|
|
return "text/css; charset=UTF-8";
|
|
|
|
}
|
|
|
|
else if (strcmp(ext, ".png") == 0)
|
|
|
|
{
|
|
|
|
return "image/png";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "application/binary";
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
const char* content_type = _ext_to_content_type(strrchr(path, '.'));
|
|
|
|
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, '.'));
|
|
|
|
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");
|
2024-02-10 11:10:58 -05:00
|
|
|
if (result != 0)
|
2024-02-08 20:21:57 -05:00
|
|
|
{
|
2024-02-10 11:10:58 -05:00
|
|
|
const char* k_payload = tf_http_status_text(404);
|
|
|
|
tf_http_respond(request, 404, NULL, 0, k_payload, strlen(k_payload));
|
2024-02-08 20:21:57 -05:00
|
|
|
tf_http_request_unref(request);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-02-10 11:10:58 -05:00
|
|
|
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);
|
|
|
|
}
|
2024-02-08 20:21:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _httpd_endpoint_static(tf_http_request_t* request)
|
|
|
|
{
|
|
|
|
if (_httpd_redirect(request))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* k_static_files[] =
|
|
|
|
{
|
|
|
|
"index.html",
|
|
|
|
"client.js",
|
|
|
|
"favicon.png",
|
|
|
|
"jszip.min.js",
|
|
|
|
"style.css",
|
|
|
|
"tfrpc.js",
|
|
|
|
"w3.css",
|
|
|
|
};
|
|
|
|
|
2024-02-09 22:14:02 -05:00
|
|
|
const char* k_map[][2] =
|
2024-02-08 20:21:57 -05:00
|
|
|
{
|
2024-02-09 22:14:02 -05:00
|
|
|
{ "/static/", "core/" },
|
|
|
|
{ "/lit/", "deps/lit/" },
|
|
|
|
{ "/codemirror/", "deps/codemirror/" },
|
|
|
|
{ "/speedscope/", "deps/speedscope/" },
|
2024-02-10 11:10:58 -05:00
|
|
|
{ "/.well-known/", "data/global/.well-known/" },
|
2024-02-09 22:14:02 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
bool is_core = false;
|
|
|
|
const char* after = NULL;
|
|
|
|
const char* file_path = NULL;
|
|
|
|
for (int i = 0; i < tf_countof(k_map) && !after; i++)
|
|
|
|
{
|
|
|
|
after = _after(request->path, k_map[i][0]);
|
|
|
|
file_path = k_map[i][1];
|
|
|
|
is_core = is_core || (after && i == 0);
|
2024-02-08 20:21:57 -05:00
|
|
|
}
|
|
|
|
|
2024-02-09 22:14:02 -05:00
|
|
|
if (!after || strstr(after, ".."))
|
2024-02-08 20:21:57 -05:00
|
|
|
{
|
|
|
|
const char* k_payload = tf_http_status_text(404);
|
|
|
|
tf_http_respond(request, 404, NULL, 0, k_payload, strlen(k_payload));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-02-09 22:14:02 -05:00
|
|
|
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;
|
|
|
|
size_t size = strlen(file_path) + strlen(after) + 1;
|
2024-02-08 20:21:57 -05:00
|
|
|
char* path = alloca(size);
|
2024-02-09 22:14:02 -05:00
|
|
|
snprintf(path, size, "%s%s", file_path, after);
|
2024-02-08 20:21:57 -05:00
|
|
|
tf_http_request_ref(request);
|
|
|
|
tf_file_stat(task, path, _httpd_endpoint_static_stat, request);
|
|
|
|
}
|
|
|
|
|
2024-02-07 20:08:05 -05:00
|
|
|
static void _httpd_endpoint_robots_txt(tf_http_request_t* request)
|
|
|
|
{
|
|
|
|
if (_httpd_redirect(request))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
char* response =
|
|
|
|
"User-Agent: *\n"
|
|
|
|
"Disallow: /*/*/edit\n"
|
|
|
|
"Allow: /\n";
|
|
|
|
const char* headers[] = { "Content-Type", "text/plain; charset=utf-8" };
|
|
|
|
tf_http_respond(request, 200, headers, tf_countof(headers) / 2, response, response ? strlen(response) : 0);
|
|
|
|
}
|
|
|
|
|
2024-01-02 18:26:42 -05:00
|
|
|
static void _httpd_endpoint_debug(tf_http_request_t* request)
|
|
|
|
{
|
|
|
|
if (_httpd_redirect(request))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tf_task_t* task = request->user_data;
|
|
|
|
char* response = tf_task_get_debug(task);
|
|
|
|
const char* headers[] =
|
|
|
|
{
|
|
|
|
"Content-Type", "application/json; charset=utf-8",
|
|
|
|
"Access-Control-Allow-Origin", "*",
|
|
|
|
};
|
|
|
|
tf_http_respond(request, 200, headers, tf_countof(headers) / 2, response, response ? strlen(response) : 0);
|
|
|
|
tf_free(response);
|
|
|
|
}
|
|
|
|
|
2023-12-20 19:56:16 -05:00
|
|
|
void tf_httpd_register(JSContext* context)
|
|
|
|
{
|
2023-12-20 20:27:57 -05:00
|
|
|
JS_NewClassID(&_httpd_class_id);
|
2023-12-21 12:45:06 -05:00
|
|
|
JS_NewClassID(&_httpd_request_class_id);
|
|
|
|
JSClassDef httpd_def =
|
2023-12-20 20:27:57 -05:00
|
|
|
{
|
|
|
|
.class_name = "Httpd",
|
|
|
|
.finalizer = &_httpd_finalizer,
|
|
|
|
};
|
2023-12-21 12:45:06 -05:00
|
|
|
if (JS_NewClass(JS_GetRuntime(context), _httpd_class_id, &httpd_def) != 0)
|
2023-12-20 20:27:57 -05:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed to register Httpd.\n");
|
|
|
|
}
|
2023-12-21 12:45:06 -05:00
|
|
|
JSClassDef request_def =
|
|
|
|
{
|
|
|
|
.class_name = "Request",
|
|
|
|
.finalizer = &_httpd_request_finalizer,
|
|
|
|
};
|
|
|
|
if (JS_NewClass(JS_GetRuntime(context), _httpd_request_class_id, &request_def) != 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed to register Request.\n");
|
|
|
|
}
|
2023-12-20 19:56:16 -05:00
|
|
|
JSValue global = JS_GetGlobalObject(context);
|
2023-12-20 20:27:57 -05:00
|
|
|
JSValue httpd = JS_NewObjectClass(context, _httpd_class_id);
|
|
|
|
|
|
|
|
tf_task_t* task = tf_task_get(context);
|
|
|
|
uv_loop_t* loop = tf_task_get_loop(task);
|
|
|
|
tf_http_t* http = tf_http_create(loop);
|
2024-01-02 21:14:17 -05:00
|
|
|
tf_http_set_trace(http, tf_task_get_trace(task));
|
2023-12-20 20:27:57 -05:00
|
|
|
JS_SetOpaque(httpd, http);
|
|
|
|
|
2024-02-09 22:14:02 -05:00
|
|
|
tf_http_add_handler(http, "/codemirror/", _httpd_endpoint_static, NULL, task);
|
|
|
|
tf_http_add_handler(http, "/lit/", _httpd_endpoint_static, NULL, task);
|
|
|
|
tf_http_add_handler(http, "/speedscope/", _httpd_endpoint_static, NULL, task);
|
2024-02-08 20:21:57 -05:00
|
|
|
tf_http_add_handler(http, "/static/", _httpd_endpoint_static, NULL, task);
|
2024-02-10 11:10:58 -05:00
|
|
|
tf_http_add_handler(http, "/.well-known/", _httpd_endpoint_static, NULL, task);
|
2024-02-09 22:14:02 -05:00
|
|
|
|
2024-02-07 20:08:05 -05:00
|
|
|
tf_http_add_handler(http, "/robots.txt", _httpd_endpoint_robots_txt, NULL, NULL);
|
2024-01-27 12:11:24 -05:00
|
|
|
tf_http_add_handler(http, "/debug", _httpd_endpoint_debug, NULL, task);
|
|
|
|
tf_http_add_handler(http, "/disconnections", _httpd_endpoint_disconnections, NULL, task);
|
|
|
|
tf_http_add_handler(http, "/hitches", _httpd_endpoint_hitches, NULL, task);
|
|
|
|
tf_http_add_handler(http, "/mem", _httpd_endpoint_mem, NULL, task);
|
|
|
|
tf_http_add_handler(http, "/trace", _httpd_endpoint_trace, NULL, task);
|
2024-01-02 10:43:17 -05:00
|
|
|
|
2023-12-20 20:27:57 -05:00
|
|
|
JS_SetPropertyStr(context, httpd, "handlers", JS_NewObject(context));
|
2024-01-02 21:14:17 -05:00
|
|
|
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));
|
2024-01-02 10:02:47 -05:00
|
|
|
JS_SetPropertyStr(context, httpd, "set_http_redirect", JS_NewCFunction(context, _httpd_set_http_redirect, "set_http_redirect", 1));
|
2023-12-30 14:47:36 -05:00
|
|
|
JS_SetPropertyStr(context, global, "httpd", httpd);
|
2023-12-20 19:56:16 -05:00
|
|
|
JS_FreeValue(context, global);
|
|
|
|
}
|