/* uhttp_cookie: a small fixed-size cookie jar for ring-3 HTTP clients. * * Deliberately minimal (v1): cookies are host-only (the Domain attribute is * ignored, so a cookie only goes back to the exact host that set it), the * Path attribute is honoured as a prefix match, Secure cookies are only sent * over https, and Max-Age<=0 or an Expires date in 1970 deletes the cookie. * Other expiry dates, SameSite and HttpOnly are ignored: the jar lives as * long as the process (or the config file it is exported to). * * Pure freestanding C with no includes, so uhttp_cookie_test.c exercises * the same file on the host. */ #ifndef UHTTP_COOKIE_H #define UHTTP_COOKIE_H /* Every OIDC login attempt adds four path-scoped interaction cookies that * live for an hour, and the browser shares one jar across all sites. */ #define UHTTP_COOKIE_MAX 48 #define UHTTP_COOKIE_HOST_MAX 128 #define UHTTP_COOKIE_PATH_MAX 160 #define UHTTP_COOKIE_NAME_MAX 64 #define UHTTP_COOKIE_VALUE_MAX 768 struct uhttp_cookie { char host[UHTTP_COOKIE_HOST_MAX]; char path[UHTTP_COOKIE_PATH_MAX]; char name[UHTTP_COOKIE_NAME_MAX]; char value[UHTTP_COOKIE_VALUE_MAX]; unsigned char used; unsigned char secure; unsigned int seq; /* insertion order, for sending */ unsigned int touch; /* last set/update, for eviction */ }; struct uhttp_jar { struct uhttp_cookie c[UHTTP_COOKIE_MAX]; unsigned int seq; unsigned int changes; /* bumped on every add/update/delete */ }; void uhttp_jar_init(struct uhttp_jar *jar); void uhttp_jar_clear(struct uhttp_jar *jar); /* Apply one Set-Cookie header value (without the "Set-Cookie:" name) from a * response to a request for host + path (the path may carry a query). * Returns 1 when the jar changed, 0 when nothing changed, -1 on a malformed * or oversized cookie. */ int uhttp_jar_set(struct uhttp_jar *jar, const char *host, const char *path, const char *set_cookie); /* Build the Cookie header value ("a=1; b=2") for a request. Returns its * length (0 when no cookie matches); the output is truncated at whole * cookies when it does not fit. */ unsigned int uhttp_jar_header(const struct uhttp_jar *jar, const char *host, const char *path, int secure, char *out, unsigned int cap); /* Import a saved Cookie header ("a=1; b=2") as host-only cookies with * Path=/ for host. Returns the number of cookies stored. */ int uhttp_jar_import(struct uhttp_jar *jar, const char *host, int secure, const char *header); /* Number of cookies currently stored for host. */ unsigned int uhttp_jar_count(const struct uhttp_jar *jar, const char *host); #endif