From dba15c946e868d4dcb3fc24747d079b7b1cdc2c9 Mon Sep 17 00:00:00 2001 From: Francesco Cozzuto Date: Sun, 20 Jul 2025 18:33:51 +0200 Subject: [PATCH] Rename http.c/.h to chttp.c/.h and improve amalgamation formatting --- Makefile | 10 ++-------- http.c => chttp.c | 42 ++++++++++++++++++++++++++++++++++++------ http.h => chttp.h | 29 +++++++++++++++++++++++++++++ misc/amalg.py | 13 +++++++++---- src/basic.c | 2 +- src/basic.h | 2 +- src/client.c | 2 +- src/client.h | 2 +- src/engine.c | 2 +- src/engine.h | 3 ++- src/parse.c | 2 +- src/parse.h | 2 +- src/server.c | 2 +- src/socket.c | 2 +- 14 files changed, 87 insertions(+), 28 deletions(-) rename http.c => chttp.c (98%) rename http.h => chttp.h (90%) diff --git a/Makefile b/Makefile index bdcd5d8..ab3ce4d 100644 --- a/Makefile +++ b/Makefile @@ -6,16 +6,10 @@ LFLAGS = -lssl -lcrypto CFILES = $(shell find src -name "*.c") HFILES = $(shell find src -name "*.h") -all: client_example server_example http.c http.h +all: chttp.c chttp.h -http.c http.h: $(HFILES) $(CFILES) +chttp.c chttp.h: $(HFILES) $(CFILES) python misc/amalg.py -client_example: examples/client_example.c http.c http.h - $(CC) examples/client_example.c http.c $(CFLAGS) -o $@ $(LFLAGS) - -server_example: examples/server_example.c http.c http.h - $(CC) examples/server_example.c http.c $(CFLAGS) -o $@ $(LFLAGS) - clean: rm -f client_example server_example diff --git a/http.c b/chttp.c similarity index 98% rename from http.c rename to chttp.c index 9b3f54f..64ffe12 100644 --- a/http.c +++ b/chttp.c @@ -30,9 +30,17 @@ #include #include +////////////////////////////////////////////////////////////////////// +// src/cert.h +////////////////////////////////////////////////////////////////////// + int http_create_test_certificate(HTTP_String C, HTTP_String O, HTTP_String CN, HTTP_String cert_file, HTTP_String key_file); +////////////////////////////////////////////////////////////////////// +// src/socket.h +////////////////////////////////////////////////////////////////////// + // This is a socket abstraction module for non-blocking TCP and TLS sockets. // // Sockets may be in a number of states based on if they are plain TCP or TLS @@ -156,6 +164,10 @@ void socket_close (Socket *sock); void socket_free (Socket *sock); int socket_wait (Socket **socks, int num_socks); +////////////////////////////////////////////////////////////////////// +// src/basic.c +////////////////////////////////////////////////////////////////////// + int http_streq(HTTP_String s1, HTTP_String s2) { if (s1.len != s2.len) @@ -246,7 +258,10 @@ void print_bytes(HTTP_String prefix, HTTP_String src) cur++; } putc('\n', stream); -} +}////////////////////////////////////////////////////////////////////// +// src/parse.c +////////////////////////////////////////////////////////////////////// + // From RFC 9112 // request-target = origin-form // / absolute-form @@ -1276,7 +1291,10 @@ HTTP_String http_getcookie(HTTP_Request *req, HTTP_String name) { // TODO return (HTTP_String) {NULL, 0}; -} +}////////////////////////////////////////////////////////////////////// +// src/engine.c +////////////////////////////////////////////////////////////////////// + // This is the implementation of a byte queue useful // for systems that need to process engs of bytes. // @@ -2260,7 +2278,10 @@ void http_engine_undo(HTTP_Engine *eng) eng->state = HTTP_ENGINE_STATE_CLIENT_PREP_URL; else eng->state = HTTP_ENGINE_STATE_SERVER_PREP_STATUS; -} +}////////////////////////////////////////////////////////////////////// +// src/socket.c +////////////////////////////////////////////////////////////////////// + void socket_global_init(void) { SSL_library_init(); @@ -3023,7 +3044,10 @@ int socket_wait(Socket **socks, int num_socks) } return -1; -} +}////////////////////////////////////////////////////////////////////// +// src/client.c +////////////////////////////////////////////////////////////////////// + // TODO #define ERROR printf("error at %s:%d\n", __FILE__, __LINE__); @@ -3387,7 +3411,10 @@ void http_request_free(HTTP_RequestHandle handle) socket_free(&conn->socket); conn->state = CLIENT_CONNECTION_FREE; client->num_conns--; -} +}////////////////////////////////////////////////////////////////////// +// src/server.c +////////////////////////////////////////////////////////////////////// + #define MAX_CONNS (1<<10) typedef struct { @@ -3825,7 +3852,10 @@ void http_response_done(HTTP_ResponseHandle res) http_engine_free(&conn->engine); server->num_conns--; } -} +}////////////////////////////////////////////////////////////////////// +// src/router.c +////////////////////////////////////////////////////////////////////// + #ifndef _WIN32 #endif diff --git a/http.h b/chttp.h similarity index 90% rename from http.h rename to chttp.h index 4bf69d7..2917539 100644 --- a/http.h +++ b/chttp.h @@ -13,6 +13,10 @@ extern "C" { #include #include +////////////////////////////////////////////////////////////////////// +// src/basic.h +////////////////////////////////////////////////////////////////////// + #define HTTP_STR(X) ((HTTP_String) {(X), sizeof(X)-1}) #define HTTP_CEIL(X, Y) (((X) + (Y) - 1) / (Y)) @@ -28,6 +32,10 @@ HTTP_String http_trim (HTTP_String s); #define HTTP_COUNT(X) (sizeof(X) / sizeof((X)[0])) #define HTTP_ASSERT(X) {if (!(X)) { __builtin_trap(); }} +////////////////////////////////////////////////////////////////////// +// src/parse.h +////////////////////////////////////////////////////////////////////// + #define HTTP_MAX_HEADERS 32 typedef struct { @@ -116,6 +124,10 @@ HTTP_String http_getqueryparam (HTTP_Request *req, HTTP_String name); HTTP_String http_getbodyparam (HTTP_Request *req, HTTP_String name); HTTP_String http_getcookie (HTTP_Request *req, HTTP_String name); +////////////////////////////////////////////////////////////////////// +// src/engine.h +////////////////////////////////////////////////////////////////////// + typedef enum { HTTP_MEMFUNC_MALLOC, HTTP_MEMFUNC_FREE, @@ -229,6 +241,11 @@ char* http_engine_bodybuf (HTTP_Engine *eng, int *cap); void http_engine_bodyack (HTTP_Engine *eng, int num); void http_engine_done (HTTP_Engine *eng); void http_engine_undo (HTTP_Engine *eng); + +////////////////////////////////////////////////////////////////////// +// src/client.h +////////////////////////////////////////////////////////////////////// + void http_global_init(void); void http_global_free(void); @@ -252,6 +269,10 @@ void http_request_submit (HTTP_RequestHandle handle); HTTP_Response* http_request_result (HTTP_RequestHandle handle); void http_request_free (HTTP_RequestHandle handle); +////////////////////////////////////////////////////////////////////// +// src/server.h +////////////////////////////////////////////////////////////////////// + typedef struct { void *data0; int data1; @@ -277,9 +298,17 @@ void http_response_bodyack (HTTP_ResponseHandle res, int num); void http_response_undo (HTTP_ResponseHandle res); void http_response_done (HTTP_ResponseHandle res); +////////////////////////////////////////////////////////////////////// +// src/cert.h +////////////////////////////////////////////////////////////////////// + int http_create_test_certificate(HTTP_String C, HTTP_String O, HTTP_String CN, HTTP_String cert_file, HTTP_String key_file); +////////////////////////////////////////////////////////////////////// +// src/router.h +////////////////////////////////////////////////////////////////////// + typedef struct HTTP_Router HTTP_Router; typedef void (*HTTP_RouterFunc)(HTTP_Request*, HTTP_ResponseHandle, void*);; diff --git a/misc/amalg.py b/misc/amalg.py index 73dfc45..96610fe 100644 --- a/misc/amalg.py +++ b/misc/amalg.py @@ -25,6 +25,11 @@ class AmalgamationBuilder: original_line_number = 1 skipped_lines = 0 first_content_line = True + + self.body += "//////////////////////////////////////////////////////////////////////\n" + self.body += "// " + filepath + "\n" + self.body += "//////////////////////////////////////////////////////////////////////\n" + self.body += "\n" for line in lines: # Check if the line is an include @@ -190,8 +195,8 @@ def main(): source = source_builder.result() # Write results - print("Writing http.h...") - with open("http.h", 'w', encoding='utf-8') as f: + print("Writing chttp.h...") + with open("chttp.h", 'w', encoding='utf-8') as f: f.write('/*\n') f.write(' * HTTP Library - Amalgamated Header\n') f.write(' * Generated automatically - do not edit manually\n') @@ -207,8 +212,8 @@ def main(): f.write('#endif\n\n') f.write('#endif /* HTTP_AMALGAMATION_H */\n') - print("Writing http.c...") - with open("http.c", 'w', encoding='utf-8') as f: + print("Writing chttp.c...") + with open("chttp.c", 'w', encoding='utf-8') as f: f.write('/*\n') f.write(' * HTTP Library - Amalgamated Source\n') f.write(' * Generated automatically - do not edit manually\n') diff --git a/src/basic.c b/src/basic.c index 2355a9d..d91cd7b 100644 --- a/src/basic.c +++ b/src/basic.c @@ -93,4 +93,4 @@ void print_bytes(HTTP_String prefix, HTTP_String src) cur++; } putc('\n', stream); -} +} \ No newline at end of file diff --git a/src/basic.h b/src/basic.h index 7feda81..460fa3f 100644 --- a/src/basic.h +++ b/src/basic.h @@ -15,4 +15,4 @@ HTTP_String http_trim (HTTP_String s); #define HTTP_COUNT(X) (sizeof(X) / sizeof((X)[0])) #define HTTP_ASSERT(X) {if (!(X)) { __builtin_trap(); }} -#endif // BASIC_INCLUDED +#endif // BASIC_INCLUDED \ No newline at end of file diff --git a/src/client.c b/src/client.c index 4e5b58d..3e90bd5 100644 --- a/src/client.c +++ b/src/client.c @@ -370,4 +370,4 @@ void http_request_free(HTTP_RequestHandle handle) socket_free(&conn->socket); conn->state = CLIENT_CONNECTION_FREE; client->num_conns--; -} +} \ No newline at end of file diff --git a/src/client.h b/src/client.h index 7129ebc..ad9cbb2 100644 --- a/src/client.h +++ b/src/client.h @@ -27,4 +27,4 @@ void http_request_submit (HTTP_RequestHandle handle); HTTP_Response* http_request_result (HTTP_RequestHandle handle); void http_request_free (HTTP_RequestHandle handle); -#endif // CLIENT_INCLUDED +#endif // CLIENT_INCLUDED \ No newline at end of file diff --git a/src/engine.c b/src/engine.c index 02114bc..7745f95 100644 --- a/src/engine.c +++ b/src/engine.c @@ -995,4 +995,4 @@ void http_engine_undo(HTTP_Engine *eng) eng->state = HTTP_ENGINE_STATE_CLIENT_PREP_URL; else eng->state = HTTP_ENGINE_STATE_SERVER_PREP_STATUS; -} +} \ No newline at end of file diff --git a/src/engine.h b/src/engine.h index 2e1c875..43472bc 100644 --- a/src/engine.h +++ b/src/engine.h @@ -115,4 +115,5 @@ char* http_engine_bodybuf (HTTP_Engine *eng, int *cap); void http_engine_bodyack (HTTP_Engine *eng, int num); void http_engine_done (HTTP_Engine *eng); void http_engine_undo (HTTP_Engine *eng); -#endif // HTTP_ENGINE_INCLUDED + +#endif // HTTP_ENGINE_INCLUDED \ No newline at end of file diff --git a/src/parse.c b/src/parse.c index d41001d..39208e4 100644 --- a/src/parse.c +++ b/src/parse.c @@ -1034,4 +1034,4 @@ HTTP_String http_getcookie(HTTP_Request *req, HTTP_String name) { // TODO return (HTTP_String) {NULL, 0}; -} +} \ No newline at end of file diff --git a/src/parse.h b/src/parse.h index 9b88ff1..21c511f 100644 --- a/src/parse.h +++ b/src/parse.h @@ -90,4 +90,4 @@ HTTP_String http_getqueryparam (HTTP_Request *req, HTTP_String name); HTTP_String http_getbodyparam (HTTP_Request *req, HTTP_String name); HTTP_String http_getcookie (HTTP_Request *req, HTTP_String name); -#endif // PARSE_INCLUDED +#endif // PARSE_INCLUDED \ No newline at end of file diff --git a/src/server.c b/src/server.c index 37c275e..96d22f1 100644 --- a/src/server.c +++ b/src/server.c @@ -444,4 +444,4 @@ void http_response_done(HTTP_ResponseHandle res) http_engine_free(&conn->engine); server->num_conns--; } -} +} \ No newline at end of file diff --git a/src/socket.c b/src/socket.c index 61e9837..e881b80 100644 --- a/src/socket.c +++ b/src/socket.c @@ -781,4 +781,4 @@ int socket_wait(Socket **socks, int num_socks) } return -1; -} +} \ No newline at end of file