2023-12-13 18:59:11 -05:00
|
|
|
#include "http.h"
|
|
|
|
|
|
|
|
#include "log.h"
|
|
|
|
#include "mem.h"
|
2023-12-30 13:59:02 -05:00
|
|
|
#include "tls.h"
|
2024-01-02 21:14:17 -05:00
|
|
|
#include "trace.h"
|
2023-12-20 18:58:28 -05:00
|
|
|
#include "util.js.h"
|
2023-12-13 18:59:11 -05:00
|
|
|
|
|
|
|
#include "picohttpparser.h"
|
|
|
|
#include "uv.h"
|
|
|
|
|
2023-12-17 12:44:54 -05:00
|
|
|
#include <assert.h>
|
2023-12-29 12:45:07 -05:00
|
|
|
#include <stdalign.h>
|
2023-12-20 18:58:28 -05:00
|
|
|
#include <stdlib.h>
|
2023-12-13 18:59:11 -05:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#if !defined(__APPLE__) && !defined(__OpenBSD__) && !defined(_WIN32)
|
|
|
|
#include <alloca.h>
|
|
|
|
#endif
|
|
|
|
|
2023-12-30 11:52:05 -05:00
|
|
|
static const int k_timeout_ms = 60000;
|
|
|
|
|
2023-12-13 18:59:11 -05:00
|
|
|
typedef struct _tf_http_connection_t
|
|
|
|
{
|
|
|
|
tf_http_t* http;
|
2023-12-30 13:59:02 -05:00
|
|
|
tf_tls_session_t* tls;
|
2023-12-13 18:59:11 -05:00
|
|
|
uv_tcp_t tcp;
|
2023-12-21 12:15:59 -05:00
|
|
|
uv_shutdown_t shutdown;
|
2023-12-30 11:52:05 -05:00
|
|
|
uv_timer_t timeout;
|
2023-12-13 18:59:11 -05:00
|
|
|
|
2023-12-21 12:45:06 -05:00
|
|
|
int ref_count;
|
2023-12-30 13:59:02 -05:00
|
|
|
bool is_handshaking;
|
|
|
|
bool is_receiving_headers;
|
|
|
|
bool is_response_sent;
|
|
|
|
bool is_shutting_down;
|
2023-12-21 12:45:06 -05:00
|
|
|
|
2023-12-20 18:58:28 -05:00
|
|
|
const char* method;
|
|
|
|
const char* path;
|
2023-12-23 14:52:59 -05:00
|
|
|
const char* query;
|
2023-12-20 19:13:03 -05:00
|
|
|
int minor_version;
|
2023-12-20 18:58:28 -05:00
|
|
|
|
2023-12-30 13:59:02 -05:00
|
|
|
char incoming[8192];
|
|
|
|
|
2023-12-20 18:58:28 -05:00
|
|
|
char headers_buffer[8192];
|
|
|
|
size_t headers_buffer_length;
|
|
|
|
int parsed_length;
|
|
|
|
|
|
|
|
struct phr_header headers[32];
|
|
|
|
int headers_length;
|
2023-12-13 18:59:11 -05:00
|
|
|
|
|
|
|
tf_http_callback_t* callback;
|
2024-01-02 21:14:17 -05:00
|
|
|
const char* trace_name;
|
2023-12-25 18:39:16 -05:00
|
|
|
tf_http_request_t* request;
|
2023-12-13 18:59:11 -05:00
|
|
|
void* user_data;
|
2023-12-20 18:58:28 -05:00
|
|
|
|
2023-12-24 17:06:11 -05:00
|
|
|
bool is_websocket;
|
2023-12-29 12:45:07 -05:00
|
|
|
int websocket_message_index;
|
2023-12-20 18:58:28 -05:00
|
|
|
void* body;
|
2023-12-30 15:35:03 -05:00
|
|
|
void* fragment;
|
2023-12-30 16:35:53 -05:00
|
|
|
int fragment_op_code;
|
2023-12-30 15:35:03 -05:00
|
|
|
size_t fragment_length;
|
2023-12-20 18:58:28 -05:00
|
|
|
size_t body_length;
|
|
|
|
size_t content_length;
|
2023-12-20 19:13:03 -05:00
|
|
|
bool connection_close;
|
2023-12-13 18:59:11 -05:00
|
|
|
} tf_http_connection_t;
|
|
|
|
|
|
|
|
typedef struct _tf_http_handler_t
|
|
|
|
{
|
|
|
|
const char* pattern;
|
|
|
|
tf_http_callback_t* callback;
|
2024-01-27 12:11:24 -05:00
|
|
|
tf_http_cleanup_t* cleanup;
|
2023-12-13 18:59:11 -05:00
|
|
|
void* user_data;
|
|
|
|
} tf_http_handler_t;
|
|
|
|
|
2023-12-30 13:59:02 -05:00
|
|
|
typedef struct _tf_http_listener_t
|
|
|
|
{
|
|
|
|
tf_http_t* http;
|
|
|
|
tf_tls_context_t* tls;
|
|
|
|
uv_tcp_t tcp;
|
2024-01-27 12:27:56 -05:00
|
|
|
tf_http_cleanup_t* cleanup;
|
|
|
|
void* user_data;
|
2023-12-30 13:59:02 -05:00
|
|
|
} tf_http_listener_t;
|
|
|
|
|
2023-12-13 18:59:11 -05:00
|
|
|
typedef struct _tf_http_t
|
|
|
|
{
|
2024-02-10 17:09:52 -05:00
|
|
|
bool is_shutting_down;
|
|
|
|
|
2023-12-30 13:59:02 -05:00
|
|
|
tf_http_listener_t** listeners;
|
2023-12-13 18:59:11 -05:00
|
|
|
int listeners_count;
|
|
|
|
|
|
|
|
tf_http_connection_t** connections;
|
|
|
|
int connections_count;
|
|
|
|
|
|
|
|
tf_http_handler_t* handlers;
|
|
|
|
int handlers_count;
|
|
|
|
|
2023-12-18 12:51:15 -05:00
|
|
|
int pending_closes;
|
2023-12-13 18:59:11 -05:00
|
|
|
uv_loop_t* loop;
|
2024-01-02 21:14:17 -05:00
|
|
|
tf_trace_t* trace;
|
2024-01-02 10:02:47 -05:00
|
|
|
|
|
|
|
void* user_data;
|
|
|
|
tf_http_cleanup_t* user_data_cleanup;
|
2023-12-13 18:59:11 -05:00
|
|
|
} tf_http_t;
|
|
|
|
|
2023-12-24 12:43:33 -05:00
|
|
|
static const char* _http_connection_get_header(const tf_http_connection_t* connection, const char* name);
|
2023-12-25 18:50:55 -05:00
|
|
|
static void _http_connection_destroy(tf_http_connection_t* connection, const char* reason);
|
2023-12-30 11:52:05 -05:00
|
|
|
static void _http_timer_reset(tf_http_connection_t* connection);
|
2023-12-30 13:59:02 -05:00
|
|
|
static void _http_tls_update(tf_http_connection_t* connection);
|
2023-12-18 12:51:15 -05:00
|
|
|
|
2023-12-13 18:59:11 -05:00
|
|
|
tf_http_t* tf_http_create(uv_loop_t* loop)
|
|
|
|
{
|
|
|
|
tf_http_t* http = tf_malloc(sizeof(tf_http_t));
|
2024-02-15 18:35:01 -05:00
|
|
|
*http = (tf_http_t) {
|
2023-12-13 18:59:11 -05:00
|
|
|
.loop = loop,
|
|
|
|
};
|
|
|
|
return http;
|
|
|
|
}
|
|
|
|
|
2024-01-02 21:14:17 -05:00
|
|
|
void tf_http_set_trace(tf_http_t* http, tf_trace_t* trace)
|
|
|
|
{
|
|
|
|
http->trace = trace;
|
|
|
|
}
|
|
|
|
|
2024-02-10 11:50:00 -05:00
|
|
|
static void _http_allocate_buffer(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf)
|
2023-12-13 18:59:11 -05:00
|
|
|
{
|
|
|
|
tf_http_connection_t* connection = handle->data;
|
2023-12-30 13:59:02 -05:00
|
|
|
*buf = uv_buf_init(connection->incoming, sizeof(connection->incoming));
|
2023-12-13 18:59:11 -05:00
|
|
|
}
|
|
|
|
|
2024-02-10 11:50:00 -05:00
|
|
|
static 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)
|
2023-12-13 18:59:11 -05:00
|
|
|
{
|
|
|
|
for (int i = 0; i < http->handlers_count; i++)
|
|
|
|
{
|
2024-02-15 18:35:01 -05:00
|
|
|
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] == '/'))
|
2023-12-13 18:59:11 -05:00
|
|
|
{
|
|
|
|
*out_callback = http->handlers[i].callback;
|
2024-01-02 21:14:17 -05:00
|
|
|
*out_trace_name = http->handlers[i].pattern;
|
2023-12-13 18:59:11 -05:00
|
|
|
*out_user_data = http->handlers[i].user_data;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-12-13 20:59:23 -05:00
|
|
|
static void _http_on_write(uv_write_t* write, int status)
|
|
|
|
{
|
2023-12-30 11:52:05 -05:00
|
|
|
_http_timer_reset(write->data);
|
2023-12-13 20:59:23 -05:00
|
|
|
tf_free(write);
|
|
|
|
}
|
|
|
|
|
2023-12-18 12:51:15 -05:00
|
|
|
static void _http_connection_on_close(uv_handle_t* handle)
|
|
|
|
{
|
|
|
|
tf_http_connection_t* connection = handle->data;
|
|
|
|
handle->data = NULL;
|
2023-12-25 18:50:55 -05:00
|
|
|
_http_connection_destroy(connection, "handle closed");
|
2023-12-18 12:51:15 -05:00
|
|
|
}
|
|
|
|
|
2023-12-25 18:50:55 -05:00
|
|
|
static void _http_connection_destroy(tf_http_connection_t* connection, const char* reason)
|
2023-12-18 12:51:15 -05:00
|
|
|
{
|
2023-12-30 13:59:02 -05:00
|
|
|
connection->is_shutting_down = true;
|
|
|
|
|
2024-01-26 21:36:08 -05:00
|
|
|
if (connection->request && connection->request->on_close)
|
|
|
|
{
|
|
|
|
tf_http_close_callback* on_close = connection->request->on_close;
|
|
|
|
connection->request->on_close = NULL;
|
|
|
|
tf_trace_begin(connection->http->trace, connection->trace_name ? connection->trace_name : "websocket");
|
|
|
|
on_close(connection->request);
|
|
|
|
tf_trace_end(connection->http->trace);
|
|
|
|
}
|
|
|
|
|
2023-12-30 11:52:05 -05:00
|
|
|
if (connection->tcp.data && !uv_is_closing((uv_handle_t*)&connection->tcp))
|
2023-12-18 12:51:15 -05:00
|
|
|
{
|
|
|
|
uv_close((uv_handle_t*)&connection->tcp, _http_connection_on_close);
|
|
|
|
}
|
2023-12-30 11:52:05 -05:00
|
|
|
if (connection->timeout.data && !uv_is_closing((uv_handle_t*)&connection->timeout))
|
|
|
|
{
|
|
|
|
uv_close((uv_handle_t*)&connection->timeout, _http_connection_on_close);
|
|
|
|
}
|
2023-12-30 13:59:02 -05:00
|
|
|
if (connection->tls)
|
|
|
|
{
|
|
|
|
tf_tls_session_destroy(connection->tls);
|
|
|
|
connection->tls = NULL;
|
|
|
|
}
|
2023-12-30 11:52:05 -05:00
|
|
|
|
2024-02-15 18:35:01 -05:00
|
|
|
if (connection->ref_count == 0 && !connection->tcp.data && !connection->shutdown.data && !connection->timeout.data)
|
2023-12-18 12:51:15 -05:00
|
|
|
{
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
}
|
2023-12-20 18:58:28 -05:00
|
|
|
if (connection->body)
|
|
|
|
{
|
|
|
|
tf_free(connection->body);
|
|
|
|
connection->body = NULL;
|
|
|
|
}
|
2023-12-30 15:35:03 -05:00
|
|
|
if (connection->fragment)
|
|
|
|
{
|
|
|
|
tf_free(connection->fragment);
|
|
|
|
connection->fragment = NULL;
|
|
|
|
}
|
2023-12-18 12:51:15 -05:00
|
|
|
tf_free(connection);
|
2024-02-10 17:09:52 -05:00
|
|
|
|
2024-02-15 18:35:01 -05:00
|
|
|
if (http->is_shutting_down && http->connections_count == 0)
|
2024-02-10 17:09:52 -05:00
|
|
|
{
|
|
|
|
tf_http_destroy(http);
|
|
|
|
}
|
2023-12-18 12:51:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-20 18:58:28 -05:00
|
|
|
static void _http_builtin_404_handler(tf_http_request_t* request)
|
|
|
|
{
|
2024-02-08 20:21:57 -05:00
|
|
|
const char* k_payload = tf_http_status_text(404);
|
2023-12-20 18:58:28 -05:00
|
|
|
tf_http_respond(request, 404, NULL, 0, k_payload, strlen(k_payload));
|
|
|
|
}
|
|
|
|
|
2023-12-20 19:13:03 -05:00
|
|
|
static void _http_reset_connection(tf_http_connection_t* connection)
|
|
|
|
{
|
2023-12-30 16:35:53 -05:00
|
|
|
connection->fragment_op_code = 0;
|
2023-12-30 15:35:03 -05:00
|
|
|
connection->fragment_length = 0;
|
2023-12-20 19:13:03 -05:00
|
|
|
connection->body_length = 0;
|
|
|
|
connection->content_length = 0;
|
2023-12-30 13:59:02 -05:00
|
|
|
connection->headers_buffer_length = 0;
|
|
|
|
connection->headers_length = 0;
|
|
|
|
connection->is_receiving_headers = true;
|
|
|
|
connection->is_response_sent = false;
|
2023-12-23 14:52:59 -05:00
|
|
|
connection->parsed_length = 0;
|
2023-12-30 13:59:02 -05:00
|
|
|
connection->path = NULL;
|
2023-12-20 19:13:03 -05:00
|
|
|
}
|
|
|
|
|
2023-12-29 12:45:07 -05:00
|
|
|
static void _http_websocket_mask_in_place(uint8_t* p, uint32_t mask, size_t size)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
for (; ((intptr_t)(p + i)) % alignof(uint32_t); i++)
|
|
|
|
{
|
|
|
|
p[i] ^= (mask & 0xff);
|
|
|
|
mask = ((mask >> 8) & 0xffffff) | ((mask & 0xff) << 24);
|
|
|
|
}
|
|
|
|
int aligned_start = i;
|
|
|
|
for (; i + 4 < (int)size; i += 4)
|
|
|
|
{
|
|
|
|
*(uint32_t*)(p + i) ^= mask;
|
|
|
|
}
|
|
|
|
for (; i < (int)size; i++)
|
|
|
|
{
|
|
|
|
p[i] ^= ((mask >> (8 * ((i - aligned_start) % 4))) & 0xff);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-20 18:58:28 -05:00
|
|
|
static void _http_add_body_bytes(tf_http_connection_t* connection, const void* data, size_t size)
|
|
|
|
{
|
2023-12-24 17:06:11 -05:00
|
|
|
if (connection->is_websocket)
|
2023-12-20 18:58:28 -05:00
|
|
|
{
|
2023-12-30 16:41:48 -05:00
|
|
|
if (size)
|
|
|
|
{
|
2024-01-27 16:29:06 -05:00
|
|
|
connection->body = tf_resize_vec(connection->body, connection->body_length + size);
|
2023-12-30 16:41:48 -05:00
|
|
|
memcpy((char*)connection->body + connection->body_length, data, size);
|
|
|
|
connection->body_length += size;
|
|
|
|
}
|
2023-12-24 17:06:11 -05:00
|
|
|
|
|
|
|
while (connection->body_length >= 2)
|
|
|
|
{
|
2023-12-29 12:45:07 -05:00
|
|
|
uint8_t* p = connection->body;
|
2023-12-24 17:06:11 -05:00
|
|
|
uint8_t bits0 = p[0];
|
|
|
|
uint8_t bits1 = p[1];
|
|
|
|
if ((bits1 & (1 << 7)) == 0)
|
|
|
|
{
|
|
|
|
/* Unmasked message. */
|
2023-12-25 18:50:55 -05:00
|
|
|
_http_connection_destroy(connection, "websocket server received unmasked bytes");
|
2023-12-24 17:06:11 -05:00
|
|
|
return;
|
|
|
|
}
|
2023-12-29 12:45:07 -05:00
|
|
|
uint8_t op_code = bits0 & 0xf;
|
2023-12-24 17:06:11 -05:00
|
|
|
bool fin = (bits0 & (1 << 7)) != 0;
|
|
|
|
size_t length = bits1 & 0x7f;
|
|
|
|
int mask_start = 2;
|
|
|
|
if (length == 126)
|
|
|
|
{
|
|
|
|
length = 0;
|
|
|
|
for (int i = 0; i < 2; i++)
|
|
|
|
{
|
|
|
|
length <<= 8;
|
|
|
|
length |= p[2 + i];
|
|
|
|
}
|
|
|
|
mask_start = 4;
|
|
|
|
}
|
|
|
|
else if (length == 127)
|
|
|
|
{
|
|
|
|
length = 0;
|
|
|
|
for (int i = 0; i < 8; i++)
|
|
|
|
{
|
|
|
|
length <<= 8;
|
|
|
|
length |= p[2 + i];
|
|
|
|
}
|
|
|
|
mask_start = 10;
|
|
|
|
}
|
2024-01-01 17:14:27 -05:00
|
|
|
size_t total_length = mask_start + 4 + length;
|
|
|
|
if (connection->body_length >= total_length)
|
2023-12-24 17:06:11 -05:00
|
|
|
{
|
|
|
|
uint32_t mask =
|
2024-02-15 18:35:01 -05:00
|
|
|
(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;
|
2024-01-01 17:14:27 -05:00
|
|
|
uint8_t* message = p + mask_start + 4;
|
|
|
|
_http_websocket_mask_in_place(message, mask, length);
|
2023-12-30 15:35:03 -05:00
|
|
|
|
2023-12-30 16:35:53 -05:00
|
|
|
if (!fin && !connection->fragment_op_code)
|
|
|
|
{
|
|
|
|
connection->fragment_op_code = op_code;
|
|
|
|
}
|
|
|
|
|
2023-12-30 15:35:03 -05:00
|
|
|
if (!fin || connection->fragment_length)
|
|
|
|
{
|
2024-01-27 16:29:06 -05:00
|
|
|
connection->fragment = tf_resize_vec(connection->fragment, connection->fragment_length + length);
|
2023-12-30 15:35:03 -05:00
|
|
|
memcpy((uint8_t*)connection->fragment + connection->fragment_length, message, length);
|
|
|
|
connection->fragment_length += length;
|
|
|
|
}
|
|
|
|
|
2023-12-24 17:06:11 -05:00
|
|
|
if (fin)
|
|
|
|
{
|
2023-12-25 18:39:16 -05:00
|
|
|
if (connection->request->on_message)
|
|
|
|
{
|
2024-01-02 21:14:17 -05:00
|
|
|
tf_trace_begin(connection->http->trace, connection->trace_name ? connection->trace_name : "websocket");
|
2024-02-15 18:35:01 -05:00
|
|
|
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);
|
2024-01-02 21:14:17 -05:00
|
|
|
tf_trace_end(connection->http->trace);
|
2023-12-25 18:39:16 -05:00
|
|
|
}
|
2023-12-30 15:35:03 -05:00
|
|
|
connection->fragment_length = 0;
|
2023-12-24 17:06:11 -05:00
|
|
|
}
|
2023-12-29 12:45:07 -05:00
|
|
|
connection->websocket_message_index++;
|
2024-01-01 17:22:03 -05:00
|
|
|
if (connection->body_length >= total_length)
|
2024-01-01 17:14:27 -05:00
|
|
|
{
|
2024-01-01 17:22:03 -05:00
|
|
|
if (connection->body_length > total_length)
|
|
|
|
{
|
|
|
|
memmove(connection->body, (char*)connection->body + total_length, connection->body_length - total_length);
|
|
|
|
}
|
2024-01-01 17:14:27 -05:00
|
|
|
connection->body_length -= total_length;
|
|
|
|
}
|
2023-12-24 17:06:11 -05:00
|
|
|
}
|
2023-12-30 13:59:02 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2023-12-24 17:06:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2023-12-20 18:58:28 -05:00
|
|
|
{
|
2023-12-24 17:06:11 -05:00
|
|
|
size_t fit = tf_min(connection->content_length - connection->body_length, size);
|
|
|
|
if (fit > 0)
|
|
|
|
{
|
|
|
|
memcpy((char*)connection->body + connection->body_length, data, fit);
|
|
|
|
connection->body_length += fit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (connection->body_length == connection->content_length)
|
2023-12-20 18:58:28 -05:00
|
|
|
{
|
2023-12-24 17:06:11 -05:00
|
|
|
tf_http_request_t* request = tf_malloc(sizeof(tf_http_request_t));
|
2024-02-15 18:35:01 -05:00
|
|
|
*request = (tf_http_request_t) {
|
2024-01-02 10:02:47 -05:00
|
|
|
.http = connection->http,
|
2023-12-24 17:06:11 -05:00
|
|
|
.connection = connection,
|
2024-01-02 10:02:47 -05:00
|
|
|
.is_tls = connection->tls != NULL,
|
2023-12-24 17:06:11 -05:00
|
|
|
.method = connection->method,
|
|
|
|
.path = connection->path,
|
|
|
|
.query = connection->query,
|
|
|
|
.body = connection->body,
|
|
|
|
.content_length = connection->content_length,
|
|
|
|
.headers = connection->headers,
|
|
|
|
.headers_count = connection->headers_length,
|
|
|
|
.user_data = connection->user_data,
|
|
|
|
};
|
2023-12-25 18:39:16 -05:00
|
|
|
connection->request = request;
|
2023-12-24 17:06:11 -05:00
|
|
|
|
|
|
|
tf_http_request_ref(request);
|
2024-01-02 21:14:17 -05:00
|
|
|
tf_trace_begin(connection->http->trace, connection->trace_name ? connection->trace_name : "http");
|
2023-12-24 17:06:11 -05:00
|
|
|
connection->callback(request);
|
2024-01-02 21:14:17 -05:00
|
|
|
tf_trace_end(connection->http->trace);
|
2024-01-27 10:45:51 -05:00
|
|
|
tf_http_request_unref(request);
|
2023-12-24 17:06:11 -05:00
|
|
|
}
|
2023-12-20 18:58:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-30 13:59:02 -05:00
|
|
|
static size_t _http_on_read_plain_internal(tf_http_connection_t* connection, const void* data, size_t read_size)
|
2023-12-13 18:59:11 -05:00
|
|
|
{
|
2023-12-30 13:59:02 -05:00
|
|
|
if (connection->is_receiving_headers)
|
2023-12-13 18:59:11 -05:00
|
|
|
{
|
2023-12-30 13:59:02 -05:00
|
|
|
size_t used_read_size = tf_min(read_size, sizeof(connection->headers_buffer) - connection->headers_buffer_length);
|
|
|
|
memcpy(connection->headers_buffer + connection->headers_buffer_length, data, used_read_size);
|
|
|
|
connection->headers_buffer_length += used_read_size;
|
2023-12-20 18:58:28 -05:00
|
|
|
|
2023-12-30 13:59:02 -05:00
|
|
|
const char* method = NULL;
|
|
|
|
size_t method_length = 0;
|
|
|
|
const char* path = NULL;
|
|
|
|
size_t path_length = 0;
|
|
|
|
size_t header_count = sizeof(connection->headers) / sizeof(*connection->headers);
|
2023-12-20 18:58:28 -05:00
|
|
|
|
2024-02-15 18:35:01 -05:00
|
|
|
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);
|
2023-12-30 13:59:02 -05:00
|
|
|
connection->parsed_length = connection->headers_buffer_length;
|
|
|
|
if (parse_result > 0)
|
|
|
|
{
|
|
|
|
connection->is_receiving_headers = false;
|
|
|
|
connection->headers_length = header_count;
|
|
|
|
connection->method = method;
|
|
|
|
((char*)connection->method)[method_length] = '\0';
|
|
|
|
connection->path = path;
|
|
|
|
((char*)connection->path)[path_length] = '\0';
|
|
|
|
char* q = strchr(connection->path, '?');
|
|
|
|
if (q)
|
2023-12-13 18:59:11 -05:00
|
|
|
{
|
2023-12-30 13:59:02 -05:00
|
|
|
*q = '\0';
|
|
|
|
connection->query = q + 1;
|
|
|
|
}
|
2023-12-13 18:59:11 -05:00
|
|
|
|
2023-12-30 13:59:02 -05:00
|
|
|
connection->connection_close = connection->minor_version == 0;
|
2023-12-20 19:13:03 -05:00
|
|
|
|
2023-12-30 13:59:02 -05:00
|
|
|
for (int i = 0; i < (int)header_count; i++)
|
|
|
|
{
|
|
|
|
for (size_t j = 0; j < connection->headers[i].name_len; j++)
|
2023-12-20 18:58:28 -05:00
|
|
|
{
|
2024-02-15 18:35:01 -05:00
|
|
|
if (connection->headers[i].name[j] >= 'A' && connection->headers[i].name[j] <= 'Z')
|
2023-12-20 19:13:03 -05:00
|
|
|
{
|
2023-12-30 13:59:02 -05:00
|
|
|
((char*)connection->headers[i].name)[j] += 'a' - 'A';
|
2023-12-20 19:13:03 -05:00
|
|
|
}
|
2023-12-20 18:58:28 -05:00
|
|
|
}
|
2023-12-30 13:59:02 -05:00
|
|
|
((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)
|
2023-12-20 18:58:28 -05:00
|
|
|
{
|
2023-12-30 13:59:02 -05:00
|
|
|
connection->content_length = strtoull(connection->headers[i].value, NULL, 10);
|
2023-12-20 18:58:28 -05:00
|
|
|
}
|
2023-12-30 13:59:02 -05:00
|
|
|
else if (strcasecmp(connection->headers[i].name, "connection") == 0)
|
2023-12-13 18:59:11 -05:00
|
|
|
{
|
2023-12-30 13:59:02 -05:00
|
|
|
if (strcasecmp(connection->headers[i].value, "close") == 0)
|
|
|
|
{
|
|
|
|
connection->connection_close = true;
|
|
|
|
}
|
2023-12-20 18:58:28 -05:00
|
|
|
}
|
2023-12-30 13:59:02 -05:00
|
|
|
}
|
2023-12-20 18:58:28 -05:00
|
|
|
|
2023-12-30 13:59:02 -05:00
|
|
|
if (connection->content_length)
|
|
|
|
{
|
2024-02-15 19:47:54 -05:00
|
|
|
connection->body = tf_realloc(connection->body, connection->content_length);
|
2023-12-20 18:58:28 -05:00
|
|
|
}
|
2023-12-30 13:59:02 -05:00
|
|
|
|
2024-02-15 18:35:01 -05:00
|
|
|
if (!_http_find_handler(connection->http, connection->path, &connection->callback, &connection->trace_name, &connection->user_data) ||
|
|
|
|
!connection->callback)
|
2023-12-20 19:00:15 -05:00
|
|
|
{
|
2023-12-30 13:59:02 -05:00
|
|
|
connection->callback = _http_builtin_404_handler;
|
2024-01-02 21:14:17 -05:00
|
|
|
connection->trace_name = "404";
|
2023-12-30 13:59:02 -05:00
|
|
|
}
|
2023-12-30 14:18:09 -05:00
|
|
|
size_t consumed = read_size - (connection->headers_buffer_length - parse_result) - (read_size - used_read_size);
|
2023-12-30 13:59:02 -05:00
|
|
|
_http_add_body_bytes(connection, NULL, 0);
|
|
|
|
return consumed;
|
|
|
|
}
|
|
|
|
else if (parse_result == -2)
|
|
|
|
{
|
|
|
|
/* Incomplete. Will try again next time. */
|
|
|
|
return used_read_size;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tf_printf("phr_parse_request: %d\n", parse_result);
|
|
|
|
_http_connection_destroy(connection, "failed to parse request headers");
|
|
|
|
return used_read_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_http_add_body_bytes(connection, data, read_size);
|
|
|
|
return read_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _http_on_read_plain(tf_http_connection_t* connection, const void* data, size_t read_size)
|
|
|
|
{
|
|
|
|
size_t total_consumed = 0;
|
|
|
|
while (total_consumed < read_size)
|
|
|
|
{
|
|
|
|
size_t consumed = _http_on_read_plain_internal(connection, ((const uint8_t*)data) + total_consumed, read_size - total_consumed);
|
|
|
|
if (!consumed)
|
|
|
|
{
|
|
|
|
_http_connection_destroy(connection, "_http_on_read_plain_internal didn't consume any data");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
total_consumed += consumed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _http_on_read(uv_stream_t* stream, ssize_t read_size, const uv_buf_t* buffer)
|
|
|
|
{
|
|
|
|
tf_http_connection_t* connection = stream->data;
|
|
|
|
_http_timer_reset(connection);
|
|
|
|
if (read_size > 0)
|
|
|
|
{
|
|
|
|
if (connection->tls)
|
|
|
|
{
|
|
|
|
if (tf_tls_session_write_encrypted(connection->tls, buffer->base, read_size) < 0)
|
|
|
|
{
|
|
|
|
_http_connection_destroy(connection, "tf_tls_session_write_encrypted");
|
2023-12-20 19:00:15 -05:00
|
|
|
}
|
2023-12-20 18:58:28 -05:00
|
|
|
else
|
|
|
|
{
|
2023-12-30 13:59:02 -05:00
|
|
|
_http_tls_update(connection);
|
2023-12-13 18:59:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-12-30 13:59:02 -05:00
|
|
|
_http_on_read_plain(connection, buffer->base, read_size);
|
2023-12-13 18:59:11 -05:00
|
|
|
}
|
|
|
|
}
|
2023-12-29 12:45:07 -05:00
|
|
|
else if (read_size < 0)
|
2023-12-18 12:51:15 -05:00
|
|
|
{
|
2023-12-25 18:50:55 -05:00
|
|
|
_http_connection_destroy(connection, uv_strerror(read_size));
|
2023-12-18 12:51:15 -05:00
|
|
|
}
|
2023-12-13 18:59:11 -05:00
|
|
|
}
|
|
|
|
|
2023-12-30 11:52:05 -05:00
|
|
|
static void _http_timer_callback(uv_timer_t* timer)
|
|
|
|
{
|
|
|
|
_http_connection_destroy(timer->data, "_http_timer_callback");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _http_timer_reset(tf_http_connection_t* connection)
|
|
|
|
{
|
|
|
|
if (connection->timeout.data)
|
|
|
|
{
|
|
|
|
int r = uv_timer_stop(&connection->timeout);
|
|
|
|
if (r)
|
|
|
|
{
|
|
|
|
tf_printf("uv_timer_stop: %s\n", uv_strerror(r));
|
|
|
|
}
|
|
|
|
r = uv_timer_start(&connection->timeout, _http_timer_callback, k_timeout_ms, 0);
|
|
|
|
if (r)
|
|
|
|
{
|
|
|
|
tf_printf("uv_timer_start: %s\n", uv_strerror(r));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-13 18:59:11 -05:00
|
|
|
static void _http_on_connection(uv_stream_t* stream, int status)
|
|
|
|
{
|
2023-12-30 13:59:02 -05:00
|
|
|
tf_http_listener_t* listener = stream->data;
|
|
|
|
tf_http_t* http = listener->http;
|
2023-12-13 18:59:11 -05:00
|
|
|
tf_http_connection_t* connection = tf_malloc(sizeof(tf_http_connection_t));
|
2023-12-30 13:59:02 -05:00
|
|
|
*connection = (tf_http_connection_t) { .http = http, .tcp = { .data = connection }, .is_receiving_headers = true };
|
|
|
|
if (listener->tls)
|
|
|
|
{
|
|
|
|
connection->tls = tf_tls_context_create_session(listener->tls);
|
|
|
|
if (!connection->tls)
|
|
|
|
{
|
|
|
|
_http_connection_destroy(connection, "tf_tls_context_create_session");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
tf_tls_session_start_accept(connection->tls);
|
|
|
|
connection->is_handshaking = true;
|
|
|
|
}
|
2023-12-13 18:59:11 -05:00
|
|
|
int r = uv_tcp_init(connection->http->loop, &connection->tcp);
|
|
|
|
if (r)
|
|
|
|
{
|
|
|
|
tf_printf("uv_tcp_init: %s\n", uv_strerror(r));
|
2023-12-30 11:52:05 -05:00
|
|
|
_http_connection_destroy(connection, "uv_tcp_init");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = uv_timer_init(connection->http->loop, &connection->timeout);
|
|
|
|
connection->timeout.data = connection;
|
|
|
|
if (r)
|
|
|
|
{
|
|
|
|
tf_printf("uv_timer_init: %s\n", uv_strerror(r));
|
|
|
|
_http_connection_destroy(connection, "uv_timer_init");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
r = uv_timer_start(&connection->timeout, _http_timer_callback, k_timeout_ms, 0);
|
|
|
|
if (r)
|
|
|
|
{
|
|
|
|
tf_printf("uv_timer_start: %s\n", uv_strerror(r));
|
|
|
|
_http_connection_destroy(connection, "uv_timer_start");
|
2023-12-13 18:59:11 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = uv_accept(stream, (uv_stream_t*)&connection->tcp);
|
|
|
|
if (r)
|
|
|
|
{
|
|
|
|
tf_printf("uv_accept: %s\n", uv_strerror(r));
|
2023-12-30 11:52:05 -05:00
|
|
|
_http_connection_destroy(connection, "uv_accept");
|
2023-12-13 18:59:11 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = uv_read_start((uv_stream_t*)&connection->tcp, _http_allocate_buffer, _http_on_read);
|
|
|
|
if (r)
|
|
|
|
{
|
2023-12-30 11:52:05 -05:00
|
|
|
tf_printf("uv_read_start: %s\n", uv_strerror(r));
|
|
|
|
_http_connection_destroy(connection, "uv_read_start");
|
2023-12-13 18:59:11 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-12-30 13:59:02 -05:00
|
|
|
if (connection->tls)
|
|
|
|
{
|
|
|
|
_http_tls_update(connection);
|
|
|
|
}
|
|
|
|
|
2024-01-27 16:29:06 -05:00
|
|
|
http->connections = tf_resize_vec(http->connections, sizeof(tf_http_connection_t*) * (http->connections_count + 1));
|
2023-12-13 18:59:11 -05:00
|
|
|
http->connections[http->connections_count++] = connection;
|
|
|
|
}
|
|
|
|
|
2024-01-27 12:27:56 -05:00
|
|
|
int tf_http_listen(tf_http_t* http, int port, tf_tls_context_t* tls, tf_http_cleanup_t* cleanup, void* user_data)
|
2023-12-13 18:59:11 -05:00
|
|
|
{
|
2023-12-30 13:59:02 -05:00
|
|
|
tf_http_listener_t* listener = tf_malloc(sizeof(tf_http_listener_t));
|
2024-02-15 18:35:01 -05:00
|
|
|
*listener = (tf_http_listener_t) {
|
2024-01-27 12:27:56 -05:00
|
|
|
.http = http,
|
|
|
|
.tls = tls,
|
|
|
|
.tcp = { .data = listener },
|
|
|
|
.cleanup = cleanup,
|
|
|
|
.user_data = user_data,
|
|
|
|
};
|
2023-12-30 13:59:02 -05:00
|
|
|
int r = uv_tcp_init(http->loop, &listener->tcp);
|
2023-12-13 18:59:11 -05:00
|
|
|
if (r)
|
|
|
|
{
|
|
|
|
tf_printf("uv_tcp_init: %s\n", uv_strerror(r));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r == 0)
|
|
|
|
{
|
2024-02-15 18:35:01 -05:00
|
|
|
struct sockaddr_in6 addr = {
|
2023-12-30 22:42:07 -05:00
|
|
|
.sin6_family = AF_INET6,
|
|
|
|
.sin6_addr = IN6ADDR_ANY_INIT,
|
|
|
|
.sin6_port = ntohs(port),
|
2023-12-13 18:59:11 -05:00
|
|
|
};
|
2023-12-30 13:59:02 -05:00
|
|
|
r = uv_tcp_bind(&listener->tcp, (struct sockaddr*)&addr, 0);
|
2023-12-13 18:59:11 -05:00
|
|
|
if (r)
|
|
|
|
{
|
|
|
|
tf_printf("uv_tcp_bind: %s\n", uv_strerror(r));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-30 14:47:36 -05:00
|
|
|
int assigned_port = 0;
|
|
|
|
if (r == 0)
|
|
|
|
{
|
|
|
|
struct sockaddr_storage name = { 0 };
|
|
|
|
int size = (int)sizeof(name);
|
|
|
|
r = uv_tcp_getsockname(&listener->tcp, (struct sockaddr*)&name, &size);
|
|
|
|
assigned_port = ntohs(((struct sockaddr_in*)&name)->sin_port);
|
|
|
|
}
|
|
|
|
|
2023-12-13 18:59:11 -05:00
|
|
|
if (r == 0)
|
|
|
|
{
|
2023-12-30 13:59:02 -05:00
|
|
|
r = uv_listen((uv_stream_t*)&listener->tcp, 16, _http_on_connection);
|
2023-12-13 18:59:11 -05:00
|
|
|
if (r)
|
|
|
|
{
|
|
|
|
tf_printf("uv_listen: %s\n", uv_strerror(r));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r == 0)
|
|
|
|
{
|
2024-01-27 12:27:56 -05:00
|
|
|
http->listeners = tf_resize_vec(http->listeners, sizeof(tf_http_listener_t*) * (http->listeners_count + 1));
|
2023-12-30 13:59:02 -05:00
|
|
|
http->listeners[http->listeners_count++] = listener;
|
2023-12-13 18:59:11 -05:00
|
|
|
}
|
2023-12-30 14:47:36 -05:00
|
|
|
return assigned_port;
|
2023-12-13 18:59:11 -05:00
|
|
|
}
|
|
|
|
|
2024-01-27 12:11:24 -05:00
|
|
|
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)
|
2023-12-13 18:59:11 -05:00
|
|
|
{
|
2024-01-27 16:29:06 -05:00
|
|
|
http->handlers = tf_resize_vec(http->handlers, sizeof(tf_http_handler_t) * (http->handlers_count + 1));
|
2024-02-15 18:35:01 -05:00
|
|
|
http->handlers[http->handlers_count++] = (tf_http_handler_t) {
|
2023-12-13 18:59:11 -05:00
|
|
|
.pattern = tf_strdup(pattern),
|
|
|
|
.callback = callback,
|
2024-01-27 12:11:24 -05:00
|
|
|
.cleanup = cleanup,
|
2023-12-13 18:59:11 -05:00
|
|
|
.user_data = user_data,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-12-30 13:59:02 -05:00
|
|
|
static void _http_free_listener_on_close(uv_handle_t* handle)
|
2023-12-18 12:51:15 -05:00
|
|
|
{
|
2023-12-30 13:59:02 -05:00
|
|
|
tf_http_listener_t* listener = handle->data;
|
2023-12-18 12:51:15 -05:00
|
|
|
handle->data = NULL;
|
2023-12-30 13:59:02 -05:00
|
|
|
tf_free(listener);
|
2023-12-18 12:51:15 -05:00
|
|
|
}
|
|
|
|
|
2023-12-13 18:59:11 -05:00
|
|
|
void tf_http_destroy(tf_http_t* http)
|
|
|
|
{
|
2024-02-10 17:09:52 -05:00
|
|
|
http->is_shutting_down = true;
|
|
|
|
|
|
|
|
for (int i = 0; i < http->connections_count; i++)
|
2023-12-18 12:51:15 -05:00
|
|
|
{
|
2024-02-10 17:09:52 -05:00
|
|
|
_http_connection_destroy(http->connections[i], "tf_http_destroy");
|
2023-12-18 12:51:15 -05:00
|
|
|
}
|
|
|
|
|
2024-02-13 18:07:36 -05:00
|
|
|
for (int i = 0; i < http->listeners_count; i++)
|
|
|
|
{
|
|
|
|
tf_http_listener_t* listener = http->listeners[i];
|
|
|
|
if (listener->cleanup)
|
|
|
|
{
|
|
|
|
listener->cleanup(listener->user_data);
|
|
|
|
listener->cleanup = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < http->handlers_count; i++)
|
|
|
|
{
|
|
|
|
if (http->handlers[i].cleanup)
|
|
|
|
{
|
|
|
|
http->handlers[i].cleanup(http->handlers[i].user_data);
|
|
|
|
http->handlers[i].cleanup = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (http->user_data_cleanup)
|
|
|
|
{
|
|
|
|
http->user_data_cleanup(http->user_data);
|
|
|
|
http->user_data = NULL;
|
|
|
|
}
|
|
|
|
|
2024-02-10 17:09:52 -05:00
|
|
|
if (http->connections_count == 0)
|
2023-12-20 18:58:28 -05:00
|
|
|
{
|
2024-02-10 17:09:52 -05:00
|
|
|
tf_free(http->connections);
|
|
|
|
http->connections = NULL;
|
|
|
|
|
|
|
|
for (int i = 0; i < http->listeners_count; i++)
|
2023-12-20 18:58:28 -05:00
|
|
|
{
|
2024-02-10 17:09:52 -05:00
|
|
|
tf_http_listener_t* listener = http->listeners[i];
|
|
|
|
uv_close((uv_handle_t*)&listener->tcp, _http_free_listener_on_close);
|
2023-12-20 18:58:28 -05:00
|
|
|
}
|
2024-02-10 17:09:52 -05:00
|
|
|
tf_free(http->listeners);
|
|
|
|
http->listeners = NULL;
|
|
|
|
http->listeners_count = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < http->handlers_count; i++)
|
2024-01-27 12:11:24 -05:00
|
|
|
{
|
2024-02-10 17:09:52 -05:00
|
|
|
if (http->handlers[i].pattern)
|
|
|
|
{
|
|
|
|
tf_free((void*)http->handlers[i].pattern);
|
|
|
|
http->handlers[i].pattern = NULL;
|
|
|
|
}
|
2024-01-27 12:11:24 -05:00
|
|
|
}
|
2024-02-10 17:09:52 -05:00
|
|
|
tf_free(http->handlers);
|
|
|
|
http->handlers_count = 0;
|
2023-12-18 12:51:15 -05:00
|
|
|
|
2024-02-10 17:09:52 -05:00
|
|
|
tf_free(http);
|
2024-01-02 10:02:47 -05:00
|
|
|
}
|
2023-12-13 18:59:11 -05:00
|
|
|
}
|
2023-12-17 12:44:54 -05:00
|
|
|
|
2024-02-08 20:21:57 -05:00
|
|
|
const char* tf_http_status_text(int status)
|
2023-12-17 12:44:54 -05:00
|
|
|
{
|
|
|
|
switch (status)
|
|
|
|
{
|
2024-02-15 18:35:01 -05:00
|
|
|
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";
|
2023-12-17 12:44:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _http_on_shutdown(uv_shutdown_t* request, int status)
|
|
|
|
{
|
2023-12-21 12:15:59 -05:00
|
|
|
request->data = NULL;
|
2023-12-17 12:44:54 -05:00
|
|
|
}
|
|
|
|
|
2023-12-30 13:59:02 -05:00
|
|
|
static void _http_write_internal(tf_http_connection_t* connection, const void* data, size_t size)
|
|
|
|
{
|
|
|
|
if (size && !connection->is_shutting_down)
|
|
|
|
{
|
|
|
|
uv_write_t* write = tf_malloc(sizeof(uv_write_t) + size);
|
|
|
|
*write = (uv_write_t) { .data = connection };
|
|
|
|
memcpy(write + 1, data, size);
|
|
|
|
int r = uv_write(write, (uv_stream_t*)&connection->tcp, &(uv_buf_t) { .base = (void*)(write + 1), .len = size }, 1, _http_on_write);
|
|
|
|
if (r)
|
|
|
|
{
|
|
|
|
tf_printf("uv_write: %s\n", uv_strerror(r));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _http_tls_update(tf_http_connection_t* connection)
|
|
|
|
{
|
|
|
|
bool again = true;
|
|
|
|
while (again)
|
|
|
|
{
|
|
|
|
again = false;
|
|
|
|
|
2023-12-30 22:05:52 -05:00
|
|
|
if (connection->is_handshaking && connection->tls)
|
2023-12-30 13:59:02 -05:00
|
|
|
{
|
|
|
|
switch (tf_tls_session_handshake(connection->tls))
|
|
|
|
{
|
|
|
|
case k_tls_handshake_done:
|
|
|
|
connection->is_handshaking = false;
|
|
|
|
break;
|
|
|
|
case k_tls_handshake_more:
|
|
|
|
break;
|
|
|
|
case k_tls_handshake_failed:
|
|
|
|
_http_connection_destroy(connection, "tf_tls_session_handshake");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-30 22:05:52 -05:00
|
|
|
/* Maybe we became disconnected and cleaned up our TLS session. */
|
|
|
|
if (connection->tls)
|
2023-12-30 13:59:02 -05:00
|
|
|
{
|
2023-12-30 22:05:52 -05:00
|
|
|
char buffer[8192];
|
|
|
|
int r = tf_tls_session_read_encrypted(connection->tls, buffer, sizeof(buffer));
|
|
|
|
if (r > 0)
|
|
|
|
{
|
|
|
|
_http_write_internal(connection, buffer, r);
|
|
|
|
again = true;
|
|
|
|
}
|
2023-12-30 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2023-12-30 22:05:52 -05:00
|
|
|
if (connection->tls)
|
2023-12-30 13:59:02 -05:00
|
|
|
{
|
2023-12-30 22:05:52 -05:00
|
|
|
char buffer[8192];
|
|
|
|
int r = tf_tls_session_read_plain(connection->tls, buffer, sizeof(buffer));
|
|
|
|
if (r > 0)
|
|
|
|
{
|
|
|
|
_http_on_read_plain(connection, buffer, r);
|
|
|
|
again = true;
|
|
|
|
}
|
2023-12-30 13:59:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-25 17:53:05 -05:00
|
|
|
static void _http_write(tf_http_connection_t* connection, const void* data, size_t size)
|
|
|
|
{
|
2023-12-30 11:52:05 -05:00
|
|
|
_http_timer_reset(connection);
|
2023-12-30 13:59:02 -05:00
|
|
|
if (connection->tls)
|
2023-12-25 17:53:05 -05:00
|
|
|
{
|
2023-12-30 13:59:02 -05:00
|
|
|
int r = tf_tls_session_write_plain(connection->tls, data, size);
|
|
|
|
if (r < (ssize_t)size)
|
|
|
|
{
|
|
|
|
char buffer[8192];
|
|
|
|
tf_tls_session_get_error(connection->tls, buffer, sizeof(buffer));
|
|
|
|
tf_printf("tf_tls_session_write_plain: %s\n", buffer);
|
|
|
|
}
|
|
|
|
_http_tls_update(connection);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_http_write_internal(connection, data, size);
|
2023-12-25 17:53:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void tf_http_request_send(tf_http_request_t* request, const void* data, size_t size)
|
|
|
|
{
|
|
|
|
_http_write(request->connection, data, size);
|
|
|
|
}
|
|
|
|
|
2023-12-18 12:51:15 -05:00
|
|
|
void tf_http_respond(tf_http_request_t* request, int status, const char** headers, int headers_count, const void* body, size_t content_length)
|
2023-12-17 12:44:54 -05:00
|
|
|
{
|
2023-12-30 13:59:02 -05:00
|
|
|
if (request->connection->is_response_sent)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
request->connection->is_response_sent = true;
|
|
|
|
|
2024-02-08 20:21:57 -05:00
|
|
|
const char* status_text = tf_http_status_text(status);
|
2023-12-17 12:44:54 -05:00
|
|
|
/* HTTP/1.x 200 OK\r\n */
|
2023-12-20 18:13:03 -05:00
|
|
|
bool sent_content_length = false;
|
2023-12-17 12:44:54 -05:00
|
|
|
int headers_length = 8 + 1 + 3 + 1 + strlen(status_text) + 2;
|
2023-12-24 16:39:51 -05:00
|
|
|
if (headers)
|
2023-12-17 12:44:54 -05:00
|
|
|
{
|
2023-12-24 16:39:51 -05:00
|
|
|
for (int i = 0; i < headers_count * 2; i += 2)
|
2023-12-20 18:13:03 -05:00
|
|
|
{
|
2023-12-24 16:39:51 -05:00
|
|
|
/* Key: Value\r\n */
|
|
|
|
headers_length += strlen(headers[i]) + 2 + strlen(headers[i + 1]) + 2;
|
|
|
|
if (strcasecmp(headers[i], "content-length") == 0)
|
|
|
|
{
|
|
|
|
sent_content_length = true;
|
|
|
|
}
|
2023-12-20 18:13:03 -05:00
|
|
|
}
|
2023-12-17 12:44:54 -05:00
|
|
|
}
|
|
|
|
/* \r\n */
|
|
|
|
headers_length += 2;
|
|
|
|
|
2023-12-20 18:13:03 -05:00
|
|
|
char content_length_buffer[32] = { 0 };
|
|
|
|
int content_length_buffer_length = 0;
|
2023-12-24 12:43:33 -05:00
|
|
|
if (!sent_content_length && status != 101)
|
2023-12-20 18:13:03 -05:00
|
|
|
{
|
|
|
|
content_length_buffer_length = snprintf(content_length_buffer, sizeof(content_length_buffer), "Content-Length: %zd\r\n", content_length);
|
|
|
|
headers_length += content_length_buffer_length;
|
|
|
|
}
|
|
|
|
|
2023-12-30 13:59:02 -05:00
|
|
|
char* buffer = alloca(headers_length + 1);
|
2023-12-20 19:13:03 -05:00
|
|
|
int offset = snprintf(buffer, headers_length + 1, "HTTP/1.%d %03d %s\r\n", request->connection->minor_version, status, status_text);
|
2023-12-24 16:39:51 -05:00
|
|
|
if (headers)
|
2023-12-17 12:44:54 -05:00
|
|
|
{
|
2023-12-24 16:39:51 -05:00
|
|
|
for (int i = 0; i < headers_count * 2; i += 2)
|
|
|
|
{
|
|
|
|
offset += snprintf(buffer + offset, headers_length + 1 - offset, "%s: %s\r\n", headers[i], headers[i + 1]);
|
|
|
|
}
|
2023-12-17 12:44:54 -05:00
|
|
|
}
|
2023-12-20 18:13:03 -05:00
|
|
|
if (!sent_content_length)
|
|
|
|
{
|
|
|
|
memcpy(buffer + offset, content_length_buffer, content_length_buffer_length);
|
|
|
|
offset += content_length_buffer_length;
|
|
|
|
}
|
2023-12-17 12:44:54 -05:00
|
|
|
offset += snprintf(buffer + offset, headers_length + 1 - offset, "\r\n");
|
|
|
|
assert(offset == headers_length);
|
2023-12-30 13:59:02 -05:00
|
|
|
_http_write(request->connection, buffer, headers_length);
|
2023-12-17 12:44:54 -05:00
|
|
|
if (content_length)
|
|
|
|
{
|
2023-12-30 13:59:02 -05:00
|
|
|
_http_write(request->connection, body, content_length);
|
2023-12-17 12:44:54 -05:00
|
|
|
}
|
2023-12-30 11:52:05 -05:00
|
|
|
_http_timer_reset(request->connection);
|
2023-12-17 12:44:54 -05:00
|
|
|
|
2024-02-15 18:35:01 -05:00
|
|
|
if (request->connection->connection_close && !request->connection->shutdown.data)
|
2023-12-20 19:13:03 -05:00
|
|
|
{
|
2023-12-21 12:15:59 -05:00
|
|
|
request->connection->shutdown.data = request->connection;
|
|
|
|
uv_shutdown(&request->connection->shutdown, (uv_stream_t*)&request->connection->tcp, _http_on_shutdown);
|
2023-12-20 19:13:03 -05:00
|
|
|
}
|
2023-12-17 12:44:54 -05:00
|
|
|
}
|
2023-12-20 18:58:28 -05:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2023-12-21 12:45:06 -05:00
|
|
|
|
|
|
|
void tf_http_request_ref(tf_http_request_t* request)
|
|
|
|
{
|
|
|
|
request->ref_count++;
|
|
|
|
request->connection->ref_count++;
|
|
|
|
}
|
|
|
|
|
2024-01-27 10:45:51 -05:00
|
|
|
void tf_http_request_unref(tf_http_request_t* request)
|
2023-12-21 12:45:06 -05:00
|
|
|
{
|
2024-02-15 19:47:54 -05:00
|
|
|
bool connection_destroyed = false;
|
|
|
|
|
2023-12-21 12:45:06 -05:00
|
|
|
if (--request->connection->ref_count == 0)
|
|
|
|
{
|
2024-02-15 19:47:54 -05:00
|
|
|
if (request->connection->http->is_shutting_down)
|
|
|
|
{
|
|
|
|
_http_connection_destroy(request->connection, "unref during shutdown");
|
|
|
|
connection_destroyed = true;
|
|
|
|
}
|
|
|
|
else if (!request->connection->is_websocket)
|
2023-12-30 13:59:02 -05:00
|
|
|
{
|
|
|
|
_http_reset_connection(request->connection);
|
|
|
|
}
|
2023-12-21 12:45:06 -05:00
|
|
|
}
|
2024-01-27 10:45:51 -05:00
|
|
|
|
2023-12-21 12:45:06 -05:00
|
|
|
if (--request->ref_count == 0)
|
|
|
|
{
|
2024-02-15 19:47:54 -05:00
|
|
|
if (!connection_destroyed)
|
|
|
|
{
|
|
|
|
request->connection->request = NULL;
|
|
|
|
}
|
2023-12-21 12:45:06 -05:00
|
|
|
tf_free(request);
|
|
|
|
}
|
|
|
|
}
|
2023-12-24 12:43:33 -05:00
|
|
|
|
|
|
|
static const char* _http_connection_get_header(const tf_http_connection_t* connection, const char* name)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < connection->headers_length; i++)
|
|
|
|
{
|
|
|
|
if (strcasecmp(connection->headers[i].name, name) == 0)
|
|
|
|
{
|
|
|
|
return connection->headers[i].value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* tf_http_request_get_header(tf_http_request_t* request, const char* name)
|
|
|
|
{
|
|
|
|
return _http_connection_get_header(request->connection, name);
|
|
|
|
}
|
2023-12-30 11:29:16 -05:00
|
|
|
|
|
|
|
void tf_http_request_websocket_upgrade(tf_http_request_t* request)
|
|
|
|
{
|
|
|
|
request->connection->is_websocket = true;
|
|
|
|
}
|
2024-01-02 10:02:47 -05:00
|
|
|
|
|
|
|
void tf_http_set_user_data(tf_http_t* http, void* user_data, tf_http_cleanup_t* cleanup)
|
|
|
|
{
|
|
|
|
if (http->user_data && http->user_data_cleanup)
|
|
|
|
{
|
|
|
|
http->user_data_cleanup(http->user_data);
|
|
|
|
}
|
|
|
|
http->user_data = user_data;
|
|
|
|
http->user_data_cleanup = cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* tf_http_get_user_data(tf_http_t* http)
|
|
|
|
{
|
|
|
|
return http->user_data;
|
|
|
|
}
|