Add a client example program and move examples to the examples/ folder

This commit is contained in:
2025-11-22 00:13:43 +01:00
parent eec1174047
commit 4ca5b67d8e
8 changed files with 80 additions and 17 deletions
+4 -2
View File
@@ -1,2 +1,4 @@
example simple_client
example.exe simple_client.exe
simple_server
simple_server.exe
+3 -3
View File
@@ -1,12 +1,12 @@
.PHONY: all clean example .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 chttp.c chttp.h: $(wildcard src/*.c src/*.h) misc/amalg.py Makefile
python misc/amalg.py python misc/amalg.py
example: main.c chttp.c chttp.h %: examples/%.c chttp.c chttp.h
gcc main.c chttp.c -o example -ggdb gcc $< chttp.c -o $@ -ggdb
clean: clean:
rm chttp.c chttp.h rm chttp.c chttp.h
+6 -5
View File
@@ -2372,7 +2372,8 @@ static int socket_manager_register_events_nolock(
continue; continue;
j++; 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) { if (s->state == SOCKET_STATE_DIED || s->state == SOCKET_STATE_ESTABLISHED_READY) {
reg->num_polled = 0; reg->num_polled = 0;
return 0; return 0;
@@ -3814,12 +3815,12 @@ bool http_client_next_response(HTTP_Client *client,
return true; 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; return;
HTTP_ClientConn *conn = (HTTP_ClientConn*) res->context; HTTP_ClientConn *conn = (HTTP_ClientConn*) response->context;
// Free the connection resources // Free the connection resources
http_client_conn_free(conn); http_client_conn_free(conn);
@@ -3827,7 +3828,7 @@ void http_free_response(HTTP_Response *res)
conn->client->num_conns--; conn->client->num_conns--;
// Mark response as freed // Mark response as freed
res->context = NULL; response->context = NULL;
} }
//////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////
+1 -1
View File
@@ -910,7 +910,7 @@ bool http_client_next_response(HTTP_Client *client,
// Free a response object. You can't access its fields // Free a response object. You can't access its fields
// again after this. // again after this.
void http_free_response(HTTP_Response *res); void http_free_response(HTTP_Response *response);
//////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////
// src/server.h // src/server.h
+60
View File
@@ -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, &reg) < 0)
return -1;
if (reg.num_polled > 0)
POLL(reg.polled, reg.num_polled, -1);
if (http_client_process_events(&client, &reg) < 0)
return -1;
HTTP_Response *response;
while (http_client_next_response(&client, &response)) {
// TODO
http_free_response(response);
}
}
http_client_free(&client);
return 0;
}
+1 -1
View File
@@ -1,4 +1,4 @@
#include "chttp.h" #include "../chttp.h"
#ifdef _WIN32 #ifdef _WIN32
#define POLL WSAPoll #define POLL WSAPoll
+4 -4
View File
@@ -422,12 +422,12 @@ bool http_client_next_response(HTTP_Client *client,
return true; 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; return;
HTTP_ClientConn *conn = (HTTP_ClientConn*) res->context; HTTP_ClientConn *conn = (HTTP_ClientConn*) response->context;
// Free the connection resources // Free the connection resources
http_client_conn_free(conn); http_client_conn_free(conn);
@@ -435,5 +435,5 @@ void http_free_response(HTTP_Response *res)
conn->client->num_conns--; conn->client->num_conns--;
// Mark response as freed // Mark response as freed
res->context = NULL; response->context = NULL;
} }
+1 -1
View File
@@ -151,4 +151,4 @@ bool http_client_next_response(HTTP_Client *client,
// Free a response object. You can't access its fields // Free a response object. You can't access its fields
// again after this. // again after this.
void http_free_response(HTTP_Response *res); void http_free_response(HTTP_Response *response);