forked from cory/tildefriends
clang-format the source. Not exactly how I want it, but automated is better than perfect.
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4845 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
85
src/http.c
85
src/http.c
@ -110,8 +110,7 @@ static void _http_tls_update(tf_http_connection_t* connection);
|
||||
tf_http_t* tf_http_create(uv_loop_t* loop)
|
||||
{
|
||||
tf_http_t* http = tf_malloc(sizeof(tf_http_t));
|
||||
*http = (tf_http_t)
|
||||
{
|
||||
*http = (tf_http_t) {
|
||||
.loop = loop,
|
||||
};
|
||||
return http;
|
||||
@ -132,10 +131,9 @@ static bool _http_find_handler(tf_http_t* http, const char* path, tf_http_callba
|
||||
{
|
||||
for (int i = 0; i < http->handlers_count; i++)
|
||||
{
|
||||
if (!http->handlers[i].pattern ||
|
||||
!*http->handlers[i].pattern ||
|
||||
strcmp(path, http->handlers[i].pattern) == 0 ||
|
||||
(*http->handlers[i].pattern && strncmp(path, http->handlers[i].pattern, strlen(http->handlers[i].pattern)) == 0 && path[strlen(http->handlers[i].pattern) - 1] == '/'))
|
||||
if (!http->handlers[i].pattern || !*http->handlers[i].pattern || strcmp(path, http->handlers[i].pattern) == 0 ||
|
||||
(*http->handlers[i].pattern && strncmp(path, http->handlers[i].pattern, strlen(http->handlers[i].pattern)) == 0 &&
|
||||
path[strlen(http->handlers[i].pattern) - 1] == '/'))
|
||||
{
|
||||
*out_callback = http->handlers[i].callback;
|
||||
*out_trace_name = http->handlers[i].pattern;
|
||||
@ -186,10 +184,7 @@ static void _http_connection_destroy(tf_http_connection_t* connection, const cha
|
||||
connection->tls = NULL;
|
||||
}
|
||||
|
||||
if (connection->ref_count == 0 &&
|
||||
!connection->tcp.data &&
|
||||
!connection->shutdown.data &&
|
||||
!connection->timeout.data)
|
||||
if (connection->ref_count == 0 && !connection->tcp.data && !connection->shutdown.data && !connection->timeout.data)
|
||||
{
|
||||
tf_http_t* http = connection->http;
|
||||
for (int i = 0; i < http->connections_count; i++)
|
||||
@ -211,8 +206,7 @@ static void _http_connection_destroy(tf_http_connection_t* connection, const cha
|
||||
}
|
||||
tf_free(connection);
|
||||
|
||||
if (http->is_shutting_down &&
|
||||
http->connections_count == 0)
|
||||
if (http->is_shutting_down && http->connections_count == 0)
|
||||
{
|
||||
tf_http_destroy(http);
|
||||
}
|
||||
@ -308,10 +302,7 @@ static void _http_add_body_bytes(tf_http_connection_t* connection, const void* d
|
||||
if (connection->body_length >= total_length)
|
||||
{
|
||||
uint32_t mask =
|
||||
(uint32_t)p[mask_start + 0] |
|
||||
(uint32_t)p[mask_start + 1] << 8 |
|
||||
(uint32_t)p[mask_start + 2] << 16 |
|
||||
(uint32_t)p[mask_start + 3] << 24;
|
||||
(uint32_t)p[mask_start + 0] | (uint32_t)p[mask_start + 1] << 8 | (uint32_t)p[mask_start + 2] << 16 | (uint32_t)p[mask_start + 3] << 24;
|
||||
uint8_t* message = p + mask_start + 4;
|
||||
_http_websocket_mask_in_place(message, mask, length);
|
||||
|
||||
@ -332,11 +323,9 @@ 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);
|
||||
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;
|
||||
@ -369,8 +358,7 @@ static void _http_add_body_bytes(tf_http_connection_t* connection, const void* d
|
||||
if (connection->body_length == connection->content_length)
|
||||
{
|
||||
tf_http_request_t* request = tf_malloc(sizeof(tf_http_request_t));
|
||||
*request = (tf_http_request_t)
|
||||
{
|
||||
*request = (tf_http_request_t) {
|
||||
.http = connection->http,
|
||||
.connection = connection,
|
||||
.is_tls = connection->tls != NULL,
|
||||
@ -408,7 +396,8 @@ static size_t _http_on_read_plain_internal(tf_http_connection_t* connection, con
|
||||
size_t path_length = 0;
|
||||
size_t header_count = sizeof(connection->headers) / sizeof(*connection->headers);
|
||||
|
||||
int parse_result = phr_parse_request(connection->headers_buffer, connection->headers_buffer_length, &method, &method_length, &path, &path_length, &connection->minor_version, connection->headers, &header_count, connection->parsed_length);
|
||||
int parse_result = phr_parse_request(connection->headers_buffer, connection->headers_buffer_length, &method, &method_length, &path, &path_length,
|
||||
&connection->minor_version, connection->headers, &header_count, connection->parsed_length);
|
||||
connection->parsed_length = connection->headers_buffer_length;
|
||||
if (parse_result > 0)
|
||||
{
|
||||
@ -431,8 +420,7 @@ static size_t _http_on_read_plain_internal(tf_http_connection_t* connection, con
|
||||
{
|
||||
for (size_t j = 0; j < connection->headers[i].name_len; j++)
|
||||
{
|
||||
if (connection->headers[i].name[j] >= 'A' &&
|
||||
connection->headers[i].name[j] <= 'Z')
|
||||
if (connection->headers[i].name[j] >= 'A' && connection->headers[i].name[j] <= 'Z')
|
||||
{
|
||||
((char*)connection->headers[i].name)[j] += 'a' - 'A';
|
||||
}
|
||||
@ -457,7 +445,8 @@ 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->trace_name, &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";
|
||||
@ -619,8 +608,7 @@ static void _http_on_connection(uv_stream_t* stream, int status)
|
||||
int tf_http_listen(tf_http_t* http, int port, tf_tls_context_t* tls, tf_http_cleanup_t* cleanup, void* user_data)
|
||||
{
|
||||
tf_http_listener_t* listener = tf_malloc(sizeof(tf_http_listener_t));
|
||||
*listener = (tf_http_listener_t)
|
||||
{
|
||||
*listener = (tf_http_listener_t) {
|
||||
.http = http,
|
||||
.tls = tls,
|
||||
.tcp = { .data = listener },
|
||||
@ -635,8 +623,7 @@ int tf_http_listen(tf_http_t* http, int port, tf_tls_context_t* tls, tf_http_cle
|
||||
|
||||
if (r == 0)
|
||||
{
|
||||
struct sockaddr_in6 addr =
|
||||
{
|
||||
struct sockaddr_in6 addr = {
|
||||
.sin6_family = AF_INET6,
|
||||
.sin6_addr = IN6ADDR_ANY_INIT,
|
||||
.sin6_port = ntohs(port),
|
||||
@ -677,8 +664,7 @@ int tf_http_listen(tf_http_t* http, int port, tf_tls_context_t* tls, tf_http_cle
|
||||
void tf_http_add_handler(tf_http_t* http, const char* pattern, tf_http_callback_t* callback, tf_http_cleanup_t* cleanup, void* user_data)
|
||||
{
|
||||
http->handlers = tf_resize_vec(http->handlers, sizeof(tf_http_handler_t) * (http->handlers_count + 1));
|
||||
http->handlers[http->handlers_count++] = (tf_http_handler_t)
|
||||
{
|
||||
http->handlers[http->handlers_count++] = (tf_http_handler_t) {
|
||||
.pattern = tf_strdup(pattern),
|
||||
.callback = callback,
|
||||
.cleanup = cleanup,
|
||||
@ -760,16 +746,26 @@ const char* tf_http_status_text(int status)
|
||||
{
|
||||
switch (status)
|
||||
{
|
||||
case 101: return "Switching Protocols";
|
||||
case 200: return "OK";
|
||||
case 303: return "See other";
|
||||
case 304: return "Not Modified";
|
||||
case 400: return "Bad Request";
|
||||
case 401: return "Unauthorized";
|
||||
case 403: return "Forbidden";
|
||||
case 404: return "File not found";
|
||||
case 500: return "Internal server error";
|
||||
default: return "Unknown";
|
||||
case 101:
|
||||
return "Switching Protocols";
|
||||
case 200:
|
||||
return "OK";
|
||||
case 303:
|
||||
return "See other";
|
||||
case 304:
|
||||
return "Not Modified";
|
||||
case 400:
|
||||
return "Bad Request";
|
||||
case 401:
|
||||
return "Unauthorized";
|
||||
case 403:
|
||||
return "Forbidden";
|
||||
case 404:
|
||||
return "File not found";
|
||||
case 500:
|
||||
return "Internal server error";
|
||||
default:
|
||||
return "Unknown";
|
||||
}
|
||||
}
|
||||
|
||||
@ -923,8 +919,7 @@ void tf_http_respond(tf_http_request_t* request, int status, const char** header
|
||||
}
|
||||
_http_timer_reset(request->connection);
|
||||
|
||||
if (request->connection->connection_close &&
|
||||
!request->connection->shutdown.data)
|
||||
if (request->connection->connection_close && !request->connection->shutdown.data)
|
||||
{
|
||||
request->connection->shutdown.data = request->connection;
|
||||
uv_shutdown(&request->connection->shutdown, (uv_stream_t*)&request->connection->tcp, _http_on_shutdown);
|
||||
|
Reference in New Issue
Block a user