forked from cory/tildefriends
Fix some http request lifetime issues, and follow the same lowercase convention for headers.
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4690 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
@ -15,7 +15,7 @@
|
||||
#endif
|
||||
|
||||
static JSClassID _httpd_class_id;
|
||||
static JSClassID _httpd_response_class_id;
|
||||
static JSClassID _httpd_request_class_id;
|
||||
|
||||
typedef struct _http_handler_data_t
|
||||
{
|
||||
@ -32,7 +32,7 @@ static JSValue _httpd_response_write_head(JSContext* context, JSValueConst this_
|
||||
|
||||
static JSValue _httpd_response_end(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
||||
{
|
||||
tf_http_request_t* request = JS_GetOpaque(this_val, _httpd_response_class_id);
|
||||
tf_http_request_t* request = JS_GetOpaque(this_val, _httpd_request_class_id);
|
||||
size_t length = 0;
|
||||
const void* data = NULL;
|
||||
JSValue buffer;
|
||||
@ -104,17 +104,18 @@ static void _httpd_callback(tf_http_request_t* request)
|
||||
JSValue request_object = JS_NewObject(context);
|
||||
JS_SetPropertyStr(context, request_object, "uri", JS_NewString(context, request->path));
|
||||
JSValue headers = JS_NewObject(context);
|
||||
JS_SetPropertyStr(context, request_object, "headers", headers);
|
||||
for (int i = 0; i < request->headers_count; i++)
|
||||
{
|
||||
JS_SetPropertyStr(context, headers, request->headers[i].name, JS_NewString(context, request->headers[i].value));
|
||||
}
|
||||
JS_SetPropertyStr(context, request_object, "headers", headers);
|
||||
|
||||
JSValue client = JS_NewObject(context);
|
||||
JS_SetPropertyStr(context, client, "tls", JS_FALSE);
|
||||
JS_SetPropertyStr(context, request_object, "client", client);
|
||||
|
||||
JSValue response_object = JS_NewObjectClass(context, _httpd_response_class_id);
|
||||
JSValue response_object = JS_NewObjectClass(context, _httpd_request_class_id);
|
||||
tf_http_request_ref(request);
|
||||
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));
|
||||
@ -161,19 +162,34 @@ void _httpd_finalizer(JSRuntime* runtime, JSValue value)
|
||||
tf_http_destroy(http);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
void tf_httpd_register(JSContext* context)
|
||||
{
|
||||
JS_NewClassID(&_httpd_class_id);
|
||||
JS_NewClassID(&_httpd_response_class_id);
|
||||
JSClassDef def =
|
||||
JS_NewClassID(&_httpd_request_class_id);
|
||||
JSClassDef httpd_def =
|
||||
{
|
||||
.class_name = "Httpd",
|
||||
.finalizer = &_httpd_finalizer,
|
||||
};
|
||||
if (JS_NewClass(JS_GetRuntime(context), _httpd_class_id, &def) != 0)
|
||||
if (JS_NewClass(JS_GetRuntime(context), _httpd_class_id, &httpd_def) != 0)
|
||||
{
|
||||
fprintf(stderr, "Failed to register Httpd.\n");
|
||||
}
|
||||
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");
|
||||
}
|
||||
JSValue global = JS_GetGlobalObject(context);
|
||||
JSValue httpd = JS_NewObjectClass(context, _httpd_class_id);
|
||||
|
||||
|
Reference in New Issue
Block a user