OCaml HTTP cookie handling library with support for Eio-based storage jars

rfc docs

Changed files
+126 -7
lib
+79 -2
lib/core/cookeio.mli
···
- IP addresses require exact match only
- Path matching requires exact match or prefix with "/" separator
-
@see <https://datatracker.ietf.org/doc/html/rfc6265> RFC 6265 - HTTP State Management Mechanism *)
+
@see <https://datatracker.ietf.org/doc/html/rfc6265> RFC 6265 - HTTP State Management Mechanism
+
+
{2 Standards and References}
+
+
This library implements and references the following IETF specifications:
+
+
{ul
+
{- {{:https://datatracker.ietf.org/doc/html/rfc6265}RFC 6265} -
+
HTTP State Management Mechanism (April 2011) - Primary specification}
+
{- {{:https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis}RFC 6265bis} -
+
Cookies: HTTP State Management Mechanism (Draft) - SameSite attribute and modern updates}
+
{- {{:https://datatracker.ietf.org/doc/html/rfc1034#section-3.5}RFC 1034 Section 3.5} -
+
Domain Names - Preferred Name Syntax for domain validation}
+
{- {{:https://datatracker.ietf.org/doc/html/rfc2616#section-2.2}RFC 2616 Section 2.2} -
+
HTTP/1.1 - Token syntax definition}
+
{- {{:https://datatracker.ietf.org/doc/html/rfc1123#section-5.2.14}RFC 1123 Section 5.2.14} -
+
Internet Host Requirements - Date format (rfc1123-date)}}
+
+
Additional standards:
+
{ul
+
{- {{:https://publicsuffix.org/}Mozilla Public Suffix List} - Registry
+
of public suffixes for cookie domain validation per RFC 6265 Section 5.3 Step 5}} *)
(** {1 Types} *)
···
Validation functions for cookie names, values, and attributes per
{{:https://datatracker.ietf.org/doc/html/rfc6265#section-4.1.1} RFC 6265 Section 4.1.1}.
+
+
These functions implement the syntactic requirements from RFC 6265 to ensure
+
cookies conform to the specification before being sent in HTTP headers.
+
All validation failures return detailed error messages citing the specific
+
RFC requirement that was violated.
+
+
{2 Validation Philosophy}
+
+
Per RFC 6265 Section 4, there is an important distinction between:
+
- {b Server requirements} (Section 4.1): Strict syntax for generating Set-Cookie headers
+
- {b User agent requirements} (Section 5): Lenient parsing for receiving Set-Cookie headers
+
+
These validation functions enforce the {b server requirements}, ensuring that
+
cookies generated by this library conform to RFC 6265 syntax. When parsing
+
cookies from HTTP headers, the library may be more lenient to maximize
+
interoperability with non-compliant servers.
+
+
{2 Character Set Requirements}
+
+
RFC 6265 restricts cookies to US-ASCII characters with specific exclusions:
+
- Cookie names: RFC 2616 tokens (no CTLs, no separators)
+
- Cookie values: cookie-octet characters (0x21, 0x23-0x2B, 0x2D-0x3A, 0x3C-0x5B, 0x5D-0x7E)
+
- Domain values: RFC 1034 domain name syntax or IP addresses
+
- Path values: Any character except CTLs and semicolon
+
These functions return [Ok value] on success or [Error msg] with a detailed
explanation of why validation failed.
···
Cookie names must be valid RFC 2616 tokens: one or more characters
excluding control characters and separators.
+
Per {{:https://datatracker.ietf.org/doc/html/rfc2616#section-2.2}RFC 2616 Section 2.2},
+
a token is defined as: one or more characters excluding control characters
+
and the following 19 separator characters: parentheses, angle brackets, at-sign,
+
comma, semicolon, colon, backslash, double-quote, forward slash, square brackets,
+
question mark, equals, curly braces, space, and horizontal tab.
+
+
This means tokens consist of visible ASCII characters (33-126) excluding
+
control characters (0-31, 127) and the separator characters listed above.
+
@param name The cookie name to validate
@return [Ok name] if valid, [Error message] with explanation if invalid
-
@see <https://datatracker.ietf.org/doc/html/rfc6265#section-4.1.1> RFC 6265 Section 4.1.1 *)
+
@see <https://datatracker.ietf.org/doc/html/rfc6265#section-4.1.1> RFC 6265 Section 4.1.1
+
@see <https://datatracker.ietf.org/doc/html/rfc2616#section-2.2> RFC 2616 Section 2.2 - Basic Rules *)
val cookie_value : string -> (string, string) result
(** Validate a cookie value per RFC 6265.
···
double quotes. Invalid characters include: control characters, space,
double quote (except as wrapper), comma, semicolon, and backslash.
+
Per {{:https://datatracker.ietf.org/doc/html/rfc6265#section-4.1.1}RFC 6265 Section 4.1.1},
+
cookie-value may be:
+
- Zero or more cookie-octet characters, or
+
- Double-quoted string containing cookie-octet characters
+
+
Where cookie-octet excludes: CTLs (0x00-0x1F, 0x7F), space (0x20),
+
double-quote (0x22), comma (0x2C), semicolon (0x3B), and backslash (0x5C).
+
+
Valid cookie-octet characters: 0x21, 0x23-0x2B, 0x2D-0x3A, 0x3C-0x5B, 0x5D-0x7E
+
@param value The cookie value to validate
@return [Ok value] if valid, [Error message] with explanation if invalid
···
- A valid domain name per RFC 1034 Section 3.5
- A valid IPv4 address
- A valid IPv6 address
+
+
Per {{:https://datatracker.ietf.org/doc/html/rfc1034#section-3.5}RFC 1034 Section 3.5},
+
preferred domain name syntax requires:
+
- Labels separated by dots
+
- Labels must start with a letter
+
- Labels must end with a letter or digit
+
- Labels may contain letters, digits, and hyphens
+
- Labels are case-insensitive
+
- Total length limited to 255 octets
+
+
Leading dots are stripped per RFC 6265 Section 5.2.3 before validation.
@param domain The domain value to validate (leading dot is stripped first)
@return [Ok domain] if valid, [Error message] with explanation if invalid
+47 -5
lib/jar/cookeio_jar.mli
···
- Delta tracking for Set-Cookie headers
- Mozilla format persistence for cross-tool compatibility
-
@see <https://datatracker.ietf.org/doc/html/rfc6265> RFC 6265 - HTTP State Management Mechanism *)
+
@see <https://datatracker.ietf.org/doc/html/rfc6265> RFC 6265 - HTTP State Management Mechanism
+
+
{2 Standards and References}
+
+
This cookie jar implements the storage model from:
+
+
{ul
+
{- {{:https://datatracker.ietf.org/doc/html/rfc6265#section-5.3}RFC 6265 Section 5.3} -
+
Storage Model - Cookie insertion, replacement, and expiration}
+
{- {{:https://datatracker.ietf.org/doc/html/rfc6265#section-5.4}RFC 6265 Section 5.4} -
+
The Cookie Header - Cookie retrieval and ordering}}
+
+
Key RFC 6265 requirements implemented:
+
{ul
+
{- Domain matching per {{:https://datatracker.ietf.org/doc/html/rfc6265#section-5.1.3}Section 5.1.3}}
+
{- Path matching per {{:https://datatracker.ietf.org/doc/html/rfc6265#section-5.1.4}Section 5.1.4}}
+
{- Cookie ordering per {{:https://datatracker.ietf.org/doc/html/rfc6265#section-5.4}Section 5.4 Step 2}}
+
{- Creation time preservation per {{:https://datatracker.ietf.org/doc/html/rfc6265#section-5.3}Section 5.3 Step 11.3}}} *)
type t
(** Cookie jar for storing and managing cookies.
···
Cookeio.t list
(** Get cookies applicable for a URL.
-
Returns all cookies that match the given domain and path, and satisfy the
-
secure flag requirement. Combines original and delta cookies, with delta
-
taking precedence. Excludes:
+
Implements the cookie retrieval algorithm from
+
{{:https://datatracker.ietf.org/doc/html/rfc6265#section-5.4}RFC 6265 Section 5.4}
+
for generating the Cookie header.
+
+
{3 Algorithm}
+
+
Per RFC 6265 Section 5.4, the user agent should:
+
1. Filter cookies by domain matching (Section 5.1.3)
+
2. Filter cookies by path matching (Section 5.1.4)
+
3. Filter out cookies with Secure attribute when request is non-secure
+
4. Filter out expired cookies
+
5. Sort remaining cookies (longer paths first, then by creation time)
+
6. Update last-access-time for retrieved cookies
+
+
This function implements all these steps, combining original and delta cookies
+
with delta taking precedence. Excludes:
- Removal cookies (empty value)
- Expired cookies (expiry-time in the past per Section 5.3)
+
- Secure cookies when [is_secure = false]
+
+
{3 Cookie Ordering}
Cookies are sorted per Section 5.4, Step 2:
- Cookies with longer paths are listed before cookies with shorter paths
- Among cookies with equal-length paths, cookies with earlier creation-times
are listed first
-
Also updates the last access time of matching cookies using the provided clock.
+
This ordering ensures more specific cookies take precedence.
+
+
{3 Matching Rules}
Domain matching follows {{:https://datatracker.ietf.org/doc/html/rfc6265#section-5.1.3} Section 5.1.3}:
- IP addresses require exact match only
- Hostnames support subdomain matching unless host-only flag is set
Path matching follows {{:https://datatracker.ietf.org/doc/html/rfc6265#section-5.1.4} Section 5.1.4}.
+
+
@param t Cookie jar
+
@param clock Clock for updating last-access-time
+
@param domain Request domain
+
@param path Request path
+
@param is_secure Whether the request is over a secure channel (HTTPS)
+
@return List of matching cookies, sorted per RFC 6265
@see <https://datatracker.ietf.org/doc/html/rfc6265#section-5.3> RFC 6265 Section 5.3 - Storage Model (expiry)
@see <https://datatracker.ietf.org/doc/html/rfc6265#section-5.4> RFC 6265 Section 5.4 - The Cookie Header *)