From 6ecbfe3de65603e58be60e3c34e8fd3db3faa8b9 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Thu, 21 Dec 2023 01:27:57 +0000 Subject: [PATCH] Sort of barely starting to call httpd callbacks with the new implementation. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4686 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- src/http.c | 4 +++- src/httpd.js.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++--- src/socket.js.c | 5 +++-- src/util.js.c | 7 +++++- 4 files changed, 68 insertions(+), 7 deletions(-) diff --git a/src/http.c b/src/http.c index 3930b0d8..8c49f71b 100644 --- a/src/http.c +++ b/src/http.c @@ -96,7 +96,9 @@ bool _http_find_handler(tf_http_t* http, const char* path, tf_http_callback_t** { for (int i = 0; i < http->handlers_count; i++) { - if (!http->handlers[i].pattern || strcmp(path, http->handlers[i].pattern) == 0) + if (!http->handlers[i].pattern || + strcmp(path, http->handlers[i].pattern) == 0 || + (strncmp(path, http->handlers[i].pattern, strlen(http->handlers[i].pattern)) == 0 && path[strlen(http->handlers[i].pattern)] == '/')) { *out_callback = http->handlers[i].callback; *out_user_data = http->handlers[i].user_data; diff --git a/src/httpd.js.c b/src/httpd.js.c index fe3a7900..998d2439 100644 --- a/src/httpd.js.c +++ b/src/httpd.js.c @@ -1,10 +1,39 @@ #include "httpd.js.h" +#include "http.h" #include "log.h" +#include "mem.h" +#include "task.h" +#include "util.js.h" + +static JSClassID _httpd_class_id; + +typedef struct _http_handler_data_t +{ + JSContext* context; + JSValue callback; +} http_handler_data_t; + +static void _httpd_callback(tf_http_request_t* request) +{ + tf_printf("httpd_callback!\n"); + http_handler_data_t* data = request->user_data; + JSValue response = JS_Call(data->context, data->callback, JS_UNDEFINED, 0, NULL); + tf_printf("%d %d\n", JS_IsUndefined(response), JS_IsException(response)); + tf_util_report_error(data->context, response); + JS_FreeValue(data->context, response); +} static JSValue _httpd_all(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) { - tf_printf("HTTPD_ALL UNIMPLEMENTED\n"); + 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); + tf_printf("HTTPD_ALL: %s\n", pattern); + JS_FreeCString(context, pattern); + return JS_UNDEFINED; } @@ -16,14 +45,38 @@ static JSValue _httpd_register_socket_handler(JSContext* context, JSValueConst t static JSValue _httpd_start(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) { - tf_printf("HTTPD_START UNIMPLEMENTED\n"); + tf_http_t* http = JS_GetOpaque(this_val, _httpd_class_id); + tf_http_listen(http, 12345); return JS_UNDEFINED; } +void _httpd_finalizer(JSRuntime* runtime, JSValue value) +{ + tf_http_t* http = JS_GetOpaque(value, _httpd_class_id); + tf_http_destroy(http); +} + void tf_httpd_register(JSContext* context) { + JS_NewClassID(&_httpd_class_id); + JSClassDef def = + { + .class_name = "Httpd", + .finalizer = &_httpd_finalizer, + }; + if (JS_NewClass(JS_GetRuntime(context), _httpd_class_id, &def) != 0) + { + fprintf(stderr, "Failed to register Httpd.\n"); + } JSValue global = JS_GetGlobalObject(context); - JSValue httpd = JS_NewObject(context); + 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)); 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)); diff --git a/src/socket.js.c b/src/socket.js.c index 008eb6e3..7006eff8 100644 --- a/src/socket.js.c +++ b/src/socket.js.c @@ -130,7 +130,8 @@ static void _socket_gc_mark(JSRuntime* runtime, JSValueConst value, JS_MarkFunc JSValue tf_socket_register(JSContext* context) { JS_NewClassID(&_classId); - JSClassDef def = { + JSClassDef def = + { .class_name = "Socket", .finalizer = &_socket_finalizer, .gc_mark = _socket_gc_mark, @@ -269,7 +270,7 @@ void _socket_close_internal(socket_t* socket) } } -void _socket_finalizer(JSRuntime *runtime, JSValue value) +void _socket_finalizer(JSRuntime* runtime, JSValue value) { socket_t* socket = JS_GetOpaque(value, _classId); socket->_object = JS_UNDEFINED; diff --git a/src/util.js.c b/src/util.js.c index 916d3e5a..ec749163 100644 --- a/src/util.js.c +++ b/src/util.js.c @@ -180,7 +180,12 @@ bool tf_util_report_error(JSContext* context, JSValue value) else if (JS_IsException(value)) { tf_task_t* task = tf_task_get(context); - tf_task_send_error_to_parent(task, value); + if (!tf_task_send_error_to_parent(task, value)) + { + JSValue exception = JS_GetException(context); + tf_util_report_error(context, exception); + JS_FreeValue(context, exception); + } is_error = true; } return is_error;