### Pretty print date time Source: https://github.com/bikallem/http-cookie/blob/master/docs/http-cookie/Http_cookie/index.html Example output of the date time formatter. ```text Sun, 06 Nov 1994 08:49:37 GMT ``` -------------------------------- ### Serialize to Set-Cookie header Source: https://github.com/bikallem/http-cookie/blob/master/docs/http-cookie/Http_cookie/index.html Example of a string returned by the to_set_cookie function. ```text SID=31d4d96e407aad42; Path=/; Secure; HttpOnly; Expires=Sun, 06 Nov 1994 08:49:37 GMT ``` -------------------------------- ### Serialize to Cookie header Source: https://github.com/bikallem/http-cookie/blob/master/docs/http-cookie/Http_cookie/index.html Example of a string returned by the to_cookie function. ```text SID=31d4d96e407aad42 ``` -------------------------------- ### Get Cookie SameSite Attribute Source: https://github.com/bikallem/http-cookie/blob/master/docs/http-cookies/Http_cookies__Cookie/index.html Retrieves the same_site attribute of a cookie. Refer to the draft IETF document for same-site specifications. ```ocaml same_site t ``` -------------------------------- ### Get Cookie Value Source: https://github.com/bikallem/http-cookie/blob/master/docs/http-cookies/Http_cookies__Cookie/index.html Retrieves the value of a cookie. Refer to RFC 6265 section 4.1.1 for cookie value specifications. ```ocaml value t ``` -------------------------------- ### Get Cookie Path Source: https://github.com/bikallem/http-cookie/blob/master/docs/http-cookies/Http_cookies__Cookie/index.html Retrieves the path attribute of a cookie. Refer to RFC 6265 section 5.2.4 for cookie path specifications. ```ocaml path t ``` -------------------------------- ### Get Cookie Name Source: https://github.com/bikallem/http-cookie/blob/master/docs/http-cookies/Http_cookies__Cookie/index.html Retrieves the name of a cookie. Refer to RFC 6265 section 4.1.1 for cookie name specifications. ```ocaml name t ``` -------------------------------- ### Get Cookie Domain Source: https://github.com/bikallem/http-cookie/blob/master/docs/http-cookies/Http_cookies__Cookie/index.html Retrieves the domain attribute of a cookie. Refer to RFC 6265 section 4.1.2.3 for cookie domain specifications. ```ocaml domain t ``` -------------------------------- ### Get Cookie Extension Source: https://github.com/bikallem/http-cookie/blob/master/docs/http-cookies/Http_cookies__Cookie/index.html Retrieves any extension attributes from a cookie. Refer to RFC 6265 section 4.1.1 for cookie extension specifications. ```ocaml extension t ``` -------------------------------- ### Get Cookie Max-Age Attribute Source: https://github.com/bikallem/http-cookie/blob/master/docs/http-cookies/Http_cookies__Cookie/index.html Retrieves the max-age attribute of a cookie. Refer to RFC 6265 sections 4.1.2.2 and 4.1.1 for max-age specifications. ```ocaml max_age t ``` -------------------------------- ### Get Cookie HttpOnly Attribute Source: https://github.com/bikallem/http-cookie/blob/master/docs/http-cookies/Http_cookies__Cookie/index.html Retrieves the http_only attribute of a cookie. Refer to RFC 6265 section 4.1.2.6 for http-only specifications. ```ocaml http_only t ``` -------------------------------- ### Get Cookie Expires Attribute Source: https://github.com/bikallem/http-cookie/blob/master/docs/http-cookies/Http_cookies__Cookie/index.html Retrieves the expires attribute of a cookie. Refer to RFC 6265 section 4.1.2.1 for cookie expires specifications. ```ocaml expires t ``` -------------------------------- ### Get Cookie Secure Attribute Source: https://github.com/bikallem/http-cookie/blob/master/docs/http-cookies/Http_cookies__Cookie/index.html Retrieves the secure attribute of a cookie. Refer to RFC 6265 section 4.1.2.5 for cookie secure specifications. ```ocaml secure t ``` -------------------------------- ### Create and Pretty Print HTTP Cookie Source: https://github.com/bikallem/http-cookie/blob/master/README.md Shows how to create a new HTTP cookie with various attributes like path, domain, secure flag, same-site policy, name, and value, and then pretty prints the resulting cookie object. ```ocaml # Http_cookie.create ~path:"/home" ~domain:"eee:aaa:abdf::223:192.168.0.1" ~secure:true ~same_site:`Strict ~name:"SID" "31d4d96e407aad42" |> pp;; name: SID value: 31d4d96e407aad42 path: /home domain: eee:aaa:abdf::223:192.168.0.1 expires: max_age: secure: true http_only: true same_site: Strict extension: - : unit = () ``` -------------------------------- ### Create a Cookie Instance Source: https://github.com/bikallem/http-cookie/blob/master/docs/http-cookies/Http_cookies__Cookie/index.html Use this constructor to create a cookie instance with various attributes. It raises a Cookie exception if any attribute fails validation. ```ocaml val create : ?path:string -> ?domain:string -> ?expires:Unix.tm -> ?max_age:int -> ?secure:bool -> ?http_only:bool -> ?same_site:[Same_site.t](Same_site/index.html#type-t) -> ?extension:string -> string -> value:string -> [t](index.html#type-t) ``` -------------------------------- ### Pretty Print Cookies and Dates Source: https://context7.com/bikallem/http-cookie/llms.txt Debugging utilities for displaying cookie information and formatting date-time objects. ```ocaml (* Pretty print a cookie *) let cookie = Http_cookie.create ~path:"/home" ~domain:"example.com" ~secure:true ~same_site:`Strict ~name:"SID" "31d4d96e407aad42" |> Result.get_ok in Format.printf "%a\n" Http_cookie.pp cookie (* Output: name: SID value: 31d4d96e407aad42 path: /home domain: example.com expires: max_age: secure: true http_only: true same_site: Strict extension: *) (* Pretty print a date_time in RFC 1123 format *) let dt = Http_cookie.date_time ~year:2021 ~month:`Jan ~weekday:`Sun ~day:23 ~hour:22 ~minutes:45 ~seconds:59 |> Result.get_ok in Format.printf "%a\n" Http_cookie.pp_date_time dt (* Output: Sun, 23 Jan 2021 22:45:59 GMT *) (* pp_rfc1123 is an alias for pp_date_time *) Format.printf "%a\n" Http_cookie.pp_rfc1123 dt (* Output: Sun, 23 Jan 2021 22:45:59 GMT *) (* Pretty print same_site value *) Format.printf "%a\n" Http_cookie.pp_same_site `Strict (* Output: Strict *) ``` -------------------------------- ### Create HTTP Cookies Source: https://context7.com/bikallem/http-cookie/llms.txt Use Http_cookie.create to generate cookies. It validates inputs and returns a Result type. ```ocaml (* Create a basic cookie *) let basic_cookie = Http_cookie.create ~name:"session_id" "abc123xyz" (* Result: Ok { name="session_id"; value="abc123xyz"; http_only=true; ... } *) (* Create a cookie with all attributes *) let expires = Http_cookie.date_time ~year:2025 ~month:`Dec ~weekday:`Fri ~day:31 ~hour:23 ~minutes:59 ~seconds:59 |> Result.get_ok in let full_cookie = Http_cookie.create ~path:"/app" ~domain:"example.com" ~expires ~max_age:86400L ~secure:true ~http_only:true ~same_site:`Strict ~name:"auth_token" "secure_value_here" (* Result: Ok cookie with all attributes set *) (* Create a cookie with IPv6 domain *) let ipv6_cookie = Http_cookie.create ~path:"/home" ~domain:"eee:aaa:abdf::223:192.168.0.1" ~secure:true ~same_site:`Strict ~name:"SID" "31d4d96e407aad42" (* Result: Ok cookie - IPv6 addresses are validated *) (* Invalid cookie name returns error *) let invalid = Http_cookie.create ~name:"he@llo" "world" (* Result: Error "name: he@llo" - @ is not allowed in cookie names *) ``` -------------------------------- ### Parse and Format Set-Cookie Header Source: https://github.com/bikallem/http-cookie/blob/master/README.md Demonstrates parsing a Set-Cookie header string into a cookie object and then formatting it back into a string. Verifies that the original and formatted strings are identical. ```ocaml # let s = "SID=31d4d96e407aad42; Path=/; Domain=ffff::0234:ddd:192.168.0.1; Expires=Sun, 06 Nov 1994 08:49:37 GMT; Secure; HttpOnly" in let c = Http_cookie.of_set_cookie s |> Result.get_ok in let s2 = Http_cookie.to_set_cookie c in s = s2;; - : bool = true ``` -------------------------------- ### Parse and Format HTTP Cookie Header Source: https://github.com/bikallem/http-cookie/blob/master/README.md Demonstrates parsing a string into an HTTP cookie object and then formatting it back into a string. Verifies that the original and formatted strings are identical. ```ocaml # let s = "SID=234234asdasdasda" in let c = List.nth (Http_cookie.of_cookie s |> Result.get_ok) 0 in let s1 = Http_cookie.to_cookie c in s = s1 ;; - : bool = true ``` -------------------------------- ### Create Cookie Instance Source: https://github.com/bikallem/http-cookie/blob/master/docs/http-cookies/Http_cookies__Cookie/index.html Creates a new cookie instance with specified attributes and values. ```APIDOC ## val create ### Description Creates a cookie instance (t) with a name, value, and optional attributes like path, domain, expiration, and security flags. ### Parameters - **name** (string) - Required - The name of the cookie. - **value** (string) - Required - The value of the cookie. - **path** (string) - Optional - The path attribute. - **domain** (string) - Optional - The domain attribute. - **expires** (Unix.tm) - Optional - The expiration date. - **max_age** (int) - Optional - The max-age attribute. - **secure** (bool) - Optional - The secure flag. - **http_only** (bool) - Optional - The HttpOnly flag. - **same_site** (Same_site.t) - Optional - The SameSite attribute. - **extension** (string) - Optional - Additional cookie extensions. ### Response - **t** (type) - Returns a cookie instance. ``` -------------------------------- ### Create Cookie Source: https://github.com/bikallem/http-cookie/blob/master/docs/http-cookies/Http_cookies/Cookie/index.html Creates a new cookie instance with specified attributes and values. ```APIDOC ## val create ### Description Creates a cookie instance (t) with a name, value, and optional attributes like path, domain, expiration, and security flags. ### Parameters - **name** (string) - Required - The name of the cookie. - **value** (string) - Required - The value of the cookie. - **path** (string) - Optional - The path attribute. - **domain** (string) - Optional - The domain attribute. - **expires** (Unix.tm) - Optional - The expiration date. - **max_age** (int) - Optional - The max-age attribute. - **secure** (bool) - Optional - The secure flag. - **http_only** (bool) - Optional - The http_only flag. - **same_site** (Same_site.t) - Optional - The Same-site attribute. - **extension** (string) - Optional - Additional cookie extensions. ### Response - **t** (type) - A cookie instance. ``` -------------------------------- ### Compare Cookies and Dates Source: https://context7.com/bikallem/http-cookie/llms.txt Comparison functions for cookies and date-time objects, returning an integer indicating relative order. ```ocaml let cookie1 = Http_cookie.create ~name:"a" "value1" |> Result.get_ok in let cookie2 = Http_cookie.create ~name:"b" "value2" |> Result.get_ok in let cookie3 = Http_cookie.create ~name:"a" "value1" |> Result.get_ok in let cmp1 = Http_cookie.compare cookie1 cookie2 (* negative: "a" < "b" *) let cmp2 = Http_cookie.compare cookie1 cookie3 (* 0: equal cookies *) (* Compare date_time values *) let dt1 = Http_cookie.date_time ~year:2021 ~month:`Jan ~weekday:`Mon ~day:1 ~hour:12 ~minutes:0 ~seconds:0 |> Result.get_ok in let dt2 = Http_cookie.date_time ~year:2022 ~month:`Jan ~weekday:`Sat ~day:1 ~hour:12 ~minutes:0 ~seconds:0 |> Result.get_ok in let date_cmp = Http_cookie.compare_date_time dt1 dt2 (* negative: 2021 < 2022 *) ``` -------------------------------- ### Http_cookie Module Overview Source: https://github.com/bikallem/http-cookie/blob/master/docs/http-cookie/Http_cookie/index.html Provides an overview of the Http_cookie module, its purpose, and the standards it implements. ```APIDOC ## Http_cookie Module A comprehensive and standards compliant HTTP cookies library for ocaml. HTTP cookie is serialized as follows: * In a `Cookie` header in a HTTP request * In a `Set-Cookie` header in a HTTP response. The library supports consuming and creating HTTP cookie in both requests and responses. The standards implemented by the library is 1. [RFC 6265 - Cookies](https://tools.ietf.org/html/rfc6265). 2. [Section 3.3.1 - HTTP Date](https://datatracker.ietf.org/doc/html/rfc1123) 3. [Domain Name](https://datatracker.ietf.org/doc/html/rfc1034#section-3.5) 4. [Hosts](https://datatracker.ietf.org/doc/html/rfc1123#section-2.1) 5. [IPv4/IPv6 Address](https://datatracker.ietf.org/doc/html/draft-main-ipaddr-text-rep-02#section-3) * [Pretty Printers](#pretty-printers) * [Create/Decode/Encode](#create/decode/encode) * [Cookie Attributes](#cookie-attributes) * [Compare](#compare) * [Updates](#updates) ``` -------------------------------- ### Same_site Type and Utility Functions Source: https://github.com/bikallem/http-cookie/blob/master/docs/http-cookies/Http_cookies__Cookie/Same_site/index.html Defines the Same_site type and associated utility functions for comparison and string conversion. ```APIDOC ## Type: Same_site.t ### Description Represents the 'Same-site' cookie attribute as defined in IETF specifications. ### Variants - **Default** - The default browser behavior. - **None** - The cookie is sent in all contexts. - **Lax** - The cookie is withheld on cross-site subrequests but sent when navigating to the origin site. - **Strict** - The cookie is only sent in a first-party context. ## Functions ### val equal - **Signature**: t -> t -> bool - **Description**: Checks if two Same_site values are equal. ### val compare - **Signature**: t -> t -> int - **Description**: Compares two Same_site values. ### val to_string - **Signature**: t -> string - **Description**: Converts the Same_site value to its string representation. ``` -------------------------------- ### Compare Cookies Source: https://github.com/bikallem/http-cookie/blob/master/docs/http-cookies/Http_cookies/Cookie/index.html Compares two cookie objects and returns an integer indicating their relative order. ```ocaml compare c1 c2 ``` -------------------------------- ### Define Core Cookie Types Source: https://context7.com/bikallem/http-cookie/llms.txt The library uses abstract types for cookies and specific variants for attributes like SameSite. ```ocaml (* The cookie type is abstract - create cookies using the provided functions *) type t (* Same-site cookie attribute for CSRF protection *) type same_site = [ `None | `Lax | `Strict ] (* Date-time type for expiration dates (RFC 1123 format) *) type date_time ``` -------------------------------- ### Serialize Cookie to Set-Cookie Header Source: https://context7.com/bikallem/http-cookie/llms.txt Use `Http_cookie.to_set_cookie` to serialize a cookie object into a full `Set-Cookie` header string, including all attributes like Path, Domain, Expires, Max-Age, Secure, HttpOnly, and SameSite. Supports round-trip serialization. ```ocaml (* Create and serialize a full Set-Cookie header *) let expires = Http_cookie.date_time ~year:2021 ~month:`Jan ~weekday:`Mon ~day:12 ~hour:23 ~minutes:23 ~seconds:59 |> Result.get_ok in let cookie = Http_cookie.create ~path:"/home/about" ~domain:"example.com" ~expires ~max_age:2342342L ~secure:true ~http_only:true ~same_site:`Strict ~name:"hello" "value1" |> Result.get_ok in let set_cookie_header = Http_cookie.to_set_cookie cookie (* Result: "hello=value1; Path=/home/about; Domain=example.com; Expires=Mon, 12 Jan 2021 23:23:59 GMT; Max-Age=2342342; Secure; HttpOnly; SameSite=Strict" *) (* Round-trip: parse and serialize Set-Cookie *) let original = "SID=31d4d96e407aad42; Path=/; Domain=ffff::0234:ddd:192.168.0.1; Expires=Sun, 06 Nov 1994 08:49:37 GMT; Secure; HttpOnly" in let cookie = Http_cookie.of_set_cookie original |> Result.get_ok in let serialized = Http_cookie.to_set_cookie cookie in assert (original = serialized) ``` -------------------------------- ### Pretty Printing Functions Source: https://context7.com/bikallem/http-cookie/llms.txt Functions for debugging and displaying cookie information in a human-readable format. ```APIDOC ## Pretty Printing ### Pretty Printer Functions Functions for debugging and displaying cookie information. #### Pretty Print Cookie - **Method**: `Http_cookie.pp` - **Parameters**: `Format.formatter`, `Http_cookie.t` - **Description**: Pretty prints a cookie object. #### Pretty Print Date Time - **Method**: `Http_cookie.pp_date_time` - **Parameters**: `Format.formatter`, `Http_cookie.date_time` - **Description**: Pretty prints a `date_time` value in RFC 1123 format. #### Pretty Print RFC 1123 - **Method**: `Http_cookie.pp_rfc1123` - **Alias**: An alias for `Http_cookie.pp_date_time`. - **Parameters**: `Format.formatter`, `Http_cookie.date_time` #### Pretty Print Same Site - **Method**: `Http_cookie.pp_same_site` - **Parameters**: `Format.formatter`, `[`Strict | `Lax | `None]` - **Description**: Pretty prints a `same_site` attribute value. ``` -------------------------------- ### Cookie Serialization and Parsing Source: https://github.com/bikallem/http-cookie/blob/master/docs/http-cookies/Http_cookies/Cookie/index.html Functions for converting cookie objects to HTTP header strings and parsing cookie header strings into objects. ```APIDOC ## Cookie Serialization and Parsing ### Description Functions to serialize cookie objects into strings suitable for HTTP headers or parse existing header strings. ### Methods - **to_set_cookie_header_value**: Serializes a cookie object into a string for the `Set-Cookie` header. - **to_cookie_header_value**: Serializes a cookie object into a string for the `Cookie` header. - **of_cookie_header**: Parses a raw cookie header string into a cookie object. ### Response Example "SID=31d4d96e407aad42; Path=/; Secure; HttpOnly; Expires=Sun, 06 Nov 1994 08:49:37 GMT" ``` -------------------------------- ### Parse Full Set-Cookie Header Source: https://context7.com/bikallem/http-cookie/llms.txt Use `Http_cookie.of_set_cookie` to parse a complete Set-Cookie header string, including attributes like Path, Domain, Expires, Max-Age, Secure, and HttpOnly. Handles IPv4 and IPv6 domains, and negative Max-Age for deletion. ```ocaml (* Parse a full Set-Cookie header *) let set_cookie_str = "SID=31d4d96e407aad42; Path=/; Domain=example.com; Secure; HttpOnly; Expires=Sun, 06 Nov 1994 08:49:37 GMT" in let cookie = Http_cookie.of_set_cookie set_cookie_str (* Result: Ok { name="SID"; value="31d4d96e407aad42"; path=Some "/"; domain=Some "example.com"; expires=Some ; secure=true; http_only=true; ... } *) ``` ```ocaml (* Parse Set-Cookie with IPv4 domain *) let ipv4_cookie = Http_cookie.of_set_cookie "SID=value123; Path=/; Domain=192.169.0.1; Secure; HttpOnly" (* Result: Ok cookie with IPv4 domain *) ``` ```ocaml (* Parse Set-Cookie with IPv6 domain *) let ipv6_cookie = Http_cookie.of_set_cookie "SID=value123; Path=/; Domain=eee::eee:ffff:234; Secure; HttpOnly" (* Result: Ok cookie with IPv6 domain *) ``` ```ocaml (* Parse Set-Cookie with Max-Age (including negative for deletion) *) let max_age_cookie = Http_cookie.of_set_cookie "SID=value; Path=/; Domain=example.com; Max-Age=-1" (* Result: Ok cookie with max_age=Some -1L (for cookie deletion) *) ``` -------------------------------- ### Cookie.Same_site Module Source: https://github.com/bikallem/http-cookie/blob/master/docs/http-cookies/Http_cookies/Cookie/Same_site/index.html Defines the Same-site attribute types and utility functions for cookie management. ```APIDOC ## Cookie.Same_site ### Description Represents the 'Same-site' cookie attribute as defined in IETF drafts. ### Types - **t** (variant) - The type representing the Same-site attribute. Possible values: Default, None, Lax, Strict. ### Functions - **equal** (t -> t -> bool) - Compares two Same_site values for equality. - **compare** (t -> t -> int) - Compares two Same_site values. - **to_string** (t -> string) - Converts the Same_site value to its string representation. ``` -------------------------------- ### Compare Cookies Source: https://context7.com/bikallem/http-cookie/llms.txt Compares two cookies or date_time values. ```APIDOC ## Comparing Cookies ### Http_cookie.compare Compares two cookies. Returns 0 if equal, positive if the first cookie is greater, negative if the first cookie is less. #### Parameters - **cookie1** (Http_cookie.t) - **cookie2** (Http_cookie.t) #### Returns - `int` - Comparison result. ### Http_cookie.compare_date_time Compares two `date_time` values. #### Parameters - **dt1** (Http_cookie.date_time) - **dt2** (Http_cookie.date_time) #### Returns - `int` - Comparison result (negative if dt1 < dt2, 0 if equal, positive if dt1 > dt2). ``` -------------------------------- ### Parse Cookie Headers Source: https://context7.com/bikallem/http-cookie/llms.txt Http_cookie.of_cookie parses raw Cookie header strings into a list of cookie objects. ```ocaml (* Parse a Cookie header with multiple cookies *) let cookies = Http_cookie.of_cookie "SID=31d4d96e407aad42; lang=en-US" (* Result: Ok [ { name="SID"; value="31d4d96e407aad42"; http_only=false; secure=false; ... }; { name="lang"; value="en-US"; http_only=false; secure=false; ... } ] *) (* Access individual cookies from the list *) let () = match cookies with | Ok cookie_list -> List.iter (fun c -> Printf.printf "Cookie: %s = %s\n" (Http_cookie.name c) (Http_cookie.value c) ) cookie_list | Error err -> Printf.printf "Parse error: %s\n" err (* Duplicate cookie names are rejected *) let duplicate = Http_cookie.of_cookie "SID=value1; SID=value2; lang=en-US" (* Result: Error "Invalid cookie : duplicate cookies found" *) (* Invalid cookie syntax returns error *) let invalid = Http_cookie.of_cookie "SID=,31d4d96e407aad42" (* Result: Error "Invalid cookie : end_of_input" *) ``` -------------------------------- ### Creating Cookies Source: https://context7.com/bikallem/http-cookie/llms.txt Functions for creating new HTTP cookies with specified attributes. Validation is performed on all parameters, and errors are returned using Result types. ```APIDOC ## Http_cookie.create ### Description Creates a new HTTP cookie with the specified name, value, and optional attributes. Returns `Ok cookie` if all parameters are valid, otherwise `Error error` with a description. By default, `http_only` is `true` for security. ### Method N/A (Function call) ### Endpoint N/A ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example ```ocaml (* Create a basic cookie *) let basic_cookie = Http_cookie.create ~name:"session_id" "abc123xyz" (* Create a cookie with all attributes *) let expires = Http_cookie.date_time ~year:2025 ~month:`Dec ~weekday:`Fri ~day:31 ~hour:23 ~minutes:59 ~seconds:59 |> Result.get_ok let full_cookie = Http_cookie.create ~path:"/app" ~domain:"example.com" ~expires ~max_age:86400L ~secure:true ~http_only:true ~same_site:`Strict ~name:"auth_token" "secure_value_here" (* Create a cookie with IPv6 domain *) let ipv6_cookie = Http_cookie.create ~path:"/home" ~domain:"eee:aaa:abdf::223:192.168.0.1" ~secure:true ~same_site:`Strict ~name:"SID" "31d4d96e407aad42" (* Invalid cookie name returns error *) let invalid = Http_cookie.create ~name:"he@llo" "world" ``` ### Response #### Success Response (Ok cookie) - **name** (string) - The name of the cookie. - **value** (string) - The value of the cookie. - **path** (string option) - The path attribute. - **domain** (string option) - The domain attribute. - **expires** (date_time option) - The expiration date. - **max_age** (int64 option) - The maximum age in seconds. - **secure** (bool) - Whether the secure attribute is set. - **http_only** (bool) - Whether the http_only attribute is set. - **same_site** (same_site option) - The same_site attribute. - **extension** (list(string) option) - Any extension fields. #### Error Response (Error error) - **error** (string) - Description of the validation error. #### Response Example ```json { "result": "Ok", "cookie": { "name": "session_id", "value": "abc123xyz", "path": null, "domain": null, "expires": null, "max_age": null, "secure": false, "http_only": true, "same_site": null, "extension": null } } ``` ```json { "result": "Error", "error": "name: he@llo" } ``` ``` -------------------------------- ### Create/Decode/Encode Source: https://github.com/bikallem/http-cookie/blob/master/docs/http-cookie/Http_cookie/index.html Functions for creating, decoding, and encoding HTTP cookies. ```APIDOC ## Create/Decode/Encode ### `val date_time` `val date_time : year:int -> month:[ `Jan | `Feb | `Mar | `Apr | `May | `Jun | `Jul | `Aug | `Sep | `Oct | `Nov | `Dec ] -> weekday:[ `Sun | `Mon | `Tue | `Wed | `Thu | `Fri | `Sat ] -> day:int -> hour:int -> minutes:int -> seconds:int -> (date_time, string) Stdlib.result` `date_time` is `Ok dt` if all of the given parameters are valid for creating [`date_time`](#type-date_time) value, otherwise it is `Error err` where err denotes the error. ### `val create` `val create : ?path:string -> ?domain:string -> ?expires:date_time -> ?max_age:int64 -> ?secure:bool -> ?http_only:bool -> ?same_site:same_site -> ?extension:string -> name:string -> string -> (t, string) Stdlib.result` `create ~path ~domain ~expires ~max_age ~secure ~http_only ~same_site ~extension ~name value` is `Ok cookie` if all of the given parameters are valid cookie attribute values. Otherwise it is `Error error` where `error` is the description of the error. ### `val of_cookie` `val of_cookie : string -> (t list, string) Stdlib.result` `of_cookie header` parses `header` - a string value which represents HTTP `Cookie` header value as defined in [https://tools.ietf.org/html/rfc6265#section-4.2](https://tools.ietf.org/html/rfc6265#section-4.2). It returns a list of `Cookie`s if it is able to successfully parse `s`, otherwise it returns `Error err`. It is an error to include duplicate cookie names. Examples This returns two cookies with cookie names `SID` and `lang`. Http_cookie.of_cookie "SID=31d4d96e407aad42; lang=en-US" ### `val to_cookie` `val to_cookie : t -> string` `to_cookie c` serializes `c` into a string which can be encoded as value for HTTP `Cookie` header. Example of a string returned by the function. SID=31d4d96e407aad42 ### `val to_set_cookie` `val to_set_cookie : t -> string` `to_set_cookie c` serializes cookie `c` into a string which can be encoded as value for HTTP `Set-Cookie` header. The datetime format for `expires` attribute is specified in [RFC 2616](https://tools.ietf.org/html/rfc2616#section-3.3.1) Example of a string returned by the function, SID=31d4d96e407aad42; Path=/; Secure; HttpOnly; Expires=Sun, 06 Nov 1994 08:49:37 GMT ### `val of_set_cookie` `val of_set_cookie : string -> (t, string) Stdlib.result` `of_set_cookie s` is `Ok cookie` if `s` can be parsed successfully to create [`t`](#type-t). `s` is the HTTP 'Set-Cookie' header value. The syntax for the value is defined as `set-cookie-string` in [RFC 6265, 4.1](https://datatracker.ietf.org/doc/html/rfc6265#section-4.1.1) ``` -------------------------------- ### Serialize Cookie to Cookie Header Source: https://context7.com/bikallem/http-cookie/llms.txt Use `Http_cookie.to_cookie` to serialize a cookie object into a string format suitable for the HTTP `Cookie` header. This function only includes the name and value. ```ocaml (* Serialize for Cookie header *) let cookie = Http_cookie.create ~name:"SID" "31asdfasddd" |> Result.get_ok in let cookie_header_value = Http_cookie.to_cookie cookie (* Result: "SID=31asdfasddd" *) (* Round-trip: parse and serialize *) let original = "SID=234234asdasdasda" in let cookie = List.nth (Http_cookie.of_cookie original |> Result.get_ok) 0 in let serialized = Http_cookie.to_cookie cookie in assert (original = serialized) (* Serialization preserves the original value *) ``` -------------------------------- ### Serialize Cookie for Headers Source: https://github.com/bikallem/http-cookie/blob/master/docs/http-cookies/Http_cookies/Cookie/index.html Serializes a cookie object into strings suitable for HTTP Set-Cookie or Cookie headers. ```ocaml to_set_header c ``` ```ocaml to_cookie_header c ``` -------------------------------- ### Query Cookie Attributes Source: https://github.com/bikallem/http-cookie/blob/master/docs/http-cookies/Http_cookies/Cookie/index.html Functions to retrieve specific attributes from a cookie object. ```ocaml name t ``` ```ocaml value t ``` ```ocaml path t ``` ```ocaml domain t ``` ```ocaml expires t ``` ```ocaml max_age t ``` ```ocaml secure t ``` ```ocaml http_only t ``` ```ocaml same_site t ``` ```ocaml extension t ``` -------------------------------- ### Type Definitions Source: https://github.com/bikallem/http-cookie/blob/master/docs/http-cookie/Http_cookie/index.html Defines the core types used within the Http_cookie library. ```APIDOC ## Type Definitions ### `type t` A HTTP cookie. ### `type date_time` normalized date time value in GMT. ### `type same_site` `and same_site = [ | None | Lax | Strict ]` 'Same-site' cookie attribute. [Same-site](https://tools.ietf.org/html/draft-ietf-httpbis-cookie-same-site-00) ``` -------------------------------- ### Generate Expiration Dates Source: https://context7.com/bikallem/http-cookie/llms.txt Http_cookie.date_time creates RFC 1123 compliant dates for the expires attribute. ```ocaml (* Create a valid expiration date *) let expiry = Http_cookie.date_time ~year:2024 ~month:`Nov ~weekday:`Sun ~day:6 ~hour:8 ~minutes:49 ~seconds:37 (* Result: Ok date_time representing "Sun, 06 Nov 2024 08:49:37 GMT" *) (* Invalid year returns error *) let invalid_year = Http_cookie.date_time ~year:0 ~month:`Jan ~weekday:`Sun ~day:23 ~hour:22 ~minutes:45 ~seconds:59 (* Result: Error "Invalid year (>1600 && < 9999): 0" *) (* Invalid hour returns error *) let invalid_hour = Http_cookie.date_time ~year:2021 ~month:`Jan ~weekday:`Sun ~day:30 ~hour:24 ~minutes:45 ~seconds:59 (* Result: Error "Invalid hour (>0 && <24): 24" *) ``` -------------------------------- ### Parsing Cookie Headers Source: https://context7.com/bikallem/http-cookie/llms.txt Functions for parsing HTTP `Cookie` headers and `Set-Cookie` headers, with validation for duplicate names and syntax errors. ```APIDOC ## Http_cookie.of_cookie ### Description Parses an HTTP `Cookie` header value and returns a list of cookies. Detects duplicate cookie names as an error. ### Method N/A (Function call) ### Endpoint N/A ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example ```ocaml (* Parse a Cookie header with multiple cookies *) let cookies = Http_cookie.of_cookie "SID=31d4d96e407aad42; lang=en-US" (* Access individual cookies from the list *) let () = match cookies with | Ok cookie_list -> List.iter (fun c -> Printf.printf "Cookie: %s = %s\n" (Http_cookie.name c) (Http_cookie.value c)) cookie_list | Error err -> Printf.printf "Parse error: %s\n" err (* Duplicate cookie names are rejected *) let duplicate = Http_cookie.of_cookie "SID=value1; SID=value2; lang=en-US" (* Invalid cookie syntax returns error *) let invalid = Http_cookie.of_cookie "SID=,31d4d96e407aad42" ``` ### Response #### Success Response (Ok [cookie]) - **cookie** (list(t)) - A list of parsed cookie objects. #### Error Response (Error error) - **error** (string) - Description of the parsing error. #### Response Example ```json { "result": "Ok", "cookies": [ { "name": "SID", "value": "31d4d96e407aad42", "http_only": false, "secure": false, "path": null, "domain": null, "expires": null, "max_age": null, "same_site": null, "extension": null }, { "name": "lang", "value": "en-US", "http_only": false, "secure": false, "path": null, "domain": null, "expires": null, "max_age": null, "same_site": null, "extension": null } ] } ``` ```json { "result": "Error", "error": "Invalid cookie : duplicate cookies found" } ``` ```json { "result": "Error", "error": "Invalid cookie : end_of_input" } ``` ``` -------------------------------- ### Pretty Print Cookie Result Source: https://github.com/bikallem/http-cookie/blob/master/README.md A helper function to pretty print the result of cookie operations, handling both success (Ok) and error (Error) cases. ```ocaml let pp = function | Ok cookie -> Format.printf "%a\n" Http_cookie.pp cookie | Error err -> Format.printf "Err: %s" err ``` -------------------------------- ### Module Cookie Source: https://github.com/bikallem/http-cookie/blob/master/docs/http-cookies/Http_cookies/index.html The Cookie module provides the core signature for cookie handling operations. ```APIDOC ## Module Cookie ### Description The Cookie module defines the signature for cookie management within the http-cookies library. ### Module Path http-cookies.Http_cookies.Cookie ``` -------------------------------- ### Parse HTTP Cookie Header Source: https://github.com/bikallem/http-cookie/blob/master/docs/http-cookies/Http_cookies/Cookie/index.html Parses a raw HTTP cookie header string into cookie objects. ```ocaml Cookie.of_cookie_header "SID=31d4d96e407aad42; lang=en-US" ``` -------------------------------- ### Pretty Printers Source: https://github.com/bikallem/http-cookie/blob/master/docs/http-cookie/Http_cookie/index.html Functions for pretty-printing cookie-related types. ```APIDOC ## Pretty Printers ### `val pp` `val pp : Stdlib.Format.formatter -> t -> unit` ### `val pp_same_site` `val pp_same_site : Stdlib.Format.formatter -> same_site -> unit` ### `val pp_date_time` `val pp_date_time : Stdlib.Format.formatter -> date_time -> unit` `pp_date_time fmt date_time` pretty prints [`date_time`](#type-date_time) in RFC 1123 format. Example: Sun, 06 Nov 1994 08:49:37 GMT ### `val pp_rfc1123` `val pp_rfc1123 : Stdlib.Format.formatter -> date_time -> unit` Alias of [`pp_date_time`](#val-pp_date_time). ``` -------------------------------- ### Http_cookie.to_set_cookie Source: https://context7.com/bikallem/http-cookie/llms.txt Serializes a cookie object into a full Set-Cookie header string including all attributes. ```APIDOC ## Http_cookie.to_set_cookie ### Description Serializes a cookie into a full `Set-Cookie` header value including all attributes like Path, Domain, Expires, Max-Age, Secure, HttpOnly, and SameSite. ### Parameters #### Request Body - **cookie** (object) - Required - The cookie object to serialize. ### Response #### Success Response (200) - **string** (string) - The full Set-Cookie header string. ``` -------------------------------- ### Expire a Cookie Source: https://context7.com/bikallem/http-cookie/llms.txt Sets Max-Age to -1 and clears the value to instruct browsers to delete the cookie. ```ocaml (* Expire an existing cookie *) let cookie = Http_cookie.create ~path:"/home/about" ~domain:"198.168.0.1" ~max_age:2342342L ~same_site:`None ~name:"hello" "value1" |> Result.get_ok in let expired = Http_cookie.expire cookie in let header = Http_cookie.to_set_cookie expired (* Result: "hello=; Max-Age=-1; HttpOnly; SameSite=None" *) ``` -------------------------------- ### Parse Cookie header Source: https://github.com/bikallem/http-cookie/blob/master/docs/http-cookie/Http_cookie/index.html Parses a string representing an HTTP Cookie header into a list of cookie objects. ```ocaml Http_cookie.of_cookie "SID=31d4d96e407aad42; lang=en-US" ``` -------------------------------- ### Update Cookie Attributes in OCaml Source: https://context7.com/bikallem/http-cookie/llms.txt Functions to modify individual cookie attributes. Note that some updates return a Result type requiring validation, while others return the cookie directly. ```ocaml let cookie = Http_cookie.create ~name:"session" "old_value" |> Result.get_ok in (* Update value - returns Result *) let updated = Http_cookie.update_value "new_value" cookie (* Result: Ok { ...; value="new_value"; ... } *) (* Update name - returns Result *) let renamed = Http_cookie.update_name "new_session" cookie (* Result: Ok { ...; name="new_session"; ... } *) (* Update path - returns Result *) let with_path = Http_cookie.update_path (Some "/new/path") cookie (* Result: Ok { ...; path=Some "/new/path"; ... } *) (* Update domain - returns Result *) let with_domain = Http_cookie.update_domain (Some "newdomain.com") cookie (* Result: Ok { ...; domain=Some "newdomain.com"; ... } *) (* Update secure flag - returns cookie directly (no validation needed) *) let secured = Http_cookie.update_secure true cookie (* { ...; secure=true; ... } *) (* Update http_only flag - returns cookie directly *) let http_only_cookie = Http_cookie.update_http_only false cookie (* { ...; http_only=false; ... } *) (* Update same_site - returns cookie directly *) let with_same_site = Http_cookie.update_same_site (Some `Lax) cookie (* { ...; same_site=Some `Lax; ... } *) (* Update expires - returns cookie directly *) let expires = Http_cookie.date_time ~year:2025 ~month:`Dec ~weekday:`Wed ~day:31 ~hour:23 ~minutes:59 ~seconds:59 |> Result.get_ok in let with_expires = Http_cookie.update_expires (Some expires) cookie (* { ...; expires=Some ; ... } *) (* Update max_age - returns Result *) let with_max_age = Http_cookie.update_max_age (Some 3600L) cookie (* Result: Ok { ...; max_age=Some 3600L; ... } *) ``` -------------------------------- ### Date Time Creation Source: https://context7.com/bikallem/http-cookie/llms.txt Function to create valid date_time values in GMT for cookie expiration attributes, with validation according to RFC 1123. ```APIDOC ## Http_cookie.date_time ### Description Creates a valid date_time value in GMT for use with the `expires` attribute. Validates all components according to RFC 1123. ### Method N/A (Function call) ### Endpoint N/A ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example ```ocaml (* Create a valid expiration date *) let expiry = Http_cookie.date_time ~year:2024 ~month:`Nov ~weekday:`Sun ~day:6 ~hour:8 ~minutes:49 ~seconds:37 (* Invalid year returns error *) let invalid_year = Http_cookie.date_time ~year:0 ~month:`Jan ~weekday:`Sun ~day:23 ~hour:22 ~minutes:45 ~seconds:59 (* Invalid hour returns error *) let invalid_hour = Http_cookie.date_time ~year:2021 ~month:`Jan ~weekday:`Sun ~day:30 ~hour:24 ~minutes:45 ~seconds:59 ``` ### Response #### Success Response (Ok date_time) - **date_time** (date_time) - A valid date_time object. #### Error Response (Error error) - **error** (string) - Description of the validation error. #### Response Example ```json { "result": "Ok", "date_time": "Sun, 06 Nov 2024 08:49:37 GMT" } ``` ```json { "result": "Error", "error": "Invalid year (>1600 && < 9999): 0" } ``` ``` -------------------------------- ### Cookie Attributes Source: https://github.com/bikallem/http-cookie/blob/master/docs/http-cookie/Http_cookie/index.html Details on cookie attributes supported by the library. ```APIDOC ## Cookie Attributes Details on cookie attributes supported by the library. ``` -------------------------------- ### Access Cookie Attributes Source: https://context7.com/bikallem/http-cookie/llms.txt Provides functions to access individual attributes of a cookie object created or parsed by the `Http_cookie` module. These getters return the attribute's value or `None` if not set. ```ocaml let cookie = Http_cookie.create ~path:"/app" ~domain:"example.com" ~secure:true ~http_only:true ~same_site:`Strict ~name:"session" "token123" |> Result.get_ok in (* Access all attributes *) let cookie_name = Http_cookie.name cookie (* "session" *) let cookie_value = Http_cookie.value cookie (* "token123" *) let cookie_path = Http_cookie.path cookie (* Some "/app" *) let cookie_domain = Http_cookie.domain cookie (* Some "example.com" *) let cookie_expires = Http_cookie.expires cookie (* None *) let cookie_max_age = Http_cookie.max_age cookie (* None *) let cookie_secure = Http_cookie.secure cookie (* true *) let cookie_http_only = Http_cookie.http_only cookie (* true *) let cookie_same_site = Http_cookie.same_site cookie (* Some `Strict *) let cookie_ext = Http_cookie.extension cookie (* None *) ``` -------------------------------- ### Http_cookie.to_cookie Source: https://context7.com/bikallem/http-cookie/llms.txt Serializes a cookie object into a string suitable for an HTTP Cookie header. ```APIDOC ## Http_cookie.to_cookie ### Description Serializes a cookie into a string suitable for an HTTP `Cookie` header (name=value format). ### Parameters #### Request Body - **cookie** (object) - Required - The cookie object to serialize. ### Response #### Success Response (200) - **string** (string) - The serialized cookie string. ``` -------------------------------- ### Http_cookie.of_set_cookie Source: https://context7.com/bikallem/http-cookie/llms.txt Parses an HTTP Set-Cookie header string into a cookie object, including attributes like Path, Domain, Expires, Max-Age, Secure, and HttpOnly. ```APIDOC ## Http_cookie.of_set_cookie ### Description Parses an HTTP `Set-Cookie` header value including all cookie attributes like Path, Domain, Expires, Max-Age, Secure, HttpOnly. ### Parameters #### Request Body - **set_cookie_str** (string) - Required - The raw Set-Cookie header string to parse. ### Response #### Success Response (200) - **cookie** (object) - A structured cookie object containing name, value, and optional attributes (path, domain, expires, secure, http_only, etc.). ``` -------------------------------- ### Parse HTTP Cookie Header Source: https://github.com/bikallem/http-cookie/blob/master/docs/http-cookies/Http_cookies__Cookie/index.html Parses a string representing an HTTP 'Cookie' header value and returns a list of Cookie instances. This function adheres to RFC 6265 section 4.2. ```ocaml val of_cookie_header : string -> [t](index.html#type-t) list ``` -------------------------------- ### Parse Cookie Header Source: https://github.com/bikallem/http-cookie/blob/master/docs/http-cookies/Http_cookies/Cookie/index.html Parses a raw HTTP Cookie header string into a list of cookie objects. ```APIDOC ## val of_cookie_header ### Description Parses a string representing an HTTP Cookie header value as defined in RFC 6265 section 4.2. ### Parameters - **s** (string) - Required - The raw string value from the HTTP Cookie header. ### Response - **t list** (list) - A list of parsed cookie objects. ``` -------------------------------- ### Cookie Attribute Accessors Source: https://github.com/bikallem/http-cookie/blob/master/docs/http-cookies/Http_cookies/Cookie/index.html Functions to retrieve specific attributes from a cookie object. ```APIDOC ## Cookie Attribute Accessors ### Description Functions to extract metadata and attributes from a cookie object `t`. ### Attributes - **name**: Returns the cookie name. - **value**: Returns the cookie value. - **path**: Returns the cookie path attribute (optional). - **domain**: Returns the cookie domain attribute (optional). - **expires**: Returns the cookie expires attribute (optional). - **max_age**: Returns the cookie max_age attribute (optional). - **secure**: Returns the secure attribute (optional). - **http_only**: Returns the http_only attribute (optional). - **same_site**: Returns the same_site attribute (optional). - **extension**: Returns the cookie extension value (optional). ```