KEEL 1.0.0
Minimal C11 HTTP client/server library built on epoll/kqueue/io_uring/poll
Loading...
Searching...
No Matches
h2_client.h
Go to the documentation of this file.
1
11#ifndef KEEL_H2_CLIENT_H
12#define KEEL_H2_CLIENT_H
13
14#include <stddef.h>
15#include <stdint.h>
16#include <keel/allocator.h>
17#include <keel/event_ctx.h>
18#include <keel/h2.h>
19#include <keel/tls.h>
20
21/* ── Defaults ────────────────────────────────────────────────────── */
22
24#define KL_H2_CLIENT_DEFAULT_TIMEOUT_MS 30000
26#define KL_H2_CLIENT_RECV_BUF_SIZE 16384
27
28/* ── Types ───────────────────────────────────────────────────────── */
29
33
34typedef struct {
35 const char *name;
36 const char *value;
38
49
50/* ── Session callbacks (session -> KEEL) ─────────────────────────── */
51
52typedef struct {
54 int (*on_send)(KlH2ClientSession *s, const void *data, size_t len);
56 void (*on_response)(KlH2ClientSession *s, int32_t stream_id,
57 int status, const KlH2ClientHeader *hdrs, int n);
59 void (*on_data)(KlH2ClientSession *s, int32_t stream_id,
60 const char *data, size_t len);
62 void (*on_stream_close)(KlH2ClientSession *s, int32_t stream_id, int err);
64
65/* ── Session vtable (user provides, wraps nghttp2 etc.) ──────────── */
66
69 int (*recv)(KlH2ClientSession *self, const char *data, size_t len);
72 const char *method, const char *path,
73 const char *authority,
74 const KlH2ClientHeader *hdrs, int n,
75 const char *body, size_t body_len);
77 int (*flush)(KlH2ClientSession *self);
79 void (*destroy)(KlH2ClientSession *self);
83 void *keel_ctx;
84};
85
87typedef KlH2ClientSession *(*KlH2ClientSessionFactory)(KlAllocator *alloc);
88
89/* ── Config ──────────────────────────────────────────────────────── */
90
97
98/* ── Callbacks ───────────────────────────────────────────────────── */
99
101typedef void (*KlH2ClientResponseFn)(KlH2ClientConn *c, int32_t stream_id,
102 const KlH2ClientResponse *resp,
103 void *user_data);
105typedef void (*KlH2ClientErrorFn)(KlH2ClientConn *c, const char *msg,
106 void *user_data);
107
108/* ── Public API ──────────────────────────────────────────────────── */
109
125 const KlH2ClientConfig *cfg,
126 const char *url,
127 KlH2ClientErrorFn on_error,
128 void *user_data);
129
135int32_t kl_h2_client_request(KlH2ClientConn *c, const char *method,
136 const char *path,
137 const KlH2ClientHeader *hdrs, int n,
138 const char *body, size_t body_len,
139 KlH2ClientResponseFn on_resp, void *ud);
140
147
148#endif
Shared HTTP/2 protocol constants.
void(* KlH2ClientErrorFn)(KlH2ClientConn *c, const char *msg, void *user_data)
Connection-level error callback.
Definition h2_client.h:105
int32_t kl_h2_client_request(KlH2ClientConn *c, const char *method, const char *path, const KlH2ClientHeader *hdrs, int n, const char *body, size_t body_len, KlH2ClientResponseFn on_resp, void *ud)
Submit an HTTP/2 request.
void kl_h2_client_response_free(KlH2ClientResponse *resp, KlAllocator *alloc)
Free a response's headers and body (allocator-owned).
KlH2ClientConn * kl_h2_client_connect(KlEventCtx *ev, KlAllocator *alloc, const KlH2ClientConfig *cfg, const char *url, KlH2ClientErrorFn on_error, void *user_data)
Connect to an HTTP/2 server.
void(* KlH2ClientResponseFn)(KlH2ClientConn *c, int32_t stream_id, const KlH2ClientResponse *resp, void *user_data)
Per-stream response completion callback.
Definition h2_client.h:101
void kl_h2_client_free(KlH2ClientConn *c)
Free all HTTP/2 client resources.
struct KlH2ClientStream KlH2ClientStream
Definition h2_client.h:31
KlH2ClientSession *(* KlH2ClientSessionFactory)(KlAllocator *alloc)
Factory for creating client-side HTTP/2 sessions.
Definition h2_client.h:87
struct KlH2ClientConn KlH2ClientConn
Definition h2_client.h:30
void kl_h2_client_close(KlH2ClientConn *c)
Close the HTTP/2 client connection.
Bring-your-own allocator vtable.
Definition allocator.h:12
Composable event loop context.
Definition event_ctx.h:42
Definition h2_client.h:52
Definition h2_client.h:91
int timeout_ms
Definition h2_client.h:92
KlTlsConfig * tls
Definition h2_client.h:94
int max_concurrent_streams
Definition h2_client.h:93
KlH2ClientSessionFactory session
Definition h2_client.h:95
Definition h2_client.h:34
const char * name
Definition h2_client.h:35
const char * value
Definition h2_client.h:36
Definition h2_client.h:40
KlH2ClientHeader * headers
Definition h2_client.h:42
size_t body_len
Definition h2_client.h:46
char * body
Definition h2_client.h:45
int status
Definition h2_client.h:41
int num_headers
Definition h2_client.h:43
size_t body_cap
Definition h2_client.h:47
int headers_cap
Definition h2_client.h:44
Definition h2_client.h:67
int(* recv)(KlH2ClientSession *self, const char *data, size_t len)
Definition h2_client.h:69
void * keel_ctx
Definition h2_client.h:83
int(* flush)(KlH2ClientSession *self)
Definition h2_client.h:77
void(* destroy)(KlH2ClientSession *self)
Definition h2_client.h:79
int32_t(* submit_request)(KlH2ClientSession *self, const char *method, const char *path, const char *authority, const KlH2ClientHeader *hdrs, int n, const char *body, size_t body_len)
Definition h2_client.h:71
KlH2ClientCallbacks keel_cbs
Definition h2_client.h:81
TLS configuration for KlConfig.
Definition tls.h:108