Module Webs_websocket

Websockets upgrade support.

References.

Keys

type key = string

The type for websocket keys. These are random 16 bytes encoded in base64 with padding.

val random_key : ?crypto_random:Webs_crypto_random.t -> unit -> key

random_key () is a random websocket key sourced from the generator crypto_random (default to Webs_crypto_random.get).

Upgrading from the client

val url_schemes : (Webs.Url.scheme * Webs.Url.Authority.port) list

url_schemes has the list of URL schemes (http, https, ws, wss) and their default ports for handshaking. Can be used with Webs.Url.to_endpoint.

val request_upgrade_of_url : ?key:key -> ?headers:Webs.Http.Headers.t -> ?log:string -> Webs.Url.t -> (key * Webs.Http.Request.t, string) Stdlib.result

request_upgrade_of_url ~url is a `GET request constructed like Webs.Http.Request.of_url and with headers add_request_upgrade_headers ?key headers added and returns key. The scheme of url must be one of "http", "https", "ws" or "wss".

val accept_upgrade : key:key -> Webs.Http.Response.t -> (unit, string) Stdlib.result

accept_upgrade ~key response finishes the upgrade request. It checks that:

After this the underlying connection can be used to exchange websocket frames.

Upgrading from the service

val is_request_upgrade : Webs.Http.Request.t -> bool

is_request_upgrade request is true iff the headers of request satisfy has_websocket_upgrade.

val upgrade_request : Webs.Http.Request.t -> (Webs.Http.Response.t, Webs.Http.Response.t) Stdlib.result

upgrade_request request responds to upgrade request to a websocket.

Headers

val has_websocket_upgrade : Webs.Http.Headers.t -> bool

has_websocket_upgrade headers checks that headers:

val accept_header_value_of_key : key -> string

accept_header_value_of_key k is a value for the sec_websocket_accept header for a key k value of a sec_websocket_key header.

val add_request_upgrade_headers : ?key:key -> Webs.Http.Headers.t -> key * Webs.Http.Headers.t

add_request_upgrade_headers ~key headers adds headers for a Webosocket upgrade using key (defaults to random_key) to headers and returns key.

Names

val sec_websocket_accept : Webs.Http.Headers.Name.t
val sec_websocket_extensions : Webs.Http.Headers.Name.t
val sec_websocket_key : Webs.Http.Headers.Name.t
val sec_websocket_protocol : Webs.Http.Headers.Name.t
val sec_websocket_version : Webs.Http.Headers.Name.t