Rename http.c/.h to chttp.c/.h and improve amalgamation formatting

This commit is contained in:
2025-07-20 18:33:51 +02:00
parent 8be1b8e3d6
commit dba15c946e
14 changed files with 87 additions and 28 deletions
+2 -8
View File
@@ -6,16 +6,10 @@ LFLAGS = -lssl -lcrypto
CFILES = $(shell find src -name "*.c") CFILES = $(shell find src -name "*.c")
HFILES = $(shell find src -name "*.h") 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 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: clean:
rm -f client_example server_example rm -f client_example server_example
+36 -6
View File
@@ -30,9 +30,17 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
//////////////////////////////////////////////////////////////////////
// src/cert.h
//////////////////////////////////////////////////////////////////////
int http_create_test_certificate(HTTP_String C, HTTP_String O, HTTP_String CN, int http_create_test_certificate(HTTP_String C, HTTP_String O, HTTP_String CN,
HTTP_String cert_file, HTTP_String key_file); HTTP_String cert_file, HTTP_String key_file);
//////////////////////////////////////////////////////////////////////
// src/socket.h
//////////////////////////////////////////////////////////////////////
// This is a socket abstraction module for non-blocking TCP and TLS sockets. // 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 // 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); void socket_free (Socket *sock);
int socket_wait (Socket **socks, int num_socks); int socket_wait (Socket **socks, int num_socks);
//////////////////////////////////////////////////////////////////////
// src/basic.c
//////////////////////////////////////////////////////////////////////
int http_streq(HTTP_String s1, HTTP_String s2) int http_streq(HTTP_String s1, HTTP_String s2)
{ {
if (s1.len != s2.len) if (s1.len != s2.len)
@@ -246,7 +258,10 @@ void print_bytes(HTTP_String prefix, HTTP_String src)
cur++; cur++;
} }
putc('\n', stream); putc('\n', stream);
} }//////////////////////////////////////////////////////////////////////
// src/parse.c
//////////////////////////////////////////////////////////////////////
// From RFC 9112 // From RFC 9112
// request-target = origin-form // request-target = origin-form
// / absolute-form // / absolute-form
@@ -1276,7 +1291,10 @@ HTTP_String http_getcookie(HTTP_Request *req, HTTP_String name)
{ {
// TODO // TODO
return (HTTP_String) {NULL, 0}; return (HTTP_String) {NULL, 0};
} }//////////////////////////////////////////////////////////////////////
// src/engine.c
//////////////////////////////////////////////////////////////////////
// This is the implementation of a byte queue useful // This is the implementation of a byte queue useful
// for systems that need to process engs of bytes. // 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; eng->state = HTTP_ENGINE_STATE_CLIENT_PREP_URL;
else else
eng->state = HTTP_ENGINE_STATE_SERVER_PREP_STATUS; eng->state = HTTP_ENGINE_STATE_SERVER_PREP_STATUS;
} }//////////////////////////////////////////////////////////////////////
// src/socket.c
//////////////////////////////////////////////////////////////////////
void socket_global_init(void) void socket_global_init(void)
{ {
SSL_library_init(); SSL_library_init();
@@ -3023,7 +3044,10 @@ int socket_wait(Socket **socks, int num_socks)
} }
return -1; return -1;
} }//////////////////////////////////////////////////////////////////////
// src/client.c
//////////////////////////////////////////////////////////////////////
// TODO // TODO
#define ERROR printf("error at %s:%d\n", __FILE__, __LINE__); #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); socket_free(&conn->socket);
conn->state = CLIENT_CONNECTION_FREE; conn->state = CLIENT_CONNECTION_FREE;
client->num_conns--; client->num_conns--;
} }//////////////////////////////////////////////////////////////////////
// src/server.c
//////////////////////////////////////////////////////////////////////
#define MAX_CONNS (1<<10) #define MAX_CONNS (1<<10)
typedef struct { typedef struct {
@@ -3825,7 +3852,10 @@ void http_response_done(HTTP_ResponseHandle res)
http_engine_free(&conn->engine); http_engine_free(&conn->engine);
server->num_conns--; server->num_conns--;
} }
} }//////////////////////////////////////////////////////////////////////
// src/router.c
//////////////////////////////////////////////////////////////////////
#ifndef _WIN32 #ifndef _WIN32
#endif #endif
+29
View File
@@ -13,6 +13,10 @@ extern "C" {
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
//////////////////////////////////////////////////////////////////////
// src/basic.h
//////////////////////////////////////////////////////////////////////
#define HTTP_STR(X) ((HTTP_String) {(X), sizeof(X)-1}) #define HTTP_STR(X) ((HTTP_String) {(X), sizeof(X)-1})
#define HTTP_CEIL(X, Y) (((X) + (Y) - 1) / (Y)) #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_COUNT(X) (sizeof(X) / sizeof((X)[0]))
#define HTTP_ASSERT(X) {if (!(X)) { __builtin_trap(); }} #define HTTP_ASSERT(X) {if (!(X)) { __builtin_trap(); }}
//////////////////////////////////////////////////////////////////////
// src/parse.h
//////////////////////////////////////////////////////////////////////
#define HTTP_MAX_HEADERS 32 #define HTTP_MAX_HEADERS 32
typedef struct { 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_getbodyparam (HTTP_Request *req, HTTP_String name);
HTTP_String http_getcookie (HTTP_Request *req, HTTP_String name); HTTP_String http_getcookie (HTTP_Request *req, HTTP_String name);
//////////////////////////////////////////////////////////////////////
// src/engine.h
//////////////////////////////////////////////////////////////////////
typedef enum { typedef enum {
HTTP_MEMFUNC_MALLOC, HTTP_MEMFUNC_MALLOC,
HTTP_MEMFUNC_FREE, 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_bodyack (HTTP_Engine *eng, int num);
void http_engine_done (HTTP_Engine *eng); void http_engine_done (HTTP_Engine *eng);
void http_engine_undo (HTTP_Engine *eng); void http_engine_undo (HTTP_Engine *eng);
//////////////////////////////////////////////////////////////////////
// src/client.h
//////////////////////////////////////////////////////////////////////
void http_global_init(void); void http_global_init(void);
void http_global_free(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); HTTP_Response* http_request_result (HTTP_RequestHandle handle);
void http_request_free (HTTP_RequestHandle handle); void http_request_free (HTTP_RequestHandle handle);
//////////////////////////////////////////////////////////////////////
// src/server.h
//////////////////////////////////////////////////////////////////////
typedef struct { typedef struct {
void *data0; void *data0;
int data1; 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_undo (HTTP_ResponseHandle res);
void http_response_done (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, int http_create_test_certificate(HTTP_String C, HTTP_String O, HTTP_String CN,
HTTP_String cert_file, HTTP_String key_file); HTTP_String cert_file, HTTP_String key_file);
//////////////////////////////////////////////////////////////////////
// src/router.h
//////////////////////////////////////////////////////////////////////
typedef struct HTTP_Router HTTP_Router; typedef struct HTTP_Router HTTP_Router;
typedef void (*HTTP_RouterFunc)(HTTP_Request*, HTTP_ResponseHandle, void*);; typedef void (*HTTP_RouterFunc)(HTTP_Request*, HTTP_ResponseHandle, void*);;
+9 -4
View File
@@ -26,6 +26,11 @@ class AmalgamationBuilder:
skipped_lines = 0 skipped_lines = 0
first_content_line = True first_content_line = True
self.body += "//////////////////////////////////////////////////////////////////////\n"
self.body += "// " + filepath + "\n"
self.body += "//////////////////////////////////////////////////////////////////////\n"
self.body += "\n"
for line in lines: for line in lines:
# Check if the line is an include # Check if the line is an include
if self._is_include_line(line): if self._is_include_line(line):
@@ -190,8 +195,8 @@ def main():
source = source_builder.result() source = source_builder.result()
# Write results # Write results
print("Writing http.h...") print("Writing chttp.h...")
with open("http.h", 'w', encoding='utf-8') as f: with open("chttp.h", 'w', encoding='utf-8') as f:
f.write('/*\n') f.write('/*\n')
f.write(' * HTTP Library - Amalgamated Header\n') f.write(' * HTTP Library - Amalgamated Header\n')
f.write(' * Generated automatically - do not edit manually\n') f.write(' * Generated automatically - do not edit manually\n')
@@ -207,8 +212,8 @@ def main():
f.write('#endif\n\n') f.write('#endif\n\n')
f.write('#endif /* HTTP_AMALGAMATION_H */\n') f.write('#endif /* HTTP_AMALGAMATION_H */\n')
print("Writing http.c...") print("Writing chttp.c...")
with open("http.c", 'w', encoding='utf-8') as f: with open("chttp.c", 'w', encoding='utf-8') as f:
f.write('/*\n') f.write('/*\n')
f.write(' * HTTP Library - Amalgamated Source\n') f.write(' * HTTP Library - Amalgamated Source\n')
f.write(' * Generated automatically - do not edit manually\n') f.write(' * Generated automatically - do not edit manually\n')
+1
View File
@@ -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_bodyack (HTTP_Engine *eng, int num);
void http_engine_done (HTTP_Engine *eng); void http_engine_done (HTTP_Engine *eng);
void http_engine_undo (HTTP_Engine *eng); void http_engine_undo (HTTP_Engine *eng);
#endif // HTTP_ENGINE_INCLUDED #endif // HTTP_ENGINE_INCLUDED