forked from cory/tildefriends
Minor cleanup. Make http.c trace its callbacks.
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4728 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
18
src/http.c
18
src/http.c
@ -3,6 +3,7 @@
|
||||
#include "log.h"
|
||||
#include "mem.h"
|
||||
#include "tls.h"
|
||||
#include "trace.h"
|
||||
#include "util.js.h"
|
||||
|
||||
#include "picohttpparser.h"
|
||||
@ -48,6 +49,7 @@ typedef struct _tf_http_connection_t
|
||||
int headers_length;
|
||||
|
||||
tf_http_callback_t* callback;
|
||||
const char* trace_name;
|
||||
tf_http_request_t* request;
|
||||
void* user_data;
|
||||
|
||||
@ -89,6 +91,7 @@ typedef struct _tf_http_t
|
||||
|
||||
int pending_closes;
|
||||
uv_loop_t* loop;
|
||||
tf_trace_t* trace;
|
||||
|
||||
void* user_data;
|
||||
tf_http_cleanup_t* user_data_cleanup;
|
||||
@ -110,13 +113,18 @@ tf_http_t* tf_http_create(uv_loop_t* loop)
|
||||
return http;
|
||||
}
|
||||
|
||||
void tf_http_set_trace(tf_http_t* http, tf_trace_t* trace)
|
||||
{
|
||||
http->trace = trace;
|
||||
}
|
||||
|
||||
void _http_allocate_buffer(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf)
|
||||
{
|
||||
tf_http_connection_t* connection = handle->data;
|
||||
*buf = uv_buf_init(connection->incoming, sizeof(connection->incoming));
|
||||
}
|
||||
|
||||
bool _http_find_handler(tf_http_t* http, const char* path, tf_http_callback_t** out_callback, void** out_user_data)
|
||||
bool _http_find_handler(tf_http_t* http, const char* path, tf_http_callback_t** out_callback, const char** out_trace_name, void** out_user_data)
|
||||
{
|
||||
for (int i = 0; i < http->handlers_count; i++)
|
||||
{
|
||||
@ -125,6 +133,7 @@ bool _http_find_handler(tf_http_t* http, const char* path, tf_http_callback_t**
|
||||
(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_trace_name = http->handlers[i].pattern;
|
||||
*out_user_data = http->handlers[i].user_data;
|
||||
return true;
|
||||
}
|
||||
@ -302,11 +311,13 @@ static void _http_add_body_bytes(tf_http_connection_t* connection, const void* d
|
||||
{
|
||||
if (connection->request->on_message)
|
||||
{
|
||||
tf_trace_begin(connection->http->trace, connection->trace_name ? connection->trace_name : "websocket");
|
||||
connection->request->on_message(
|
||||
connection->request,
|
||||
connection->fragment_length ? connection->fragment_op_code : op_code,
|
||||
connection->fragment_length ? connection->fragment : message,
|
||||
connection->fragment_length ? connection->fragment_length : length);
|
||||
tf_trace_end(connection->http->trace);
|
||||
}
|
||||
connection->fragment_length = 0;
|
||||
}
|
||||
@ -355,7 +366,9 @@ static void _http_add_body_bytes(tf_http_connection_t* connection, const void* d
|
||||
connection->request = request;
|
||||
|
||||
tf_http_request_ref(request);
|
||||
tf_trace_begin(connection->http->trace, connection->trace_name ? connection->trace_name : "http");
|
||||
connection->callback(request);
|
||||
tf_trace_end(connection->http->trace);
|
||||
tf_http_request_release(request);
|
||||
}
|
||||
}
|
||||
@ -424,9 +437,10 @@ static size_t _http_on_read_plain_internal(tf_http_connection_t* connection, con
|
||||
connection->body = tf_malloc(connection->content_length);
|
||||
}
|
||||
|
||||
if (!_http_find_handler(connection->http, connection->path, &connection->callback, &connection->user_data) || !connection->callback)
|
||||
if (!_http_find_handler(connection->http, connection->path, &connection->callback, &connection->trace_name, &connection->user_data) || !connection->callback)
|
||||
{
|
||||
connection->callback = _http_builtin_404_handler;
|
||||
connection->trace_name = "404";
|
||||
}
|
||||
size_t consumed = read_size - (connection->headers_buffer_length - parse_result) - (read_size - used_read_size);
|
||||
_http_add_body_bytes(connection, NULL, 0);
|
||||
|
Reference in New Issue
Block a user