iroh_net/relay/
http.rs

1//! HTTP-specific constants for the relay server and client.
2
3pub(crate) const HTTP_UPGRADE_PROTOCOL: &str = "iroh derp http";
4pub(crate) const WEBSOCKET_UPGRADE_PROTOCOL: &str = "websocket";
5#[cfg(feature = "iroh-relay")] // only used in the server for now
6#[cfg_attr(iroh_docsrs, doc(cfg(feature = "iroh-relay")))]
7pub(crate) const SUPPORTED_WEBSOCKET_VERSION: &str = "13";
8
9/// The HTTP path under which the relay accepts relaying connections
10/// (over websockets and a custom upgrade protocol).
11pub const RELAY_PATH: &str = "/relay";
12/// The HTTP path under which the relay allows doing latency queries for testing.
13pub const RELAY_PROBE_PATH: &str = "/relay/probe";
14/// The legacy HTTP path under which the relay used to accept relaying connections.
15/// We keep this for backwards compatibility.
16#[cfg(feature = "iroh-relay")] // legacy paths only used on server-side for backwards compat
17#[cfg_attr(iroh_docsrs, doc(cfg(feature = "iroh-relay")))]
18pub(crate) const LEGACY_RELAY_PATH: &str = "/derp";
19/// The legacy HTTP path under which the relay used to allow latency queries.
20/// We keep this for backwards compatibility.
21#[cfg(feature = "iroh-relay")] // legacy paths only used on server-side for backwards compat
22#[cfg_attr(iroh_docsrs, doc(cfg(feature = "iroh-relay")))]
23pub(crate) const LEGACY_RELAY_PROBE_PATH: &str = "/derp/probe";
24
25/// The HTTP upgrade protocol used for relaying.
26#[derive(Debug, Copy, Clone, PartialEq, Eq)]
27pub enum Protocol {
28    /// Relays over the custom relaying protocol with a custom HTTP upgrade header.
29    Relay,
30    /// Relays over websockets.
31    ///
32    /// Originally introduced to support browser connections.
33    Websocket,
34}
35
36impl Protocol {
37    /// The HTTP upgrade header used or expected.
38    pub const fn upgrade_header(&self) -> &'static str {
39        match self {
40            Protocol::Relay => HTTP_UPGRADE_PROTOCOL,
41            Protocol::Websocket => WEBSOCKET_UPGRADE_PROTOCOL,
42        }
43    }
44
45    /// Tries to match the value of an HTTP upgrade header to figure out which protocol should be initiated.
46    pub fn parse_header(header: &http::HeaderValue) -> Option<Self> {
47        let header_bytes = header.as_bytes();
48        if header_bytes == Protocol::Relay.upgrade_header().as_bytes() {
49            Some(Protocol::Relay)
50        } else if header_bytes == Protocol::Websocket.upgrade_header().as_bytes() {
51            Some(Protocol::Websocket)
52        } else {
53            None
54        }
55    }
56}