Bug fixes

This commit is contained in:
2025-11-21 21:10:54 +01:00
parent 51d3f84715
commit 54c02827be
11 changed files with 111 additions and 86 deletions
-1
View File
@@ -54,7 +54,6 @@ static bool is_printable(char c)
return c >= ' ' && c <= '~';
}
#include <stdio.h>
void print_bytes(HTTP_String prefix, HTTP_String src)
{
if (src.len == 0)
+3 -7
View File
@@ -268,17 +268,13 @@ int http_request_builder_send(HTTP_RequestBuilder builder)
// Set up target based on host type
if (conn->url.authority.host.mode == HTTP_HOST_MODE_NAME) {
target.type = CONNECT_TARGET_NAME;
target.name = (String) {
conn->url.authority.host.name.ptr,
conn->url.authority.host.name.len
};
target.name = conn->url.authority.host.name;
} else if (conn->url.authority.host.mode == HTTP_HOST_MODE_IPV4) {
target.type = CONNECT_TARGET_IPV4;
target.ipv4 = (IPv4) { conn->url.authority.host.ipv4.data };
target.ipv4 = conn->url.authority.host.ipv4;
} else if (conn->url.authority.host.mode == HTTP_HOST_MODE_IPV6) {
target.type = CONNECT_TARGET_IPV6;
for (int i = 0; i < 8; i++)
target.ipv6.data[i] = conn->url.authority.host.ipv6.data[i];
target.ipv6 = conn->url.authority.host.ipv6;
} else {
// Invalid host mode - clean up connection
http_client_conn_free(conn);
+2 -1
View File
@@ -1,4 +1,5 @@
#include <stdio.h>
#include <stdint.h>
#include <assert.h>
#include <stdbool.h>
@@ -17,7 +18,7 @@
#include <pthread.h>
#include <poll.h>
#include <fcntl.h>
#include <errno.h>
#include <errno.h>-
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
+4 -4
View File
@@ -42,7 +42,7 @@ void http_server_free(HTTP_Server *server)
for (int i = 0, j = 0; j < server->num_conns; i++) {
HTTP_ServerConn *conn = &server->conns[i];
if (conn->state != HTTP_SERVER_CONN_FREE)
if (conn->state == HTTP_SERVER_CONN_FREE)
continue;
j++;
@@ -212,7 +212,7 @@ int http_server_process_events(HTTP_Server *server,
int num = 0;
ByteView src = byte_queue_read_buf(&conn->output);
if (src.len) num = socket_recv(&server->sockets, conn->handle, src.ptr, src.len);
if (src.len) num = socket_send(&server->sockets, conn->handle, src.ptr, src.len);
byte_queue_read_ack(&conn->output, num);
if (byte_queue_error(&conn->output))
@@ -430,7 +430,7 @@ static void patch_special_headers(HTTP_ServerConn *conn)
byte_queue_patch(&conn->output, conn->content_length_value_offset, tmp, len);
}
void http_response_builder_body(HTTP_ResponseBuilder builder, String str)
void http_response_builder_body(HTTP_ResponseBuilder builder, HTTP_String str)
{
HTTP_ServerConn *conn = builder_to_conn(builder);
if (conn == NULL)
@@ -447,7 +447,7 @@ void http_response_builder_body(HTTP_ResponseBuilder builder, String str)
byte_queue_write(&conn->output, str.ptr, str.len);
}
void http_response_builder_send(HTTP_ResponseBuilder builder, String str)
void http_response_builder_send(HTTP_ResponseBuilder builder, HTTP_String str)
{
HTTP_ServerConn *conn = builder_to_conn(builder);
if (conn == NULL)
+2 -2
View File
@@ -201,8 +201,8 @@ void http_response_builder_status(HTTP_ResponseBuilder builder, int status);
void http_response_builder_header(HTTP_ResponseBuilder builder, HTTP_String str);
// Append some bytes to the response's body
void http_response_builder_body(HTTP_ResponseBuilder builder, String str);
void http_response_builder_body(HTTP_ResponseBuilder builder, HTTP_String str);
// Mark the response as complete. This will invalidate
// the response builder handle.
void http_response_builder_send(HTTP_ResponseBuilder builder, String str);
void http_response_builder_send(HTTP_ResponseBuilder builder, HTTP_String str);
+23 -9
View File
@@ -231,7 +231,8 @@ int socket_manager_add_certificate(SocketManager *sm,
static bool is_secure(Socket *s)
{
#ifdef HTTPS_ENABLED
return (s->server_secure_context != NULL);
return s->server_secure_context != NULL
|| s->client_secure_context != NULL;
#else
return false;
#endif
@@ -358,7 +359,7 @@ static void socket_update(Socket *s)
struct sockaddr_in buf;
buf.sin_family = AF_INET;
buf.sin_port = htons(addr.port);
memcpy(&buf.sin_addr, &addr.ipv4, sizeof(IPv4));
memcpy(&buf.sin_addr, &addr.ipv4, sizeof(HTTP_IPv4));
ret = connect(sock, (struct sockaddr*) &buf, sizeof(buf));
} else {
struct sockaddr_in6 buf;
@@ -442,7 +443,7 @@ static void socket_update(Socket *s)
} else {
#ifdef HTTPS_ENABLED
if (s->ssl == NULL) {
s->ssl = SSL_new(s->server_secure_context->p);
s->ssl = SSL_new(s->client_secure_context->p);
if (s->ssl == NULL) {
s->state = SOCKET_STATE_DIED;
s->events = 0;
@@ -667,7 +668,7 @@ static int socket_manager_register_events_nolock(
// be processed immediately.
for (int i = 0, j = 0; j < sm->num_used; i++) {
Socket *s = &sm->sockets[i];
if (s->state = SOCKET_STATE_FREE)
if (s->state == SOCKET_STATE_FREE)
continue;
j++;
@@ -760,6 +761,10 @@ static int socket_manager_translate_events_nolock(
s->user = NULL;
#ifdef HTTPS_ENABLED
s->ssl = NULL;
s->server_secure_context = NULL;
s->client_secure_context = NULL;
if (secure)
&s->server_secure_context = sm->server_secure_context;
#endif
socket_update(s);
@@ -889,7 +894,7 @@ static int resolve_connect_targets(ConnectTarget *targets,
for (struct addrinfo *rp = res; rp; rp = rp->ai_next) {
if (rp->ai_family == AF_INET) {
IPv4 ipv4 = *(IPv4*) &((struct sockaddr_in*)rp->ai_addr)->sin_addr;
HTTP_IPv4 ipv4 = *(HTTP_IPv4*) &((struct sockaddr_in*)rp->ai_addr)->sin_addr;
if (num_resolved < max_resolved) {
resolved[num_resolved].is_ipv4 = true;
resolved[num_resolved].ipv4 = ipv4;
@@ -901,7 +906,7 @@ static int resolve_connect_targets(ConnectTarget *targets,
num_resolved++;
}
} else if (rp->ai_family == AF_INET6) {
IPv6 ipv6 = *(IPv6*) &((struct sockaddr_in6*)rp->ai_addr)->sin6_addr;
HTTP_IPv6 ipv6 = *(HTTP_IPv6*) &((struct sockaddr_in6*)rp->ai_addr)->sin6_addr;
if (num_resolved < max_resolved) {
resolved[num_resolved].is_ipv4 = false;
resolved[num_resolved].ipv6 = ipv6;
@@ -958,7 +963,13 @@ int socket_connect(SocketManager *sm, int num_targets,
if (sm->num_used == sm->max_used)
return -1;
#ifndef HTTPS_ENABLED
#ifdef HTTPS_ENABLED
if (!sm->at_least_one_secure_connect) {
if (client_secure_context_init(&sm->client_secure_context) < 0)
return -1;
sm->at_least_one_secure_connect = true;
}
#else
if (secure)
return -1;
#endif
@@ -994,8 +1005,11 @@ int socket_connect(SocketManager *sm, int num_targets,
s->sock = NATIVE_SOCKET_INVALID;
s->user = user;
#ifdef HTTPS_ENABLED
s->server_secure_context = &sm->server_secure_context;
s->server_secure_context = NULL;
s->client_secure_context = NULL;
s->ssl = NULL;
if (secure)
s->client_secure_context = &sm->client_secure_context;
#endif
sm->num_used++;
return 0;
@@ -1048,7 +1062,7 @@ static int socket_recv_nolock(SocketManager *sm, SocketHandle handle,
}
ret = 0;
}
return 0;
return ret;
} else {
#ifdef HTTPS_ENABLED
int ret = SSL_read(s->ssl, dst, max);
+6 -18
View File
@@ -61,21 +61,8 @@
typedef uint32_t SocketHandle;
#define SOCKET_HANDLE_INVALID ((SocketHandle) 0)
typedef struct {
char *ptr;
int len;
} String;
typedef uint16_t Port;
typedef struct {
uint32_t data;
} IPv4;
typedef struct {
uint16_t data[8];
} IPv6;
typedef enum {
SOCKET_EVENT_READY,
SOCKET_EVENT_DISCONNECT,
@@ -136,8 +123,8 @@ typedef struct {
// Internal use only
typedef struct {
union {
IPv4 ipv4;
IPv6 ipv6;
HTTP_IPv4 ipv4;
HTTP_IPv6 ipv6;
};
bool is_ipv4;
Port port;
@@ -183,6 +170,7 @@ typedef struct {
};
#ifdef HTTPS_ENABLED
ClientSecureContext *client_secure_context;
ServerSecureContext *server_secure_context;
SSL *ssl;
#endif
@@ -313,9 +301,9 @@ typedef struct {
ConnectTargetType type;
Port port;
union {
IPv4 ipv4;
IPv6 ipv6;
String name;
HTTP_IPv4 ipv4;
HTTP_IPv6 ipv6;
HTTP_String name;
};
} ConnectTarget;