diff --git a/CHANGES.md b/CHANGES.md index cd7e0f7d..fdb8c4b7 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -6,6 +6,9 @@ * Migrate to tokio 0.2 +### Fixed + +* Allow comma-separated websocket subprotocols without spaces (#1172) ## [2.0.0-alpha.1] - 2019-11-22 diff --git a/actix-web-actors/src/ws.rs b/actix-web-actors/src/ws.rs index e25a7e6e..0b026e35 100644 --- a/actix-web-actors/src/ws.rs +++ b/actix-web-actors/src/ws.rs @@ -152,7 +152,8 @@ pub fn handshake_with_protocols( .and_then(|req_protocols| { let req_protocols = req_protocols.to_str().ok()?; req_protocols - .split(", ") + .split(',') + .map(|req_p| req_p.trim()) .find(|req_p| protocols.iter().any(|p| p == req_p)) }); @@ -736,5 +737,46 @@ mod tests { .headers() .get(&header::SEC_WEBSOCKET_PROTOCOL) ); + + let req = TestRequest::default() + .header( + header::UPGRADE, + header::HeaderValue::from_static("websocket"), + ) + .header( + header::CONNECTION, + header::HeaderValue::from_static("upgrade"), + ) + .header( + header::SEC_WEBSOCKET_VERSION, + header::HeaderValue::from_static("13"), + ) + .header( + header::SEC_WEBSOCKET_KEY, + header::HeaderValue::from_static("13"), + ) + .header( + header::SEC_WEBSOCKET_PROTOCOL, + header::HeaderValue::from_static("p1,p2,p3"), + ) + .to_http_request(); + + let protocols = vec!["p3", "p2"]; + + assert_eq!( + StatusCode::SWITCHING_PROTOCOLS, + handshake_with_protocols(&req, &protocols) + .unwrap() + .finish() + .status() + ); + assert_eq!( + Some(&header::HeaderValue::from_static("p2")), + handshake_with_protocols(&req, &protocols) + .unwrap() + .finish() + .headers() + .get(&header::SEC_WEBSOCKET_PROTOCOL) + ); } }