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:
34
src/http.c
34
src/http.c
@ -21,6 +21,8 @@ typedef struct _tf_http_connection_t
|
||||
uv_tcp_t tcp;
|
||||
uv_shutdown_t shutdown;
|
||||
|
||||
int ref_count;
|
||||
|
||||
const char* method;
|
||||
const char* path;
|
||||
int minor_version;
|
||||
@ -127,7 +129,7 @@ static void _http_connection_destroy(tf_http_connection_t* connection)
|
||||
{
|
||||
uv_close((uv_handle_t*)&connection->tcp, _http_connection_on_close);
|
||||
}
|
||||
else
|
||||
else if (connection->ref_count == 0)
|
||||
{
|
||||
tf_http_t* http = connection->http;
|
||||
for (int i = 0; i < http->connections_count; i++)
|
||||
@ -182,7 +184,9 @@ static void _http_add_body_bytes(tf_http_connection_t* connection, const void* d
|
||||
.headers_count = connection->headers_length,
|
||||
.user_data = connection->user_data,
|
||||
};
|
||||
tf_http_request_ref(request);
|
||||
connection->callback(request);
|
||||
tf_http_request_release(request);
|
||||
_http_reset_connection(connection);
|
||||
}
|
||||
}
|
||||
@ -207,6 +211,7 @@ static void _http_on_read(uv_stream_t* stream, ssize_t read_size, const uv_buf_t
|
||||
if (parse_result > 0)
|
||||
{
|
||||
connection->headers_done = true;
|
||||
connection->headers_length = header_count;
|
||||
connection->method = method;
|
||||
((char*)connection->method)[method_length] = '\0';
|
||||
connection->path = path;
|
||||
@ -216,6 +221,14 @@ static void _http_on_read(uv_stream_t* stream, ssize_t read_size, const uv_buf_t
|
||||
|
||||
for (int i = 0; i < (int)header_count; i++)
|
||||
{
|
||||
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')
|
||||
{
|
||||
((char*)connection->headers[i].name)[j] += 'a' - 'A';
|
||||
}
|
||||
}
|
||||
((char*)connection->headers[i].name)[connection->headers[i].name_len] = '\0';
|
||||
((char*)connection->headers[i].value)[connection->headers[i].value_len] = '\0';
|
||||
if (strcasecmp(connection->headers[i].name, "content-length") == 0)
|
||||
@ -464,7 +477,6 @@ void tf_http_respond(tf_http_request_t* request, int status, const char** header
|
||||
request->connection->shutdown.data = request->connection;
|
||||
uv_shutdown(&request->connection->shutdown, (uv_stream_t*)&request->connection->tcp, _http_on_shutdown);
|
||||
}
|
||||
tf_free(request);
|
||||
}
|
||||
|
||||
size_t tf_http_get_body(const tf_http_request_t* request, const void** out_data)
|
||||
@ -472,3 +484,21 @@ size_t tf_http_get_body(const tf_http_request_t* request, const void** out_data)
|
||||
*out_data = request->connection->body;
|
||||
return request->connection->content_length;
|
||||
}
|
||||
|
||||
void tf_http_request_ref(tf_http_request_t* request)
|
||||
{
|
||||
request->ref_count++;
|
||||
request->connection->ref_count++;
|
||||
}
|
||||
|
||||
void tf_http_request_release(tf_http_request_t* request)
|
||||
{
|
||||
if (--request->connection->ref_count == 0)
|
||||
{
|
||||
/* Reset the connection? */
|
||||
}
|
||||
if (--request->ref_count == 0)
|
||||
{
|
||||
tf_free(request);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user