Bug fixes & cleanups

This commit is contained in:
2025-11-23 23:07:08 +01:00
parent 655289e542
commit 3c0aecb5f0
6 changed files with 69 additions and 54 deletions
+22 -6
View File
@@ -1,3 +1,4 @@
static void http_client_conn_free(HTTP_ClientConn *conn)
{
byte_queue_free(&conn->output);
@@ -218,19 +219,31 @@ void http_request_builder_url(HTTP_RequestBuilder builder,
if (conn->state != HTTP_CLIENT_CONN_WAIT_LINE)
return; // Request line already written
// Allocate a copy of the URL string so the parsed URL pointers remain valid
// Allocate a copy of the URL string so the parsed
// URL pointers remain valid
char *url_copy = malloc(url.len);
if (url_copy == NULL)
return; // TODO: set error
if (url_copy == NULL) {
conn->state = HTTP_CLIENT_CONN_COMPLETE;
conn->result = -1;
return;
}
memcpy(url_copy, url.ptr, url.len);
conn->url_buffer.ptr = url_copy;
conn->url_buffer.len = url.len;
// Parse the copied URL (all url.* pointers will reference url_buffer)
if (http_parse_url(conn->url_buffer.ptr, conn->url_buffer.len, &conn->url) != 1) {
free(conn->url_buffer.ptr);
return; // TODO: set error
if (http_parse_url(conn->url_buffer.ptr, conn->url_buffer.len, &conn->url) < 0) {
conn->state = HTTP_CLIENT_CONN_COMPLETE;
conn->result = -1;
return;
}
if (!http_streq(conn->url.scheme, HTTP_STR("http")) &&
!http_streq(conn->url.scheme, HTTP_STR("https"))) {
conn->state = HTTP_CLIENT_CONN_COMPLETE;
conn->result = -1;
return;
}
// Write method
@@ -370,6 +383,9 @@ int http_request_builder_send(HTTP_RequestBuilder builder)
if (conn == NULL)
return -1;
if (conn->state == HTTP_CLIENT_CONN_COMPLETE)
goto error; // Early completion due to an error
if (conn->state == HTTP_CLIENT_CONN_WAIT_HEADER) {
// No body, just end headers
byte_queue_write(&conn->output, "\r\n", 2);
-1
View File
@@ -134,7 +134,6 @@ struct HTTP_Client {
// allocating the exact number of sockets we
// will need.
Socket socket_pool[HTTP_CLIENT_CAPACITY];
};
// Initialize an HTTP client object. This allows one to
+4 -13
View File
@@ -620,16 +620,9 @@ int socket_manager_wakeup(SocketManager *sm)
return ret;
}
static int socket_manager_register_events_nolock(
static void socket_manager_register_events_nolock(
SocketManager *sm, EventRegister *reg)
{
// The poll array must be able to hold descriptors
// for a socket manager at full capacity. Note that
// other than having a number of connection sockets,
// the manager also needs 2 for the listeners and
// one for the wakeup self-pipe.
if (reg->max_polled < sm->max_used+3)
return -1;
reg->num_polled = 0;
reg->polled[reg->num_polled].fd = sm->wait_sock;
@@ -674,7 +667,7 @@ static int socket_manager_register_events_nolock(
// empty list.
if (s->state == SOCKET_STATE_DIED || s->state == SOCKET_STATE_ESTABLISHED_READY) {
reg->num_polled = 0;
return 0;
return;
}
if (s->events) {
@@ -685,8 +678,6 @@ static int socket_manager_register_events_nolock(
reg->num_polled++;
}
}
return 0;
}
int socket_manager_register_events(SocketManager *sm,
@@ -695,11 +686,11 @@ int socket_manager_register_events(SocketManager *sm,
if (mutex_lock(&sm->mutex) < 0)
return -1;
int ret = socket_manager_register_events_nolock(sm, reg);
socket_manager_register_events_nolock(sm, reg);
if (mutex_unlock(&sm->mutex) < 0)
return -1;
return ret;
return 0;
}
static SocketHandle