support client specifying multiple protocols in Sec-WebSocket-Protocol negotiation

This commit is contained in:
Patrick Fernie 2020-12-04 12:16:14 -05:00
parent b3bd8603e3
commit 6f3e861e27
2 changed files with 15 additions and 13 deletions

View File

@ -60,8 +60,11 @@ where
.headers()
.get("sec-websocket-protocol")
.and_then(|value| value.to_str().ok())
.and_then(|value| WebSocketProtocols::from_str(value).ok())
{
.and_then(|protocols| {
protocols
.split(',')
.find_map(|p| WebSocketProtocols::from_str(p.trim()).ok())
}) {
Some(protocol) => protocol,
None => {
// default to the prior standard

View File

@ -65,22 +65,21 @@ where
F: FnOnce(serde_json::Value) -> Result<Data> + Send + Sync + Clone + 'static,
{
use async_graphql::http::WebSocketProtocols;
use std::str::FromStr;
warp::ws()
.and(warp::header::optional::<WebSocketProtocols>(
"sec-websocket-protocol",
))
.map(move |ws: ws::Ws, protocol| {
.and(warp::header::optional::<String>("sec-websocket-protocol"))
.map(move |ws: ws::Ws, protocols: Option<String>| {
let schema = schema.clone();
let initializer = initializer.clone();
let protocol = match protocol {
Some(protocol) => protocol,
None => {
// default to the prior standard
WebSocketProtocols::SubscriptionsTransportWS
}
};
let protocol = protocols
.and_then(|protocols| {
protocols
.split(',')
.find_map(|p| WebSocketProtocols::from_str(p.trim()).ok())
})
.unwrap_or(WebSocketProtocols::SubscriptionsTransportWS);
let reply = ws.on_upgrade(move |websocket| {
let (ws_sender, ws_receiver) = websocket.split();