From 0c50cbff33865aa144a2360f5dc2eec30c6e7d19 Mon Sep 17 00:00:00 2001 From: Francesco Cozzuto Date: Wed, 26 Nov 2025 21:11:39 +0100 Subject: [PATCH] Separate http_request_builder_url into http_request_builder_method/_target --- chttp.c | 31 ++++++++++++++++++++++--------- chttp.h | 26 ++++++++++++++++++-------- examples/simple_client.c | 8 +++----- src/client.c | 31 ++++++++++++++++++++++--------- src/client.h | 26 ++++++++++++++++++-------- 5 files changed, 83 insertions(+), 39 deletions(-) diff --git a/chttp.c b/chttp.c index 969eb7c..60124bb 100644 --- a/chttp.c +++ b/chttp.c @@ -3844,7 +3844,7 @@ HTTP_RequestBuilder http_client_get_builder(HTTP_Client *client) } client->num_conns++; - client->conns[i].state = HTTP_CLIENT_CONN_WAIT_LINE; + client->conns[i].state = HTTP_CLIENT_CONN_WAIT_METHOD; client->conns[i].handle = SOCKET_HANDLE_INVALID; client->conns[i].client = client; client->conns[i].user = NULL; @@ -3952,14 +3952,32 @@ void http_request_builder_set_trace_bytes(HTTP_RequestBuilder builder, bool trac conn->trace_bytes = trace_bytes; } -void http_request_builder_url(HTTP_RequestBuilder builder, - HTTP_Method method, HTTP_String url) +void http_request_builder_method(HTTP_RequestBuilder builder, + HTTP_Method method) { HTTP_ClientConn *conn = request_builder_to_conn(builder); if (conn == NULL) return; // Invalid builder - if (conn->state != HTTP_CLIENT_CONN_WAIT_LINE) + if (conn->state != HTTP_CLIENT_CONN_WAIT_METHOD) + return; // Request line already written + + // 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); + + conn->state = HTTP_CLIENT_CONN_WAIT_URL; +} + +void http_request_builder_target(HTTP_RequestBuilder builder, + HTTP_String url) +{ + HTTP_ClientConn *conn = request_builder_to_conn(builder); + if (conn == NULL) + return; // Invalid builder + + if (conn->state != HTTP_CLIENT_CONN_WAIT_URL) return; // Request line already written // Allocate a copy of the URL string so the parsed @@ -3989,11 +4007,6 @@ void http_request_builder_url(HTTP_RequestBuilder builder, return; } - // 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); diff --git a/chttp.h b/chttp.h index 0c6e8b4..7e852c0 100644 --- a/chttp.h +++ b/chttp.h @@ -879,7 +879,8 @@ typedef struct { typedef enum { HTTP_CLIENT_CONN_FREE, - HTTP_CLIENT_CONN_WAIT_LINE, + HTTP_CLIENT_CONN_WAIT_METHOD, + HTTP_CLIENT_CONN_WAIT_URL, HTTP_CLIENT_CONN_WAIT_HEADER, HTTP_CLIENT_CONN_WAIT_BODY, HTTP_CLIENT_CONN_FLUSHING, @@ -991,23 +992,32 @@ typedef struct { HTTP_RequestBuilder http_client_get_builder(HTTP_Client *client); // TODO: comment -void http_request_builder_set_user(HTTP_RequestBuilder builder, void *user); +void http_request_builder_set_user(HTTP_RequestBuilder builder, + void *user); // TODO: comment -void http_request_builder_set_trace_bytes(HTTP_RequestBuilder builder, bool trace_bytes); +void http_request_builder_set_trace_bytes(HTTP_RequestBuilder builder, + bool trace_bytes); -// Set the method and URL of the current request. This is the first +// Set the method of the current request. This is the first // function of the request builder that the user must call. -void http_request_builder_url(HTTP_RequestBuilder builder, - HTTP_Method method, HTTP_String url); +void http_request_builder_method(HTTP_RequestBuilder builder, + HTTP_Method method); + +// Set the URL of the current request. This must be set after +// the method and before any header/body +void http_request_builder_target(HTTP_RequestBuilder builder, + HTTP_String url); // After the URL, the user may set zero or more headers. -void http_request_builder_header(HTTP_RequestBuilder builder, HTTP_String str); +void http_request_builder_header(HTTP_RequestBuilder builder, + HTTP_String str); // Append bytes to the request's body. You can call this // any amount of times, as long as it's after having set // the URL. -void http_request_builder_body(HTTP_RequestBuilder builder, HTTP_String str); +void http_request_builder_body(HTTP_RequestBuilder builder, + HTTP_String str); // Mark this request as complete. This invalidates the // builder. diff --git a/examples/simple_client.c b/examples/simple_client.c index 8f125d7..ca3a95c 100644 --- a/examples/simple_client.c +++ b/examples/simple_client.c @@ -16,11 +16,9 @@ int main(void) http_request_builder_set_trace_bytes(builder, true); - http_request_builder_url(builder, - HTTP_METHOD_GET, HTTP_STR("http://coz.is")); - - http_request_builder_header(builder, - HTTP_STR("Greeting: Hello from the cHTTP example!")); + http_request_builder_method(builder, HTTP_METHOD_GET); + http_request_builder_target(builder, HTTP_STR("http://coz.is")); + http_request_builder_header(builder, HTTP_STR("Greeting: Hello from the cHTTP example!")); if (http_request_builder_send(builder) < 0) return -1; diff --git a/src/client.c b/src/client.c index e45d650..d550886 100644 --- a/src/client.c +++ b/src/client.c @@ -93,7 +93,7 @@ HTTP_RequestBuilder http_client_get_builder(HTTP_Client *client) } client->num_conns++; - client->conns[i].state = HTTP_CLIENT_CONN_WAIT_LINE; + client->conns[i].state = HTTP_CLIENT_CONN_WAIT_METHOD; client->conns[i].handle = SOCKET_HANDLE_INVALID; client->conns[i].client = client; client->conns[i].user = NULL; @@ -201,14 +201,32 @@ void http_request_builder_set_trace_bytes(HTTP_RequestBuilder builder, bool trac conn->trace_bytes = trace_bytes; } -void http_request_builder_url(HTTP_RequestBuilder builder, - HTTP_Method method, HTTP_String url) +void http_request_builder_method(HTTP_RequestBuilder builder, + HTTP_Method method) { HTTP_ClientConn *conn = request_builder_to_conn(builder); if (conn == NULL) return; // Invalid builder - if (conn->state != HTTP_CLIENT_CONN_WAIT_LINE) + if (conn->state != HTTP_CLIENT_CONN_WAIT_METHOD) + return; // Request line already written + + // 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); + + conn->state = HTTP_CLIENT_CONN_WAIT_URL; +} + +void http_request_builder_target(HTTP_RequestBuilder builder, + HTTP_String url) +{ + HTTP_ClientConn *conn = request_builder_to_conn(builder); + if (conn == NULL) + return; // Invalid builder + + if (conn->state != HTTP_CLIENT_CONN_WAIT_URL) return; // Request line already written // Allocate a copy of the URL string so the parsed @@ -238,11 +256,6 @@ void http_request_builder_url(HTTP_RequestBuilder builder, return; } - // 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); diff --git a/src/client.h b/src/client.h index e770720..7598954 100644 --- a/src/client.h +++ b/src/client.h @@ -50,7 +50,8 @@ typedef struct { typedef enum { HTTP_CLIENT_CONN_FREE, - HTTP_CLIENT_CONN_WAIT_LINE, + HTTP_CLIENT_CONN_WAIT_METHOD, + HTTP_CLIENT_CONN_WAIT_URL, HTTP_CLIENT_CONN_WAIT_HEADER, HTTP_CLIENT_CONN_WAIT_BODY, HTTP_CLIENT_CONN_FLUSHING, @@ -162,23 +163,32 @@ typedef struct { HTTP_RequestBuilder http_client_get_builder(HTTP_Client *client); // TODO: comment -void http_request_builder_set_user(HTTP_RequestBuilder builder, void *user); +void http_request_builder_set_user(HTTP_RequestBuilder builder, + void *user); // TODO: comment -void http_request_builder_set_trace_bytes(HTTP_RequestBuilder builder, bool trace_bytes); +void http_request_builder_set_trace_bytes(HTTP_RequestBuilder builder, + bool trace_bytes); -// Set the method and URL of the current request. This is the first +// Set the method of the current request. This is the first // function of the request builder that the user must call. -void http_request_builder_url(HTTP_RequestBuilder builder, - HTTP_Method method, HTTP_String url); +void http_request_builder_method(HTTP_RequestBuilder builder, + HTTP_Method method); + +// Set the URL of the current request. This must be set after +// the method and before any header/body +void http_request_builder_target(HTTP_RequestBuilder builder, + HTTP_String url); // After the URL, the user may set zero or more headers. -void http_request_builder_header(HTTP_RequestBuilder builder, HTTP_String str); +void http_request_builder_header(HTTP_RequestBuilder builder, + HTTP_String str); // Append bytes to the request's body. You can call this // any amount of times, as long as it's after having set // the URL. -void http_request_builder_body(HTTP_RequestBuilder builder, HTTP_String str); +void http_request_builder_body(HTTP_RequestBuilder builder, + HTTP_String str); // Mark this request as complete. This invalidates the // builder.