|
cwist
Modern C web framework with libttak runtime
|
Header: <cwist/net/http/cookie.h>
Helpers for reading the request Cookie header and writing Set-Cookie response headers. Parsed cookies are stored in a cwist_query_map (see the Query & URI API).
Parses a Cookie header value of the form name1=value1; name2=value2 and stores each pair in map, which must come from cwist_query_map_create().
cwist_cookie_decode.=, with an empty name, or whose decoded value does not fit in 4096 bytes are skipped.NULL or empty header, or a NULL map, does nothing. Returns the decoded value stored for name, or NULL if there is none. The pointer belongs to map and is valid until the map is destroyed.
Attributes for cwist_cookie_set. A NULL path, domain or same_site is left out, and http_only and secure are added only when true.
max_age_seconds is written whenever it is 0 or greater, so a zero-initialised struct produces Max-Age=0, which tells the client to delete the cookie. Set it to -1 to omit the attribute. same_site is copied as given and is not checked against the three listed values.
Adds a Set-Cookie header to res in the form name=<encoded value>; Path=...; Domain=...; Max-Age=...; HttpOnly; Secure; SameSite=.... The value is encoded with cwist_cookie_encode; the name and the attribute strings are written as given. A NULL value is sent as an empty value, and a NULL opts sends only name=value.
Returns 0 on success and -1 on failure: res or name is NULL, an allocation fails, or the header is rejected because it contains a CR or LF character.
Adds a Set-Cookie: name=; Path=/; Max-Age=0 header, which tells the client to remove the cookie. The path is fixed to /, so a cookie that was set with a different path is not matched by this header. Returns 0 on success and -1 on failure.
Percent-encodes value. The characters A-Z a-z 0-9 - _ . ~ are kept and every other byte becomes XX with upper-case hex digits (a space becomes %20). Returns a heap string that the caller releases with cwist_free(), or NULL if value is NULL or allocation fails.
Decodes in into the caller's buffer out of size out_len. XX sequences (upper or lower case hex) become the byte they name, and + becomes a space. A % that is not followed by two hex digits is copied unchanged.
Returns the number of bytes written, not counting the terminating \0, or -1 if an argument is NULL, out_len is 0, or the result does not fit in out together with the terminating \0.
Read a cookie from the request and set one on the response:
See tests/test_cookie.c for the inputs and outputs of each function.