Separate http_request_builder_url into http_request_builder_method/_target

This commit is contained in:
2025-11-26 21:11:39 +01:00
parent 5706f12298
commit 0c50cbff33
5 changed files with 83 additions and 39 deletions
+22 -9
View File
@@ -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);
+18 -8
View File
@@ -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.
+3 -5
View File
@@ -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;
+22 -9
View File
@@ -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);
+18 -8
View File
@@ -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.