From 4ca5b67d8eda9edc32685bc130be5d95cfd8066a Mon Sep 17 00:00:00 2001 From: Francesco Cozzuto Date: Sat, 22 Nov 2025 00:11:47 +0100 Subject: [PATCH] Add a client example program and move examples to the examples/ folder --- .gitignore | 6 ++- Makefile | 6 +-- chttp.c | 11 +++--- chttp.h | 2 +- examples/simple_client.c | 60 ++++++++++++++++++++++++++++++ main.c => examples/simple_server.c | 2 +- src/client.c | 8 ++-- src/client.h | 2 +- 8 files changed, 80 insertions(+), 17 deletions(-) create mode 100644 examples/simple_client.c rename main.c => examples/simple_server.c (98%) diff --git a/.gitignore b/.gitignore index fb80c20..77f1b92 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ -example -example.exe +simple_client +simple_client.exe +simple_server +simple_server.exe diff --git a/Makefile b/Makefile index 9955ee5..8271ab1 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,12 @@ .PHONY: all clean example -all: chttp.c chttp.h example +all: chttp.c chttp.h simple_client simple_server chttp.c chttp.h: $(wildcard src/*.c src/*.h) misc/amalg.py Makefile python misc/amalg.py -example: main.c chttp.c chttp.h - gcc main.c chttp.c -o example -ggdb +%: examples/%.c chttp.c chttp.h + gcc $< chttp.c -o $@ -ggdb clean: rm chttp.c chttp.h diff --git a/chttp.c b/chttp.c index 3cdee62..f9ef146 100644 --- a/chttp.c +++ b/chttp.c @@ -2372,7 +2372,8 @@ static int socket_manager_register_events_nolock( continue; j++; - // TODO: comment + // If at least one socket can be processed, return an + // empty list. if (s->state == SOCKET_STATE_DIED || s->state == SOCKET_STATE_ESTABLISHED_READY) { reg->num_polled = 0; return 0; @@ -3814,12 +3815,12 @@ bool http_client_next_response(HTTP_Client *client, return true; } -void http_free_response(HTTP_Response *res) +void http_free_response(HTTP_Response *response) { - if (res == NULL || res->context == NULL) + if (response == NULL || response->context == NULL) return; - HTTP_ClientConn *conn = (HTTP_ClientConn*) res->context; + HTTP_ClientConn *conn = (HTTP_ClientConn*) response->context; // Free the connection resources http_client_conn_free(conn); @@ -3827,7 +3828,7 @@ void http_free_response(HTTP_Response *res) conn->client->num_conns--; // Mark response as freed - res->context = NULL; + response->context = NULL; } //////////////////////////////////////////////////////////////////////////////////////// diff --git a/chttp.h b/chttp.h index 5792972..6c5a168 100644 --- a/chttp.h +++ b/chttp.h @@ -910,7 +910,7 @@ bool http_client_next_response(HTTP_Client *client, // Free a response object. You can't access its fields // again after this. -void http_free_response(HTTP_Response *res); +void http_free_response(HTTP_Response *response); //////////////////////////////////////////////////////////////////////////////////////// // src/server.h diff --git a/examples/simple_client.c b/examples/simple_client.c new file mode 100644 index 0000000..d58b012 --- /dev/null +++ b/examples/simple_client.c @@ -0,0 +1,60 @@ +#include "../chttp.h" + +#ifdef _WIN32 +#define POLL WSAPoll +#else +#define POLL poll +#endif + +int main(void) +{ + HTTP_Client client; + if (http_client_init(&client) < 0) + return -1; + + HTTP_RequestBuilder builder; + if (http_client_get_builder(&client, NULL, &builder) < 0) + return -1; + + 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!")); + + if (http_request_builder_send(builder) < 0) + return -1; + + for (;;) { + + void *ptrs[HTTP_CLIENT_CAPACITY+1]; + struct pollfd polled[HTTP_CLIENT_CAPACITY+1]; + + EventRegister reg = { + .ptrs=ptrs, + .polled=polled, + .num_polled=0, + .max_polled=HTTP_CLIENT_CAPACITY+1, + }; + + if (http_client_register_events(&client, ®) < 0) + return -1; + + if (reg.num_polled > 0) + POLL(reg.polled, reg.num_polled, -1); + + if (http_client_process_events(&client, ®) < 0) + return -1; + + HTTP_Response *response; + while (http_client_next_response(&client, &response)) { + // TODO + http_free_response(response); + } + } + + http_client_free(&client); + return 0; +} diff --git a/main.c b/examples/simple_server.c similarity index 98% rename from main.c rename to examples/simple_server.c index 9864620..3ce3d34 100644 --- a/main.c +++ b/examples/simple_server.c @@ -1,4 +1,4 @@ -#include "chttp.h" +#include "../chttp.h" #ifdef _WIN32 #define POLL WSAPoll diff --git a/src/client.c b/src/client.c index 144a793..8d8876b 100644 --- a/src/client.c +++ b/src/client.c @@ -422,12 +422,12 @@ bool http_client_next_response(HTTP_Client *client, return true; } -void http_free_response(HTTP_Response *res) +void http_free_response(HTTP_Response *response) { - if (res == NULL || res->context == NULL) + if (response == NULL || response->context == NULL) return; - HTTP_ClientConn *conn = (HTTP_ClientConn*) res->context; + HTTP_ClientConn *conn = (HTTP_ClientConn*) response->context; // Free the connection resources http_client_conn_free(conn); @@ -435,5 +435,5 @@ void http_free_response(HTTP_Response *res) conn->client->num_conns--; // Mark response as freed - res->context = NULL; + response->context = NULL; } diff --git a/src/client.h b/src/client.h index 31c5d98..21007d3 100644 --- a/src/client.h +++ b/src/client.h @@ -151,4 +151,4 @@ bool http_client_next_response(HTTP_Client *client, // Free a response object. You can't access its fields // again after this. -void http_free_response(HTTP_Response *res); +void http_free_response(HTTP_Response *response);