Skip to content

Commit

Permalink
refactor: Introduce const SUPPORTED_WEBSOCKET_VERSION
Browse files Browse the repository at this point in the history
  • Loading branch information
matheus23 committed Jun 24, 2024
1 parent 160a2a0 commit e99e7af
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
1 change: 1 addition & 0 deletions iroh-net/src/relay/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub use self::server::{Protocol, Server, ServerBuilder, ServerHandle, TlsAccepto

pub(crate) const HTTP_UPGRADE_PROTOCOL: &str = "iroh derp http";
pub(crate) const WEBSOCKET_UPGRADE_PROTOCOL: &str = "websocket";
pub(crate) const SUPPORTED_WEBSOCKET_VERSION: &str = "13";

#[cfg(test)]
mod tests {
Expand Down
8 changes: 5 additions & 3 deletions iroh-net/src/relay/http/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ use tracing::{debug, debug_span, error, info, info_span, warn, Instrument};
use tungstenite::handshake::derive_accept_key;

use crate::key::SecretKey;
use crate::relay::http::{HTTP_UPGRADE_PROTOCOL, WEBSOCKET_UPGRADE_PROTOCOL};
use crate::relay::http::{
HTTP_UPGRADE_PROTOCOL, SUPPORTED_WEBSOCKET_VERSION, WEBSOCKET_UPGRADE_PROTOCOL,
};
use crate::relay::server::{ClientConnHandler, MaybeTlsStream};
use crate::relay::MaybeTlsStreamServer;

Expand Down Expand Up @@ -487,12 +489,12 @@ impl Service<Request<Incoming>> for ClientConnHandler {
.expect("valid body"));
};

if version.as_bytes() != b"13" {
if version.as_bytes() != SUPPORTED_WEBSOCKET_VERSION.as_bytes() {
warn!("invalid header Sec-WebSocket-Version: {:?}", version);
return Ok(builder
.status(StatusCode::BAD_REQUEST)
// It's convention to send back the version(s) we *do* support
.header("Sec-WebSocket-Version", "13")
.header("Sec-WebSocket-Version", SUPPORTED_WEBSOCKET_VERSION)
.body(body_empty())
.expect("valid body"));
}
Expand Down

0 comments on commit e99e7af

Please sign in to comment.