Skip to content

Commit

Permalink
Fix Axum panic caused by missing upgrade header & HTTP 400 on error (#75
Browse files Browse the repository at this point in the history
)
  • Loading branch information
nitn3lav authored Mar 30, 2024
1 parent fa93b34 commit bd38ea5
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,26 +76,29 @@ impl<S> axum_core::extract::FromRequestParts<S> for IncomingUpgrade
where
S: Sync,
{
type Rejection = ();
type Rejection = hyper::StatusCode;

async fn from_request_parts(
parts: &mut http::request::Parts,
_state: &S,
) -> Result<Self, Self::Rejection> {
let key = parts.headers.get("Sec-WebSocket-Key").ok_or(())?;
let key = parts
.headers
.get("Sec-WebSocket-Key")
.ok_or(hyper::StatusCode::BAD_REQUEST)?;
if parts
.headers
.get("Sec-WebSocket-Version")
.map(|v| v.as_bytes())
!= Some(b"13")
{
return Err(());
return Err(hyper::StatusCode::BAD_REQUEST);
}

let on_upgrade = parts
.extensions
.remove::<hyper::upgrade::OnUpgrade>()
.unwrap();
.ok_or(hyper::StatusCode::BAD_REQUEST)?;
Ok(Self {
on_upgrade,
key: sec_websocket_protocol(key.as_bytes()),
Expand Down

0 comments on commit bd38ea5

Please sign in to comment.