Line data Source code
1 : /*
2 : MIT License
3 :
4 : Copyright (c) 2026 - A bunch of nerds
5 :
6 : Permission is hereby granted, free of charge, to any person obtaining a copy
7 : of this software and associated documentation files (the "Software"), to deal
8 : in the Software without restriction, including without limitation the rights
9 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 : copies of the Software, and to permit persons to whom the Software is
11 : furnished to do so, subject to the following conditions:
12 :
13 : The above copyright notice and this permission notice shall be included in all
14 : copies or substantial portions of the Software.
15 :
16 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 : SOFTWARE.
23 : */
24 :
25 : #pragma once
26 :
27 : #include "chttp.h"
28 : #include "citerators.h"
29 : #include "clogger.h"
30 : #include "common.h"
31 : #include "cthreadpool.h"
32 :
33 : // Everything from here to the matching pop is the library's public ABI and is
34 : // exported; the library builds with -fvisibility=hidden, so anything outside
35 : // these blocks stays internal.
36 : //
37 : // chttpclient is an HTTP/1.1 client over raw sockets, with ctls for TLS. It has
38 : // three tiers: chttpcli_do blocks the calling thread (Tier 1),
39 : // chttpcli_do_async gives a future and runs on one shared engine (Tier 2), and
40 : // chttpcli_do_pooled blocks on that engine (Tier 3).
41 : //
42 : // Each client limits how many Tier 1 requests run at once (the CPU count by
43 : // default) and keeps idle keep-alive connections per origin, so a request can
44 : // skip DNS, the TCP handshake and the TLS handshake. A pooled connection is
45 : // probed before reuse, and the pool is capped per origin and in total.
46 : //
47 : // Every function that takes a chttpcli is thread-safe, and a destroy may run
48 : // while other threads make requests; it waits for them. Two destroys of the
49 : // same handle are a fatal error. A chttp_request or chttpcli_response has no
50 : // locking of its own, so share one between threads only with your own lock.
51 : // chttp_default_client() gives a process-wide client that chttp_get and the
52 : // other short calls use.
53 : //
54 : // chttpcli_response *resp;
55 : // if (chttp_get("https://api.example.com/users", &resp) == ccol_success) {
56 : // printf("status=%d body=%s\n", resp->status_code,
57 : // resp->body ? resp->body : "");
58 : // chttpcli_resp_free(resp);
59 : // }
60 : #pragma GCC visibility push(default)
61 :
62 : // The handle of an HTTP client. It's a value that packs a slot index and a
63 : // generation, not a pointer, so never cast it to or from void *. Compare it
64 : // with CHTTPCLI_NULL (which is 0). Every call checks the handle against the
65 : // library's slot table first, so a stale handle is caught instead of touching
66 : // freed memory.
67 : typedef uint64_t chttpcli;
68 :
69 : // The "no client" value of a chttpcli; a failed create gives it.
70 : #define CHTTPCLI_NULL ((chttpcli)0)
71 :
72 : // The connect and inactivity timeouts a new client starts with (30 s and 60 s,
73 : // in microseconds), so no request waits for ever on a silent peer.
74 : #define CHTTPCLI_DEFAULT_CONNECT_TIMEOUT_US ((uint64_t)30000000)
75 :
76 : #define CHTTPCLI_DEFAULT_INACTIVITY_TIMEOUT_US ((uint64_t)60000000)
77 :
78 : // The callback of a streaming request, which gets the response body in chunks
79 : // as they arrive (data is not NUL-terminated) instead of a buffer. ctx is what
80 : // you passed with it. Return len to go on; anything less aborts the transfer
81 : // with ccol_http_transfer_aborted. For Tier 2 and 3 no call starts after the
82 : // future is fulfilled, so ctx may be freed once you have the result. Don't
83 : // destroy the client that runs the request from inside it.
84 : typedef size_t (*chttpcli_write_fn)(const void *data, size_t len, void *ctx);
85 :
86 : // An HTTP request. method, expect_continue and
87 : // prevent_tls_downgrade_on_redirect may be set directly. url and body are
88 : // copies the request owns; read them, but build another request rather than
89 : // change them. Headers are set and read through chttp_request_set_header and
90 : // chttp_request_get_header. Create it with chttp_request_create_mp and free it
91 : // with chttp_request_free.
92 : typedef struct chttp_request {
93 : chttp_method method;
94 : char *url;
95 : chttp_request_body body;
96 : chmap_declare(headers, char *, char *);
97 : ccol_memmgmt_procs *ccol__m_procs;
98 : // Sends "Expect: 100-continue" and holds the body back for up to 1 s, until
99 : // the server answers. A final response that comes first means the body is
100 : // never sent. It applies only to a hop that carries a body.
101 : bool expect_continue;
102 : // Refuses a redirect from https to http with ccol_http_invalid_url. It's off
103 : // by default, as in curl and Go, which means the peer can move the rest of
104 : // the chain to clear text; set it wherever that's not acceptable. Credentials
105 : // you set are dropped on a cross-origin hop either way, and an AF_UNIX target
106 : // is never reachable through a redirect.
107 : bool prevent_tls_downgrade_on_redirect;
108 : } chttp_request;
109 :
110 : // An HTTP response. status_code, body, body_len and headers are public. body is
111 : // a NUL-terminated buffer, or NULL on the streaming path and for an empty body
112 : // (204, HEAD, Content-Length: 0), so check it before you use it. Read headers
113 : // through chttpcli_resp_header and its siblings, and free the response with
114 : // chttpcli_resp_free; it carries its own allocator copy, so that works after
115 : // the client is destroyed.
116 : typedef struct chttpcli_response {
117 : int status_code;
118 : char *body;
119 : size_t body_len;
120 : chmap_declare(headers, char *, char *);
121 : ccol_memmgmt_procs *ccol__m_procs;
122 : void *ccol__field_lines;
123 : } chttpcli_response;
124 :
125 : // Request lifecycle.
126 :
127 : // Creates an HTTP request whose memory comes from mprocs (NULL for malloc and
128 : // free). method is one of CHTTP_GET to CHTTP_OPTIONS (CHTTP_ANY and any other
129 : // value are refused). url, body->data and body->content_type are copied, so you
130 : // may free yours at once; body may be NULL or &CHTTP_NO_BODY, and body->data
131 : // may be NULL only when body->len is 0. Gives the request, which you free with
132 : // chttp_request_free(), or NULL with a static string in *err_str (err_str may
133 : // be NULL) for a NULL url, a bad method or body, or a failed allocation.
134 : chttp_request *chttp_request_create_mp(chttp_method method, const char *url,
135 : const chttp_request_body *body,
136 : ccol_memmgmt_procs *mprocs,
137 : char **err_str);
138 :
139 : // Creates an HTTP request with malloc. method is one of CHTTP_GET to
140 : // CHTTP_OPTIONS (CHTTP_ANY and any other value are refused). url, body->data
141 : // and body->content_type are copied, so you may free yours at once; body may be
142 : // NULL or &CHTTP_NO_BODY, and body->data may be NULL only when body->len is 0.
143 : // Gives the request, which you free with chttp_request_free(), or NULL with a
144 : // static string in *err_str (err_str may be NULL) for a NULL url, a bad method
145 : // or body, or a failed allocation.
146 : static inline __attribute__((always_inline)) chttp_request *
147 220 : chttp_request_create(chttp_method method, const char *url,
148 : const chttp_request_body *body, char **err_str) {
149 1385 : return chttp_request_create_mp(method, url, body, NULL, err_str);
150 : }
151 :
152 : // Sets a request header, replacing an earlier value of the same name, so you
153 : // can add authentication, content negotiation and the like. Names are stored in
154 : // lower case. The name must be a token, and the value may hold no control byte
155 : // but a tab (CR and LF would split the request). Transfer-Encoding is refused,
156 : // since a request body always goes out whole with Content-Length. Gives
157 : // ccol_success, ccol_invalid_args for a NULL argument or a refused name or
158 : // value, or ccol_not_enough_memory.
159 : ccol_retval chttp_request_set_header(chttp_request *req, const char *name,
160 : const char *value);
161 :
162 : // Looks up a request header by name, case ignored, so you can check what a
163 : // request will send. Gives the value, valid until the next
164 : // chttp_request_set_header() on req or until req is freed, or NULL when req or
165 : // name is NULL or the header isn't set.
166 : const char *chttp_request_get_header(const chttp_request *req,
167 : const char *name);
168 :
169 : // Frees a request from chttp_request_create() or chttp_request_create_mp() and
170 : // everything it owns (URL, body copy, headers). NULL is safe.
171 : void chttp_request_free(chttp_request *req);
172 :
173 : // Client constructors.
174 :
175 : // Creates an HTTP client that allocates through mprocs (NULL for malloc and
176 : // free). It starts with the pool size of the CPU count, a 30 s connect timeout,
177 : // a 60 s inactivity timeout, no request timeout, no body cap and full TLS
178 : // verification against the system CA bundle; change them with the
179 : // chttpcli_set_* calls before the first request. Gives the handle, which you
180 : // destroy with chttpcli_destroy(), or CHTTPCLI_NULL with a static string in
181 : // *err_str (err_str may be NULL).
182 : chttpcli chttpcli_create_mp(ccol_memmgmt_procs *mprocs, char **err_str);
183 :
184 : // Creates an HTTP client that allocates with malloc. It starts with the pool
185 : // size of the CPU count, a 30 s connect timeout, a 60 s inactivity timeout, no
186 : // request timeout, no body cap and full TLS verification against the system CA
187 : // bundle; change them with the chttpcli_set_* calls before the first request.
188 : // Gives the handle, which you destroy with chttpcli_destroy(), or CHTTPCLI_NULL
189 : // with a static string in *err_str (err_str may be NULL).
190 : static inline __attribute__((always_inline)) chttpcli
191 29 : chttpcli_create(char **err_str) {
192 2738 : return chttpcli_create_mp(NULL, err_str);
193 : }
194 :
195 : // Client configuration.
196 :
197 : // Sets how many Tier 1 requests of cli may run at once (0 gives the CPU count),
198 : // so a client can bound its own load. A Tier 1 call beyond it blocks for a free
199 : // slot, within its request timeout. Tier 2 and Tier 3 aren't limited by it.
200 : // Gives ccol_success, or ccol_invalid_args for a CHTTPCLI_NULL or stale handle.
201 : ccol_retval chttpcli_set_pool_size(chttpcli cli, size_t n);
202 :
203 : // Sets the connect timeout of cli in microseconds (0 for none; a new client has
204 : // 30 s). It covers the name lookup, the TCP connect and, for https, the TLS
205 : // handshake, as one budget. A lookup can't be cancelled: Tier 1 gives
206 : // ccol_timed_out once it returns, Tiers 2 and 3 at the deadline itself. Applies
207 : // to requests that start later. Gives ccol_success, or ccol_invalid_args for a
208 : // CHTTPCLI_NULL or stale handle.
209 : ccol_retval chttpcli_set_connect_timeout(chttpcli cli, uint64_t us);
210 :
211 : // Sets the total timeout of cli's requests in microseconds (0 for none, the
212 : // default), from the call until the last body byte. Every tier checks it before
213 : // each read, so a response not fully read by the deadline ends in
214 : // ccol_timed_out even when the rest is buffered. Time waiting for a pool slot
215 : // counts. Gives ccol_success, or ccol_invalid_args for a CHTTPCLI_NULL or stale
216 : // handle.
217 : ccol_retval chttpcli_set_request_timeout(chttpcli cli, uint64_t us);
218 :
219 : // Sets the inactivity timeout of cli in microseconds (0 for none; a new client
220 : // has 60 s): A request ends with ccol_timed_out once it has waited this long on
221 : // its connection with no byte moving either way. Every byte restarts it, and
222 : // time spent in a streaming write_fn doesn't count. A server that takes longer
223 : // to start its response (a long poll) needs a larger value or 0. Gives
224 : // ccol_success, or ccol_invalid_args for a CHTTPCLI_NULL or stale handle.
225 : ccol_retval chttpcli_set_inactivity_timeout(chttpcli cli, uint64_t us);
226 :
227 : // Caps a buffered response body of cli at max_bytes (0 for none), so a bad
228 : // server can't make you hold an unbounded body. A larger declared
229 : // Content-Length fails before the body is read, and any other body fails as it
230 : // crosses the cap, with ccol_msg_too_large. Streaming requests aren't affected;
231 : // their write_fn bounds their memory. Gives ccol_success, or ccol_invalid_args
232 : // for a CHTTPCLI_NULL or stale handle.
233 : ccol_retval chttpcli_set_max_response_body_size(chttpcli cli, size_t max_bytes);
234 :
235 : // Sets cli's TLS configuration (see chttp_tls_config in chttp.h); NULL, or a
236 : // zeroed struct, means full verification against the system CA bundle and no
237 : // client certificate. The system bundle is read once per process. A file that
238 : // doesn't exist yet isn't an error here: Requests that need TLS then fail with
239 : // ccol_http_tls_cert_load_failed. Pooled connections made under the old
240 : // settings aren't reused. Gives ccol_success; ccol_invalid_args for a bad
241 : // handle, a certificate without its key (or a key without one), or
242 : // insecure_skip_verify beside a CA bundle, as two conflicting policies; or
243 : // ccol_not_enough_memory, which puts the defaults back.
244 : ccol_retval chttpcli_set_tls(chttpcli cli, const chttp_tls_config *tls);
245 :
246 : // Engine settings. They apply to the shared engine of Tier 2 and Tier 3.
247 :
248 : // Installs a logger for the diagnostics of the shared Tier 2 and Tier 3 engine
249 : // (failed handshakes, connect errors), so you choose where they go. The engine
250 : // derives its own handle from cl, and the setting holds across engine restarts.
251 : // CLOG_NULL releases it, which is ccol_not_permitted while the engine runs or
252 : // stops (wait with chttpcli_engine_wait() first). Gives ccol_success,
253 : // ccol_invalid_args for a closed or stale cl (the engine keeps its logger), or
254 : // ccol_not_enough_memory.
255 : ccol_retval chttpcli_set_engine_logger(clog cl);
256 :
257 : // Sends the allocations of the shared Tier 2 and Tier 3 engine (its reactor,
258 : // dispatch state and lookup workers) to mp, or back to the default for NULL. It
259 : // applies to the next engine start. mp must stay valid until
260 : // chttpcli_engine_wait() returns after your last client. Gives ccol_success,
261 : // ccol_invalid_args for an mp with a NULL function pointer, or
262 : // ccol_not_permitted while the engine runs.
263 : ccol_retval chttpcli_set_engine_mem_mgmt_procs(ccol_memmgmt_procs *mp);
264 :
265 : // Sets how many threads the shared Tier 2 and Tier 3 engine's reactor uses (0
266 : // for the CPU count; 1 polls and dispatches on one thread), so you can size it.
267 : // It applies to the next engine start. Gives ccol_success, or
268 : // ccol_not_permitted while the engine runs.
269 : ccol_retval chttpcli_set_engine_num_reactor_threads(size_t num_threads);
270 :
271 : // Blocks until the shared Tier 2 and Tier 3 engine has stopped completely and
272 : // its threads are joined, so the engine allocator and logger can go away
273 : // safely. The engine stops on its own once no client holds it. It returns at
274 : // once when no engine runs, and calling it from an engine thread (a write_fn)
275 : // is a fatal error.
276 : void chttpcli_engine_wait(void);
277 :
278 : // Client destruction.
279 :
280 : // Destroys cli; use the chttpcli_destroy macro, which also clears the handle.
281 : // It waits for every request in flight on cli and closes its idle connections.
282 : // A handle that isn't live (stale, being destroyed elsewhere, garbage) is a
283 : // fatal error, and so is a destroy from inside one of cli's own requests or
284 : // from any engine thread, since it would wait for itself. Don't destroy the
285 : // default client.
286 : void chttpcli__destroy(chttpcli cli);
287 :
288 : // The cleanup function of the scoped macros, for ccol__destructor. A NULL
289 : // handle does nothing.
290 : static inline __attribute__((always_inline)) void chttpcli__destroy_cleanup(
291 : chttpcli *pp) {
292 21 : if (pp && *pp) {
293 21 : chttpcli__destroy(*pp);
294 21 : *pp = CHTTPCLI_NULL;
295 : }
296 21 : }
297 :
298 : // Destroys the client cli and sets the variable to CHTTPCLI_NULL. It waits for
299 : // every request in flight on cli and closes its idle connections. A handle
300 : // that isn't live (stale, being destroyed elsewhere, garbage) is a fatal
301 : // error, and so is a destroy from inside one of cli's own requests or from any
302 : // engine thread (a write_fn), since it would wait for itself. Never destroy
303 : // the default client. cli is evaluated once and must be a modifiable lvalue.
304 : #define chttpcli_destroy(cli) \
305 : ccol__chttpcli_destroy_impl( \
306 : cli, ccol__uniq(ccol__chttpcli_destroy_slot, __COUNTER__))
307 :
308 : // The body of chttpcli_destroy. slot is a ccol__uniq() name, so the macro nests
309 : // inside another destroy macro's argument without -Wshadow warnings.
310 : #define ccol__chttpcli_destroy_impl(cli, slot) \
311 : do { \
312 : __typeof__(cli) *slot = &(cli); \
313 : chttpcli__destroy(*slot); \
314 : *slot = CHTTPCLI_NULL; \
315 : } while (0)
316 :
317 : // Lifecycle macros.
318 :
319 : // Declares a chttpcli variable called name with no value; give it one with
320 : // chttpcli_create() and destroy it with chttpcli_destroy().
321 : #define chttpcli_declare(name) chttpcli name
322 :
323 : // Declares a chttpcli variable called name, set to CHTTPCLI_NULL, that is
324 : // destroyed when it goes out of scope if it then holds a client. Give it a
325 : // value with chttpcli_create(). The scope-end destroy waits for the client's
326 : // requests in flight, and a stale handle there is a fatal error.
327 : #define chttpcli_declare_scoped(name) \
328 : chttpcli name ccol__destructor(chttpcli__destroy_cleanup) = CHTTPCLI_NULL;
329 :
330 : // Declares a chttpcli variable called name and creates a client in it with
331 : // the default allocator and settings, for code that has no use for a failed
332 : // create: A failed create is a fatal error that names the variable and the
333 : // reason. Destroy it with chttpcli_destroy().
334 : #define chttpcli_construct(name) \
335 : chttpcli name = CHTTPCLI_NULL; \
336 : do { \
337 : char *chttpcli__err = NULL; \
338 : (name) = chttpcli_create(&chttpcli__err); \
339 : if (!(name)) { \
340 : ccol__fatal_err("chttpcli_construct('%s'): %s", #name, \
341 : chttpcli__err ? chttpcli__err : "unknown error"); \
342 : } \
343 : } while (0)
344 :
345 : // Declares a chttpcli variable called name and creates a client in it with
346 : // the default allocator and settings; the client is destroyed when the
347 : // variable goes out of scope, after its requests in flight end. A failed
348 : // create is a fatal error that names the variable and the reason.
349 : #define chttpcli_construct_scoped(name) \
350 : chttpcli name ccol__destructor(chttpcli__destroy_cleanup) = CHTTPCLI_NULL; \
351 : do { \
352 : char *chttpcli__err = NULL; \
353 : (name) = chttpcli_create(&chttpcli__err); \
354 : if (!(name)) { \
355 : ccol__fatal_err("chttpcli_construct_scoped('%s'): %s", #name, \
356 : chttpcli__err ? chttpcli__err : "unknown error"); \
357 : } \
358 : } while (0)
359 :
360 : // Request execution.
361 :
362 : // Runs req on cli synchronously and buffers the whole response, for the plain
363 : // blocking call. It waits for a pool slot, then follows up to 50 redirects
364 : // (301, 302 and 303 turn into a bodiless GET unless HEAD; 307 and 308 keep the
365 : // method and body), resolving every Location by RFC 3986. A redirect can't
366 : // reach an AF_UNIX socket other than the one the chain started on, and drops
367 : // your credential headers on a hop to another origin. The URL may be http://,
368 : // https:// or http+unix://, with optional userinfo that becomes a Basic
369 : // Authorization header. A reused connection that died before answering is
370 : // retried once when the request is safe to resend. *resp_out is NULL until a
371 : // success, and the response is yours to free with chttpcli_resp_free(). Gives
372 : // ccol_success, or a failure code such as ccol_invalid_args (a NULL argument,
373 : // a bad or stale cli), ccol_timed_out, ccol_http_invalid_url,
374 : // ccol_http_connection_failed, a TLS code, ccol_http_transfer_aborted or
375 : // ccol_msg_too_large (see chttpcli_do(3) for the full list).
376 : ccol_retval chttpcli_do(chttpcli cli, const chttp_request *req,
377 : chttpcli_response **resp_out);
378 :
379 : // Runs req on cli synchronously and passes the response body to write_fn in
380 : // chunks, with write_ctx, so a large body needs no buffer; it follows
381 : // redirects and applies cli's timeouts like a buffered request.
382 : // *status_code_out (may be NULL) gets the status after a success. Gives
383 : // ccol_success, or a failure code such as ccol_invalid_args, ccol_timed_out,
384 : // ccol_http_invalid_url, ccol_http_connection_failed or a TLS code, and
385 : // ccol_http_transfer_aborted when write_fn returns anything but len; never
386 : // ccol_msg_too_large (see chttpcli_do(3)).
387 : ccol_retval chttpcli_do_streaming(chttpcli cli, const chttp_request *req,
388 : chttpcli_write_fn write_fn, void *write_ctx,
389 : int *status_code_out);
390 :
391 : // Async API (Tier 2).
392 :
393 : // What an async request's future gives: rv as chttpcli_do would give it, and
394 : // resp when rv is ccol_success. Free resp with chttpcli_resp_free, then this
395 : // with chttpcli_async_result_free.
396 : typedef struct chttpcli_async_result {
397 : ccol_retval rv;
398 : chttpcli_response *resp;
399 : ccol_memmgmt_procs *ccol__m_procs;
400 : } chttpcli_async_result;
401 :
402 : // Submits req to the shared async engine and returns at once, so a thread can
403 : // run many requests without blocking. The engine starts on first use, and the
404 : // whole request, redirects included, runs on its threads. req may be freed
405 : // right after the call. Gives a future, or NULL when the request can't be
406 : // queued (a NULL req, a bad or stale cli, a malformed URL, unusable TLS, no
407 : // memory, or an engine that fails to start). Get the result with
408 : // chttpcli_async_result_get(), free its resp with chttpcli_resp_free() and the
409 : // result with chttpcli_async_result_free(), then free the future once with
410 : // cthrpool_future_free().
411 : cthrpool_future *chttpcli_do_async(chttpcli cli, const chttp_request *req);
412 :
413 : // Submits req to the shared async engine and returns at once; the response
414 : // body goes to write_fn in chunks, with write_ctx. write_fn runs on an engine
415 : // thread, so it must not block and must not wait on another request of the
416 : // engine. Gives a future, or NULL when the request can't be queued (a NULL req
417 : // or write_fn, a bad or stale cli, a malformed URL, unusable TLS, no memory, or
418 : // an engine that fails to start). Get the result with
419 : // chttpcli_async_result_get(); its rv is ccol_http_transfer_aborted when
420 : // write_fn returns anything but len. Free the result with
421 : // chttpcli_async_result_free() and the future once with cthrpool_future_free().
422 : cthrpool_future *chttpcli_do_async_streaming(chttpcli cli,
423 : const chttp_request *req,
424 : chttpcli_write_fn write_fn,
425 : void *write_ctx);
426 :
427 : // Blocks until the future f of an async request is fulfilled and gives its
428 : // result: rv holds what chttpcli_do() would give, and resp is set only when rv
429 : // is ccol_success (free it with chttpcli_resp_free()). Calling it again gives
430 : // the same pointer. Gives NULL for a NULL or cancelled future. Freeing the
431 : // future doesn't free the result; free it once with
432 : // chttpcli_async_result_free().
433 : chttpcli_async_result *chttpcli_async_result_get(cthrpool_future *f);
434 :
435 : // Frees a result from chttpcli_async_result_get(). It frees neither
436 : // result->resp (use chttpcli_resp_free()) nor the future (use
437 : // cthrpool_future_free()). It works after the client is destroyed. NULL is
438 : // safe.
439 : void chttpcli_async_result_free(chttpcli_async_result *result);
440 :
441 : // Pooled sync API (Tier 3).
442 :
443 : // Runs req on cli through the shared async engine and blocks until it ends, so
444 : // many callers share a few engine threads instead of one blocked thread each.
445 : // Redirects, timeouts and retries work as for a Tier 1 request. *resp_out is
446 : // NULL until a success, and the response is yours to free with
447 : // chttpcli_resp_free(). Gives the codes of chttpcli_do(), such as
448 : // ccol_invalid_args, ccol_timed_out or ccol_http_connection_failed, and
449 : // ccol_not_enough_memory or ccol_unexpected_failure when the engine can't
450 : // start.
451 : ccol_retval chttpcli_do_pooled(chttpcli cli, const chttp_request *req,
452 : chttpcli_response **resp_out);
453 :
454 : // Runs req on cli through the shared async engine and blocks until it ends,
455 : // with the response body passed to write_fn in chunks, with write_ctx, on an
456 : // engine thread (it must not block). *status_code_out (may be NULL) gets the
457 : // status. Gives the codes of chttpcli_do(), such as ccol_invalid_args or
458 : // ccol_timed_out, and ccol_http_transfer_aborted when write_fn returns
459 : // anything but len; never ccol_msg_too_large.
460 : ccol_retval chttpcli_do_pooled_streaming(chttpcli cli, const chttp_request *req,
461 : chttpcli_write_fn write_fn,
462 : void *write_ctx, int *status_code_out);
463 :
464 : // Default client and convenience API.
465 :
466 : // Gives the process's default client, created on first use and thread-safe, for
467 : // callers that need no client of their own. Configure it with the
468 : // chttpcli_set_* calls, but never destroy it: The library does at exit, and
469 : // afterwards this gives CHTTPCLI_NULL. It also gives CHTTPCLI_NULL when the
470 : // first creation fails.
471 : chttpcli chttp_default_client(void);
472 :
473 : // Runs req synchronously with the process's default client and buffers the
474 : // whole response, following redirects. *resp_out is NULL until a success, and
475 : // the response is yours to free with chttpcli_resp_free(). Gives the codes of
476 : // chttpcli_do(), such as ccol_invalid_args, ccol_timed_out,
477 : // ccol_http_invalid_url or ccol_http_connection_failed.
478 : ccol_retval chttp_do_default(const chttp_request *req,
479 : chttpcli_response **resp_out);
480 :
481 : // Builds a request from method, url, body and headers, runs it synchronously
482 : // with the default client and frees it, for a one-call request. headers (may
483 : // be NULL) is borrowed, never consumed. *resp_out is NULL until a success, and
484 : // the response is yours to free with chttpcli_resp_free(). Gives the codes of
485 : // chttpcli_do(); a NULL url or resp_out, a bad method, a body with NULL data
486 : // and a non-zero len, or a refused header is ccol_invalid_args, and a failed
487 : // allocation is ccol_not_enough_memory.
488 : ccol_retval chttp_perform(chttp_method method, const char *url,
489 : const chttp_request_body *body, chmap headers,
490 : chttpcli_response **resp_out);
491 :
492 : // Runs a GET of url with the default client, the shortest way to fetch a URL.
493 : // *resp_out is NULL until a success, and the response is yours to free with
494 : // chttpcli_resp_free(). Gives ccol_success or a failure code of chttpcli_do(),
495 : // such as ccol_http_invalid_url, ccol_timed_out or
496 : // ccol_http_connection_failed.
497 : ccol_retval chttp_get(const char *url, chttpcli_response **resp_out);
498 :
499 : // Runs a POST of body (may be NULL for none) to url with the default client.
500 : // *resp_out is NULL until a success, and the response is yours to free with
501 : // chttpcli_resp_free(). Gives ccol_success or a failure code of chttpcli_do(),
502 : // such as ccol_http_invalid_url or ccol_timed_out; a body with NULL data and a
503 : // non-zero len is ccol_invalid_args.
504 : ccol_retval chttp_post(const char *url, const chttp_request_body *body,
505 : chttpcli_response **resp_out);
506 :
507 : // Runs a PUT of body (may be NULL for none) to url with the default client.
508 : // *resp_out is NULL until a success, and the response is yours to free with
509 : // chttpcli_resp_free(). Gives ccol_success or a failure code of chttpcli_do(),
510 : // such as ccol_http_invalid_url or ccol_timed_out; a body with NULL data and a
511 : // non-zero len is ccol_invalid_args.
512 : ccol_retval chttp_put(const char *url, const chttp_request_body *body,
513 : chttpcli_response **resp_out);
514 :
515 : // Runs a DELETE of url, with no body, using the default client. *resp_out is
516 : // NULL until a success, and the response is yours to free with
517 : // chttpcli_resp_free(). Gives ccol_success or a failure code of chttpcli_do(),
518 : // such as ccol_http_invalid_url, ccol_timed_out or
519 : // ccol_http_connection_failed.
520 : ccol_retval chttp_delete(const char *url, chttpcli_response **resp_out);
521 :
522 : // Runs a PATCH of body (may be NULL for none) to url with the default client.
523 : // *resp_out is NULL until a success, and the response is yours to free with
524 : // chttpcli_resp_free(). Gives ccol_success or a failure code of chttpcli_do(),
525 : // such as ccol_http_invalid_url or ccol_timed_out; a body with NULL data and a
526 : // non-zero len is ccol_invalid_args.
527 : ccol_retval chttp_patch(const char *url, const chttp_request_body *body,
528 : chttpcli_response **resp_out);
529 :
530 : // Response API.
531 :
532 : // Looks up a response header by name, case ignored, for the common case of one
533 : // value per name. A repeated name gives every occurrence joined by ", ", except
534 : // Set-Cookie, which gives its first. Gives the value, valid until
535 : // chttpcli_resp_free(), or NULL when the header is missing.
536 : const char *chttpcli_resp_header(const chttpcli_response *resp,
537 : const char *name);
538 :
539 : // Counts the field lines named name (case ignored) in resp, so you can read a
540 : // repeated header one by one with chttpcli_resp_header_at(). It's 0 for a
541 : // missing name, a NULL argument or a streaming response.
542 : size_t chttpcli_resp_header_count(const chttpcli_response *resp,
543 : const char *name);
544 :
545 : // Gives occurrence index (from 0, in wire order) of the header name (case
546 : // ignored) in resp, exactly as the server sent it and valid until
547 : // chttpcli_resp_free(), or NULL when index is out of range, an argument is NULL
548 : // or the response came from a streaming request.
549 : const char *chttpcli_resp_header_at(const chttpcli_response *resp,
550 : const char *name, size_t index);
551 :
552 : // Frees a response from a client call and everything it owns (body, headers).
553 : // NULL is safe. It uses the allocator copy the response carries, so it works
554 : // after the client is destroyed.
555 : void chttpcli_resp_free(chttpcli_response *resp);
556 :
557 : #pragma GCC visibility pop
|