Implement HTTP client in consistent style with server.c

Refactored HTTP client implementation to follow the same coding
patterns and style as server.c:

- Added static helper functions (http_client_conn_init,
  http_client_conn_free, check_response_buffer, request_builder_to_conn)
- Implemented buffer limit setters (http_client_set_input_limit,
  http_client_set_output_limit)
- Fully implemented request builder API (url, header, body, send)
- Implemented event processing with state machine approach
- Added connection reuse support via http_free_response
- Used consistent naming, formatting, and error handling patterns
- Renamed builder_to_conn to request_builder_to_conn to avoid
  symbol conflicts with server.c during amalgamation

The implementation follows the same architecture as the server:
- Connection state machine for managing request/response lifecycle
- ByteQueue for input/output buffering
- Ready queue for completed responses
- Generation counters for builder validation
- Socket manager integration for async I/O
This commit is contained in:
Claude
2025-11-21 19:17:18 +00:00
parent 2d407b63c2
commit 9868ae2e33
4 changed files with 842 additions and 113 deletions
+34 -4
View File
@@ -16,18 +16,43 @@ typedef enum {
} HTTP_ClientConnState;
// Fields of this struct are private
typedef struct HTTP_Client HTTP_Client;
typedef struct {
HTTP_ClientConnState state;
// Handle to the socket
SocketHandle handle;
// Pointer back to the client
HTTP_Client *client;
// Generation counter for request builder validation
uint16_t gen;
// Data received from the server
ByteQueue input;
// Data being sent to the server
ByteQueue output;
// Parsed URL for connection establishment
HTTP_URL url;
// Parsed response once complete
HTTP_Response response;
} HTTP_ClientConn;
// Fields of this struct are private
typedef struct {
struct HTTP_Client {
// Size limit of the input and output buffer of each
// connection.
uint32_t input_buffer_limit;
uint32_t output_buffer_limit;
// Array of connections. The counter contains the
// number of structs such that state=FREE.
// number of structs such that state!=FREE.
int num_conns;
HTTP_ClientConn conns[HTTP_CLIENT_CAPACITY];
@@ -40,14 +65,14 @@ typedef struct {
// Asynchronous TCP and TLS socket abstraction
SocketManager sockets;
// The server object doesn't interact with this
// The client object doesn't interact with this
// field directly, it just initializes the socket
// manager with a pointer to it. This allows
// allocating the exact number of sockets we
// will need.
Socket socket_pool[HTTP_CLIENT_CAPACITY];
} HTTP_Client;
};
// Initialize an HTTP client object. This allows one to
// perform a number of requests in parallel.
@@ -56,6 +81,11 @@ int http_client_init(HTTP_Client *client);
// Release resources associated to a client object.
void http_client_free(HTTP_Client *client);
// Set input and output buffer size limit for any
// given connection. The default value is 1MB
void http_client_set_input_limit(HTTP_Client *client, uint32_t limit);
void http_client_set_output_limit(HTTP_Client *client, uint32_t limit);
// When a thread is blocked waiting for client events,
// other threads can call this function to wake it up.
int http_client_wakeup(HTTP_Client *client);