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

axum: fix panic caused by missing upgrade header; HTTP 400 on error #75

Merged
merged 1 commit into from
Mar 30, 2024
Merged
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
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
Loading