diff --git a/src/http.c b/src/http.c index 3d5b64c7..4ea717b9 100644 --- a/src/http.c +++ b/src/http.c @@ -25,6 +25,7 @@ typedef struct _tf_http_connection_t const char* method; const char* path; + const char* query; int minor_version; char headers_buffer[8192]; @@ -160,6 +161,7 @@ static void _http_reset_connection(tf_http_connection_t* connection) connection->headers_buffer_length = 0; connection->body_length = 0; connection->content_length = 0; + connection->parsed_length = 0; } static void _http_add_body_bytes(tf_http_connection_t* connection, const void* data, size_t size) @@ -180,6 +182,9 @@ static void _http_add_body_bytes(tf_http_connection_t* connection, const void* d .phase = k_http_callback_phase_headers_received, .method = connection->method, .path = connection->path, + .query = connection->query, + .body = connection->body, + .content_length = connection->content_length, .headers = connection->headers, .headers_count = connection->headers_length, .user_data = connection->user_data, @@ -216,6 +221,12 @@ static void _http_on_read(uv_stream_t* stream, ssize_t read_size, const uv_buf_t ((char*)connection->method)[method_length] = '\0'; connection->path = path; ((char*)connection->path)[path_length] = '\0'; + char* q = strchr(connection->path, '?'); + if (q) + { + *q = '\0'; + connection->query = q + 1; + } connection->connection_close = connection->minor_version == 0; diff --git a/src/http.h b/src/http.h index 556b5f41..dda5afd9 100644 --- a/src/http.h +++ b/src/http.h @@ -18,6 +18,9 @@ typedef struct _tf_http_request_t tf_http_connection_t* connection; const char* method; const char* path; + const char* query; + void* body; + size_t content_length; struct phr_header* headers; int headers_count; void* user_data; diff --git a/src/httpd.js.c b/src/httpd.js.c index a8ba6cc2..fd868bc6 100644 --- a/src/httpd.js.c +++ b/src/httpd.js.c @@ -102,6 +102,7 @@ static void _httpd_callback(tf_http_request_t* request) http_handler_data_t* data = request->user_data; JSContext* context = data->context; JSValue request_object = JS_NewObject(context); + JS_SetPropertyStr(context, request_object, "method", JS_NewString(context, request->method)); 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++) @@ -109,6 +110,14 @@ static void _httpd_callback(tf_http_request_t* request) JS_SetPropertyStr(context, headers, request->headers[i].name, JS_NewString(context, request->headers[i].value)); } JS_SetPropertyStr(context, request_object, "headers", headers); + 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)); + } JSValue client = JS_NewObject(context); JS_SetPropertyStr(context, client, "tls", JS_FALSE);