Make the http test complete successfully.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4680 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2023-12-18 17:51:15 +00:00
parent 7b3a9e0f63
commit 03e4b37c04
3 changed files with 81 additions and 8 deletions

View File

@ -44,9 +44,12 @@ typedef struct _tf_http_t
tf_http_handler_t* handlers;
int handlers_count;
int pending_closes;
uv_loop_t* loop;
} tf_http_t;
static void _http_connection_destroy(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));
@ -82,6 +85,33 @@ static void _http_on_write(uv_write_t* write, int status)
tf_free(write);
}
static void _http_connection_on_close(uv_handle_t* handle)
{
tf_http_connection_t* connection = handle->data;
handle->data = NULL;
_http_connection_destroy(connection);
}
static void _http_connection_destroy(tf_http_connection_t* connection)
{
if (connection->tcp.data)
{
uv_close((uv_handle_t*)&connection->tcp, _http_connection_on_close);
}
else
{
tf_http_t* http = connection->http;
for (int i = 0; i < http->connections_count; i++)
{
if (http->connections[i] == connection)
{
http->connections[i] = http->connections[--http->connections_count];
}
}
tf_free(connection);
}
}
void _http_on_read(uv_stream_t* stream, ssize_t read_size, const uv_buf_t* buffer)
{
tf_http_connection_t* connection = stream->data;
@ -129,6 +159,10 @@ void _http_on_read(uv_stream_t* stream, ssize_t read_size, const uv_buf_t* buffe
tf_printf("phr_parse_request: %d\n", parse_result);
}
}
else
{
_http_connection_destroy(connection);
}
}
static void _http_on_connection(uv_stream_t* stream, int status)
@ -218,8 +252,28 @@ void tf_http_add_handler(tf_http_t* http, const char* pattern, tf_http_callback_
};
}
static void _http_free_on_close(uv_handle_t* handle)
{
handle->data = NULL;
tf_free(handle);
}
void tf_http_destroy(tf_http_t* http)
{
for (int i = 0; i < http->listeners_count; i++)
{
uv_close((uv_handle_t*)http->listeners[i], _http_free_on_close);
}
tf_free(http->listeners);
http->listeners = NULL;
http->listeners_count = 0;
tf_free(http->handlers);
http->handlers_count = 0;
tf_free(http->connections);
http->connections_count = 0;
tf_free(http);
}
@ -238,7 +292,7 @@ static void _http_on_shutdown(uv_shutdown_t* request, int status)
tf_free(request);
}
void tf_http_respond(tf_http_request_t* request, int status, const char** headers, int headers_count, void* body, size_t content_length)
void tf_http_respond(tf_http_request_t* request, int status, const char** headers, int headers_count, const void* body, size_t content_length)
{
const char* status_text = _http_status_text(status);
/* HTTP/1.x 200 OK\r\n */