uhttp_cookie.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. #define UHTTP_COOKIE_MAX 24
  15. #define UHTTP_COOKIE_HOST_MAX 128
  16. #define UHTTP_COOKIE_PATH_MAX 160
  17. #define UHTTP_COOKIE_NAME_MAX 64
  18. #define UHTTP_COOKIE_VALUE_MAX 768
  19. struct uhttp_cookie {
  20. char host[UHTTP_COOKIE_HOST_MAX];
  21. char path[UHTTP_COOKIE_PATH_MAX];
  22. char name[UHTTP_COOKIE_NAME_MAX];
  23. char value[UHTTP_COOKIE_VALUE_MAX];
  24. unsigned char used;
  25. unsigned char secure;
  26. unsigned int seq; /* insertion order, for sending */
  27. };
  28. struct uhttp_jar {
  29. struct uhttp_cookie c[UHTTP_COOKIE_MAX];
  30. unsigned int seq;
  31. unsigned int changes; /* bumped on every add/update/delete */
  32. };
  33. void uhttp_jar_init(struct uhttp_jar *jar);
  34. void uhttp_jar_clear(struct uhttp_jar *jar);
  35. /* Apply one Set-Cookie header value (without the "Set-Cookie:" name) from a
  36. * response to a request for host + path (the path may carry a query).
  37. * Returns 1 when the jar changed, 0 when nothing changed, -1 on a malformed
  38. * or oversized cookie. */
  39. int uhttp_jar_set(struct uhttp_jar *jar, const char *host, const char *path,
  40. const char *set_cookie);
  41. /* Build the Cookie header value ("a=1; b=2") for a request. Returns its
  42. * length (0 when no cookie matches); the output is truncated at whole
  43. * cookies when it does not fit. */
  44. unsigned int uhttp_jar_header(const struct uhttp_jar *jar, const char *host,
  45. const char *path, int secure, char *out,
  46. unsigned int cap);
  47. /* Import a saved Cookie header ("a=1; b=2") as host-only cookies with
  48. * Path=/ for host. Returns the number of cookies stored. */
  49. int uhttp_jar_import(struct uhttp_jar *jar, const char *host, int secure,
  50. const char *header);
  51. /* Number of cookies currently stored for host. */
  52. unsigned int uhttp_jar_count(const struct uhttp_jar *jar, const char *host);
  53. #endif