cwist
Modern C web framework with libttak runtime
Loading...
Searching...
No Matches
cookie.h File Reference

HTTP cookie parsing and construction helpers. More...

#include <cwist/net/http/http.h>
#include <cwist/query.h>
#include <stdbool.h>
Include dependency graph for cookie.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  cwist_cookie_options
 Options for setting a response cookie. More...
 

Typedefs

typedef struct cwist_cookie_options cwist_cookie_options
 Options for setting a response cookie.
 

Functions

void cwist_cookie_parse (cwist_query_map *map, const char *header)
 Parse a Cookie header value into a query-style map.
 
char * cwist_cookie_encode (const char *value)
 URL-encode a string for safe cookie values.
 
int cwist_cookie_decode (const char *in, char *out, size_t out_len)
 URL-decode a cookie value in-place into a caller-owned buffer.
 
int cwist_cookie_set (cwist_http_response *res, const char *name, const char *value, const cwist_cookie_options *opts)
 Add a Set-Cookie header to the response.
 
int cwist_cookie_delete (cwist_http_response *res, const char *name)
 Add a Set-Cookie header that deletes a cookie.
 
const char * cwist_cookie_get (cwist_query_map *map, const char *name)
 Get a cookie value by name from a parsed map.
 

Detailed Description

HTTP cookie parsing and construction helpers.

Definition in file cookie.h.

Typedef Documentation

◆ cwist_cookie_options

Options for setting a response cookie.

Function Documentation

◆ cwist_cookie_decode()

int cwist_cookie_decode ( const char *  in,
char *  out,
size_t  out_len 
)

URL-decode a cookie value in-place into a caller-owned buffer.

Parameters
inEncoded value.
outOutput buffer.
out_lenSize of output buffer.
Returns
Number of bytes written, or -1 on overflow.

Definition at line 55 of file cookie.c.

References cap.

Referenced by cwist_cookie_parse().

◆ cwist_cookie_delete()

int cwist_cookie_delete ( cwist_http_response *  res,
const char *  name 
)

Add a Set-Cookie header that deletes a cookie.

Definition at line 167 of file cookie.c.

References cap, cwist_sstring_append(), cwist_sstring_create(), and cwist_sstring_destroy().

Referenced by cwist_session_commit().

◆ cwist_cookie_encode()

char * cwist_cookie_encode ( const char *  value)

URL-encode a string for safe cookie values.

Returns
Heap-encoded string, or NULL on allocation failure. Caller frees.

Definition at line 35 of file cookie.c.

References cap, and cwist_alloc().

Referenced by cwist_cookie_set().

◆ cwist_cookie_get()

const char * cwist_cookie_get ( cwist_query_map *  map,
const char *  name 
)

Get a cookie value by name from a parsed map.

Returns
Pointer to value, or NULL if not found.

Definition at line 114 of file cookie.c.

References cap, and cwist_query_map_get().

Referenced by cwist_session_start().

◆ cwist_cookie_parse()

void cwist_cookie_parse ( cwist_query_map *  map,
const char *  header 
)

Parse a Cookie header value into a query-style map.

Handles "name1=value1; name2=value2" format. Values are URL-decoded.

Parameters
mapMap to populate. Must be created by cwist_query_map_create().
headerRaw Cookie header value (may be NULL).

Definition at line 80 of file cookie.c.

References cap, cwist_cookie_decode(), cwist_free(), cwist_query_map_set(), and cwist_strdup().

Referenced by cwist_session_start().

◆ cwist_cookie_set()

int cwist_cookie_set ( cwist_http_response *  res,
const char *  name,
const char *  value,
const cwist_cookie_options *  opts 
)

Add a Set-Cookie header to the response.

Parameters
resResponse to modify.
nameCookie name.
valueCookie value (will be encoded).
optsOptional attributes. May be NULL for defaults.
Returns
0 on success, -1 on failure.

Definition at line 119 of file cookie.c.

References cap, cwist_cookie_encode(), cwist_free(), cwist_sstring_append(), cwist_sstring_create(), cwist_sstring_destroy(), cwist_strdup(), cwist_cookie_options::domain, cwist_cookie_options::http_only, cwist_cookie_options::max_age_seconds, cwist_cookie_options::path, cwist_cookie_options::same_site, and cwist_cookie_options::secure.

Referenced by cwist_session_commit().