Skip to content

beryl_ewe

Ewe WebSocket transport for beryl

This module provides the bridge between Ewe's native WebSocket handling and the beryl runtime using Ewe request and response types directly.

It mirrors the beryl_mist package: the two transports expose the same handler API, so an integrator can run beryl sockets on either web server by choosing the matching transport package.

Transport configuration (path, on_connect authentication, origin policy) lives in beryl/transport/server; build a server.TransportConfig with its config builders and pass it to handler or upgrade. This module supplies only the Ewe-specific glue: the WebSocket upgrade call, frame sending, and peer IP extraction.

Origin policy migration: beryl/transport/origin.SameOrigin is now SameAuthority. Update explicit constructor references; calls to server.default_config need no change. The default still compares host and explicit port, ignores scheme, and admits requests without Origin. It does not normalize default ports or trust forwarded-host headers. Use server.with_allowed_origins for exact scheme/host/port matching; that policy rejects requests without Origin.

pub fn handler(
beryl.Sockets,
server.TransportConfig(connection.Connection),
fn(request.Request(connection.Connection)) -> response.Response(ewe.Body)
) -> fn(request.Request(connection.Connection)) -> response.Response(ewe.Body)

Build a combined request handler that serves both WebSocket upgrades and regular HTTP from a single Ewe listener.

The returned function inspects and routes each request:

  • It passes WebSocket upgrade and CONNECT requests for the socket path to upgrade, which also runs any on_connect callback. Ewe validates the HTTP/1 handshake or HTTP/2 extended CONNECT protocol.
  • It passes all other requests to http_fallback. This includes non-upgrade requests and upgrades for a different path.

This removes the need to write an upgrade guard:

ewe_transport.handler(sockets, server.default_config("/socket"), http_handler)
|> ewe.new
|> ewe.listening(on: 8000)
|> ewe.start
pub fn upgrade(
request.Request(connection.Connection),
beryl.Sockets,
server.TransportConfig(connection.Connection),
fn() -> response.Response(ewe.Body)
) -> response.Response(ewe.Body)

Upgrade a request to WebSocket if it matches the configured path.

Usage in your Ewe handler:

fn handle_request(http_request: Request(Connection), sockets: Sockets) -> Response(Body) {
use <- ewe_transport.upgrade(http_request, sockets, server.default_config("/socket"))
// Fall through to regular HTTP routing
case request.path_segments(http_request) {
[] -> index_page()
_ -> response.new(404) |> response.set_body(ewe.Empty)
}
}

Path matching, origin policy, ?vsn version negotiation, connection limits (per-IP and per beryl system on the current node, rejected with 429 Too Many Requests), and the on_connect callback use the shared admission pipeline. See beryl/transport/server.upgrade for the full contract. Enforcement defaults to the real TCP peer IP; forwarded headers are ignored. Use server.with_client_ip_extractor only with an explicit trusted-proxy policy. With either per-IP control enabled, address failures reject with 403 before admission. Without either control, an address is not required.

HTTP/2 extended CONNECT uses Ewe's parsed :authority for origin checks when there is no Host header.

Each HTTP/2 WebSocket monitors its beryl runtime outside the frame handler. If that runtime exits, the stream worker is killed even when a write is blocked by flow control. Ewe resets that stream and discards its unsent buffered output without closing the shared HTTP/2 connection. This is runtime-exit cancellation, not a general write deadline.