48#if defined(_WIN32) || defined(_WIN64)
53#define FETCH_INVALID_FILE_HANDLE INVALID_HANDLE_VALUE
59#define FETCH_INVALID_FILE_HANDLE NULL
125#define FETCH_USER_AGENT "libfetch/" FETCH_VERSION_STRING
144 return FETCH_VERSION_STRING;
173 int current = FETCH_VERSION_NUMBER;
174 int compare = (major * 10000) + (minor * 100) + patch;
175 return current - compare;
210#define FETCH_FLAG_GET(flags, bit) (((flags) >> (bit)) & 1U)
212#define FETCH_FLAG_SET(flags, bit) ((flags) | (1U << (bit)))
214#define FETCH_FLAG_CLEAR(flags, bit) ((flags) & ~(1U << (bit)))
216#define FETCH_FLAG_TOGGLE(flags, bit) ((flags) ^ (1U << (bit)))
1106 const char **value);
1176 const char *content_type);
1331 const char *content_type,
bool close_on_free,
1379 const char *name,
const char *value);
1390 const char *name,
const char *value);
1464 const char **key,
const char **value);
1632 const char *reason);
1889 .default_timeout_ms = 30000,
1890 .max_connections = 1000,
1891 .max_connections_per_host = 6,
1892 .keep_alive_timeout_ms = 115000,
1893 .pool_cleanup_interval_ms = 30000,
1894 .max_pooled_connections = 100,
2065#define FETCH_GET(url) fetch(url, NULL)
2068#define FETCH_ASYNC_GET(url) fetch_async(url, NULL)
2071#define FETCH_IS_OK(response) \
2072 ((response) != NULL && fetch_response_ok(response))
2075#define FETCH_PROMISE_IS_DONE(promise) \
2076 ((promise) != NULL && fetch_promise_poll(promise))
2086#define FETCH_TIMEOUT_INFINITE 0
2087#define FETCH_TIMEOUT_DEFAULT 30000
2088#define FETCH_TIMEOUT_SHORT 5000
2089#define FETCH_TIMEOUT_LONG 60000
HTTP Cookie management library for C.
#define FETCH_USER_AGENT
Default user agent string.
Definition fetch.h:125
void fetch_abort_controller_free(fetch_abort_controller_t *controller)
Free an abort controller.
Definition fetch.c:8857
fetch_abort_controller_t * fetch_abort_controller_new(void)
Create a new abort controller.
Definition fetch.c:8833
bool fetch_abort_controller_aborted(const fetch_abort_controller_t *controller)
Check if controller has been aborted.
Definition fetch.c:8852
void fetch_abort_controller_abort(fetch_abort_controller_t *controller, const char *reason)
Abort operations using this controller.
Definition fetch.c:8839
struct fetch_abort_controller fetch_abort_controller_t
Abort controller for cancelling requests.
fetch_body_t * fetch_body_text(const char *text)
Create a text body.
Definition fetch.c:8207
fetch_body_t * fetch_body_form_data(const char *form_data)
Create a form data body.
Definition fetch.c:8274
void fetch_body_free(fetch_body_t *body)
Free a body object.
Definition fetch.c:8295
fetch_body_t * fetch_body_json(const char *json)
Create a JSON body.
Definition fetch.c:8228
fetch_body_t * fetch_body_file(FETCH_FILE_HANDLE file_handle, size_t size, const char *content_type, bool close_on_free, fetch_file_continue_cb continue_cb, void *userdata)
Create a body from a file handle for streaming.
Definition fetch.c:8788
fetch_body_t * fetch_body_binary(const void *data, size_t size, const char *content_type)
Create a binary body.
Definition fetch.c:8249
fetch_body_t * fetch_body_url_search_params(fetch_url_search_params_t *params)
Create a body from URL search parameters.
Definition fetch.c:8766
fetch_body_type_t
Types of request/response bodies.
Definition fetch.h:419
@ FETCH_BODY_BINARY
Definition fetch.h:422
@ FETCH_BODY_JSON
Definition fetch.h:424
@ FETCH_BODY_TEXT
Definition fetch.h:421
@ FETCH_BODY_NONE
Definition fetch.h:420
@ FETCH_BODY_FORM_DATA
Definition fetch.h:423
@ FETCH_BODY_FILE
Definition fetch.h:425
fetch_cache_t
Cache control modes.
Definition fetch.h:287
@ FETCH_CACHE_NO_STORE
Definition fetch.h:289
@ FETCH_CACHE_NO_CACHE
Definition fetch.h:291
@ FETCH_CACHE_RELOAD
Definition fetch.h:290
@ FETCH_CACHE_FORCE_CACHE
Definition fetch.h:292
@ FETCH_CACHE_ONLY_IF_CACHED
Definition fetch.h:293
@ FETCH_CACHE_DEFAULT
Definition fetch.h:288
void fetch_global_dispose(void)
Clean up the fetch library.
Definition fetch.c:7620
void fetch_global_init(const fetch_config_t *config)
Initialize the fetch library with configuration.
Definition fetch.c:7519
static fetch_config_t fetch_config_default(void)
Get default configuration.
Definition fetch.h:1885
struct fetch_config fetch_config_t
Global configuration for the fetch library.
static bool fetch_config_get_flag(uint32_t flags, fetch_flag_bit_t bit)
Get a configuration flag value.
Definition fetch.h:1840
static void fetch_config_set_flag(fetch_config_t *config, fetch_flag_bit_t flag, bool value)
Set or clear a configuration flag.
Definition fetch.h:1850
bool fetch_save_cookies(const char *filename, cookie_jar_t *jar)
Save cookies to a file.
Definition fetch.c:8984
void fetch_disable_cookies(void)
Disable cookie handling.
Definition fetch.c:8951
void fetch_cookie_jar_clear(void)
Clear all cookies from the jar.
Definition fetch.c:8944
cookie_jar_t * fetch_get_cookie_jar(void)
Get the current cookie jar.
Definition fetch.c:8926
cookie_jar_t * fetch_create_cookie_jar(const char *persistent_file)
Create a new cookie jar.
Definition fetch.c:8962
void fetch_cookie_jar_print(cookie_jar_t *jar, const char *domain_filter)
Print cookies to stdout (for debugging)
Definition fetch.c:8998
size_t fetch_cookie_jar_count(const char *domain_filter)
Count cookies in the jar.
Definition fetch.c:8933
bool fetch_load_cookies(const char *filename, cookie_jar_t *jar)
Load cookies from a file.
Definition fetch.c:8991
void fetch_cookie_jar_free(cookie_jar_t *jar)
Free a cookie jar.
Definition fetch.c:8982
fetch_response_t * fetch(const char *url, fetch_init_t *init)
Make a synchronous HTTP request (BLOCKING)
Definition fetch.c:7835
fetch_promise_t * fetch_async(const char *url, fetch_init_t *init)
Make an asynchronous HTTP request (NON-BLOCKING)
Definition fetch.c:7764
fetch_credentials_t
Credential handling modes.
Definition fetch.h:270
@ FETCH_CREDENTIALS_SAME_ORIGIN
Definition fetch.h:272
@ FETCH_CREDENTIALS_OMIT
Definition fetch.h:271
@ FETCH_CREDENTIALS_INCLUDE
Definition fetch.h:273
fetch_error_t
Error codes for fetch operations.
Definition fetch.h:341
@ FETCH_ERROR_INVALID_METHOD
Definition fetch.h:346
@ FETCH_ERROR_MEMORY
Definition fetch.h:348
@ FETCH_ERROR_NETWORK
Definition fetch.h:343
@ FETCH_ERROR_TOO_MANY_REDIRECTS
Definition fetch.h:350
@ FETCH_ERROR_INVALID_URL
Definition fetch.h:345
@ FETCH_ERROR_CONNECTION_REFUSED
Definition fetch.h:351
@ FETCH_ERROR_ABORTED
Definition fetch.h:349
@ FETCH_ERROR_INVALID_HEADERS
Definition fetch.h:347
@ FETCH_ERROR_TIMEOUT
Definition fetch.h:344
@ FETCH_ERROR_NONE
Definition fetch.h:342
@ FETCH_ERROR_PROTOCOL_ERROR
Definition fetch.h:353
@ FETCH_ERROR_DNS_RESOLUTION
Definition fetch.h:352
int fetch_event_loop_process(uint32_t timeout_ms)
Process events in the event loop (NON-BLOCKING with timeout)
Definition fetch.c:7339
void fetch_event_loop_stop(void)
Stop the event loop.
Definition fetch.c:7436
bool fetch_event_loop_is_running(void)
Check if the event loop is running.
Definition fetch.c:7472
bool fetch_event_loop_start(void)
Start the event loop.
Definition fetch.c:7407
#define FETCH_FLAG_SET(flags, bit)
Set a flag in a flags bitfield.
Definition fetch.h:212
fetch_flag_bit_t
Configuration flags for the fetch library.
Definition fetch.h:200
#define FETCH_FLAG_CLEAR(flags, bit)
Clear a flag in a flags bitfield.
Definition fetch.h:214
#define FETCH_FLAG_GET(flags, bit)
Get a flag value from a flags bitfield.
Definition fetch.h:210
@ FETCH_FLAG_DEBUG_LOGGING
Definition fetch.h:206
@ FETCH_FLAG_FOLLOW_REDIRECTS
Definition fetch.h:203
@ FETCH_FLAG_ENABLE_COMPRESSION
Definition fetch.h:204
@ FETCH_FLAG_ACCEPT_INVALID_CERTS
Definition fetch.h:202
@ FETCH_FLAG_ENABLE_COOKIES
Definition fetch.h:205
@ FETCH_FLAG_KEEP_ALIVE_DEFAULT
Definition fetch.h:201
fetch_mode_t
Request modes for CORS handling.
Definition fetch.h:252
@ FETCH_MODE_NAVIGATE
Definition fetch.h:256
@ FETCH_MODE_SAME_ORIGIN
Definition fetch.h:255
@ FETCH_MODE_NO_CORS
Definition fetch.h:254
@ FETCH_MODE_CORS
Definition fetch.h:253
FILE * FETCH_FILE_HANDLE
File handle type for streaming file content.
Definition fetch.h:57
fetch_stream_result_t(* fetch_file_continue_cb)(void *userdata)
Callback function for controlling file streaming continuation.
Definition fetch.h:115
fetch_stream_result_t
Return codes for file streaming continue callback.
Definition fetch.h:73
@ FETCH_STREAM_DONE
Definition fetch.h:75
@ FETCH_STREAM_SKIP
Definition fetch.h:76
@ FETCH_STREAM_READ
Definition fetch.h:74
http_method_t
HTTP request methods.
Definition fetch.h:229
@ HTTP_METHOD_POST
Definition fetch.h:232
@ HTTP_METHOD_HEAD
Definition fetch.h:231
@ HTTP_METHOD_CONNECT
Definition fetch.h:235
@ HTTP_METHOD_DELETE
Definition fetch.h:234
@ HTTP_METHOD_GET
Definition fetch.h:230
@ HTTP_METHOD_TRACE
Definition fetch.h:237
@ HTTP_METHOD_PATCH
Definition fetch.h:238
@ HTTP_METHOD_OPTIONS
Definition fetch.h:236
@ HTTP_METHOD_PUT
Definition fetch.h:233
void fetch_init_free(fetch_init_t *init)
Free a request configuration.
Definition fetch.c:8881
fetch_init_t * fetch_init_method(fetch_init_t *init, http_method_t method)
Set HTTP method (fluent interface)
Definition fetch.c:8891
fetch_init_t * fetch_init_headers(fetch_init_t *init, fetch_headers_t *headers)
Set request headers (fluent interface)
Definition fetch.c:8897
fetch_init_t * fetch_init_timeout(fetch_init_t *init, uint32_t timeout_ms)
Set request timeout (fluent interface)
Definition fetch.c:8913
fetch_init_t * fetch_init_new(void)
Create a new request configuration.
Definition fetch.c:8861
fetch_init_t * fetch_init_body(fetch_init_t *init, fetch_body_t *body)
Set request body (fluent interface)
Definition fetch.c:8905
fetch_init_t * fetch_init_signal(fetch_init_t *init, fetch_abort_controller_t *signal)
Set abort signal (fluent interface)
Definition fetch.c:8919
void fetch_promise_free(fetch_promise_t *promise)
Free a promise object.
Definition fetch.c:8401
fetch_error_t fetch_promise_error(const fetch_promise_t *promise)
Get the error code from a rejected promise.
Definition fetch.c:8008
const char * fetch_promise_error_message(const fetch_promise_t *promise)
Get the error message from a rejected promise.
Definition fetch.c:8012
fetch_promise_state_t fetch_promise_state(const fetch_promise_t *promise)
Get the current state of a promise.
Definition fetch.c:8004
bool fetch_promise_rejected(const fetch_promise_t *promise)
Check if promise was rejected.
Definition fetch.c:8024
fetch_response_t * fetch_promise_response(const fetch_promise_t *promise)
Get the response from a fulfilled promise.
Definition fetch.c:8000
bool fetch_promise_fulfilled(const fetch_promise_t *promise)
Check if promise was fulfilled successfully.
Definition fetch.c:8020
bool fetch_promise_pending(const fetch_promise_t *promise)
Check if promise is still pending.
Definition fetch.c:8016
bool fetch_promise_poll(fetch_promise_t *promise)
Check if a promise has completed (NON-BLOCKING)
Definition fetch.c:7900
bool fetch_promise_cancel(fetch_promise_t *promise, const char *reason)
Cancel a pending promise.
Definition fetch.c:7943
bool fetch_promise_cancelled(const fetch_promise_t *promise)
Check if a promise was cancelled.
Definition fetch.c:7992
bool fetch_promise_await(fetch_promise_t *promise, uint32_t timeout_ms)
Wait for a promise to complete (BLOCKING)
Definition fetch.c:7907
fetch_promise_state_t
Promise states for asynchronous operations.
Definition fetch.h:324
@ FETCH_PROMISE_PENDING
Definition fetch.h:325
@ FETCH_PROMISE_FULFILLED
Definition fetch.h:326
@ FETCH_PROMISE_REJECTED
Definition fetch.h:327
void(* fetch_on_rejected_cb)(fetch_error_t error, const char *message, void *userdata)
Callback for promise rejection/failure.
Definition fetch.h:551
void(* fetch_on_fulfilled_cb)(fetch_response_t *response, void *userdata)
Callback for successful promise completion.
Definition fetch.h:542
struct fetch_promise fetch_promise_t
Promise for asynchronous fetch operations.
fetch_redirect_t
Redirect handling modes.
Definition fetch.h:307
@ FETCH_REDIRECT_FOLLOW
Definition fetch.h:308
@ FETCH_REDIRECT_ERROR
Definition fetch.h:309
@ FETCH_REDIRECT_MANUAL
Definition fetch.h:310
struct fetch_init fetch_init_t
Request configuration options.
void fetch_response_free(fetch_response_t *response)
Free a response object.
Definition fetch.c:8389
fetch_headers_t * fetch_response_headers(const fetch_response_t *response)
Get response headers.
Definition fetch.c:8385
fetch_response_t * fetch_response_clone(const fetch_response_t *response)
Clone a response object.
bool fetch_response_ok(const fetch_response_t *response)
Check if response is successful (2xx status)
Definition fetch.c:8369
const void * fetch_response_array_buffer(fetch_response_t *response, size_t *size)
Get response body as binary data.
Definition fetch.c:8341
const char * fetch_response_json(fetch_response_t *response)
Get response body as JSON text.
Definition fetch.c:8350
const char * fetch_response_text(fetch_response_t *response)
Get response body as text.
Definition fetch.c:8329
const char * fetch_response_status_text(const fetch_response_t *response)
Get HTTP status text.
Definition fetch.c:8377
const char * fetch_response_url(const fetch_response_t *response)
Get final URL (after redirects)
Definition fetch.c:8381
uint16_t fetch_response_status(const fetch_response_t *response)
Get HTTP status code.
Definition fetch.c:8373
struct fetch_response fetch_response_t
HTTP response object.
void fetch_url_search_params_delete(fetch_url_search_params_t *params, const char *name)
Remove all parameters with the given name.
Definition fetch.c:8617
void fetch_url_search_params_append(fetch_url_search_params_t *params, const char *name, const char *value)
Add a parameter (allows duplicates)
Definition fetch.c:8578
bool fetch_url_search_params_has(const fetch_url_search_params_t *params, const char *name)
Check if a parameter exists.
Definition fetch.c:8655
fetch_url_search_params_t * fetch_url_search_params_new(void)
Create a new URL search parameters container.
Definition fetch.c:8505
bool fetch_url_search_params_next(fetch_url_search_params_iterator_t *iter, const char **key, const char **value)
Get the next parameter from an iterator.
Definition fetch.c:8818
void fetch_url_search_params_set(fetch_url_search_params_t *params, const char *name, const char *value)
Set a parameter (replaces existing)
Definition fetch.c:8601
const char * fetch_url_search_params_get(const fetch_url_search_params_t *params, const char *name)
Get the first parameter value with the given name.
Definition fetch.c:8641
fetch_url_search_params_iterator_t fetch_url_search_params_entries(const fetch_url_search_params_t *params)
Create an iterator for URL search parameters.
Definition fetch.c:8812
void fetch_url_search_params_free(fetch_url_search_params_t *params)
Free a URL search parameters container.
Definition fetch.c:8538
char * fetch_url_search_params_to_string(const fetch_url_search_params_t *params)
Convert parameters to URL-encoded string.
Definition fetch.c:8661
struct fetch_url_search_params fetch_url_search_params_t
URL search parameters container.
http_method_t fetch_method_from_string(const char *method_str)
Convert string to HTTP method enum.
Definition fetch.c:9077
const char * fetch_error_to_string(fetch_error_t error)
Convert error code to human-readable string.
Definition fetch.c:9114
const char * fetch_method_to_string(http_method_t method)
Convert HTTP method enum to string.
Definition fetch.c:9052
bool fetch_is_valid_url(const char *url)
Check if a URL is valid.
Definition fetch.c:9103
static int fetch_version_minor(void)
Get minor version number at runtime.
Definition fetch.h:157
static bool fetch_version_at_least(int major, int minor, int patch)
Check if current version is at least the specified version.
Definition fetch.h:185
static const char * fetch_version_string(void)
Get clean semantic version at runtime.
Definition fetch.h:143
static int fetch_version_major(void)
Get major version number at runtime.
Definition fetch.h:151
static int fetch_version_patch(void)
Get patch version number at runtime.
Definition fetch.h:163
static int fetch_version_compare(int major, int minor, int patch)
Compare version against another version.
Definition fetch.h:172
static const char * fetch_version(void)
Get version information at runtime.
Definition fetch.h:137
Cookie jar implementation.
Definition cookie.h:184
Abort controller for cancelling requests.
Definition fetch.h:371
const char * reason
Definition fetch.h:373
void(* on_abort)(void *userdata)
Definition fetch.h:374
bool aborted
Definition fetch.h:372
void * userdata
Definition fetch.h:375
Request or response body.
Definition fetch.h:441
const char * content_type
Definition fetch.h:463
fetch_file_continue_cb continue_cb
Definition fetch.h:458
size_t size
Definition fetch.h:448
void * userdata
Definition fetch.h:459
bool close_on_free
Definition fetch.h:456
FETCH_FILE_HANDLE handle
Definition fetch.h:453
size_t offset
Definition fetch.h:455
const void * data
Definition fetch.h:447
fetch_body_type_t type
Definition fetch.h:442
Global configuration for the fetch library.
Definition fetch.h:1818
const char * origin
Definition fetch.h:1820
uint32_t max_pooled_connections
Definition fetch.h:1829
uint32_t max_connections_per_host
Definition fetch.h:1826
uint32_t flags
Definition fetch.h:1831
const char * user_agent
Definition fetch.h:1819
uint32_t default_timeout_ms
Definition fetch.h:1824
uint32_t pool_cleanup_interval_ms
Definition fetch.h:1828
uint32_t keep_alive_timeout_ms
Definition fetch.h:1827
cookie_jar_t * cookie_jar
Definition fetch.h:1822
uint32_t max_connections
Definition fetch.h:1825
Request configuration options.
Definition fetch.h:482
const char * referrer_policy
Definition fetch.h:491
fetch_mode_t mode
Definition fetch.h:486
const char * referrer
Definition fetch.h:490
uint32_t timeout_ms
Definition fetch.h:496
fetch_credentials_t credentials
Definition fetch.h:487
fetch_headers_t * headers
Definition fetch.h:484
bool keepalive
Definition fetch.h:493
const char * integrity
Definition fetch.h:492
fetch_body_t * body
Definition fetch.h:485
http_method_t method
Definition fetch.h:483
fetch_abort_controller_t * signal
Definition fetch.h:495
uint32_t max_redirects
Definition fetch.h:497
fetch_cache_t cache
Definition fetch.h:488
fetch_redirect_t redirect
Definition fetch.h:489
Promise for asynchronous fetch operations.
Definition fetch.h:563
fetch_promise_state_t state
Definition fetch.h:565
const char * error_message
Definition fetch.h:568
volatile bool detached
Definition fetch.h:575
void * userdata
Definition fetch.h:572
fetch_on_rejected_cb on_rejected
Definition fetch.h:571
uint64_t promise_id
Definition fetch.h:564
void * internal_state
Definition fetch.h:574
fetch_error_t error
Definition fetch.h:567
fetch_response_t * response
Definition fetch.h:566
fetch_on_fulfilled_cb on_fulfilled
Definition fetch.h:570
HTTP response object.
Definition fetch.h:515
fetch_error_t error
Definition fetch.h:525
const char * url
Definition fetch.h:520
void * body
Definition fetch.h:522
const char * status_text
Definition fetch.h:517
const char * error_message
Definition fetch.h:526
fetch_headers_t * headers
Definition fetch.h:521
fetch_body_type_t body_type
Definition fetch.h:524
uint16_t status
Definition fetch.h:516
bool redirected
Definition fetch.h:519
bool ok
Definition fetch.h:518
size_t body_size
Definition fetch.h:523
Iterator for URL search parameters.
Definition fetch.h:604
const fetch_url_search_params_t * params
Definition fetch.h:605
size_t index
Definition fetch.h:606
URL search parameters container.
Definition fetch.h:594
size_t count
Definition fetch.h:597
char ** keys
Definition fetch.h:595
char ** values
Definition fetch.h:596
size_t capacity
Definition fetch.h:598