libfetch 0.0.0
A lightweight asynchronous HTTP/1.1 client library implementing a subset of the WHATWG Fetch API.
Loading...
Searching...
No Matches
fetch.c File Reference
#include "fetch.h"
#include "ada_c.h"
#include "cookie.h"
#include "dns.h"
#include "picohttpparser.h"
#include "sets.h"
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdatomic.h>

Data Structures

struct  tls_context
 
struct  fetch_url
 
struct  fetch_request
 
struct  pooled_connection
 
struct  connection_pool
 
struct  connection_pool_manager_t
 
struct  fetch_connection
 
struct  fetch_timer
 
struct  fetch_event_loop
 
struct  http_parse_context
 

Macros

#define FETCH_SOCKET   int
 
#define FETCH_INVALID_SOCKET   (-1)
 
#define fetch_close_socket   close
 
#define fetch_socket_error   errno
 
#define FETCH_EVENT_INVALID   -1
 
#define FETCH_WAIT_INFINITE   -1
 
#define fetch_atomic_load(ptr)
 
#define fetch_atomic_store(ptr, val)
 
#define fetch_atomic_cas(ptr, expected, desired)
 
#define fetch_atomic_inc(ptr)
 
#define fetch_atomic_dec(ptr)
 
#define FETCH_MAX_URL_LENGTH   8192
 
#define FETCH_MAX_HOSTNAME_LENGTH   253
 
#define FETCH_MAX_HEADER_VALUE_LENGTH   8192
 
#define FETCH_MAX_HEADER_NAME_LENGTH   256
 
#define FETCH_MAX_METHOD_LENGTH   16
 
#define FETCH_MAX_PATH_LENGTH   4096
 
#define FETCH_MAX_BODY_SIZE   (50 * 1024 * 1024)
 
#define FETCH_MAX_STRING_LENGTH   4096
 
#define FETCH_MAX_FORM_PARAM_LENGTH   1024
 
#define FETCH_MAX_CONTENT_TYPE_LENGTH   256
 
#define SAFE_APPEND(str)
 

Typedefs

typedef int fetch_event_handle_t
 
typedef struct tls_context tls_context_t
 
typedef enum host_type_t fetch_host_type_t
 
typedef struct fetch_url fetch_url_t
 
typedef struct fetch_request fetch_request_t
 
typedef struct pooled_connection pooled_connection_t
 
typedef struct connection_pool connection_pool_t
 
typedef struct fetch_connection fetch_connection_t
 
typedef struct fetch_timer fetch_timer_t
 
typedef struct http_parse_context http_parse_context_t
 

Enumerations

enum  connection_state_t {
  CONN_STATE_NONE = 0 , CONN_STATE_RESOLVING , CONN_STATE_CONNECTING , CONN_STATE_TLS_HANDSHAKE ,
  CONN_STATE_SENDING , CONN_STATE_RECEIVING , CONN_STATE_REDIRECTING , CONN_STATE_COMPLETE ,
  CONN_STATE_ERROR , CONN_STATE_CANCELLED
}
 
enum  io_operation_t { IO_OP_CONNECT = 1 , IO_OP_SEND = 2 , IO_OP_RECV = 4 , IO_OP_TIMEOUT = 8 }
 
enum  host_type_t { HOST_TYPE_DOMAIN = 0 , HOST_TYPE_IPV4 = 1 , HOST_TYPE_IPV6 = 2 }
 
enum  socket_op_result_t {
  SOCKET_OP_SUCCESS , SOCKET_OP_WOULD_BLOCK , SOCKET_OP_ERROR , SOCKET_OP_CLOSED ,
  SOCKET_OP_IN_PROGRESS
}
 

Functions

static char to_lower (const char x)
 
static int fetch_strcasecmp (const char *s1, const char *s2)
 
static void http_parse_context_free (struct http_parse_context *ctx)
 
static struct http_parse_contexthttp_parse_context_new (void)
 
static bool is_response_complete (const struct http_parse_context *ctx)
 
static size_t format_chunk_header (char *buffer, size_t data_size)
 
static size_t format_chunk_trailer (char *buffer)
 
static size_t format_final_chunk (char *buffer)
 
static size_t read_file_chunk (struct fetch_connection *conn, char *buffer, size_t buffer_size)
 
static bool has_more_file_data (struct fetch_connection *conn)
 
static fetch_url_tfetch_url_parse (const char *url)
 
static void fetch_url_free (fetch_url_t *fetch_url)
 
static bool fetch_url_is_same_origin (const fetch_url_t *url1, const fetch_url_t *url2)
 
static fetch_response_tcreate_response_from_context (const struct http_parse_context *ctx, const fetch_url_t *final_url, bool *supports_keep_alive, const fetch_request_t *request)
 
static void fetch_request_free (fetch_request_t *request)
 
static fetch_request_tfetch_request_new (fetch_url_t *parsed_url, fetch_init_t *init)
 
static uint64_t get_connection_timeout_ms (const fetch_connection_t *conn)
 
static uint32_t get_connection_max_redirects (const fetch_connection_t *conn)
 
static bool get_connection_keepalive (const fetch_connection_t *conn)
 
static http_method_t get_connection_method (const fetch_connection_t *conn)
 
static const fetch_url_tget_connection_url (const fetch_connection_t *conn)
 
static fetch_headers_tget_connection_headers (const fetch_connection_t *conn)
 
static fetch_body_tget_connection_body (const fetch_connection_t *conn)
 
static bool should_include_credentials (fetch_credentials_t credentials, const fetch_url_t *request_url, const char *origin)
 
static uint64_t fetch_get_time_ms (void)
 
static pooled_connection_tpooled_connection_new (FETCH_SOCKET socket, const char *host, uint16_t port, bool is_tls, tls_context_t *tls_context)
 
static void pooled_connection_free (pooled_connection_t *conn)
 
static bool pooled_connection_is_valid (pooled_connection_t *conn)
 
static connection_pool_tfind_or_create_pool (const char *host, uint16_t port)
 
static void remove_from_available (connection_pool_t *pool, pooled_connection_t *conn)
 
static void add_to_available (connection_pool_t *pool, pooled_connection_t *conn)
 
static void cleanup_expired_connections (connection_pool_t *pool)
 
static void evict_if_needed (connection_pool_t *pool)
 
static pooled_connection_tacquire_pooled_connection (const char *host, uint16_t port, bool is_tls)
 
static bool return_connection_to_pool (FETCH_SOCKET socket, const char *host, uint16_t port, bool keep_alive, bool is_tls, tls_context_t *tls_context)
 
static uint64_t fetch_next_connection_id (void)
 
static void insert_timer (fetch_timer_t *timer)
 
static fetch_timer_textract_expired_timers (uint64_t current_time_ms)
 
static bool add_connection_timeout (fetch_connection_t *conn)
 
static void remove_connection_timeout (uint64_t connection_id)
 
static fetch_connection_tfetch_connection_new (fetch_promise_t *promise, fetch_request_t *request)
 
static void remove_socket_from_event_system (FETCH_SOCKET socket)
 
static void fetch_connection_free (fetch_connection_t *conn)
 
static void add_active_connection (fetch_connection_t *conn)
 
static void remove_active_connection (fetch_connection_t *conn)
 
static void move_to_completed (fetch_connection_t *conn)
 
static fetch_connection_tfind_connection_by_id (uint64_t connection_id)
 
static void set_connection_error (fetch_connection_t *conn, fetch_error_t error, const char *message)
 
static void set_connection_cancelled (fetch_connection_t *conn, const char *reason)
 
static void set_connection_complete (fetch_connection_t *conn, fetch_response_t *response)
 
static void update_connection_activity (fetch_connection_t *conn)
 
static bool is_connection_timed_out (fetch_connection_t *conn, uint64_t current_time_ms)
 
static void process_completed_connections (void)
 
static bool is_redirect_status (int status_code)
 
static char * resolve_redirect_url (const fetch_url_t *base_url, const char *location)
 
static http_method_t get_redirect_method (http_method_t original_method, int status_code)
 
static bool reset_connection_for_redirect (fetch_connection_t *conn, const char *new_url, http_method_t new_method)
 
static bool init_event_loop_select (void)
 
static void cleanup_event_loop_select (void)
 
static void wakeup_event_loop_select (void)
 
static bool init_event_loop_platform (void)
 
static void cleanup_event_loop_platform (void)
 
static void wakeup_event_loop (void)
 
static FETCH_SOCKET create_nonblocking_socket (int family)
 
static void dns_resolution_callback (dns_result_t *result, void *user_data)
 
static socket_op_result_t start_connect (fetch_connection_t *conn)
 
static bool is_socket_connected (FETCH_SOCKET socket)
 
static socket_op_result_t check_connect_completion (fetch_connection_t *conn)
 
static socket_op_result_t send_data (fetch_connection_t *conn)
 
static socket_op_result_t receive_data (fetch_connection_t *conn)
 
static bool prepare_socket_for_state (fetch_connection_t *conn)
 
static void add_cookie_header_to_request (fetch_headers_t *headers, const fetch_url_t *url, const fetch_request_t *request)
 
static void process_set_cookie_headers (const fetch_headers_t *response_headers, const fetch_url_t *url, const fetch_request_t *request)
 
static char * build_request_line (const char *method, const fetch_url_t *url)
 
static char * build_host_header (const fetch_url_t *url)
 
static char * build_http_headers (const fetch_headers_t *headers, const char *host_header, const char *user_agent, const char *content_type, size_t content_length, bool has_body, bool use_chunked_encoding, bool use_keep_alive, const fetch_url_t *url, const fetch_request_t *request, size_t *total_size)
 
static bool build_http_request (fetch_connection_t *conn)
 
static bool ensure_body_capacity (http_parse_context_t *ctx, size_t needed)
 
static bool append_to_body (http_parse_context_t *ctx, const char *data, size_t size)
 
static bool convert_phr_headers (http_parse_context_t *ctx)
 
static void analyze_headers (http_parse_context_t *ctx)
 
static bool parse_http_response (http_parse_context_t *ctx, const char *data, size_t size, size_t *consumed)
 
static bool is_response_complete (const http_parse_context_t *ctx)
 
static void process_timeout_events (void)
 
static bool handle_redirect_response (fetch_connection_t *conn)
 
static void process_connection_state (fetch_connection_t *conn)
 
static int process_events_select (uint32_t timeout_ms)
 
static void perform_pool_maintenance (void)
 
int fetch_event_loop_process (uint32_t timeout_ms)
 Process events in the event loop (NON-BLOCKING with timeout)
 
bool fetch_event_loop_start (void)
 Start the event loop.
 
void fetch_event_loop_stop (void)
 Stop the event loop.
 
bool fetch_event_loop_is_running (void)
 Check if the event loop is running.
 
static bool submit_connection_to_event_loop (fetch_connection_t *conn)
 
void fetch_global_init (const fetch_config_t *config)
 Initialize the fetch library with configuration.
 
void fetch_global_dispose (void)
 Clean up the fetch library.
 
static fetch_error_t validate_request (const char *url, const fetch_init_t *init, fetch_url_t **parsed_url_out)
 
static fetch_response_tcreate_error_response (fetch_error_t error, const char *message, const char *url)
 
fetch_promise_tfetch_async (const char *url, fetch_init_t *init)
 Make an asynchronous HTTP request (NON-BLOCKING)
 
fetch_response_tfetch (const char *url, fetch_init_t *init)
 Make a synchronous HTTP request (BLOCKING)
 
bool fetch_promise_poll (fetch_promise_t *promise)
 Check if a promise has completed (NON-BLOCKING)
 
bool fetch_promise_await (fetch_promise_t *promise, uint32_t timeout_ms)
 Wait for a promise to complete (BLOCKING)
 
bool fetch_promise_cancel (fetch_promise_t *promise, const char *reason)
 Cancel a pending promise.
 
bool fetch_promise_cancelled (const fetch_promise_t *promise)
 Check if a promise was cancelled.
 
fetch_response_tfetch_promise_response (const fetch_promise_t *promise)
 Get the response from a fulfilled promise.
 
fetch_promise_state_t fetch_promise_state (const fetch_promise_t *promise)
 Get the current state of a promise.
 
fetch_error_t fetch_promise_error (const fetch_promise_t *promise)
 Get the error code from a rejected promise.
 
const char * fetch_promise_error_message (const fetch_promise_t *promise)
 Get the error message from a rejected promise.
 
bool fetch_promise_pending (const fetch_promise_t *promise)
 Check if promise is still pending.
 
bool fetch_promise_fulfilled (const fetch_promise_t *promise)
 Check if promise was fulfilled successfully.
 
bool fetch_promise_rejected (const fetch_promise_t *promise)
 Check if promise was rejected.
 
fetch_headers_tfetch_headers_new (void)
 Create a new headers container.
 
void fetch_headers_free (fetch_headers_t *headers)
 Free a headers container and all its contents.
 
static bool fetch_headers_resize (fetch_headers_t *headers)
 
void fetch_headers_append (fetch_headers_t *headers, const char *name, const char *value)
 Add a header (allows duplicates)
 
void fetch_headers_set (fetch_headers_t *headers, const char *name, const char *value)
 Set a header (replaces existing)
 
void fetch_headers_delete (fetch_headers_t *headers, const char *name)
 Remove all headers with the given name.
 
const char * fetch_headers_get (const fetch_headers_t *headers, const char *name)
 Get the first header value with the given name.
 
bool fetch_headers_has (const fetch_headers_t *headers, const char *name)
 Check if a header exists.
 
fetch_headers_iterator_t fetch_headers_entries (const fetch_headers_t *headers)
 Create an iterator for headers.
 
bool fetch_headers_next (fetch_headers_iterator_t *iter, const char **key, const char **value)
 Get the next header from an iterator.
 
fetch_body_tfetch_body_text (const char *text)
 Create a text body.
 
fetch_body_tfetch_body_json (const char *json)
 Create a JSON body.
 
fetch_body_tfetch_body_binary (const void *data, size_t size, const char *content_type)
 Create a binary body.
 
fetch_body_tfetch_body_form_data (const char *form_data)
 Create a form data body.
 
void fetch_body_free (fetch_body_t *body)
 Free a body object.
 
const char * fetch_response_text (fetch_response_t *response)
 Get response body as text.
 
const void * fetch_response_array_buffer (fetch_response_t *response, size_t *size)
 Get response body as binary data.
 
const char * fetch_response_json (fetch_response_t *response)
 Get response body as JSON text.
 
bool fetch_response_ok (const fetch_response_t *response)
 Check if response is successful (2xx status)
 
uint16_t fetch_response_status (const fetch_response_t *response)
 Get HTTP status code.
 
const char * fetch_response_status_text (const fetch_response_t *response)
 Get HTTP status text.
 
const char * fetch_response_url (const fetch_response_t *response)
 Get final URL (after redirects)
 
fetch_headers_tfetch_response_headers (const fetch_response_t *response)
 Get response headers.
 
void fetch_response_free (fetch_response_t *response)
 Free a response object.
 
void fetch_promise_free (fetch_promise_t *promise)
 Free a promise object.
 
bool percent_encode (const char *input, const uint8_t character_set[], char *output, bool append)
 
fetch_url_search_params_tfetch_url_search_params_new (void)
 Create a new URL search parameters container.
 
void fetch_url_search_params_free (fetch_url_search_params_t *params)
 Free a URL search parameters container.
 
static bool fetch_url_search_params_resize (fetch_url_search_params_t *params)
 
void fetch_url_search_params_append (fetch_url_search_params_t *params, const char *name, const char *value)
 Add a parameter (allows duplicates)
 
void fetch_url_search_params_set (fetch_url_search_params_t *params, const char *name, const char *value)
 Set a parameter (replaces existing)
 
void fetch_url_search_params_delete (fetch_url_search_params_t *params, const char *name)
 Remove all parameters with the given name.
 
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.
 
bool fetch_url_search_params_has (const fetch_url_search_params_t *params, const char *name)
 Check if a parameter exists.
 
char * fetch_url_search_params_to_string (const fetch_url_search_params_t *params)
 Convert parameters to URL-encoded string.
 
fetch_body_tfetch_body_url_search_params (fetch_url_search_params_t *params)
 Create a body from URL search parameters.
 
fetch_body_tfetch_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.
 
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.
 
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.
 
fetch_abort_controller_tfetch_abort_controller_new (void)
 Create a new abort controller.
 
void fetch_abort_controller_abort (fetch_abort_controller_t *controller, const char *reason)
 Abort operations using this controller.
 
bool fetch_abort_controller_aborted (const fetch_abort_controller_t *controller)
 Check if controller has been aborted.
 
void fetch_abort_controller_free (fetch_abort_controller_t *controller)
 Free an abort controller.
 
fetch_init_tfetch_init_new (void)
 Create a new request configuration.
 
void fetch_init_free (fetch_init_t *init)
 Free a request configuration.
 
fetch_init_tfetch_init_method (fetch_init_t *init, http_method_t method)
 Set HTTP method (fluent interface)
 
fetch_init_tfetch_init_headers (fetch_init_t *init, fetch_headers_t *headers)
 Set request headers (fluent interface)
 
fetch_init_tfetch_init_body (fetch_init_t *init, fetch_body_t *body)
 Set request body (fluent interface)
 
fetch_init_tfetch_init_timeout (fetch_init_t *init, uint32_t timeout_ms)
 Set request timeout (fluent interface)
 
fetch_init_tfetch_init_signal (fetch_init_t *init, fetch_abort_controller_t *signal)
 Set abort signal (fluent interface)
 
cookie_jar_tfetch_get_cookie_jar (void)
 Get the current cookie jar.
 
size_t fetch_cookie_jar_count (const char *domain_filter)
 Count cookies in the jar.
 
void fetch_cookie_jar_clear (void)
 Clear all cookies from the jar.
 
void fetch_disable_cookies (void)
 Disable cookie handling.
 
cookie_jar_tfetch_create_cookie_jar (const char *persistent_file)
 Create a new cookie jar.
 
void fetch_cookie_jar_free (cookie_jar_t *jar)
 Free a cookie jar.
 
bool fetch_save_cookies (const char *filename, cookie_jar_t *jar)
 Save cookies to a file.
 
bool fetch_load_cookies (const char *filename, cookie_jar_t *jar)
 Load cookies from a file.
 
void fetch_cookie_jar_print (cookie_jar_t *jar, const char *domain_filter)
 Print cookies to stdout (for debugging)
 
const char * fetch_method_to_string (http_method_t method)
 Convert HTTP method enum to string.
 
http_method_t fetch_method_from_string (const char *method_str)
 Convert string to HTTP method enum.
 
bool fetch_is_valid_url (const char *url)
 Check if a URL is valid.
 
const char * fetch_error_to_string (fetch_error_t error)
 Convert error code to human-readable string.
 

Variables

static struct fetch_event_loop g_event_loop = {0}
 
static connection_pool_manager_t g_pool_manager = {0}
 
static dns_resolver_tg_dns_resolver = NULL
 
static fetch_config_t g_fetch_config
 
static bool g_fetch_initialized = false
 
static bool g_user_agent_allocated = false
 

Macro Definition Documentation

◆ fetch_atomic_cas

#define fetch_atomic_cas ( ptr,
expected,
desired )
Value:
atomic_compare_exchange_strong_explicit((ptr), &(expected), (desired), \
memory_order_acq_rel, \
memory_order_acquire)

◆ fetch_atomic_dec

#define fetch_atomic_dec ( ptr)
Value:
(atomic_fetch_sub_explicit((ptr), 1, memory_order_acq_rel) + 1)

◆ fetch_atomic_inc

#define fetch_atomic_inc ( ptr)
Value:
(atomic_fetch_add_explicit((ptr), 1, memory_order_acq_rel) + 1)

◆ fetch_atomic_load

#define fetch_atomic_load ( ptr)
Value:
atomic_load_explicit((ptr), memory_order_acquire)

◆ fetch_atomic_store

#define fetch_atomic_store ( ptr,
val )
Value:
atomic_store_explicit((ptr), (val), memory_order_release)

◆ fetch_close_socket

#define fetch_close_socket   close

◆ FETCH_EVENT_INVALID

#define FETCH_EVENT_INVALID   -1

◆ FETCH_INVALID_SOCKET

#define FETCH_INVALID_SOCKET   (-1)

◆ FETCH_MAX_BODY_SIZE

#define FETCH_MAX_BODY_SIZE   (50 * 1024 * 1024)

◆ FETCH_MAX_CONTENT_TYPE_LENGTH

#define FETCH_MAX_CONTENT_TYPE_LENGTH   256

◆ FETCH_MAX_FORM_PARAM_LENGTH

#define FETCH_MAX_FORM_PARAM_LENGTH   1024

◆ FETCH_MAX_HEADER_NAME_LENGTH

#define FETCH_MAX_HEADER_NAME_LENGTH   256

◆ FETCH_MAX_HEADER_VALUE_LENGTH

#define FETCH_MAX_HEADER_VALUE_LENGTH   8192

◆ FETCH_MAX_HOSTNAME_LENGTH

#define FETCH_MAX_HOSTNAME_LENGTH   253

◆ FETCH_MAX_METHOD_LENGTH

#define FETCH_MAX_METHOD_LENGTH   16

◆ FETCH_MAX_PATH_LENGTH

#define FETCH_MAX_PATH_LENGTH   4096

◆ FETCH_MAX_STRING_LENGTH

#define FETCH_MAX_STRING_LENGTH   4096

◆ FETCH_MAX_URL_LENGTH

#define FETCH_MAX_URL_LENGTH   8192

◆ FETCH_SOCKET

#define FETCH_SOCKET   int

◆ fetch_socket_error

#define fetch_socket_error   errno

◆ FETCH_WAIT_INFINITE

#define FETCH_WAIT_INFINITE   -1

◆ SAFE_APPEND

#define SAFE_APPEND ( str)
Value:
do { \
size_t len = strlen(str); \
if (current_pos + len >= estimated_size) { \
goto cleanup; \
} \
memcpy(buffer + current_pos, str, len); \
current_pos += len; \
} while (0)

Typedef Documentation

◆ connection_pool_t

◆ fetch_connection_t

◆ fetch_event_handle_t

typedef int fetch_event_handle_t

◆ fetch_host_type_t

◆ fetch_request_t

◆ fetch_timer_t

typedef struct fetch_timer fetch_timer_t

◆ fetch_url_t

typedef struct fetch_url fetch_url_t

◆ http_parse_context_t

◆ pooled_connection_t

◆ tls_context_t

typedef struct tls_context tls_context_t

Enumeration Type Documentation

◆ connection_state_t

Enumerator
CONN_STATE_NONE 
CONN_STATE_RESOLVING 
CONN_STATE_CONNECTING 
CONN_STATE_TLS_HANDSHAKE 
CONN_STATE_SENDING 
CONN_STATE_RECEIVING 
CONN_STATE_REDIRECTING 
CONN_STATE_COMPLETE 
CONN_STATE_ERROR 
CONN_STATE_CANCELLED 

◆ host_type_t

Enumerator
HOST_TYPE_DOMAIN 
HOST_TYPE_IPV4 
HOST_TYPE_IPV6 

◆ io_operation_t

Enumerator
IO_OP_CONNECT 
IO_OP_SEND 
IO_OP_RECV 
IO_OP_TIMEOUT 

◆ socket_op_result_t

Enumerator
SOCKET_OP_SUCCESS 
SOCKET_OP_WOULD_BLOCK 
SOCKET_OP_ERROR 
SOCKET_OP_CLOSED 
SOCKET_OP_IN_PROGRESS 

Function Documentation

◆ acquire_pooled_connection()

static pooled_connection_t * acquire_pooled_connection ( const char * host,
uint16_t port,
bool is_tls )
static

◆ add_active_connection()

static void add_active_connection ( fetch_connection_t * conn)
static

◆ add_connection_timeout()

static bool add_connection_timeout ( fetch_connection_t * conn)
static

◆ add_cookie_header_to_request()

static void add_cookie_header_to_request ( fetch_headers_t * headers,
const fetch_url_t * url,
const fetch_request_t * request )
static

◆ add_to_available()

static void add_to_available ( connection_pool_t * pool,
pooled_connection_t * conn )
static

◆ analyze_headers()

static void analyze_headers ( http_parse_context_t * ctx)
static

◆ append_to_body()

static bool append_to_body ( http_parse_context_t * ctx,
const char * data,
size_t size )
static

◆ build_host_header()

static char * build_host_header ( const fetch_url_t * url)
static

◆ build_http_headers()

static char * build_http_headers ( const fetch_headers_t * headers,
const char * host_header,
const char * user_agent,
const char * content_type,
size_t content_length,
bool has_body,
bool use_chunked_encoding,
bool use_keep_alive,
const fetch_url_t * url,
const fetch_request_t * request,
size_t * total_size )
static

◆ build_http_request()

static bool build_http_request ( fetch_connection_t * conn)
static

◆ build_request_line()

static char * build_request_line ( const char * method,
const fetch_url_t * url )
static

◆ check_connect_completion()

static socket_op_result_t check_connect_completion ( fetch_connection_t * conn)
static

◆ cleanup_event_loop_platform()

static void cleanup_event_loop_platform ( void )
static

◆ cleanup_event_loop_select()

static void cleanup_event_loop_select ( void )
static

◆ cleanup_expired_connections()

static void cleanup_expired_connections ( connection_pool_t * pool)
static

◆ convert_phr_headers()

static bool convert_phr_headers ( http_parse_context_t * ctx)
static

◆ create_error_response()

static fetch_response_t * create_error_response ( fetch_error_t error,
const char * message,
const char * url )
static

◆ create_nonblocking_socket()

static FETCH_SOCKET create_nonblocking_socket ( int family)
static

◆ create_response_from_context()

static fetch_response_t * create_response_from_context ( const struct http_parse_context * ctx,
const fetch_url_t * final_url,
bool * supports_keep_alive,
const fetch_request_t * request )
static

◆ dns_resolution_callback()

static void dns_resolution_callback ( dns_result_t * result,
void * user_data )
static

◆ ensure_body_capacity()

static bool ensure_body_capacity ( http_parse_context_t * ctx,
size_t needed )
static

◆ evict_if_needed()

static void evict_if_needed ( connection_pool_t * pool)
static

◆ extract_expired_timers()

static fetch_timer_t * extract_expired_timers ( uint64_t current_time_ms)
static

◆ fetch_connection_free()

static void fetch_connection_free ( fetch_connection_t * conn)
static

◆ fetch_connection_new()

static fetch_connection_t * fetch_connection_new ( fetch_promise_t * promise,
fetch_request_t * request )
static

◆ fetch_get_time_ms()

static uint64_t fetch_get_time_ms ( void )
static

◆ fetch_headers_resize()

static bool fetch_headers_resize ( fetch_headers_t * headers)
static

◆ fetch_next_connection_id()

static uint64_t fetch_next_connection_id ( void )
static

◆ fetch_request_free()

static void fetch_request_free ( fetch_request_t * request)
static

◆ fetch_request_new()

static fetch_request_t * fetch_request_new ( fetch_url_t * parsed_url,
fetch_init_t * init )
static

◆ fetch_strcasecmp()

static int fetch_strcasecmp ( const char * s1,
const char * s2 )
static

◆ fetch_url_free()

static void fetch_url_free ( fetch_url_t * fetch_url)
static

◆ fetch_url_is_same_origin()

static bool fetch_url_is_same_origin ( const fetch_url_t * url1,
const fetch_url_t * url2 )
static

◆ fetch_url_parse()

static fetch_url_t * fetch_url_parse ( const char * url)
static

◆ fetch_url_search_params_resize()

static bool fetch_url_search_params_resize ( fetch_url_search_params_t * params)
static

◆ find_connection_by_id()

static fetch_connection_t * find_connection_by_id ( uint64_t connection_id)
static

◆ find_or_create_pool()

static connection_pool_t * find_or_create_pool ( const char * host,
uint16_t port )
static

◆ format_chunk_header()

static size_t format_chunk_header ( char * buffer,
size_t data_size )
static

◆ format_chunk_trailer()

static size_t format_chunk_trailer ( char * buffer)
static

◆ format_final_chunk()

static size_t format_final_chunk ( char * buffer)
static

◆ get_connection_body()

static fetch_body_t * get_connection_body ( const fetch_connection_t * conn)
static

◆ get_connection_headers()

static fetch_headers_t * get_connection_headers ( const fetch_connection_t * conn)
static

◆ get_connection_keepalive()

static bool get_connection_keepalive ( const fetch_connection_t * conn)
static

◆ get_connection_max_redirects()

static uint32_t get_connection_max_redirects ( const fetch_connection_t * conn)
static

◆ get_connection_method()

static http_method_t get_connection_method ( const fetch_connection_t * conn)
static

◆ get_connection_timeout_ms()

static uint64_t get_connection_timeout_ms ( const fetch_connection_t * conn)
static

◆ get_connection_url()

static const fetch_url_t * get_connection_url ( const fetch_connection_t * conn)
static

◆ get_redirect_method()

static http_method_t get_redirect_method ( http_method_t original_method,
int status_code )
static

◆ handle_redirect_response()

static bool handle_redirect_response ( fetch_connection_t * conn)
static

◆ has_more_file_data()

static bool has_more_file_data ( struct fetch_connection * conn)
static

◆ http_parse_context_free()

static void http_parse_context_free ( struct http_parse_context * ctx)
static

◆ http_parse_context_new()

static http_parse_context_t * http_parse_context_new ( void )
static

◆ init_event_loop_platform()

static bool init_event_loop_platform ( void )
static

◆ init_event_loop_select()

static bool init_event_loop_select ( void )
static

◆ insert_timer()

static void insert_timer ( fetch_timer_t * timer)
static

◆ is_connection_timed_out()

static bool is_connection_timed_out ( fetch_connection_t * conn,
uint64_t current_time_ms )
static

◆ is_redirect_status()

static bool is_redirect_status ( int status_code)
static

◆ is_response_complete() [1/2]

static bool is_response_complete ( const http_parse_context_t * ctx)
static

◆ is_response_complete() [2/2]

static bool is_response_complete ( const struct http_parse_context * ctx)
static

◆ is_socket_connected()

static bool is_socket_connected ( FETCH_SOCKET socket)
static

◆ move_to_completed()

static void move_to_completed ( fetch_connection_t * conn)
static

◆ parse_http_response()

static bool parse_http_response ( http_parse_context_t * ctx,
const char * data,
size_t size,
size_t * consumed )
static

◆ percent_encode()

bool percent_encode ( const char * input,
const uint8_t character_set[],
char * output,
bool append )

◆ perform_pool_maintenance()

static void perform_pool_maintenance ( void )
static

◆ pooled_connection_free()

static void pooled_connection_free ( pooled_connection_t * conn)
static

◆ pooled_connection_is_valid()

static bool pooled_connection_is_valid ( pooled_connection_t * conn)
static

◆ pooled_connection_new()

static pooled_connection_t * pooled_connection_new ( FETCH_SOCKET socket,
const char * host,
uint16_t port,
bool is_tls,
tls_context_t * tls_context )
static

◆ prepare_socket_for_state()

static bool prepare_socket_for_state ( fetch_connection_t * conn)
static

◆ process_completed_connections()

static void process_completed_connections ( void )
static

◆ process_connection_state()

static void process_connection_state ( fetch_connection_t * conn)
static

◆ process_events_select()

static int process_events_select ( uint32_t timeout_ms)
static

◆ process_set_cookie_headers()

static void process_set_cookie_headers ( const fetch_headers_t * response_headers,
const fetch_url_t * url,
const fetch_request_t * request )
static

◆ process_timeout_events()

static void process_timeout_events ( void )
static

◆ read_file_chunk()

static size_t read_file_chunk ( struct fetch_connection * conn,
char * buffer,
size_t buffer_size )
static

◆ receive_data()

static socket_op_result_t receive_data ( fetch_connection_t * conn)
static

◆ remove_active_connection()

static void remove_active_connection ( fetch_connection_t * conn)
static

◆ remove_connection_timeout()

static void remove_connection_timeout ( uint64_t connection_id)
static

◆ remove_from_available()

static void remove_from_available ( connection_pool_t * pool,
pooled_connection_t * conn )
static

◆ remove_socket_from_event_system()

static void remove_socket_from_event_system ( FETCH_SOCKET socket)
inlinestatic

◆ reset_connection_for_redirect()

static bool reset_connection_for_redirect ( fetch_connection_t * conn,
const char * new_url,
http_method_t new_method )
static

◆ resolve_redirect_url()

static char * resolve_redirect_url ( const fetch_url_t * base_url,
const char * location )
static

◆ return_connection_to_pool()

static bool return_connection_to_pool ( FETCH_SOCKET socket,
const char * host,
uint16_t port,
bool keep_alive,
bool is_tls,
tls_context_t * tls_context )
static

◆ send_data()

static socket_op_result_t send_data ( fetch_connection_t * conn)
static

◆ set_connection_cancelled()

static void set_connection_cancelled ( fetch_connection_t * conn,
const char * reason )
static

◆ set_connection_complete()

static void set_connection_complete ( fetch_connection_t * conn,
fetch_response_t * response )
static

◆ set_connection_error()

static void set_connection_error ( fetch_connection_t * conn,
fetch_error_t error,
const char * message )
static

◆ should_include_credentials()

static bool should_include_credentials ( fetch_credentials_t credentials,
const fetch_url_t * request_url,
const char * origin )
static

◆ start_connect()

static socket_op_result_t start_connect ( fetch_connection_t * conn)
static

◆ submit_connection_to_event_loop()

static bool submit_connection_to_event_loop ( fetch_connection_t * conn)
static

◆ to_lower()

static char to_lower ( const char x)
static

◆ update_connection_activity()

static void update_connection_activity ( fetch_connection_t * conn)
static

◆ validate_request()

static fetch_error_t validate_request ( const char * url,
const fetch_init_t * init,
fetch_url_t ** parsed_url_out )
static

◆ wakeup_event_loop()

static void wakeup_event_loop ( void )
static

◆ wakeup_event_loop_select()

static void wakeup_event_loop_select ( void )
static

Variable Documentation

◆ g_dns_resolver

dns_resolver_t* g_dns_resolver = NULL
static

◆ g_event_loop

struct fetch_event_loop g_event_loop = {0}
static

◆ g_fetch_config

fetch_config_t g_fetch_config
static
Initial value:
= {
.user_agent = FETCH_USER_AGENT,
.origin = NULL,
.cookie_jar = NULL,
.default_timeout_ms = 30000,
.max_connections = 1000,
.max_connections_per_host = 6,
.keep_alive_timeout_ms = 115000,
.pool_cleanup_interval_ms = 30000,
.max_pooled_connections = 100,
.flags = (1U << FETCH_FLAG_KEEP_ALIVE_DEFAULT) |
#define FETCH_USER_AGENT
Default user agent string.
Definition fetch.h:125
@ FETCH_FLAG_FOLLOW_REDIRECTS
Definition fetch.h:203
@ FETCH_FLAG_ENABLE_COMPRESSION
Definition fetch.h:204
@ FETCH_FLAG_ENABLE_COOKIES
Definition fetch.h:205
@ FETCH_FLAG_KEEP_ALIVE_DEFAULT
Definition fetch.h:201

◆ g_fetch_initialized

bool g_fetch_initialized = false
static

◆ g_pool_manager

connection_pool_manager_t g_pool_manager = {0}
static

◆ g_user_agent_allocated

bool g_user_agent_allocated = false
static