diff --git a/rs/web-transport-wasm/examples/harness.rs b/rs/web-transport-wasm/examples/harness.rs index d2c9e71c..58ab8476 100644 --- a/rs/web-transport-wasm/examples/harness.rs +++ b/rs/web-transport-wasm/examples/harness.rs @@ -62,6 +62,7 @@ pub async fn run(url: String, hash: String) -> Result { } check!("echo round trip", echo_round_trip); + check!("no subprotocol reports None", no_subprotocol_is_none); check!( "dropped pending opens release stream credit", dropped_pending_opens_release_credit @@ -132,6 +133,19 @@ async fn echo_round_trip(session: Session) -> Result { expect(got == "hello harness", format!("echoed {got:?}")) } +/// `connect` above offers no subprotocols, so the browser negotiates none and +/// reports `""`. The trait contract says an unnegotiated protocol is `None`, and a +/// `Some("")` reaching a caller gets matched against their protocol table as if the +/// peer had chosen something. +async fn no_subprotocol_is_none(session: Session) -> Result { + let protocol = session.protocol(); + + expect( + protocol.is_none(), + format!("protocol() returned {protocol:?}"), + ) +} + /// Dropping a handle cannot cancel the browser promise returned by open. Churn /// past the peer's stream limit, then prove those abandoned opens were reset and /// returned their credit rather than blocking the next real stream forever. diff --git a/rs/web-transport-wasm/src/session.rs b/rs/web-transport-wasm/src/session.rs index ed044fd7..6542db25 100644 --- a/rs/web-transport-wasm/src/session.rs +++ b/rs/web-transport-wasm/src/session.rs @@ -222,9 +222,15 @@ impl Session { pub fn new(inner: WebTransport, url: Url) -> Self { // TODO use the web_sys bindings when updated. // Until then, we try to access the protocol property on the inner object. + // + // The browser reports `""` when no subprotocol was negotiated, which is what + // `None` means here. Normalizing at the capture site keeps every reader — + // the inherent getter and both trait impls — from having to know that, and + // stops `Some("")` being matched against an application's protocol table. let protocol = Reflect::get(&inner, &"protocol".into()) .ok() - .and_then(|p| p.as_string()); + .and_then(|p| p.as_string()) + .filter(|p| !p.is_empty()); Self { shared: Rc::new(Shared { @@ -556,7 +562,8 @@ impl Session { &self.shared.url } - /// Return the application protocol used to create the session. + /// Return the subprotocol the server selected, or `None` when none was + /// negotiated. pub fn protocol(&self) -> Option<&str> { self.shared.protocol.as_deref() } diff --git a/rs/web-transport/src/wasm.rs b/rs/web-transport/src/wasm.rs index 643a57bd..b62892d3 100644 --- a/rs/web-transport/src/wasm.rs +++ b/rs/web-transport/src/wasm.rs @@ -116,6 +116,8 @@ impl Session { } /// Return the application protocol used to create the session. + /// + /// This is the negotiated subprotocol, or `None` when none was negotiated. pub fn protocol(&self) -> Option<&str> { self.0.protocol() }