Skip to content

Commit

Permalink
docs: Document Protocol struct
Browse files Browse the repository at this point in the history
  • Loading branch information
matheus23 committed Jun 21, 2024
1 parent fdd1214 commit 7def324
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
4 changes: 3 additions & 1 deletion iroh-net/src/relay/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl ClientReceiver {
pub struct InnerClient {
/// Our local address, if known.
///
/// `None` if we don't control the connection establishment, e.g. in browsers.
/// Is `None` in tests or when using websockets (because we don't control connection establishment in browsers).
local_addr: Option<SocketAddr>,
/// Channel on which to communicate to the server. The associated [`mpsc::Receiver`] will close
/// if there is ever an error writing to the server.
Expand Down Expand Up @@ -126,6 +126,8 @@ impl Client {
}

/// The local address that the [`Client`] is listening on.
///
/// `None`, when run in a testing environment or when using websockets.
pub fn local_addr(&self) -> Option<SocketAddr> {
self.inner.local_addr
}
Expand Down
10 changes: 10 additions & 0 deletions iroh-net/src/relay/http/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,29 @@ fn downcast_upgrade(upgraded: Upgraded) -> Result<(MaybeTlsStream, Bytes)> {
}
}

/// The HTTP upgrade protocol used for relaying.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Protocol {
/// Relays over the custom relaying protocol with a custom HTTP upgrade header
Relay,
/// Relays over websockets
///
/// Originally introduced to support browser connections.
Websocket,
}

impl Protocol {
/// The HTTP upgrade header used or expected
pub const fn upgrade_header(&self) -> &'static str {
match self {
Protocol::Relay => HTTP_UPGRADE_PROTOCOL,
Protocol::Websocket => WEBSOCKET_UPGRADE_PROTOCOL,
}
}

/// Determines which protocol to use depending on a URL.
///
/// `ws(s)` parses as websockets, `http(s)` parses to the custom relay protocol.
pub fn from_url_scheme(url: &RelayUrl) -> Self {
match url.scheme() {
"ws" => Protocol::Websocket,
Expand All @@ -82,6 +91,7 @@ impl Protocol {
}
}

/// Tries to match the value of an HTTP upgrade header to figure out which protocol should be initiated.
pub fn parse_header(header: &HeaderValue) -> Option<Self> {
let header_bytes = header.as_bytes();
if header_bytes == Protocol::Relay.upgrade_header().as_bytes() {
Expand Down

0 comments on commit 7def324

Please sign in to comment.