Skip to content

Commit 445e832

Browse files
committed
native: handshake: Support redirection
1 parent 9eb5a1a commit 445e832

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,16 @@ pub enum WebSocketError {
130130
#[cfg(not(target_arch = "wasm32"))]
131131
InvalidStatusCode(u16),
132132

133+
/// Server responded with a redirect status code.
134+
#[error("Redirected with status code {status_code} to {location}")]
135+
#[cfg(not(target_arch = "wasm32"))]
136+
Redirected {
137+
/// The HTTP status code (e.g. 301, 302, 307, 308).
138+
status_code: u16,
139+
/// The target location from the `Location` header.
140+
location: String,
141+
},
142+
133143
/// Missing or invalid "Upgrade: websocket" header.
134144
#[error("Invalid upgrade header")]
135145
#[cfg(not(target_arch = "wasm32"))]
@@ -247,6 +257,7 @@ impl WebSocketError {
247257
matches!(
248258
self,
249259
Self::InvalidStatusCode(_)
260+
| Self::Redirected { .. }
250261
| Self::InvalidUpgradeHeader
251262
| Self::InvalidConnectionHeader
252263
| Self::InvalidSecWebsocketVersion

src/native/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,6 +1349,20 @@ fn verify_reqwest(response: &reqwest::Response, options: Options) -> Result<Nego
13491349
}
13501350

13511351
fn verify(response: &Response<Incoming>, options: Options) -> Result<Negotiation> {
1352+
// Detect HTTP redirects before checking for 101.
1353+
if response.status().is_redirection() {
1354+
let location = response
1355+
.headers()
1356+
.get(header::LOCATION)
1357+
.and_then(|v| v.to_str().ok())
1358+
.unwrap_or("")
1359+
.to_string();
1360+
return Err(WebSocketError::Redirected {
1361+
status_code: response.status().as_u16(),
1362+
location,
1363+
});
1364+
}
1365+
13521366
if response.status() != StatusCode::SWITCHING_PROTOCOLS {
13531367
return Err(WebSocketError::InvalidStatusCode(
13541368
response.status().as_u16(),

0 commit comments

Comments
 (0)