KEEL 1.0.0
Minimal C11 HTTP client/server library built on epoll/kqueue/io_uring/poll
Loading...
Searching...
No Matches
client.h
Go to the documentation of this file.
1
12#ifndef KEEL_CLIENT_H
13#define KEEL_CLIENT_H
14
15#include <keel/allocator.h>
16#include <keel/decompress.h>
17#include <keel/error.h>
18#include <keel/event_ctx.h>
19#include <keel/parser.h>
20#include <keel/resolver.h>
21#include <keel/tls.h>
22#include <keel/url.h>
23
24/* ── Constants ────────────────────────────────────────────────────── */
25
27#define KL_CLIENT_HOSTNAME_MAX 256
29#define KL_CLIENT_REQ_BUF_SIZE 4096
31#define KL_CLIENT_MAX_REQ_HEADERS 64
33#define KL_CLIENT_DEFAULT_TIMEOUT_MS 30000
35#define KL_CLIENT_DEFAULT_MAX_RESP (4 * 1024 * 1024)
37#define KL_CLIENT_RECV_BUF_SIZE 8192
39#define KL_CLIENT_CHUNK_BUF_SIZE 4096
41#define KL_CLIENT_CHUNK_HDR_SIZE 16
43#define KL_CLIENT_FINAL_CHUNK_LEN 5
45/* ── Proxy ────────────────────────────────────────────────────────── */
46
50typedef struct {
51 const char *host;
52 uint16_t port;
53 const char *auth;
55
56/* ── Types ────────────────────────────────────────────────────────── */
57
58typedef struct {
59 const char *name;
60 const char *value;
62
72
81
82/* ── Streaming types ──────────────────────────────────────────────── */
83
91typedef int (*KlClientBodyFn)(const char *data, size_t len, void *user_data);
92
97typedef int (*KlClientHeadersFn)(int status, const KlClientHeader *headers,
98 int num_headers, void *user_data);
99
107typedef ssize_t (*KlClientReadFn)(char *buf, size_t buf_len, void *user_data);
108
127
128/* ── Sync API ─────────────────────────────────────────────────────── */
129
148 const char *method, const char *url,
149 const KlClientHeader *headers, int num_headers,
150 const char *body, size_t body_len,
151 KlClientResponse *resp);
152
172 const char *method, const char *url,
173 const KlClientHeader *headers, int num_headers,
174 const char *body, size_t body_len,
175 const KlClientStreamCfg *stream,
176 KlClientResponse *resp);
177
182
183/* ── Async API (requires KlEventCtx, NOT KlServer) ───────────────── */
184
185typedef struct KlClient KlClient;
186
193typedef void (*KlClientDoneFn)(KlClient *client, void *user_data);
194
219 const KlClientConfig *cfg,
220 const char *method, const char *url,
221 const KlClientHeader *headers, int num_headers,
222 const char *body, size_t body_len,
223 KlClientDoneFn on_done, void *user_data);
224
246 const KlClientConfig *cfg,
247 const char *method, const char *url,
248 const KlClientHeader *headers, int num_headers,
249 const char *body, size_t body_len,
250 const KlClientStreamCfg *stream,
251 KlClientDoneFn on_done, void *user_data);
252
258
263int kl_client_error(const KlClient *client);
264
270
275
282
283/* ── Streaming parser factory ─────────────────────────────────────── */
284
301 KlAllocator *alloc,
302 KlClientBodyFn on_body,
303 KlClientHeadersFn on_headers,
304 void (*on_complete)(void *),
305 void *stream_user_data);
306
307#endif /* KEEL_CLIENT_H */
void kl_client_response_free(KlClientResponse *resp)
Free response resources (body, headers).
void kl_client_free(KlClient *client)
Free all client resources (response, buffers, parser).
KlError kl_client_last_error(const KlClient *client)
Get the specific error code from a completed async request.
ssize_t(* KlClientReadFn)(char *buf, size_t buf_len, void *user_data)
Request body streaming callback (pull-based, like read()).
Definition client.h:107
void(* KlClientDoneFn)(KlClient *client, void *user_data)
Callback invoked when an async HTTP request completes.
Definition client.h:193
KlClient * kl_client_start(KlEventCtx *ev_ctx, KlAllocator *alloc, const KlClientConfig *cfg, const char *method, const char *url, const KlClientHeader *headers, int num_headers, const char *body, size_t body_len, KlClientDoneFn on_done, void *user_data)
Start an asynchronous HTTP request.
int kl_client_request(KlAllocator *alloc, const KlClientConfig *cfg, const char *method, const char *url, const KlClientHeader *headers, int num_headers, const char *body, size_t body_len, KlClientResponse *resp)
Perform a synchronous (blocking) HTTP request.
int kl_client_error(const KlClient *client)
Check if the async request completed with an error.
KlResponseParser * kl_response_parser_llhttp_s(size_t max_response_size, KlAllocator *alloc, KlClientBodyFn on_body, KlClientHeadersFn on_headers, void(*on_complete)(void *), void *stream_user_data)
Create a streaming-capable llhttp response parser.
void kl_client_cancel(KlClient *client)
Cancel an in-flight async request (removes watcher, closes socket).
int kl_client_request_s(KlAllocator *alloc, const KlClientConfig *cfg, const char *method, const char *url, const KlClientHeader *headers, int num_headers, const char *body, size_t body_len, const KlClientStreamCfg *stream, KlClientResponse *resp)
Perform a synchronous (blocking) HTTP request with streaming.
KlClient * kl_client_start_s(KlEventCtx *ev_ctx, KlAllocator *alloc, const KlClientConfig *cfg, const char *method, const char *url, const KlClientHeader *headers, int num_headers, const char *body, size_t body_len, const KlClientStreamCfg *stream, KlClientDoneFn on_done, void *user_data)
Start an asynchronous HTTP request with streaming.
int(* KlClientBodyFn)(const char *data, size_t len, void *user_data)
Response body streaming callback (push-based).
Definition client.h:91
struct KlClient KlClient
Definition client.h:185
int(* KlClientHeadersFn)(int status, const KlClientHeader *headers, int num_headers, void *user_data)
Called when response headers are complete.
Definition client.h:97
const KlClientResponse * kl_client_response(const KlClient *client)
Get the response from a completed async request.
KlError
Diagnostic error codes for Keel public functions.
Definition error.h:11
Bring-your-own allocator vtable.
Definition allocator.h:12
Definition client.h:73
KlTlsConfig * tls
Definition client.h:76
KlProxyConfig * proxy
Definition client.h:79
KlDecompressConfig * decompress
Definition client.h:78
size_t max_response_size
Definition client.h:75
KlResolver * resolver
Definition client.h:77
int timeout_ms
Definition client.h:74
Definition client.h:58
const char * value
Definition client.h:60
const char * name
Definition client.h:59
Definition client.h:63
KlClientHeader * headers
Definition client.h:67
char * body
Definition client.h:65
KlError error
Definition client.h:70
KlAllocator alloc
Definition client.h:69
int num_headers
Definition client.h:68
size_t body_len
Definition client.h:66
int status
Definition client.h:64
Per-request streaming configuration.
Definition client.h:118
KlClientBodyFn on_body
Definition client.h:119
KlClientHeadersFn on_headers
Definition client.h:120
KlClientReadFn body_read
Definition client.h:123
void * user_data
Definition client.h:125
Decompression configuration.
Definition decompress.h:79
Composable event loop context.
Definition event_ctx.h:42
HTTP proxy configuration (borrowed pointers, caller-owned).
Definition client.h:50
uint16_t port
Definition client.h:52
const char * auth
Definition client.h:53
const char * host
Definition client.h:51
Async DNS resolver vtable.
Definition resolver.h:40
Definition parser.h:42
TLS configuration for KlConfig.
Definition tls.h:108