Treat the ?query string and body the same as httpd.js does. Now I can auth.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4691 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2023-12-23 19:52:59 +00:00
parent 38ab32dad9
commit 196ab66e14
3 changed files with 23 additions and 0 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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);