package http-cookie

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type

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 standard implemented by the library is RFC 6265.

Types

module Same_site : sig ... end

Represents 'Same-site' cookie attribute. See https://tools.ietf.org/html/draft-ietf-httpbis-cookie-same-site-00.

type t

Represents a cookie name-value in Cookie request header or a set of cookie attributes in Set-Cookie response header.

type date_time = {
  1. year : int;
    (*

    Four digit year value, e.g. 2020, 2019, 1980 etc.

    *)
  2. month : int;
    (*

    Begins from 0, i.e. January = 0.

    *)
  3. weekday : [ `Sun | `Mon | `Tue | `Wed | `Thu | `Fri | `Sat ];
  4. day_of_month : int;
    (*

    Day of the month value from 1 - 31.

    *)
  5. hour : int;
    (*

    24 hour value from 0-23

    *)
  6. minutes : int;
    (*

    Minutes value from 0 - 59

    *)
  7. seconds : int;
    (*

    Seconds value from 0 - 60

    *)
}

Constructors

val create : ?path:string -> ?domain:string -> ?expires:date_time -> ?max_age:int -> ?secure:bool -> ?http_only:bool -> ?same_site:Same_site.t -> ?extension:string -> string -> value:string -> t

create ~path ~domain ~expires ~max_age ~secure ~http_only ~same_site ~extension name ~value returns a cookie instance t with cookie name name and value value along with the given attributes.

  • raises Cookie

    if any of the cookie attributes fail validation.

of_cookie_header s parses s - a string value which represents HTTP Cookie header value as defined in https://tools.ietf.org/html/rfc6265#section-4.2 and returns a list of Cookies.

Examples

This returns two cookies with cookie names SID and lang-en.

Http_cookie.of_cookie_header "SID=31d4d96e407aad42; lang=en-US" 

to_set_header 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

Example of a string returned by the function,

SID=31d4d96e407aad42; Path=/; Secure; HttpOnly; Expires=Sun, 06 Nov 1994 08:49:37 GMT

to_cookie_header 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

Cookie attributes are defined precisely at RFC 6262

val name : t -> string

name t returns a cookie name.

See cookie-name

val value : t -> string

value t returns a cookie value.

See cookie-value

val path : t -> string option

path t returns cookie path attribute.

See cookie-path

val domain : t -> string option

domain t returns cookie domain attribute.

See cookie-domain

val expires : t -> date_time option

expires t returns a coookie expires attribute.

See cookie-expires

val max_age : t -> int option

max_age t returns a cookie max_age attribute.

See max-age and max-age-av

val secure : t -> bool option

secure t returns a secure attribute.

See cookie-secure

val http_only : t -> bool option

http_only t returns a http_only attribute.

See http-only

val same_site : t -> Same_site.t option

same_site t returns a same_site attribute.

See same-site

val extension : t -> string option

extension t returns a cookie extension value.

See cookie-extension

Compare

val compare : t -> t -> int

compare c1 c2 returns 0 if c1 and c2 are equal, a positive integer if c1 is greater than c2 and a negative integer if c1 is less than c2

Updates

val update_value : string -> t -> t
val update_name : string -> t -> t
val update_path : string option -> t -> t
val update_domain : string option -> t -> t
val update_expires : date_time option -> t -> t
val update_max_age : int option -> t -> t
val update_secure : bool option -> t -> t
val update_http_only : bool option -> t -> t
val update_same_site : Same_site.t option -> t -> t
val update_extension : string option -> t -> t
OCaml

Innovation. Community. Security.