uhttp_cookie.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /* uhttp_cookie: a small fixed-size cookie jar for ring-3 HTTP clients.
  2. *
  3. * Deliberately minimal (v1): cookies are host-only (the Domain attribute is
  4. * ignored, so a cookie only goes back to the exact host that set it), the
  5. * Path attribute is honoured as a prefix match, Secure cookies are only sent
  6. * over https, and Max-Age<=0 or an Expires date in 1970 deletes the cookie.
  7. * Other expiry dates, SameSite and HttpOnly are ignored: the jar lives as
  8. * long as the process (or the config file it is exported to).
  9. *
  10. * Pure freestanding C with no includes, so uhttp_cookie_test.c exercises
  11. * the same file on the host. */
  12. #ifndef UHTTP_COOKIE_H
  13. #define UHTTP_COOKIE_H
  14. /* Every OIDC login attempt adds four path-scoped interaction cookies that
  15. * live for an hour, and the browser shares one jar across all sites. */
  16. #define UHTTP_COOKIE_MAX 48
  17. #define UHTTP_COOKIE_HOST_MAX 128
  18. #define UHTTP_COOKIE_PATH_MAX 160
  19. #define UHTTP_COOKIE_NAME_MAX 64
  20. #define UHTTP_COOKIE_VALUE_MAX 768
  21. struct uhttp_cookie {
  22. char host[UHTTP_COOKIE_HOST_MAX];
  23. char path[UHTTP_COOKIE_PATH_MAX];
  24. char name[UHTTP_COOKIE_NAME_MAX];
  25. char value[UHTTP_COOKIE_VALUE_MAX];
  26. unsigned char used;
  27. unsigned char secure;
  28. unsigned int seq; /* insertion order, for sending */
  29. unsigned int touch; /* last set/update, for eviction */
  30. };
  31. struct uhttp_jar {
  32. struct uhttp_cookie c[UHTTP_COOKIE_MAX];
  33. unsigned int seq;
  34. unsigned int changes; /* bumped on every add/update/delete */
  35. };
  36. void uhttp_jar_init(struct uhttp_jar *jar);
  37. void uhttp_jar_clear(struct uhttp_jar *jar);
  38. /* Apply one Set-Cookie header value (without the "Set-Cookie:" name) from a
  39. * response to a request for host + path (the path may carry a query).
  40. * Returns 1 when the jar changed, 0 when nothing changed, -1 on a malformed
  41. * or oversized cookie. */
  42. int uhttp_jar_set(struct uhttp_jar *jar, const char *host, const char *path,
  43. const char *set_cookie);
  44. /* Build the Cookie header value ("a=1; b=2") for a request. Returns its
  45. * length (0 when no cookie matches); the output is truncated at whole
  46. * cookies when it does not fit. */
  47. unsigned int uhttp_jar_header(const struct uhttp_jar *jar, const char *host,
  48. const char *path, int secure, char *out,
  49. unsigned int cap);
  50. /* Import a saved Cookie header ("a=1; b=2") as host-only cookies with
  51. * Path=/ for host. Returns the number of cookies stored. */
  52. int uhttp_jar_import(struct uhttp_jar *jar, const char *host, int secure,
  53. const char *header);
  54. /* Number of cookies currently stored for host. */
  55. unsigned int uhttp_jar_count(const struct uhttp_jar *jar, const char *host);
  56. #endif