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.c File Reference
#include "cookie.h"
#include "ada_c.h"
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <libgen.h>
#include <sys/stat.h>
#include <unistd.h>

Data Structures

struct  cookie_binary_header_t
 Binary format header for individual cookies. More...
 
struct  cookie_jar_binary_header_t
 Binary format header for cookie jar files. More...
 

Macros

#define _GNU_SOURCE
 
#define COOKIE_HASH_TABLE_SIZE   256
 
#define COOKIE_NETSCAPE_HEADER    "# Netscape HTTP Cookie File\n# This is a generated file! Do not edit.\n\n"
 
#define COOKIE_MAX_LINE_LENGTH   8192
 
#define COOKIE_CLEANUP_INTERVAL   3600
 
#define RFC1123_DATE_FORMAT   "%a, %d %b %Y %H:%M:%S GMT"
 Standard date formats for cookie expiration parsing.
 
#define RFC850_DATE_FORMAT   "%A, %d-%b-%y %H:%M:%S GMT"
 
#define ASCTIME_DATE_FORMAT   "%a %b %d %H:%M:%S %Y"
 
#define COOKIE_BINARY_MAGIC   0x52414A43
 Binary file format constants.
 
#define COOKIE_BINARY_VERSION   1
 

Functions

static char * trim_whitespace (char *str)
 Remove leading and trailing whitespace from a string.
 
static uint32_t hash_domain (const char *domain)
 Calculate FNV-1a hash for domain strings.
 
static time_t parse_cookie_date (const char *date_str)
 Parse cookie expiration date from various formats.
 
static cookie_domain_bucket_tfind_domain_bucket (const cookie_jar_t *jar, const char *domain)
 Find domain bucket in hash table.
 
static cookie_domain_bucket_tcreate_domain_bucket (cookie_jar_t *jar, const char *domain)
 Create new domain bucket and add to hash table.
 
static bool remove_cookie_from_bucket (cookie_domain_bucket_t *bucket, cookie_t *cookie)
 Remove cookie from its domain bucket.
 
static bool add_cookie_to_bucket (cookie_domain_bucket_t *bucket, cookie_t *cookie)
 Add cookie to domain bucket.
 
static bool validate_cookie_domain (const char *cookie_domain, const char *request_host)
 Validate cookie domain against request host.
 
static bool validate_cookie_prefix (const char *name, const cookie_t *cookie, bool is_secure_request)
 Validate cookie name prefix security attributes.
 
static size_t calculate_binary_size (const cookie_jar_t *jar, uint32_t *cookie_count)
 Calculate total size needed for binary serialization.
 
static bool serialize_to_buffer (const cookie_jar_t *jar, char *buffer, size_t buffer_size)
 Serialize cookie jar to binary buffer.
 
static bool deserialize_from_buffer (cookie_jar_t *jar, const char *buffer, size_t buffer_size)
 Deserialize cookie jar from binary buffer.
 
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.
 
cookie_jar_config_t cookie_jar_default_config (void)
 Get default cookie jar configuration.
 
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.
 
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_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_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.
 
cookie_parse_result_t cookie_parse_set_cookie (const char *header_value, const char *request_url, cookie_t **cookie)
 Parse a Set-Cookie header.
 
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.
 
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_save_binary (const cookie_jar_t *jar, const char *filename)
 Save cookies to binary file.
 
bool cookie_jar_load_binary (cookie_jar_t *jar, const char *filename)
 Load cookies from 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.
 

Macro Definition Documentation

◆ _GNU_SOURCE

#define _GNU_SOURCE

◆ ASCTIME_DATE_FORMAT

#define ASCTIME_DATE_FORMAT   "%a %b %d %H:%M:%S %Y"

◆ COOKIE_BINARY_MAGIC

#define COOKIE_BINARY_MAGIC   0x52414A43

Binary file format constants.

◆ COOKIE_BINARY_VERSION

#define COOKIE_BINARY_VERSION   1

◆ COOKIE_CLEANUP_INTERVAL

#define COOKIE_CLEANUP_INTERVAL   3600

◆ COOKIE_HASH_TABLE_SIZE

#define COOKIE_HASH_TABLE_SIZE   256

◆ COOKIE_MAX_LINE_LENGTH

#define COOKIE_MAX_LINE_LENGTH   8192

◆ COOKIE_NETSCAPE_HEADER

#define COOKIE_NETSCAPE_HEADER    "# Netscape HTTP Cookie File\n# This is a generated file! Do not edit.\n\n"

◆ RFC1123_DATE_FORMAT

#define RFC1123_DATE_FORMAT   "%a, %d %b %Y %H:%M:%S GMT"

Standard date formats for cookie expiration parsing.

◆ RFC850_DATE_FORMAT

#define RFC850_DATE_FORMAT   "%A, %d-%b-%y %H:%M:%S GMT"

Function Documentation

◆ add_cookie_to_bucket()

static bool add_cookie_to_bucket ( cookie_domain_bucket_t * bucket,
cookie_t * cookie )
static

Add cookie to domain bucket.

Parameters
bucketDomain bucket to add cookie to
cookieCookie to add
Returns
true on success

◆ calculate_binary_size()

static size_t calculate_binary_size ( const cookie_jar_t * jar,
uint32_t * cookie_count )
static

Calculate total size needed for binary serialization.

Parameters
jarCookie jar to serialize
cookie_countOutput parameter for number of cookies to save
Returns
Total buffer size needed

◆ create_domain_bucket()

static cookie_domain_bucket_t * create_domain_bucket ( cookie_jar_t * jar,
const char * domain )
static

Create new domain bucket and add to hash table.

Parameters
jarCookie jar to add bucket to
domainDomain for the new bucket
Returns
Pointer to new bucket, or NULL on memory error

◆ deserialize_from_buffer()

static bool deserialize_from_buffer ( cookie_jar_t * jar,
const char * buffer,
size_t buffer_size )
static

Deserialize cookie jar from binary buffer.

Parameters
jarCookie jar to populate
bufferBuffer containing serialized data
buffer_sizeSize of the buffer
Returns
true on successful deserialization

◆ find_domain_bucket()

static cookie_domain_bucket_t * find_domain_bucket ( const cookie_jar_t * jar,
const char * domain )
static

Find domain bucket in hash table.

Parameters
jarCookie jar to search
domainDomain to find bucket for
Returns
Pointer to domain bucket, or NULL if not found

◆ hash_domain()

static uint32_t hash_domain ( const char * domain)
static

Calculate FNV-1a hash for domain strings.

Uses the FNV-1a hash algorithm for consistent domain hashing. Converts to lowercase for case-insensitive domain matching.

Parameters
domainDomain string to hash
Returns
32-bit hash value

◆ parse_cookie_date()

static time_t parse_cookie_date ( const char * date_str)
static

Parse cookie expiration date from various formats.

Supports RFC 1123, RFC 850, and asctime date formats as specified in RFC 6265. Handles Y2K issues in RFC 850 format.

Parameters
date_strDate string to parse
Returns
Unix timestamp, or 0 if parsing failed

◆ remove_cookie_from_bucket()

static bool remove_cookie_from_bucket ( cookie_domain_bucket_t * bucket,
cookie_t * cookie )
static

Remove cookie from its domain bucket.

Parameters
bucketDomain bucket containing the cookie
cookieCookie to remove
Returns
true if cookie was found and removed

◆ serialize_to_buffer()

static bool serialize_to_buffer ( const cookie_jar_t * jar,
char * buffer,
size_t buffer_size )
static

Serialize cookie jar to binary buffer.

Parameters
jarCookie jar to serialize
bufferPre-allocated buffer to write to
buffer_sizeSize of the buffer
Returns
true on successful serialization

◆ trim_whitespace()

static char * trim_whitespace ( char * str)
static

Remove leading and trailing whitespace from a string.

Modifies the string in place by moving content and null-terminating.

Parameters
strString to trim (modified in place)
Returns
Pointer to the trimmed string (same as input)

◆ validate_cookie_domain()

static bool validate_cookie_domain ( const char * cookie_domain,
const char * request_host )
static

Validate cookie domain against request host.

Implements domain validation rules from RFC 6265, including:

  • ASCII-only domains
  • No percent-encoding
  • Proper subdomain matching for dot-prefixed domains
Parameters
cookie_domainDomain attribute from cookie
request_hostHost from request URL
Returns
true if domain is valid for the request host

◆ validate_cookie_prefix()

static bool validate_cookie_prefix ( const char * name,
const cookie_t * cookie,
bool is_secure_request )
static

Validate cookie name prefix security attributes.

Implements __Secure- and __Host- prefix validation from RFC 6265bis.

Parameters
nameCookie name to validate
cookieCookie with security attributes
is_secure_requestWhether the request was made over HTTPS
Returns
true if prefix requirements are met