2023-12-20 19:56:16 -05:00
|
|
|
#include "httpd.js.h"
|
|
|
|
|
2023-12-20 20:27:57 -05:00
|
|
|
#include "http.h"
|
2023-12-20 19:56:16 -05:00
|
|
|
#include "log.h"
|
2023-12-20 20:27:57 -05:00
|
|
|
#include "mem.h"
|
|
|
|
#include "task.h"
|
|
|
|
#include "util.js.h"
|
|
|
|
|
2023-12-20 21:04:20 -05:00
|
|
|
#include "picohttpparser.h"
|
|
|
|
|
2023-12-20 21:06:17 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2023-12-20 21:04:20 -05:00
|
|
|
#if !defined(__APPLE__) && !defined(__OpenBSD__) && !defined(_WIN32)
|
|
|
|
#include <alloca.h>
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
JSValue buffer;
|
|
|
|
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);
|
|
|
|
|
|
|
|
const char** headers = NULL;
|
|
|
|
int headers_length = 0;
|
|
|
|
JSValue response_headers = JS_GetPropertyStr(context, this_val, "response_headers");
|
|
|
|
JSPropertyEnum* ptab;
|
|
|
|
uint32_t plen;
|
|
|
|
JS_GetOwnPropertyNames(context, &ptab, &plen, response_headers, JS_GPN_STRING_MASK);
|
|
|
|
headers = alloca(sizeof(const char*) * plen * 2);
|
|
|
|
headers_length = plen;
|
|
|
|
for (uint32_t i = 0; i < plen; ++i)
|
|
|
|
{
|
|
|
|
JSValue key = JS_AtomToString(context, ptab[i].atom);
|
|
|
|
JSPropertyDescriptor desc;
|
|
|
|
JSValue key_value = JS_NULL;
|
|
|
|
if (JS_GetOwnProperty(context, &desc, response_headers, ptab[i].atom) == 1)
|
|
|
|
{
|
|
|
|
key_value = desc.value;
|
|
|
|
JS_FreeValue(context, desc.setter);
|
|
|
|
JS_FreeValue(context, desc.getter);
|
|
|
|
}
|
|
|
|
headers[i * 2 + 0] = JS_ToCString(context, key);
|
|
|
|
headers[i * 2 + 1] = JS_ToCString(context, key_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
tf_http_respond(request, status, headers, headers_length, data, length);
|
|
|
|
|
|
|
|
for (int i = 0; i < headers_length * 2; i++)
|
|
|
|
{
|
|
|
|
JS_FreeCString(context, headers[i]);
|
|
|
|
}
|
|
|
|
for (uint32_t i = 0; i < plen; ++i)
|
|
|
|
{
|
|
|
|
JS_FreeAtom(context, ptab[i].atom);
|
|
|
|
}
|
|
|
|
js_free(context, ptab);
|
|
|
|
JS_FreeValue(context, buffer);
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
2023-12-20 20:27:57 -05:00
|
|
|
static void _httpd_callback(tf_http_request_t* request)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
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-20 21:04:20 -05:00
|
|
|
|
|
|
|
JSValue client = JS_NewObject(context);
|
|
|
|
JS_SetPropertyStr(context, client, "tls", JS_FALSE);
|
|
|
|
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);
|
|
|
|
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));
|
|
|
|
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_object);
|
|
|
|
JS_FreeValue(context, response);
|
2023-12-20 20:27:57 -05:00
|
|
|
}
|
2023-12-20 19:56:16 -05:00
|
|
|
|
|
|
|
static JSValue _httpd_all(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
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]) };
|
|
|
|
tf_http_add_handler(http, pattern, _httpd_callback, data);
|
|
|
|
JS_FreeCString(context, pattern);
|
|
|
|
|
2023-12-20 19:56:16 -05:00
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSValue _httpd_register_socket_handler(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
tf_printf("HTTPD_REGISTER_SOCKET_HANDLER UNIMPLEMENTED\n");
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSValue _httpd_start(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2023-12-20 20:27:57 -05:00
|
|
|
tf_http_t* http = JS_GetOpaque(this_val, _httpd_class_id);
|
|
|
|
tf_http_listen(http, 12345);
|
2023-12-20 19:56:16 -05:00
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
2023-12-20 20:27:57 -05:00
|
|
|
void _httpd_finalizer(JSRuntime* runtime, JSValue value)
|
|
|
|
{
|
|
|
|
tf_http_t* http = JS_GetOpaque(value, _httpd_class_id);
|
|
|
|
tf_http_destroy(http);
|
|
|
|
}
|
|
|
|
|
2023-12-21 12:45:06 -05:00
|
|
|
void _httpd_request_finalizer(JSRuntime* runtime, JSValue value)
|
|
|
|
{
|
|
|
|
tf_http_request_t* request = JS_GetOpaque(value, _httpd_request_class_id);
|
|
|
|
tf_http_request_release(request);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
JS_SetOpaque(httpd, http);
|
|
|
|
|
|
|
|
JS_SetPropertyStr(context, httpd, "handlers", JS_NewObject(context));
|
2023-12-20 19:56:16 -05:00
|
|
|
JS_SetPropertyStr(context, httpd, "all", JS_NewCFunction(context, _httpd_all, "all", 2));
|
|
|
|
JS_SetPropertyStr(context, httpd, "registerSocketHandler", JS_NewCFunction(context, _httpd_register_socket_handler, "register_socket_handler", 2));
|
|
|
|
JS_SetPropertyStr(context, httpd, "start", JS_NewCFunction(context, _httpd_start, "start", 0));
|
|
|
|
JS_SetPropertyStr(context, global, "httpdc", httpd);
|
|
|
|
JS_FreeValue(context, global);
|
|
|
|
}
|