6#define _POSIX_C_SOURCE 200809L
51 size_t total = size *
nmemb;
52 response_body_t *body = (response_body_t *)
userp;
56 size_t need = body->size + total;
57 if (
need > body->cap) {
58 size_t new_cap = body->cap ? body->cap * 2 : 4096;
86static size_t header_callback(
char *ptr,
size_t size,
size_t nmemb,
void *
userp) {
87 size_t total = size *
nmemb;
88 response_headers_t *
rh = (response_headers_t *)
userp;
91 if (total > 5 &&
strncasecmp(ptr,
"HTTP/", 5) == 0) {
102 rh->header_count = 0;
107 if (
rh->header_count >= 200)
return 0;
111 if (!
colon)
return total;
116 char *value =
colon + 1;
132 if (!name)
return total;
138 if (!
val)
return total;
143 if (!node)
return total;
168 int follow_redirects;
201 client->follow_redirects = 1;
202 client->timeout_ms = 30000;
203 client->ca_bundle =
NULL;
204 client->altsvc_enabled = 0;
205 client->altsvc_db =
NULL;
245 client->follow_redirects =
follow;
257 client->timeout_ms = timeout_ms;
273 client->ca_bundle = path ?
strdup(path) :
NULL;
289 client->altsvc_enabled = enabled;
305 client->altsvc_db = path ?
strdup(path) :
NULL;
338 const char *body,
size_t body_len,
341 if (!client || !client->curl || !url || !
out_response) {
342 err.error.err_i16 = -1;
348 CURL *curl = client->curl;
359 if (client->ca_bundle) {
362 if (client->altsvc_enabled) {
411 if (body && body_len > 0) {
417 response_body_t resp_body = {0};
437 free(resp_body.data);
439 err.error.err_i16 = -1;
446 free(resp_body.data);
448 err.error.err_i16 = -1;
453 if (resp_body.data && resp_body.size > 0) {
487 free(resp_body.data);
489 err.error.err_i16 = 0;
Memory allocation wrappers backed by libttak.
void cwist_free(void *ptr)
Free memory obtained via cwist_alloc/cwist_strdup/etc.
void * cwist_alloc(size_t size)
Allocate zeroed memory tracked by libttak.
#define CWIST_SCRATCH_DEFER
Attach to a cwist_scratch_t variable's declaration so its allocation (arena- or heap-backed,...
cwist_sstring * cwist_sstring_create(void)
Create a new sstring instance.
cwist_error_t cwist_sstring_assign_len(cwist_sstring *str, const char *data, size_t len)
Assign raw bytes with length.
cwist_error_t cwist_sstring_assign(cwist_sstring *str, const char *data)
Assign a C string.
void cwist_sstring_destroy(cwist_sstring *str)
Destroy an sstring instance.
void cwist_curl_global_acquire(void)
Acquire a reference on the global libcurl instance.
void cwist_curl_global_release(void)
Release a reference on the global libcurl instance.
@ CWIST_ERR_INT16
used when checking common errcodes in Unix/Linux
cwist_error_t make_error(cwist_errtype_t type)
Creates an error object initialized with the specified type.
#define CWIST_HTTP_MAX_BODY_SIZE
@ CWIST_HTTP_UNAUTHORIZED
@ CWIST_HTTP_INTERNAL_ERROR
@ CWIST_HTTP_NOT_IMPLEMENTED
void cwist_http_header_free_all(cwist_http_header_node *head)
Free all headers.
cwist_http_response * cwist_http_response_create(void)
Create a new HTTP response object.
const char * cwist_http_method_to_string(cwist_http_method_t method)
Convert method enum to string.
cwist_http_client * cwist_http_client_create(void)
Create a new HTTP client handle with default settings.
void cwist_http_client_set_altsvc_db(cwist_http_client *client, const char *path)
Set the filesystem path of the alt-svc cache database.
cwist_error_t cwist_http_client_request(cwist_http_client *client, const char *url, cwist_http_method_t method, cwist_http_header_node *headers, const char *body, size_t body_len, cwist_http_response **out_response)
Execute a single HTTP request.
void cwist_http_client_set_ca_bundle(cwist_http_client *client, const char *path)
Set the path to a custom CA certificate bundle for TLS verification.
void cwist_http_client_set_follow_redirects(cwist_http_client *client, int follow)
Enable or disable automatic redirect following.
void cwist_http_client_destroy(cwist_http_client *client)
Destroy a client handle and release all associated resources.
void cwist_http_client_enable_altsvc(cwist_http_client *client, int enabled)
Enable or disable HTTP alternative services (H1/H2/H3 upgrade).
void cwist_http_client_set_timeout_ms(cwist_http_client *client, int timeout_ms)
Set the total request timeout in milliseconds.
Unified HTTP/1.1 and HTTP/2 client using libcurl.
struct cwist_http_client cwist_http_client
Opaque HTTP client handle.
@ CWIST_HTTP_SERVICE_UNAVAILABLE
Integrated error structure used throughout the system.
HTTP Response Object. Supports standard string body or Zero-Copy pointer body.
Backing state for cwist_scratch_alloc()/CWIST_SCRATCH_DEFER: a single block-scoped allocation that pr...
char * data
Access directly only when raw handling is necessary.
Unified error code and type definitions for the CWIST system layer.