Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Accepting a Browser Initiated WebTransport Session Always Fails #935

Merged
merged 2 commits into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions crates/core/src/conn/quinn/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,16 @@ async fn process_web_transport(
} else {
conn = response
.extensions_mut()
.remove::<Mutex<salvo_http3::server::Connection<salvo_http3::http3_quinn::Connection, Bytes>>>()
.remove::<Arc<Mutex<salvo_http3::server::Connection<salvo_http3::http3_quinn::Connection, Bytes>>>>()
.map(|c| {
c.into_inner()
Arc::into_inner(c).unwrap().into_inner()
.map_err(|e| IoError::new(ErrorKind::Other, format!("failed to get conn : {}", e)))
})
.transpose()?;
stream = response
.extensions_mut()
.remove::<salvo_http3::server::RequestStream<salvo_http3::http3_quinn::BidiStream<Bytes>, Bytes>>();
.remove::<Arc<salvo_http3::server::RequestStream<salvo_http3::http3_quinn::BidiStream<Bytes>, Bytes>>>()
.and_then(|stream|Arc::into_inner(stream));
}

let Some(conn) = conn else {
Expand Down
30 changes: 21 additions & 9 deletions crates/core/src/http/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,20 +527,32 @@ impl Request {
pub async fn web_transport_mut(&mut self) -> Result<&mut crate::proto::WebTransportSession<salvo_http3::http3_quinn::Connection, Bytes>, crate::Error> {
if self.is_wt_connect() {
if self.extensions.get::<crate::proto::WebTransportSession<salvo_http3::http3_quinn::Connection, Bytes>>().is_none() {
let conn = self.extensions.remove::<std::sync::Mutex<salvo_http3::server::Connection<salvo_http3::http3_quinn::Connection, Bytes>>>();
let stream = self.extensions.remove::<salvo_http3::server::RequestStream<salvo_http3::http3_quinn::BidiStream<Bytes>, Bytes>>();
let conn = self.extensions.remove::<Arc<std::sync::Mutex<salvo_http3::server::Connection<salvo_http3::http3_quinn::Connection, Bytes>>>>();
let stream = self.extensions.remove::<Arc<salvo_http3::server::RequestStream<salvo_http3::http3_quinn::BidiStream<Bytes>, Bytes>>>();
match (conn, stream) {
(Some(conn), Some(stream)) => {
if let Ok(conn) = conn.into_inner() {
let session = crate::proto::WebTransportSession::accept(stream, conn).await?;
self.extensions.insert(Arc::new(session));
if let Some(session) = self.extensions.get_mut::<crate::proto::WebTransportSession<salvo_http3::http3_quinn::Connection, Bytes>>() {
Ok(session)
if let Some(conn) = Arc::into_inner(conn) {
if let Ok(conn) = conn.into_inner() {
if let Some(stream) = Arc::into_inner(stream) {
let session = crate::proto::WebTransportSession::accept(stream, conn).await?;
self.extensions.insert(Arc::new(session));
if let Some(session) = self.extensions.get_mut::<Arc<crate::proto::WebTransportSession<salvo_http3::http3_quinn::Connection, Bytes>>>() {
if let Some(session) = Arc::get_mut(session) {
Ok(session)
} else {
Err(crate::Error::Other("web transport session should not used twice".into()))
}
} else {
Err(crate::Error::Other("web transport session not found in request extension".into()))
}
} else {
Err(crate::Error::Other("invalid web transport".into()))
Err(crate::Error::Other("web transport stream should not used twice".into()))
}
} else {
Err(crate::Error::Other("invalid web transport".into()))
}
} else {
Err(crate::Error::Other("invalid web transport".into()))
Err(crate::Error::Other("quinn connection should not used twice".into()))
}
}
(Some(conn), None) => {
Expand Down
34 changes: 19 additions & 15 deletions crates/core/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,25 +318,29 @@ impl HyperHandler {
#[cfg(feature = "quinn")]
{
use bytes::Bytes;
use parking_lot::Mutex;
if let Some(session) =
req.extensions.remove::<crate::proto::WebTransportSession<
salvo_http3::http3_quinn::Connection,
Bytes,
>>()
{
res.extensions.insert(Arc::new(session));
use std::sync::Mutex;
if let Some(session) = req.extensions.remove::<Arc<
crate::proto::WebTransportSession<salvo_http3::http3_quinn::Connection, Bytes>,
>>() {
res.extensions.insert(session);
}
if let Some(conn) = req.extensions.remove::<Mutex<
salvo_http3::server::Connection<salvo_http3::http3_quinn::Connection, Bytes>,
if let Some(conn) = req.extensions.remove::<Arc<
Mutex<
salvo_http3::server::Connection<
salvo_http3::http3_quinn::Connection,
Bytes,
>,
>,
>>() {
res.extensions.insert(Arc::new(conn));
res.extensions.insert(conn);
}
if let Some(stream) = req.extensions.remove::<salvo_http3::server::RequestStream<
salvo_http3::http3_quinn::BidiStream<Bytes>,
Bytes,
if let Some(stream) = req.extensions.remove::<Arc<
salvo_http3::server::RequestStream<
salvo_http3::http3_quinn::BidiStream<Bytes>,
Bytes,
>,
>>() {
res.extensions.insert(Arc::new(stream));
res.extensions.insert(stream);
}
}
res
Expand Down