pub fn handshake_with_protocols(
    req: &HttpRequest,
    protocols: &[&str],
) -> Result<HttpResponseBuilder, HandshakeError> {
    // WebSocket accepts only GET
    if *req.method() != Method::GET {
        return Err(HandshakeError::GetMethodRequired);
    }

    // Check for "UPGRADE" to websocket header
    let has_hdr = if let Some(hdr) = req.headers().get(&header::UPGRADE) {
        if let Ok(s) = hdr.to_str() {
            s.to_ascii_lowercase().contains("websocket")
        } else {
            false
        }
    } else {
        false
    };
    if !has_hdr {
        return Err(HandshakeError::NoWebsocketUpgrade);
    }

    // Upgrade connection
    if !req.head().upgrade() {
        return Err(HandshakeError::NoConnectionUpgrade);
    }

    // check supported version
    if !req.headers().contains_key(&header::SEC_WEBSOCKET_VERSION) {
        return Err(HandshakeError::NoVersionHeader);
    }
    let supported_ver = {
        if let Some(hdr) = req.headers().get(&header::SEC_WEBSOCKET_VERSION) {
            hdr == "13" || hdr == "8" || hdr == "7"
        } else {
            false
        }
    };
    if !supported_ver {
        return Err(HandshakeError::UnsupportedVersion);
    }

    // check client handshake for validity
    if !req.headers().contains_key(&header::SEC_WEBSOCKET_KEY) {
        return Err(HandshakeError::BadWebsocketKey);
    }
    let key = {
        let key = req.headers().get(&header::SEC_WEBSOCKET_KEY).unwrap();
        hash_key(key.as_ref())
    };

    // check requested protocols
    let protocol =
        req.headers()
            .get(&header::SEC_WEBSOCKET_PROTOCOL)
            .and_then(|req_protocols| {
                let req_protocols = req_protocols.to_str().ok()?;
                req_protocols
                    .split(',')
                    .map(|req_p| req_p.trim())
                    .find(|req_p| protocols.iter().any(|p| p == req_p))
            });

    let mut response = HttpResponse::build(StatusCode::SWITCHING_PROTOCOLS)
        .upgrade("websocket")
        .header(header::TRANSFER_ENCODING, "chunked")
        .header(header::SEC_WEBSOCKET_ACCEPT, key.as_str())
        .take();

    if let Some(protocol) = protocol {
        response.header(&header::SEC_WEBSOCKET_PROTOCOL, protocol);
    }

    Ok(response)
}
/// Prepare `WebSocket` handshake response.
///
/// This function returns handshake `HttpResponse`, ready to send to peer.
/// It does not perform any IO.
///
/// `protocols` is a sequence of known protocols. On successful handshake,
/// the returned response headers contain the first protocol in this list
/// which the server also knows.
Prepare `WebSocket` handshake response.
This function returns handshake `HttpResponse`, ready to send to peer.
It does not perform any IO.

`protocols` is a sequence of known protocols. On successful handshake,
the returned response headers contain the first protocol in this list
which the server also knows.
Prepare ` WebSocket ` handshake response . This function returns handshake ` HttpResponse ` ready to send to peer . It does not perform any IO . ` protocols ` is a sequence of known protocols . On successful handshake the returned response headers contain the first protocol in this list which the server also knows .
pub fn handshake_with_protocols(
    req: &HttpRequest,
    protocols: &[&str],
) -> Result<HttpResponseBuilder, HandshakeError> {
    // WebSocket accepts only GET
    if *req.method() != Method::GET {
        return Err(HandshakeError::GetMethodRequired);
    }
    // Check for "UPGRADE" to websocket header
    let has_hdr = if let Some(hdr) = req.headers().get(&header::UPGRADE) {
        if let Ok(s) = hdr.to_str() {
            s.to_ascii_lowercase().contains("websocket")
        } else {
            false
        }
    } else {
        false
    };
    if !has_hdr {
        return Err(HandshakeError::NoWebsocketUpgrade);
    }
    // Upgrade connection
    if !req.head().upgrade() {
        return Err(HandshakeError::NoConnectionUpgrade);
    }
    // check supported version
    if !req.headers().contains_key(&header::SEC_WEBSOCKET_VERSION) {
        return Err(HandshakeError::NoVersionHeader);
    }
    let supported_ver = {
        if let Some(hdr) = req.headers().get(&header::SEC_WEBSOCKET_VERSION) {
            hdr == "13" || hdr == "8" || hdr == "7"
        } else {
            false
        }
    };
    if !supported_ver {
        return Err(HandshakeError::UnsupportedVersion);
    }
    // check client handshake for validity
    if !req.headers().contains_key(&header::SEC_WEBSOCKET_KEY) {
        return Err(HandshakeError::BadWebsocketKey);
    }
    let key = {
        let key = req.headers().get(&header::SEC_WEBSOCKET_KEY).unwrap();
        hash_key(key.as_ref())
    };
    // check requested protocols
    let protocol =
        req.headers()
            .get(&header::SEC_WEBSOCKET_PROTOCOL)
            .and_then(|req_protocols| {
                let req_protocols = req_protocols.to_str().ok()?;
                req_protocols
                    .split(',')
                    .map(|req_p| req_p.trim())
                    .find(|req_p| protocols.iter().any(|p| p == req_p))
            });
    let mut response = HttpResponse::build(StatusCode::SWITCHING_PROTOCOLS)
        .upgrade("websocket")
        .header(header::TRANSFER_ENCODING, "chunked")
        .header(header::SEC_WEBSOCKET_ACCEPT, key.as_str())
        .take();
    if let Some(protocol) = protocol {
        response.header(&header::SEC_WEBSOCKET_PROTOCOL, protocol);
    }
    Ok(response)
}
pub fn handshake_with_protocols ( req : & HttpRequest , protocols : & [ & str ] , ) -> Result < HttpResponseBuilder , HandshakeError > { // WebSocket accepts only GET if * req . method ( ) != Method :: GET { return Err ( HandshakeError :: GetMethodRequired ) ; } // Check for "UPGRADE" to websocket header let has_hdr = if let Some ( hdr ) = req . headers ( ) . get ( & header :: UPGRADE ) { if let Ok ( s ) = hdr . to_str ( ) { s . to_ascii_lowercase ( ) . contains ( " " ) } else { false } } else { false } ; if ! has_hdr { return Err ( HandshakeError :: NoWebsocketUpgrade ) ; } // Upgrade connection if ! req . head ( ) . upgrade ( ) { return Err ( HandshakeError :: NoConnectionUpgrade ) ; } // check supported version if ! req . headers ( ) . contains_key ( & header :: SEC_WEBSOCKET_VERSION ) { return Err ( HandshakeError :: NoVersionHeader ) ; } let supported_ver = { if let Some ( hdr ) = req . headers ( ) . get ( & header :: SEC_WEBSOCKET_VERSION ) { hdr == " " || hdr == " " || hdr == " " } else { false } } ; if ! supported_ver { return Err ( HandshakeError :: UnsupportedVersion ) ; } // check client handshake for validity if ! req . headers ( ) . contains_key ( & header :: SEC_WEBSOCKET_KEY ) { return Err ( HandshakeError :: BadWebsocketKey ) ; } let key = { let key = req . headers ( ) . get ( & header :: SEC_WEBSOCKET_KEY ) . unwrap ( ) ; hash_key ( key . as_ref ( ) ) } ; // check requested protocols let protocol = req . headers ( ) . get ( & header :: SEC_WEBSOCKET_PROTOCOL ) . and_then ( | req_protocols | { let req_protocols = req_protocols . to_str ( ) . ok ( ) ? ; req_protocols . split ( ',' ) . map ( | req_p | req_p . trim ( ) ) . find ( | req_p | protocols . iter ( ) . any ( | p | p == req_p ) ) } ) ; let mut response = HttpResponse :: build ( StatusCode :: SWITCHING_PROTOCOLS ) . upgrade ( " " ) . header ( header :: TRANSFER_ENCODING , " " ) . header ( header :: SEC_WEBSOCKET_ACCEPT , key . as_str ( ) ) . take ( ) ; if let Some ( protocol ) = protocol { response . header ( & header :: SEC_WEBSOCKET_PROTOCOL , protocol ) ; } Ok ( response ) }
Prepare `WebSocket` handshake response.
Prepare ` WebSocket ` handshake response .
req
&HttpRequest
protocols
&[&str]