Module type Session_cookie.S
The signature for a cookie session manager.
type t
= private
{
key : key;
They key for the session in the backend and in cookies.
mutable value : value;
The value for the session stored in the backend.
mutable expiry_period : period;
The period from now in seconds that the session will expire.
mutable modified : bool;
Whether the session data or expiry have been modified
}
The session type.
This type is marked private, so the record fields can be accessed and pattern-matched as usual. However, values of this type cannot be constructed directly. To create a new session, use
generate
. To retrieve an existingsession
, useof_key
orof_header
. To modifyt.expiry_period
ort.value
, useset
. Finally, useto_cookie_hdrs
to smartly generateSet-Cookie
and related headers.
val of_key : backend -> key -> (t, Session.S.error) Stdlib.result io
of_key backend key
fetches the session associated withkey
from the backend, if present and unexpired.
val of_header : backend -> string -> Cookie.header -> (t option, Session.S.error) Stdlib.result io
of_header backend cookie_key header
retrieves the session key from the cookies inheader
. Ifcookie_key
is not present in any cookies inheader
, then this function will returnNone
. If a session key is found, it will call{!val:of_key} backend key
. If both lookups were successful, then this function will returnSome session
. If no key was found inheader
, it will returnNone
.
val of_header_or_create : ?expiry:period -> backend -> string -> value -> Cookie.header -> t io
of_header_or_create ?expiry backend cookie_key default header
retrieves the session key from the cookies inheader
. Ifcookie_key
is not present in any cookies in theheader
or if the session is not a valid one, a new session will be usingexpiry
for the expiration period anddefault
as the value.
val to_cookie_hdrs : ?discard:bool -> ?path:string -> ?domain:string -> ?secure:bool -> ?http_only:bool -> string -> t -> (string * string) list
to_cookie_hdrs cookie_key session
will generate response headers to communicate session changes to the client. This function takes into account thet.modified
field of the session type, and will not generate headers if they are not needed.
val clear_hdrs : ?path:string -> ?domain:string -> string -> (string * string) list
clear_hdrs cookie_key
will generate response headers to communicate that the client should evict the session with keycookie_key
.
val generate : ?expiry:period -> backend -> value -> t io
generate ?expiry backend value
will allocate a new session in the backendbackend
. The session will expireexpiry
seconds from now, defaulting todefault_period backend
if one is not explicitly specified.
val clear : backend -> t -> unit io
clear backend session
removessession
frombackend
. The backend may choose to persist the session value beyond this call, but any subsequent operations involvingkey
should behave as ifkey
is not present in the backend.The
value
andt.expiry_period
ofsession
will be zero'd out, and thet.modified
flag will be set. Callingto_cookie_hdrs
on a cleared session will generate the appropriate headers directing the client to clear the associated cookie.
val set : ?expiry:period -> ?value:value -> backend -> t -> unit io
set ?expiry ?value backend session
sets thevalue
for the session associatedkey
inbackend
and sets the session to expireexpiry
seconds from now. Ifexpiry
is not provided, the expiry period reported bydefault_period backend
will be used instead. If no value is provided, then only the expiry will be updated.