Skip to main content

Crate h2ts_server

Crate h2ts_server 

Source
Expand description

Make a WebSocket carry a raw byte stream, and serve/proxy HTTP/2 over it.

accept performs the server-side WebSocket handshake on a hyper request (pluggable into any hyper/axum route) and yields the upgraded connection as a byte stream. bridge then pumps bytes full-duplex between that stream and any AsyncRead + AsyncWrite peer (a TCP upstream, an in-process h2c server, …). WebSocket message payloads become a continuous byte stream, so h2c framing rides straight through.

Framing is done by wslay (vendored C, via the wslay-sys crate). Driven through its event API with buffering off, wslay streams each frame’s payload incrementally — it never holds a whole frame in memory, no matter how large — and auto-handles ping/close.

Three entry points sit on top of bridge:

  • WsByteStream — the WebSocket as an AsyncRead + AsyncWrite handle.
  • serve_h2 — run any hyper Service as HTTP/2 over the tunnel.
  • the h2ts-proxy binary — a standalone WS→upstream-h2c proxy.

Part of h2ts: this server pairs with the TypeScript @debdattabasu/h2ts and Rust/WASM h2ts-client frontend clients — both behavior-mirrored against it by the shared conformance suite — or any client that forwards a byte stream over a WebSocket.

Structs§

AcceptOptions
Options controlling how the handshake picks a subprotocol to echo when the handler’s selector declines (see accept_with_options).
BridgeConfig
Control-frame configuration and hooks for bridge_with / serve_h2_with.
CloseFrame
A WebSocket close: status code and (UTF-8) reason. Uses RFC 6455 close codes; the reason should be at most 123 bytes.
ControlReceiver
The receiving half of a control_channel; give it to BridgeConfig::control.
KeepAlive
Server-initiated keepalive: send a Ping when the connection goes idle, and disconnect the peer if it doesn’t respond in time.
ServeConfig
Configuration for serve_h2_with_config: the WebSocket BridgeConfig plus an optional HTTP/2 idle timeout.
WsByteStream
A WebSocket presented as a raw byte duplex (AsyncRead + AsyncWrite) — item 1 in its “looks like a TCP stream” form.
WsControl
Sends WebSocket control frames into a running bridge_with. Cheaply cloneable; call from any task. Frames are queued and flushed by the bridge on its next turn; a send fails only once the bridge has ended.

Enums§

WebSocketError
Errors from the WebSocket handshake.

Constants§

DEFAULT_SUBPROTOCOL
The subprotocol h2ts clients offer by default. accept echoes it when the client offered it and the handler didn’t choose another (see accept_with).

Functions§

accept
Accept a WebSocket upgrade, requiring the DEFAULT_SUBPROTOCOL (h2ts).
accept_with
Accept a WebSocket upgrade, choosing which offered subprotocol to echo.
accept_with_options
Accept a WebSocket upgrade, choosing which offered subprotocol to echo, with explicit AcceptOptions.
bridge
Pump bytes full-duplex between a WebSocket and a byte-stream peer until either side closes, using wslay for framing. Equivalent to bridge_with with a default BridgeConfig.
bridge_with
Like bridge, but with control-frame configuration and hooks (BridgeConfig): send control frames via control_channel, observe received close/ping/pong, and set the close sent on teardown.
control_channel
Create a control channel. Keep the WsControl to send control frames; put the ControlReceiver in BridgeConfig::control.
is_upgrade_request
Whether request is a WebSocket upgrade: Upgrade: websocket, Connection: upgrade, and a Sec-WebSocket-Key.
offered_protocols
The WebSocket subprotocols the client offered, in order, whitespace-trimmed; empty if it offered none. Gives a handler full visibility into the offer so it can pass one to accept_with.
serve_h2
Serve any hyper HTTP/2 service over a WebSocket tunnel (item 2).
serve_h2_with
Like serve_h2, but with control-frame configuration and hooks (BridgeConfig) applied to the underlying WebSocket bridge — send control frames via a control_channel, observe the peer’s close reason, etc.
serve_h2_with_config
Like serve_h2_with, but also accepts an HTTP/2 idle_timeout via ServeConfig: after that long with no open HTTP/2 streams, the connection is closed with a graceful GOAWAY (then the WebSocket close), so a healthy but idle client reconnects fresh. hyper has no built-in idle timeout, so this tracks open streams and drives graceful_shutdown itself; a live stream (even a quiet one) is never reaped, and pings never reset the timer.

Type Aliases§

CloseHook
Hook invoked with the close frame describing why the connection ended.
ControlHook
Hook invoked with a received Ping/Pong payload.
UpgradedIo
The upgraded WebSocket connection, presented as a raw tokio byte stream (AsyncRead + AsyncWrite). Hand it to bridge, serve_h2, or WsByteStream.