forked from cory/tildefriends
Some quick http refactors to make websockets less magic.
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4705 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
26
src/http.c
26
src/http.c
@ -40,7 +40,6 @@ typedef struct _tf_http_connection_t
|
||||
int headers_length;
|
||||
bool headers_done;
|
||||
|
||||
int flags;
|
||||
tf_http_callback_t* callback;
|
||||
tf_http_request_t* request;
|
||||
void* user_data;
|
||||
@ -56,7 +55,6 @@ typedef struct _tf_http_connection_t
|
||||
typedef struct _tf_http_handler_t
|
||||
{
|
||||
const char* pattern;
|
||||
int flags;
|
||||
tf_http_callback_t* callback;
|
||||
void* user_data;
|
||||
} tf_http_handler_t;
|
||||
@ -103,14 +101,13 @@ void _http_allocate_buffer(uv_handle_t* handle, size_t suggestedSize, uv_buf_t*
|
||||
}
|
||||
}
|
||||
|
||||
bool _http_find_handler(tf_http_t* http, const char* path, int flags, 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, void** out_user_data)
|
||||
{
|
||||
for (int i = 0; i < http->handlers_count; i++)
|
||||
{
|
||||
if (http->handlers[i].flags == flags &&
|
||||
(!http->handlers[i].pattern ||
|
||||
if (!http->handlers[i].pattern ||
|
||||
strcmp(path, http->handlers[i].pattern) == 0 ||
|
||||
(strncmp(path, http->handlers[i].pattern, strlen(http->handlers[i].pattern)) == 0 && path[strlen(http->handlers[i].pattern)] == '/')))
|
||||
(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_user_data = http->handlers[i].user_data;
|
||||
@ -271,10 +268,6 @@ static void _http_add_body_bytes(tf_http_connection_t* connection, const void* d
|
||||
|
||||
if (connection->body_length == connection->content_length)
|
||||
{
|
||||
if (connection->flags & k_tf_http_handler_flag_websocket)
|
||||
{
|
||||
connection->is_websocket = true;
|
||||
}
|
||||
tf_http_request_t* request = tf_malloc(sizeof(tf_http_request_t));
|
||||
*request = (tf_http_request_t)
|
||||
{
|
||||
@ -282,7 +275,6 @@ 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,
|
||||
.flags = connection->flags,
|
||||
.query = connection->query,
|
||||
.body = connection->body,
|
||||
.content_length = connection->content_length,
|
||||
@ -367,9 +359,7 @@ static void _http_on_read(uv_stream_t* stream, ssize_t read_size, const uv_buf_t
|
||||
connection->body = tf_malloc(connection->content_length);
|
||||
}
|
||||
|
||||
int flags = _http_connection_get_header(connection, "upgrade") ? k_tf_http_handler_flag_websocket : 0;
|
||||
connection->flags = flags;
|
||||
if (!_http_find_handler(connection->http, connection->path, flags, &connection->callback, &connection->user_data) || !connection->callback)
|
||||
if (!_http_find_handler(connection->http, connection->path, &connection->callback, &connection->user_data) || !connection->callback)
|
||||
{
|
||||
connection->callback = _http_builtin_404_handler;
|
||||
}
|
||||
@ -475,13 +465,12 @@ void tf_http_listen(tf_http_t* http, int port)
|
||||
}
|
||||
}
|
||||
|
||||
void tf_http_add_handler(tf_http_t* http, const char* pattern, int flags, tf_http_callback_t* callback, void* user_data)
|
||||
void tf_http_add_handler(tf_http_t* http, const char* pattern, tf_http_callback_t* callback, void* user_data)
|
||||
{
|
||||
http->handlers = tf_realloc(http->handlers, sizeof(tf_http_handler_t) * (http->handlers_count + 1));
|
||||
http->handlers[http->handlers_count++] = (tf_http_handler_t)
|
||||
{
|
||||
.pattern = tf_strdup(pattern),
|
||||
.flags = flags,
|
||||
.callback = callback,
|
||||
.user_data = user_data,
|
||||
};
|
||||
@ -664,3 +653,8 @@ const char* tf_http_request_get_header(tf_http_request_t* request, const char* n
|
||||
{
|
||||
return _http_connection_get_header(request->connection, name);
|
||||
}
|
||||
|
||||
void tf_http_request_websocket_upgrade(tf_http_request_t* request)
|
||||
{
|
||||
request->connection->is_websocket = true;
|
||||
}
|
||||
|
Reference in New Issue
Block a user