File tree Expand file tree Collapse file tree 2 files changed +25
-0
lines changed
Expand file tree Collapse file tree 2 files changed +25
-0
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change @@ -1349,6 +1349,20 @@ fn verify_reqwest(response: &reqwest::Response, options: Options) -> Result<Nego
13491349}
13501350
13511351fn 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 ( ) ,
You can’t perform that action at this time.
0 commit comments