Separate http_request_builder_url into http_request_builder_method/_target
This commit is contained in:
@@ -3844,7 +3844,7 @@ HTTP_RequestBuilder http_client_get_builder(HTTP_Client *client)
|
|||||||
}
|
}
|
||||||
client->num_conns++;
|
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].handle = SOCKET_HANDLE_INVALID;
|
||||||
client->conns[i].client = client;
|
client->conns[i].client = client;
|
||||||
client->conns[i].user = NULL;
|
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;
|
conn->trace_bytes = trace_bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
void http_request_builder_url(HTTP_RequestBuilder builder,
|
void http_request_builder_method(HTTP_RequestBuilder builder,
|
||||||
HTTP_Method method, HTTP_String url)
|
HTTP_Method method)
|
||||||
{
|
{
|
||||||
HTTP_ClientConn *conn = request_builder_to_conn(builder);
|
HTTP_ClientConn *conn = request_builder_to_conn(builder);
|
||||||
if (conn == NULL)
|
if (conn == NULL)
|
||||||
return; // Invalid builder
|
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
|
return; // Request line already written
|
||||||
|
|
||||||
// Allocate a copy of the URL string so the parsed
|
// Allocate a copy of the URL string so the parsed
|
||||||
@@ -3989,11 +4007,6 @@ void http_request_builder_url(HTTP_RequestBuilder builder,
|
|||||||
return;
|
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
|
// Write path
|
||||||
if (conn->url.path.len == 0)
|
if (conn->url.path.len == 0)
|
||||||
byte_queue_write(&conn->output, "/", 1);
|
byte_queue_write(&conn->output, "/", 1);
|
||||||
|
|||||||
@@ -879,7 +879,8 @@ typedef struct {
|
|||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
HTTP_CLIENT_CONN_FREE,
|
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_HEADER,
|
||||||
HTTP_CLIENT_CONN_WAIT_BODY,
|
HTTP_CLIENT_CONN_WAIT_BODY,
|
||||||
HTTP_CLIENT_CONN_FLUSHING,
|
HTTP_CLIENT_CONN_FLUSHING,
|
||||||
@@ -991,23 +992,32 @@ typedef struct {
|
|||||||
HTTP_RequestBuilder http_client_get_builder(HTTP_Client *client);
|
HTTP_RequestBuilder http_client_get_builder(HTTP_Client *client);
|
||||||
|
|
||||||
// TODO: comment
|
// 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
|
// 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.
|
// function of the request builder that the user must call.
|
||||||
void http_request_builder_url(HTTP_RequestBuilder builder,
|
void http_request_builder_method(HTTP_RequestBuilder builder,
|
||||||
HTTP_Method method, HTTP_String url);
|
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.
|
// 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
|
// Append bytes to the request's body. You can call this
|
||||||
// any amount of times, as long as it's after having set
|
// any amount of times, as long as it's after having set
|
||||||
// the URL.
|
// 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
|
// Mark this request as complete. This invalidates the
|
||||||
// builder.
|
// builder.
|
||||||
|
|||||||
@@ -16,11 +16,9 @@ int main(void)
|
|||||||
|
|
||||||
http_request_builder_set_trace_bytes(builder, true);
|
http_request_builder_set_trace_bytes(builder, true);
|
||||||
|
|
||||||
http_request_builder_url(builder,
|
http_request_builder_method(builder, HTTP_METHOD_GET);
|
||||||
HTTP_METHOD_GET, HTTP_STR("http://coz.is"));
|
http_request_builder_target(builder, HTTP_STR("http://coz.is"));
|
||||||
|
http_request_builder_header(builder, HTTP_STR("Greeting: Hello from the cHTTP example!"));
|
||||||
http_request_builder_header(builder,
|
|
||||||
HTTP_STR("Greeting: Hello from the cHTTP example!"));
|
|
||||||
|
|
||||||
if (http_request_builder_send(builder) < 0)
|
if (http_request_builder_send(builder) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|||||||
+22
-9
@@ -93,7 +93,7 @@ HTTP_RequestBuilder http_client_get_builder(HTTP_Client *client)
|
|||||||
}
|
}
|
||||||
client->num_conns++;
|
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].handle = SOCKET_HANDLE_INVALID;
|
||||||
client->conns[i].client = client;
|
client->conns[i].client = client;
|
||||||
client->conns[i].user = NULL;
|
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;
|
conn->trace_bytes = trace_bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
void http_request_builder_url(HTTP_RequestBuilder builder,
|
void http_request_builder_method(HTTP_RequestBuilder builder,
|
||||||
HTTP_Method method, HTTP_String url)
|
HTTP_Method method)
|
||||||
{
|
{
|
||||||
HTTP_ClientConn *conn = request_builder_to_conn(builder);
|
HTTP_ClientConn *conn = request_builder_to_conn(builder);
|
||||||
if (conn == NULL)
|
if (conn == NULL)
|
||||||
return; // Invalid builder
|
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
|
return; // Request line already written
|
||||||
|
|
||||||
// Allocate a copy of the URL string so the parsed
|
// Allocate a copy of the URL string so the parsed
|
||||||
@@ -238,11 +256,6 @@ void http_request_builder_url(HTTP_RequestBuilder builder,
|
|||||||
return;
|
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
|
// Write path
|
||||||
if (conn->url.path.len == 0)
|
if (conn->url.path.len == 0)
|
||||||
byte_queue_write(&conn->output, "/", 1);
|
byte_queue_write(&conn->output, "/", 1);
|
||||||
|
|||||||
+18
-8
@@ -50,7 +50,8 @@ typedef struct {
|
|||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
HTTP_CLIENT_CONN_FREE,
|
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_HEADER,
|
||||||
HTTP_CLIENT_CONN_WAIT_BODY,
|
HTTP_CLIENT_CONN_WAIT_BODY,
|
||||||
HTTP_CLIENT_CONN_FLUSHING,
|
HTTP_CLIENT_CONN_FLUSHING,
|
||||||
@@ -162,23 +163,32 @@ typedef struct {
|
|||||||
HTTP_RequestBuilder http_client_get_builder(HTTP_Client *client);
|
HTTP_RequestBuilder http_client_get_builder(HTTP_Client *client);
|
||||||
|
|
||||||
// TODO: comment
|
// 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
|
// 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.
|
// function of the request builder that the user must call.
|
||||||
void http_request_builder_url(HTTP_RequestBuilder builder,
|
void http_request_builder_method(HTTP_RequestBuilder builder,
|
||||||
HTTP_Method method, HTTP_String url);
|
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.
|
// 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
|
// Append bytes to the request's body. You can call this
|
||||||
// any amount of times, as long as it's after having set
|
// any amount of times, as long as it's after having set
|
||||||
// the URL.
|
// 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
|
// Mark this request as complete. This invalidates the
|
||||||
// builder.
|
// builder.
|
||||||
|
|||||||
Reference in New Issue
Block a user