ws_h1_upgrade (ws v0.1.1)

View Source

RFC 6455 handshake helpers for WebSocket over HTTP/1.1.

Embedders provide a parsed HTTP request or response (method, target, headers) and this module validates the WebSocket-specific pieces and builds the peer-side headers. The embedder is responsible for actually driving the socket: writing the request line / status line, serialising the response headers, handing the byte stream over to ws_session once the handshake is done.

Headers are accepted in either list({binary(), binary()}) or map() shape; names are compared case-insensitively.

Summary

Types

client_opts/0

-type client_opts() ::
          #{subprotocols => [binary()],
            extensions => [binary()],
            extra_headers => [header()],
            origin => binary()}.

header/0

-type header() :: {binary(), binary()}.

headers/0

-type headers() :: [header()] | map().

request_info/0

-type request_info() ::
          #{key := binary(),
            version := binary(),
            subprotocols := [binary()],
            extensions := [binary()],
            accept := binary(),
            origin => binary()}.

request_opts/0

-type request_opts() :: #{required_subprotocols => [binary()]}.

Functions

accept_key(Key)

-spec accept_key(binary()) -> binary().

build_request(Host, Port, Path)

-spec build_request(binary(), binary(), binary()) -> {Key :: binary(), Headers :: [header()]}.

build_request(Host, Port, Path, Opts)

-spec build_request(binary(), binary() | integer(), binary(), client_opts()) ->
                       {Key :: binary(), Headers :: [header()]}.

client_key()

-spec client_key() -> binary().

response_headers(Info)

-spec response_headers(request_info()) -> [header()].

response_headers(Info, ExtraOpts)

-spec response_headers(request_info(), map()) -> [header()].

validate_request(Headers)

-spec validate_request(headers()) -> {ok, request_info()} | {error, term()}.

validate_request(Headers, Opts)

-spec validate_request(headers(), request_opts()) -> {ok, request_info()} | {error, term()}.

validate_response(Status, Headers)

-spec validate_response(integer(), headers()) ->
                           {ok,
                            #{accept := binary(), subprotocol => binary(), extensions => [binary()]}} |
                           {error, term()}.