Skip to main content

try_upgrade

Function try_upgrade 

Source
pub fn try_upgrade(
    req: &HttpRequest,
    upgrades: &BTreeMap<String, UpgradeHandler>,
) -> Option<Result<(String, UpgradeHandler), ()>>
Expand description

The RFC 6455 handshake decision for req against upgrades, in ONE place so every tier that drives its own connection loop (the tokio serve_connection below; the interpreter, which is !Send and never reaches serve_connection at all) reaches the exact same answer rather than hand-copying the accept-key derivation and the 101 response line a second time.

  • None: req does not ask to upgrade a registered path at all (wrong method, no Upgrade: websocket header, or a path nothing is registered for) — proceed to the ordinary request path exactly as if upgrades were empty.
  • Some(Ok((response, handler))): the handshake is complete — write response verbatim, then hand the raw connection to handler.
  • Some(Err(())): the path/method/header matched a registered upgrade, but the handshake itself could not be completed (no Sec-WebSocket-Key, or the relay feature is not compiled in). The connection must be DROPPED WITH NO RESPONSE — never a fallback to the ordinary handler, and never a generic error page (see UpgradeHandler’s own doc comment: this is what actually gates the behaviour, not a second check at the call site).