pub fn upgrade_connection<T: Default>(
    mut req: Request<Body>,
) -> Result<
    (
        Response<Body>,
        impl Future<Output = Result<WebSocket<T>, ()>> + Send,
    ),
    Response<Body>,
> {
    let mut res = Response::new(Body::empty());
    let mut header_error = false;
    debug!("We got these headers: {:?}", req.headers());

    if !header_matches(req.headers(), header::UPGRADE, "websocket") {
        error!("Upgrade is not to websocket");
        header_error = true;
    }

    if !header_matches(req.headers(), header::SEC_WEBSOCKET_VERSION, "13") {
        error!("Websocket protocol version must be 13");
        header_error = true;
    }

    if !req
        .headers()
        .typed_get::<headers::Connection>()
        .map(|h| h.contains("Upgrade"))
        .unwrap_or(false)
    {
        error!("It must be upgrade connection");
        header_error = true;
    }

    let key = req.headers().typed_get::<headers::SecWebsocketKey>();

    if key.is_none() {
        error!("Websocket key missing");
        header_error = true;
    }

    if header_error {
        *res.status_mut() = StatusCode::BAD_REQUEST;
        return Err(res);
    }

    *res.status_mut() = StatusCode::SWITCHING_PROTOCOLS;
    let h = res.headers_mut();
    h.typed_insert(headers::Upgrade::websocket());
    h.typed_insert(headers::SecWebsocketAccept::from(key.unwrap()));
    h.typed_insert(headers::Connection::upgrade());
    let upgraded = upgrade::on(&mut req)
        .map_err(|err| error!("Cannot create websocket: {} ", err))
        .and_then(|upgraded| async {
            debug!("Connection upgraded to websocket");
            let r = WebSocket::new(upgraded).await;
            Ok(r)
        });

    Ok((res, upgraded))
}
/// This function does basic websocket handshake,
/// return tuple of successful HTTP response (with status 101 - Protocol Upgrade) and
/// future resolving to Websocket( struct implementing Stream and Sink of messages) or
/// error response (status 400) if websocket handshake was not successful
///
/// Websocket can have context of type T, which is then shared (guarded by RwLock) with all
/// messages in this websocket.
This function does basic websocket handshake,
return tuple of successful HTTP response (with status 101 - Protocol Upgrade) and
future resolving to Websocket( struct implementing Stream and Sink of messages) or
error response (status 400) if websocket handshake was not successful

Websocket can have context of type T, which is then shared (guarded by RwLock) with all
messages in this websocket.
This function does basic websocket handshake return tuple of successful HTTP response ( with status 101 - Protocol Upgrade ) and future resolving to Websocket ( struct implementing Stream and Sink of messages ) or error response ( status 400 ) if websocket handshake was not successful Websocket can have context of type T which is then shared ( guarded by RwLock ) with all messages in this websocket .
pub fn upgrade_connection<T: Default>(
    mut req: Request<Body>,
) -> Result<
    (
        Response<Body>,
        impl Future<Output = Result<WebSocket<T>, ()>> + Send,
    ),
    Response<Body>,
> {
    let mut res = Response::new(Body::empty());
    let mut header_error = false;
    debug!("We got these headers: {:?}", req.headers());
    if !header_matches(req.headers(), header::UPGRADE, "websocket") {
        error!("Upgrade is not to websocket");
        header_error = true;
    }
    if !header_matches(req.headers(), header::SEC_WEBSOCKET_VERSION, "13") {
        error!("Websocket protocol version must be 13");
        header_error = true;
    }
    if !req
        .headers()
        .typed_get::<headers::Connection>()
        .map(|h| h.contains("Upgrade"))
        .unwrap_or(false)
    {
        error!("It must be upgrade connection");
        header_error = true;
    }
    let key = req.headers().typed_get::<headers::SecWebsocketKey>();
    if key.is_none() {
        error!("Websocket key missing");
        header_error = true;
    }
    if header_error {
        *res.status_mut() = StatusCode::BAD_REQUEST;
        return Err(res);
    }
    *res.status_mut() = StatusCode::SWITCHING_PROTOCOLS;
    let h = res.headers_mut();
    h.typed_insert(headers::Upgrade::websocket());
    h.typed_insert(headers::SecWebsocketAccept::from(key.unwrap()));
    h.typed_insert(headers::Connection::upgrade());
    let upgraded = upgrade::on(&mut req)
        .map_err(|err| error!("Cannot create websocket: {} ", err))
        .and_then(|upgraded| async {
            debug!("Connection upgraded to websocket");
            let r = WebSocket::new(upgraded).await;
            Ok(r)
        });
    Ok((res, upgraded))
}
pub fn upgrade_connection < T : Default > ( mut req : Request < Body > , ) -> Result < ( Response < Body > , impl Future < Output = Result < WebSocket < T > , ( ) > > + Send , ) , Response < Body > , > { let mut res = Response :: new ( Body :: empty ( ) ) ; let mut header_error = false ; debug ! ( " " req headers ( ) ) ; if ! header_matches ( req . headers ( ) , header :: UPGRADE , " " ) { error ! ( " " ) ; header_error = true ; } if ! header_matches ( req . headers ( ) , header :: SEC_WEBSOCKET_VERSION , " " ) { error ! ( " " ) ; header_error = true ; } if ! req . headers ( ) . typed_get :: < headers :: Connection > ( ) . map ( | h | h . contains ( " " ) ) . unwrap_or ( false ) { error ! ( " " ) ; header_error = true ; } let key = req . headers ( ) . typed_get :: < headers :: SecWebsocketKey > ( ) ; if key . is_none ( ) { error ! ( " " ) ; header_error = true ; } if header_error { * res . status_mut ( ) = StatusCode :: BAD_REQUEST ; return Err ( res ) ; } * res . status_mut ( ) = StatusCode :: SWITCHING_PROTOCOLS ; let h = res . headers_mut ( ) ; h . typed_insert ( headers :: Upgrade :: websocket ( ) ) ; h . typed_insert ( headers :: SecWebsocketAccept :: from ( key . unwrap ( ) ) ) ; h . typed_insert ( headers :: Connection :: upgrade ( ) ) ; let upgraded = upgrade :: on ( & mut req ) . map_err ( | err | error ! ( " " err ) ) . and_then ( | upgraded | async { debug ! ( " " ) ; let r = WebSocket :: new ( upgraded ) . await ; Ok ( r ) } ) ; Ok ( ( res , upgraded ) ) }
This function does basic websocket handshake,
return tuple of successful HTTP response (with status 101 - Protocol Upgrade) and
future resolving to Websocket( struct implementing Stream and Sink of messages) or
error response (status 400) if websocket handshake was not successful
This function does basic websocket handshake return tuple of successful HTTP response ( with status 101 - Protocol Upgrade ) and future resolving to Websocket ( struct implementing Stream and Sink of messages ) or error response ( status 400 ) if websocket handshake was not successful
req
Request<Body>
req
Request<Body>