Delete httpd.js.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4709 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2023-12-30 19:47:36 +00:00
parent 4f3f4295ea
commit 0dcc879eb1
5 changed files with 29 additions and 611 deletions

View File

@ -544,7 +544,7 @@ static void _http_on_connection(uv_stream_t* stream, int status)
http->connections[http->connections_count++] = connection;
}
void tf_http_listen(tf_http_t* http, int port, tf_tls_context_t* tls)
int tf_http_listen(tf_http_t* http, int port, tf_tls_context_t* tls)
{
tf_http_listener_t* listener = tf_malloc(sizeof(tf_http_listener_t));
*listener = (tf_http_listener_t) { .http = http, .tls = tls, .tcp = { .data = listener } };
@ -569,6 +569,15 @@ void tf_http_listen(tf_http_t* http, int port, tf_tls_context_t* tls)
}
}
int assigned_port = 0;
if (r == 0)
{
struct sockaddr_storage name = { 0 };
int size = (int)sizeof(name);
r = uv_tcp_getsockname(&listener->tcp, (struct sockaddr*)&name, &size);
assigned_port = ntohs(((struct sockaddr_in*)&name)->sin_port);
}
if (r == 0)
{
r = uv_listen((uv_stream_t*)&listener->tcp, 16, _http_on_connection);
@ -583,6 +592,7 @@ void tf_http_listen(tf_http_t* http, int port, tf_tls_context_t* tls)
http->listeners = tf_realloc(http->listeners, sizeof(tf_http_listener_t*) * (http->listeners_count + 1));
http->listeners[http->listeners_count++] = listener;
}
return assigned_port;
}
void tf_http_add_handler(tf_http_t* http, const char* pattern, tf_http_callback_t* callback, void* user_data)

View File

@ -36,7 +36,7 @@ typedef struct _tf_http_request_t
typedef void (tf_http_callback_t)(tf_http_request_t* request);
tf_http_t* tf_http_create(uv_loop_t* loop);
void tf_http_listen(tf_http_t* http, int port, tf_tls_context_t* tls);
int tf_http_listen(tf_http_t* http, int port, tf_tls_context_t* tls);
void tf_http_add_handler(tf_http_t* http, const char* pattern, tf_http_callback_t* callback, void* user_data);
void tf_http_respond(tf_http_request_t* request, int status, const char** headers, int headers_count, const void* body, size_t content_length);
size_t tf_http_get_body(const tf_http_request_t* request, const void** out_data);

View File

@ -293,8 +293,8 @@ static JSValue _httpd_start(JSContext* context, JSValueConst this_val, int argc,
int port = 0;
JS_ToInt32(context, &port, argv[0]);
tf_tls_context_t* tls = tf_tls_context_get(JS_DupValue(context, argv[1]));
tf_http_listen(http, port, tls);
return JS_UNDEFINED;
int assigned_port = tf_http_listen(http, port, tls);
return JS_NewInt32(context, assigned_port);
}
void _httpd_finalizer(JSRuntime* runtime, JSValue value)
@ -343,6 +343,6 @@ void tf_httpd_register(JSContext* 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", 2));
JS_SetPropertyStr(context, global, "httpdc", httpd);
JS_SetPropertyStr(context, global, "httpd", httpd);
JS_FreeValue(context, global);
}