|
cwist
Modern C web framework with libttak runtime
|
libcurl-based HTTP/1.1 and HTTP/2 client for CWIST. More...
#include <cwist/net/http/http_client.h>#include <cwist/core/mem/alloc.h>#include <cwist/sys/err/cwist_err.h>#include "curl_global.h"#include <stdio.h>#include <stdlib.h>#include <string.h>#include <strings.h>#include <curl/curl.h>#include <ctype.h>
Go to the source code of this file.
Macros | |
| #define | _POSIX_C_SOURCE 200809L |
Functions | |
| cwist_http_client * | cwist_http_client_create (void) |
| Create a new HTTP client handle with default settings. | |
| void | cwist_http_client_destroy (cwist_http_client *client) |
| Destroy a client handle and release all associated resources. | |
| void | cwist_http_client_set_follow_redirects (cwist_http_client *client, int follow) |
| Enable or disable automatic redirect following. | |
| void | cwist_http_client_set_timeout_ms (cwist_http_client *client, int timeout_ms) |
| Set the total request timeout in milliseconds. | |
| 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_enable_altsvc (cwist_http_client *client, int enabled) |
| Enable or disable HTTP alternative services (H1/H2/H3 upgrade). | |
| 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. | |
libcurl-based HTTP/1.1 and HTTP/2 client for CWIST.
Definition in file http_client.c.
Definition at line 6 of file http_client.c.
| cwist_http_client * cwist_http_client_create | ( | void | ) |
Create a new HTTP client handle with default settings.
Create a new HTTP client.
Acquires the shared curl global state, then initializes an easy handle with defaults: redirect following (max 10 hops), 30 s timeout, TCP keepalive, automatic content decoding, and a CWIST user agent.
Definition at line 185 of file http_client.c.
References cap, cwist_alloc(), cwist_curl_global_acquire(), cwist_curl_global_release(), and cwist_free().
| void cwist_http_client_destroy | ( | cwist_http_client * | client | ) |
Destroy a client handle and release all associated resources.
Destroy an HTTP client and release all associated resources.
Cleans up the curl easy handle, frees the CA bundle and alt-svc paths, frees the handle, and releases the shared curl global reference. Safe to call with NULL.
| client | client handle to destroy, or NULL. |
Definition at line 226 of file http_client.c.
References cap, cwist_curl_global_release(), and cwist_free().
Enable or disable HTTP alternative services (H1/H2/H3 upgrade).
Enable or disable Alt-Svc caching for HTTP/3 upgrade.
When enabled, libcurl may advertise and switch to alternative services for subsequent requests; requires a database path set via cwist_http_client_set_altsvc_db.
| client | client handle. |
| enabled | non-zero to enable alt-svc, 0 to disable. |
Definition at line 287 of file http_client.c.
References cap.
| 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.
Perform a synchronous HTTP request.
The handle is reset and re-configured from the client settings before each call, so a client may be reused for multiple requests. The response body is capped at CWIST_HTTP_MAX_BODY_SIZE and headers at 200 entries; exceeding either cap fails the transfer. HTTP/2 is preferred with downgrade to HTTP/1.1. On success a cwist_http_response is allocated and stored in *out_response (caller must free it with cwist_http_response_free); *out_response is always set to NULL before the transfer starts. The client is not thread-safe; concurrent requests require separate handles.
| client | client handle. |
| url | request URL. |
| method | HTTP method. |
| headers | linked list of request headers, or NULL. |
| body | request body, or NULL for no body. |
| body_len | length of body in bytes. |
| out_response | output for the allocated response object. |
Definition at line 336 of file http_client.c.
References cap, CWIST_ERR_INT16, CWIST_HTTP_BAD_REQUEST, CWIST_HTTP_CREATED, CWIST_HTTP_FORBIDDEN, cwist_http_header_free_all(), CWIST_HTTP_INTERNAL_ERROR, cwist_http_method_to_string(), CWIST_HTTP_NO_CONTENT, CWIST_HTTP_NOT_FOUND, CWIST_HTTP_NOT_IMPLEMENTED, CWIST_HTTP_OK, cwist_http_response_create(), CWIST_HTTP_SERVICE_UNAVAILABLE, CWIST_HTTP_UNAUTHORIZED, cwist_sstring_assign(), cwist_sstring_assign_len(), cwist_sstring::data, cwist_http_header_node::key, make_error(), cwist_http_header_node::next, and cwist_http_header_node::value.
Set the filesystem path of the alt-svc cache database.
Set Alt-Svc cache database file path.
Replaces any previously set path. Pass NULL to clear it.
| client | client handle. |
| path | path to the alt-svc cache file, or NULL. |
Definition at line 302 of file http_client.c.
References cap, and cwist_free().
Set the path to a custom CA certificate bundle for TLS verification.
Set path to CA bundle for TLS verification.
Replaces any previously set path. Pass NULL to fall back to libcurl's default trust store.
| client | client handle. |
| path | filesystem path to the CA bundle, or NULL. |
Definition at line 270 of file http_client.c.
References cap, and cwist_free().
Enable or disable automatic redirect following.
| client | client handle. |
| follow | non-zero to follow redirects, 0 to disable. |
Definition at line 243 of file http_client.c.
References cap.
Set the total request timeout in milliseconds.
Set request timeout in milliseconds.
| client | client handle. |
| timeout_ms | timeout in milliseconds (applied to the whole transfer). |
Definition at line 255 of file http_client.c.
References cap.