5 Module N_COOKIES
gullradriel edited this page 2026-10-09 00:55:18 +02:00

COOKIES: domain-aware HTTP cookie jar

Structures

Name
struct N_COOKIE_JAR
a thread-safe cookie jar
struct N_COOKIE
a single stored cookie

Functions

Name
size_t n_cookiejar_remove(N_COOKIE_JAR * jar, const char * name, const char * domain, const char * path)
delete one stored cookie: name must match exactly, domain (case-insensitive) and path narrow it further, and either may be NULL to match any.
N_COOKIE_JAR * n_cookiejar_new(void )
create an empty cookie jar; free with n_cookiejar_free.
void n_cookiejar_free(N_COOKIE_JAR ** jar)
free a cookie jar and all its cookies, then NULL the pointer
size_t n_cookiejar_count(N_COOKIE_JAR * jar)
number of cookies currently stored
void n_cookiejar_clear(N_COOKIE_JAR * jar)
drop every stored cookie
N_STR * n_cookiejar_build_header(N_COOKIE_JAR * jar, const char * domain, const char * path, int is_secure)
build a "name=value; name2=value2" Cookie header value for a request (domain/path/secure/expiry matched); free with free_nstr.
void n_cookiejar_add_from_header(N_COOKIE_JAR * jar, const char * set_cookie_value, const char * request_domain)
parse one Set-Cookie value (without the "Set-Cookie:" name) and store it; request_domain/path supply the defaults

Functions Documentation

function n_cookiejar_remove

size_t n_cookiejar_remove(
    N_COOKIE_JAR * jar,
    const char * name,
    const char * domain,
    const char * path
)

delete one stored cookie: name must match exactly, domain (case-insensitive) and path narrow it further, and either may be NULL to match any.

Returns how many cookies were deleted — 0 when nothing matched, and at most 1 when name+domain are both given, since that pair is what n_cookiejar_add_from_header keeps unique. Enumerate jar->cookies (a LIST of N_COOKIE*) to pick the one to delete.

function n_cookiejar_new

N_COOKIE_JAR * n_cookiejar_new(
    void 
)

create an empty cookie jar; free with n_cookiejar_free.

Returns NULL on error.

function n_cookiejar_free

void n_cookiejar_free(
    N_COOKIE_JAR ** jar
)

free a cookie jar and all its cookies, then NULL the pointer

function n_cookiejar_count

size_t n_cookiejar_count(
    N_COOKIE_JAR * jar
)

number of cookies currently stored

function n_cookiejar_clear

void n_cookiejar_clear(
    N_COOKIE_JAR * jar
)

drop every stored cookie

function n_cookiejar_build_header

N_STR * n_cookiejar_build_header(
    N_COOKIE_JAR * jar,
    const char * domain,
    const char * path,
    int is_secure
)

build a "name=value; name2=value2" Cookie header value for a request (domain/path/secure/expiry matched); free with free_nstr.

Returns NULL when no cookie matches.

function n_cookiejar_add_from_header

void n_cookiejar_add_from_header(
    N_COOKIE_JAR * jar,
    const char * set_cookie_value,
    const char * request_domain
)

parse one Set-Cookie value (without the "Set-Cookie:" name) and store it; request_domain/path supply the defaults