···4545 - IP addresses require exact match only
4646 - Path matching requires exact match or prefix with "/" separator
47474848- @see <https://datatracker.ietf.org/doc/html/rfc6265> RFC 6265 - HTTP State Management Mechanism *)
4848+ @see <https://datatracker.ietf.org/doc/html/rfc6265> RFC 6265 - HTTP State Management Mechanism
4949+5050+ {2 Standards and References}
5151+5252+ This library implements and references the following IETF specifications:
5353+5454+ {ul
5555+ {- {{:https://datatracker.ietf.org/doc/html/rfc6265}RFC 6265} -
5656+ HTTP State Management Mechanism (April 2011) - Primary specification}
5757+ {- {{:https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis}RFC 6265bis} -
5858+ Cookies: HTTP State Management Mechanism (Draft) - SameSite attribute and modern updates}
5959+ {- {{:https://datatracker.ietf.org/doc/html/rfc1034#section-3.5}RFC 1034 Section 3.5} -
6060+ Domain Names - Preferred Name Syntax for domain validation}
6161+ {- {{:https://datatracker.ietf.org/doc/html/rfc2616#section-2.2}RFC 2616 Section 2.2} -
6262+ HTTP/1.1 - Token syntax definition}
6363+ {- {{:https://datatracker.ietf.org/doc/html/rfc1123#section-5.2.14}RFC 1123 Section 5.2.14} -
6464+ Internet Host Requirements - Date format (rfc1123-date)}}
6565+6666+ Additional standards:
6767+ {ul
6868+ {- {{:https://publicsuffix.org/}Mozilla Public Suffix List} - Registry
6969+ of public suffixes for cookie domain validation per RFC 6265 Section 5.3 Step 5}} *)
49705071(** {1 Types} *)
5172···265286266287 Validation functions for cookie names, values, and attributes per
267288 {{:https://datatracker.ietf.org/doc/html/rfc6265#section-4.1.1} RFC 6265 Section 4.1.1}.
289289+290290+ These functions implement the syntactic requirements from RFC 6265 to ensure
291291+ cookies conform to the specification before being sent in HTTP headers.
292292+ All validation failures return detailed error messages citing the specific
293293+ RFC requirement that was violated.
294294+295295+ {2 Validation Philosophy}
296296+297297+ Per RFC 6265 Section 4, there is an important distinction between:
298298+ - {b Server requirements} (Section 4.1): Strict syntax for generating Set-Cookie headers
299299+ - {b User agent requirements} (Section 5): Lenient parsing for receiving Set-Cookie headers
300300+301301+ These validation functions enforce the {b server requirements}, ensuring that
302302+ cookies generated by this library conform to RFC 6265 syntax. When parsing
303303+ cookies from HTTP headers, the library may be more lenient to maximize
304304+ interoperability with non-compliant servers.
305305+306306+ {2 Character Set Requirements}
307307+308308+ RFC 6265 restricts cookies to US-ASCII characters with specific exclusions:
309309+ - Cookie names: RFC 2616 tokens (no CTLs, no separators)
310310+ - Cookie values: cookie-octet characters (0x21, 0x23-0x2B, 0x2D-0x3A, 0x3C-0x5B, 0x5D-0x7E)
311311+ - Domain values: RFC 1034 domain name syntax or IP addresses
312312+ - Path values: Any character except CTLs and semicolon
313313+268314 These functions return [Ok value] on success or [Error msg] with a detailed
269315 explanation of why validation failed.
270316···277323 Cookie names must be valid RFC 2616 tokens: one or more characters
278324 excluding control characters and separators.
279325326326+ Per {{:https://datatracker.ietf.org/doc/html/rfc2616#section-2.2}RFC 2616 Section 2.2},
327327+ a token is defined as: one or more characters excluding control characters
328328+ and the following 19 separator characters: parentheses, angle brackets, at-sign,
329329+ comma, semicolon, colon, backslash, double-quote, forward slash, square brackets,
330330+ question mark, equals, curly braces, space, and horizontal tab.
331331+332332+ This means tokens consist of visible ASCII characters (33-126) excluding
333333+ control characters (0-31, 127) and the separator characters listed above.
334334+280335 @param name The cookie name to validate
281336 @return [Ok name] if valid, [Error message] with explanation if invalid
282337283283- @see <https://datatracker.ietf.org/doc/html/rfc6265#section-4.1.1> RFC 6265 Section 4.1.1 *)
338338+ @see <https://datatracker.ietf.org/doc/html/rfc6265#section-4.1.1> RFC 6265 Section 4.1.1
339339+ @see <https://datatracker.ietf.org/doc/html/rfc2616#section-2.2> RFC 2616 Section 2.2 - Basic Rules *)
284340285341 val cookie_value : string -> (string, string) result
286342 (** Validate a cookie value per RFC 6265.
···289345 double quotes. Invalid characters include: control characters, space,
290346 double quote (except as wrapper), comma, semicolon, and backslash.
291347348348+ Per {{:https://datatracker.ietf.org/doc/html/rfc6265#section-4.1.1}RFC 6265 Section 4.1.1},
349349+ cookie-value may be:
350350+ - Zero or more cookie-octet characters, or
351351+ - Double-quoted string containing cookie-octet characters
352352+353353+ Where cookie-octet excludes: CTLs (0x00-0x1F, 0x7F), space (0x20),
354354+ double-quote (0x22), comma (0x2C), semicolon (0x3B), and backslash (0x5C).
355355+356356+ Valid cookie-octet characters: 0x21, 0x23-0x2B, 0x2D-0x3A, 0x3C-0x5B, 0x5D-0x7E
357357+292358 @param value The cookie value to validate
293359 @return [Ok value] if valid, [Error message] with explanation if invalid
294360···301367 - A valid domain name per RFC 1034 Section 3.5
302368 - A valid IPv4 address
303369 - A valid IPv6 address
370370+371371+ Per {{:https://datatracker.ietf.org/doc/html/rfc1034#section-3.5}RFC 1034 Section 3.5},
372372+ preferred domain name syntax requires:
373373+ - Labels separated by dots
374374+ - Labels must start with a letter
375375+ - Labels must end with a letter or digit
376376+ - Labels may contain letters, digits, and hyphens
377377+ - Labels are case-insensitive
378378+ - Total length limited to 255 octets
379379+380380+ Leading dots are stripped per RFC 6265 Section 5.2.3 before validation.
304381305382 @param domain The domain value to validate (leading dot is stripped first)
306383 @return [Ok domain] if valid, [Error message] with explanation if invalid
+47-5
lib/jar/cookeio_jar.mli
···1818 - Delta tracking for Set-Cookie headers
1919 - Mozilla format persistence for cross-tool compatibility
20202121- @see <https://datatracker.ietf.org/doc/html/rfc6265> RFC 6265 - HTTP State Management Mechanism *)
2121+ @see <https://datatracker.ietf.org/doc/html/rfc6265> RFC 6265 - HTTP State Management Mechanism
2222+2323+ {2 Standards and References}
2424+2525+ This cookie jar implements the storage model from:
2626+2727+ {ul
2828+ {- {{:https://datatracker.ietf.org/doc/html/rfc6265#section-5.3}RFC 6265 Section 5.3} -
2929+ Storage Model - Cookie insertion, replacement, and expiration}
3030+ {- {{:https://datatracker.ietf.org/doc/html/rfc6265#section-5.4}RFC 6265 Section 5.4} -
3131+ The Cookie Header - Cookie retrieval and ordering}}
3232+3333+ Key RFC 6265 requirements implemented:
3434+ {ul
3535+ {- Domain matching per {{:https://datatracker.ietf.org/doc/html/rfc6265#section-5.1.3}Section 5.1.3}}
3636+ {- Path matching per {{:https://datatracker.ietf.org/doc/html/rfc6265#section-5.1.4}Section 5.1.4}}
3737+ {- Cookie ordering per {{:https://datatracker.ietf.org/doc/html/rfc6265#section-5.4}Section 5.4 Step 2}}
3838+ {- Creation time preservation per {{:https://datatracker.ietf.org/doc/html/rfc6265#section-5.3}Section 5.3 Step 11.3}}} *)
22392340type t
2441(** Cookie jar for storing and managing cookies.
···101118 Cookeio.t list
102119(** Get cookies applicable for a URL.
103120104104- Returns all cookies that match the given domain and path, and satisfy the
105105- secure flag requirement. Combines original and delta cookies, with delta
106106- taking precedence. Excludes:
121121+ Implements the cookie retrieval algorithm from
122122+ {{:https://datatracker.ietf.org/doc/html/rfc6265#section-5.4}RFC 6265 Section 5.4}
123123+ for generating the Cookie header.
124124+125125+ {3 Algorithm}
126126+127127+ Per RFC 6265 Section 5.4, the user agent should:
128128+ 1. Filter cookies by domain matching (Section 5.1.3)
129129+ 2. Filter cookies by path matching (Section 5.1.4)
130130+ 3. Filter out cookies with Secure attribute when request is non-secure
131131+ 4. Filter out expired cookies
132132+ 5. Sort remaining cookies (longer paths first, then by creation time)
133133+ 6. Update last-access-time for retrieved cookies
134134+135135+ This function implements all these steps, combining original and delta cookies
136136+ with delta taking precedence. Excludes:
107137 - Removal cookies (empty value)
108138 - Expired cookies (expiry-time in the past per Section 5.3)
139139+ - Secure cookies when [is_secure = false]
140140+141141+ {3 Cookie Ordering}
109142110143 Cookies are sorted per Section 5.4, Step 2:
111144 - Cookies with longer paths are listed before cookies with shorter paths
112145 - Among cookies with equal-length paths, cookies with earlier creation-times
113146 are listed first
114147115115- Also updates the last access time of matching cookies using the provided clock.
148148+ This ordering ensures more specific cookies take precedence.
149149+150150+ {3 Matching Rules}
116151117152 Domain matching follows {{:https://datatracker.ietf.org/doc/html/rfc6265#section-5.1.3} Section 5.1.3}:
118153 - IP addresses require exact match only
119154 - Hostnames support subdomain matching unless host-only flag is set
120155121156 Path matching follows {{:https://datatracker.ietf.org/doc/html/rfc6265#section-5.1.4} Section 5.1.4}.
157157+158158+ @param t Cookie jar
159159+ @param clock Clock for updating last-access-time
160160+ @param domain Request domain
161161+ @param path Request path
162162+ @param is_secure Whether the request is over a secure channel (HTTPS)
163163+ @return List of matching cookies, sorted per RFC 6265
122164123165 @see <https://datatracker.ietf.org/doc/html/rfc6265#section-5.3> RFC 6265 Section 5.3 - Storage Model (expiry)
124166 @see <https://datatracker.ietf.org/doc/html/rfc6265#section-5.4> RFC 6265 Section 5.4 - The Cookie Header *)