libfetch 0.0.0
A lightweight asynchronous HTTP/1.1 client library implementing a subset of the WHATWG Fetch API.
Loading...
Searching...
No Matches
cookie.h File Reference

HTTP Cookie management library for C. More...

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <time.h>

Go to the source code of this file.

Data Structures

struct  cookie
 HTTP cookie structure. More...
 
struct  cookie_jar_config_t
 Cookie jar configuration. More...
 
struct  cookie_domain_bucket
 Domain bucket for cookie storage. More...
 
struct  cookie_jar
 Cookie jar implementation. More...
 
struct  cookie_match
 Cookie match result. More...
 
struct  cookie_iterator
 Cookie iterator implementation. More...
 

Macros

#define COOKIE_MAX_NAME_LENGTH   256
 Maximum cookie name length.
 
#define COOKIE_MAX_VALUE_LENGTH   4096
 Maximum cookie value length.
 
#define COOKIE_MAX_DOMAIN_LENGTH   253
 Maximum domain length.
 
#define COOKIE_MAX_PATH_LENGTH   1024
 Maximum path length.
 
#define COOKIE_DEFAULT_MAX_COOKIES   3000
 Default maximum total cookies.
 
#define COOKIE_DEFAULT_MAX_PER_DOMAIN   50
 Default maximum cookies per domain.
 
#define COOKIE_DEFAULT_MAX_AGE_SECONDS   (365 * 24 * 60 * 60)
 Default maximum cookie age (1 year)
 
#define COOKIE_IS_VALID_FOR_HTTP(cookie)
 Check if cookie is valid for HTTP requests.
 
#define COOKIE_IS_VALID_FOR_HTTPS(cookie)
 Check if cookie is valid for HTTPS requests.
 

Typedefs

typedef struct cookie cookie_t
 Individual HTTP cookie.
 
typedef struct cookie_jar cookie_jar_t
 Container for multiple cookies.
 
typedef struct cookie_iterator cookie_iterator_t
 Iterator for traversing cookies.
 
typedef struct cookie_domain_bucket cookie_domain_bucket_t
 Domain bucket for cookie storage.
 
typedef struct cookie_match cookie_match_t
 Cookie match result.
 

Enumerations

enum  cookie_flags_t {
  COOKIE_FLAG_SECURE = 1 << 0 , COOKIE_FLAG_HTTP_ONLY = 1 << 1 , COOKIE_FLAG_HOST_ONLY = 1 << 2 , COOKIE_FLAG_SESSION = 1 << 3 ,
  COOKIE_FLAG_PERSISTENT = 1 << 4
}
 Cookie attribute flags. More...
 
enum  cookie_samesite_t { COOKIE_SAMESITE_NONE = 0 , COOKIE_SAMESITE_LAX , COOKIE_SAMESITE_STRICT }
 SameSite attribute values. More...
 
enum  cookie_priority_t { COOKIE_PRIORITY_LOW = 0 , COOKIE_PRIORITY_MEDIUM , COOKIE_PRIORITY_HIGH }
 Cookie priority levels. More...
 
enum  cookie_parse_result_t {
  COOKIE_PARSE_SUCCESS = 0 , COOKIE_PARSE_ERROR_INVALID_FORMAT , COOKIE_PARSE_ERROR_INVALID_NAME , COOKIE_PARSE_ERROR_INVALID_VALUE ,
  COOKIE_PARSE_ERROR_INVALID_DOMAIN , COOKIE_PARSE_ERROR_INVALID_PATH , COOKIE_PARSE_ERROR_INVALID_EXPIRES , COOKIE_PARSE_ERROR_MEMORY
}
 Cookie parsing result codes. More...
 

Functions

cookie_jar_tcookie_jar_new (void)
 Create a new cookie jar with default configuration.
 
cookie_jar_tcookie_jar_new_with_config (const cookie_jar_config_t *config)
 Create a new cookie jar with custom configuration.
 
void cookie_jar_free (cookie_jar_t *jar)
 Free a cookie jar and all its cookies.
 
cookie_jar_config_t cookie_jar_default_config (void)
 Get default cookie jar configuration.
 
void cookie_jar_clear (cookie_jar_t *jar)
 Clear all cookies from a jar.
 
size_t cookie_jar_count (const cookie_jar_t *jar)
 Get total number of cookies in jar.
 
size_t cookie_jar_count_for_domain (const cookie_jar_t *jar, const char *domain)
 Get number of cookies for a specific domain.
 
cookie_tcookie_new (const char *name, const char *value, const char *domain, const char *path)
 Create a new cookie.
 
cookie_parse_result_t cookie_parse_set_cookie (const char *header_value, const char *request_url, cookie_t **cookie)
 Parse a Set-Cookie header.
 
cookie_parse_result_t cookie_parse_set_cookie_headers (const char **header_values, size_t count, const char *request_url, cookie_t ***cookies, size_t *cookie_count)
 Parse multiple Set-Cookie headers.
 
cookie_tcookie_clone (const cookie_t *cookie)
 Clone a cookie.
 
void cookie_free (cookie_t *cookie)
 Free a cookie.
 
void cookie_set_expires (cookie_t *cookie, time_t expires)
 Set cookie expiration time.
 
void cookie_set_max_age (cookie_t *cookie, int64_t max_age_seconds)
 Set cookie max-age.
 
void cookie_set_secure (cookie_t *cookie, bool secure)
 Set Secure flag.
 
void cookie_set_http_only (cookie_t *cookie, bool http_only)
 Set HttpOnly flag.
 
void cookie_set_samesite (cookie_t *cookie, cookie_samesite_t samesite)
 Set SameSite attribute.
 
void cookie_set_priority (cookie_t *cookie, cookie_priority_t priority)
 Set Priority attribute.
 
bool cookie_is_secure (const cookie_t *cookie)
 Check if cookie has Secure flag.
 
bool cookie_is_http_only (const cookie_t *cookie)
 Check if cookie has HttpOnly flag.
 
bool cookie_is_session (const cookie_t *cookie)
 Check if cookie is a session cookie.
 
bool cookie_is_expired (const cookie_t *cookie, time_t current_time)
 Check if cookie is expired.
 
bool cookie_is_host_only (const cookie_t *cookie)
 Check if cookie is host-only.
 
cookie_samesite_t cookie_get_samesite (const cookie_t *cookie)
 Get cookie's SameSite attribute.
 
cookie_priority_t cookie_get_priority (const cookie_t *cookie)
 Get cookie's priority.
 
void cookie_touch (cookie_t *cookie)
 Update cookie's last access time.
 
bool cookie_jar_add (cookie_jar_t *jar, cookie_t *cookie)
 Add a cookie to a jar.
 
bool cookie_jar_remove (cookie_jar_t *jar, const char *name, const char *domain, const char *path)
 Remove a specific cookie from jar.
 
size_t cookie_jar_remove_domain (cookie_jar_t *jar, const char *domain)
 Remove all cookies for a domain.
 
size_t cookie_jar_cleanup_expired (cookie_jar_t *jar)
 Remove expired cookies from jar.
 
size_t cookie_jar_remove_session (cookie_jar_t *jar)
 Remove all session cookies from jar.
 
cookie_match_tcookie_jar_get_cookies_for_url (cookie_jar_t *jar, const char *url, bool include_http_only)
 Get cookies that match a URL.
 
cookie_match_tcookie_jar_get_cookies (cookie_jar_t *jar, const char *domain, const char *path, bool secure_only, bool include_http_only)
 Get cookies matching domain and path.
 
void cookie_match_free (cookie_match_t *matches)
 Free a list of cookie matches.
 
char * cookie_match_to_header (const cookie_match_t *matches)
 Convert cookie matches to HTTP Cookie header.
 
cookie_iterator_t cookie_jar_iterator (cookie_jar_t *jar)
 Create iterator for all cookies in jar.
 
cookie_iterator_t cookie_jar_iterator_domain (cookie_jar_t *jar, const char *domain)
 Create iterator for cookies in specific domain.
 
cookie_tcookie_iterator_next (cookie_iterator_t *iter)
 Get next cookie from iterator.
 
bool cookie_iterator_has_next (const cookie_iterator_t *iter)
 Check if iterator has more cookies.
 
void cookie_iterator_reset (cookie_iterator_t *iter)
 Reset iterator to beginning.
 
bool cookie_jar_load_binary (cookie_jar_t *jar, const char *filename)
 Load cookies from binary file.
 
bool cookie_jar_save_binary (const cookie_jar_t *jar, const char *filename)
 Save cookies to binary file.
 
char * cookie_jar_save_binary_buffer (const cookie_jar_t *jar, size_t *buffer_size)
 Save cookies to memory buffer.
 
bool cookie_jar_load_binary_buffer (cookie_jar_t *jar, const char *buffer, size_t buffer_size)
 Load cookies from memory buffer.
 
bool cookie_domain_matches (const char *cookie_domain, const char *request_domain)
 Check if cookie domain matches request domain.
 
bool cookie_path_matches (const char *cookie_path, const char *request_path)
 Check if cookie path matches request path.
 
char * cookie_canonicalize_domain (const char *domain)
 Convert domain to canonical lowercase form.
 
char * cookie_default_path (const char *url)
 Extract default path from URL.
 
bool cookie_is_public_suffix (const char *domain)
 Check if domain is a public suffix.
 
bool cookie_is_valid_name (const char *name)
 Check if cookie name is valid.
 
bool cookie_is_valid_value (const char *value)
 Check if cookie value is valid.
 
char * cookie_url_decode (const char *encoded)
 URL-decode a string.
 
char * cookie_url_encode (const char *decoded)
 URL-encode a string.
 
const char * cookie_samesite_to_string (cookie_samesite_t samesite)
 Convert SameSite enum to string.
 
cookie_samesite_t cookie_samesite_from_string (const char *str)
 Convert string to SameSite enum.
 
const char * cookie_priority_to_string (cookie_priority_t priority)
 Convert Priority enum to string.
 
cookie_priority_t cookie_priority_from_string (const char *str)
 Convert string to Priority enum.
 
const char * cookie_parse_error_string (cookie_parse_result_t result)
 Get error message for parse result.
 

Detailed Description

HTTP Cookie management library for C.

This library provides comprehensive HTTP cookie handling including parsing, storage, matching, and persistence. It supports all modern cookie attributes including Secure, HttpOnly, SameSite, and Priority flags.