Fix HTTP request creation and simple client example

This commit is contained in:
2025-11-23 23:27:08 +01:00
parent 3c0aecb5f0
commit f6b2c805c6
4 changed files with 41 additions and 11 deletions
+15 -3
View File
@@ -2305,7 +2305,12 @@ static void socket_update(Socket *s)
continue;
}
}
AddressAndPort addr = s->addrs[s->next_addr];
AddressAndPort addr;
if (s->num_addr == 1)
addr = s->addr;
else
addr = s->addrs[s->next_addr];
int family = (addr.is_ipv4 ? AF_INET : AF_INET6);
NATIVE_SOCKET sock = socket(family, SOCK_STREAM, 0);
@@ -2972,7 +2977,6 @@ int socket_connect(SocketManager *sm, int num_targets,
s->state = SOCKET_STATE_PENDING;
s->sock = NATIVE_SOCKET_INVALID;
s->events = 0;
s->user = user;
#ifdef HTTPS_ENABLED
s->server_secure_context = NULL;
@@ -2982,6 +2986,8 @@ int socket_connect(SocketManager *sm, int num_targets,
s->client_secure_context = &sm->client_secure_context;
#endif
sm->num_used++;
socket_update(s);
return 0;
}
@@ -3902,16 +3908,22 @@ void http_request_builder_url(HTTP_RequestBuilder builder,
// Write method
HTTP_String method_str = get_method_string(method);
byte_queue_write(&conn->output, method_str.ptr, method_str.len);
byte_queue_write(&conn->output, " ", 1);
// Write path
if (conn->url.path.len == 0)
byte_queue_write(&conn->output, "/", 1);
else
byte_queue_write(&conn->output, conn->url.path.ptr, conn->url.path.len);
// Write query string
HTTP_String query = conn->url.query;
if (query.len > 0) {
byte_queue_write(&conn->output, "?", 1);
byte_queue_write(&conn->output, query.ptr, query.len);
}
HTTP_String version = HTTP_STR("HTTP/1.1");
HTTP_String version = HTTP_STR(" HTTP/1.1");
byte_queue_write(&conn->output, version.ptr, version.len);
byte_queue_write(&conn->output, "\r\n", 2);
+5
View File
@@ -48,12 +48,17 @@ int main(void)
if (http_client_process_events(&client, &reg) < 0)
return -1;
bool done = false;
HTTP_Response *response;
void *user;
while (http_client_next_response(&client, &response, &user)) {
printf("Received response\n");
http_free_response(response);
done = true;
}
if (done) break;
}
http_client_free(&client);
+7 -1
View File
@@ -249,16 +249,22 @@ void http_request_builder_url(HTTP_RequestBuilder builder,
// Write method
HTTP_String method_str = get_method_string(method);
byte_queue_write(&conn->output, method_str.ptr, method_str.len);
byte_queue_write(&conn->output, " ", 1);
// Write path
if (conn->url.path.len == 0)
byte_queue_write(&conn->output, "/", 1);
else
byte_queue_write(&conn->output, conn->url.path.ptr, conn->url.path.len);
// Write query string
HTTP_String query = conn->url.query;
if (query.len > 0) {
byte_queue_write(&conn->output, "?", 1);
byte_queue_write(&conn->output, query.ptr, query.len);
}
HTTP_String version = HTTP_STR("HTTP/1.1");
HTTP_String version = HTTP_STR(" HTTP/1.1");
byte_queue_write(&conn->output, version.ptr, version.len);
byte_queue_write(&conn->output, "\r\n", 2);
+8 -1
View File
@@ -335,7 +335,12 @@ static void socket_update(Socket *s)
continue;
}
}
AddressAndPort addr = s->addrs[s->next_addr];
AddressAndPort addr;
if (s->num_addr == 1)
addr = s->addr;
else
addr = s->addrs[s->next_addr];
int family = (addr.is_ipv4 ? AF_INET : AF_INET6);
NATIVE_SOCKET sock = socket(family, SOCK_STREAM, 0);
@@ -1011,6 +1016,8 @@ int socket_connect(SocketManager *sm, int num_targets,
s->client_secure_context = &sm->client_secure_context;
#endif
sm->num_used++;
socket_update(s);
return 0;
}