pub fn finish(&mut self) -> Result<WsClient<F, T>, WsClientBuilderError> {
        if let Some(e) = self.err.take() {
            return Err(WsClientBuilderError::Http(e));
        }

        // #[allow(unused_mut)]
        let mut inner = self.inner.take().expect("cannot reuse WsClient builder");

        // validate uri
        let uri = &inner.head.uri;
        if uri.host().is_none() {
            return Err(WsClientBuilderError::MissingHost);
        } else if uri.scheme().is_none() {
            return Err(WsClientBuilderError::MissingScheme);
        } else if let Some(scheme) = uri.scheme() {
            match scheme.as_str() {
                "http" | "ws" | "https" | "wss" => (),
                _ => return Err(WsClientBuilderError::UnknownScheme),
            }
        } else {
            return Err(WsClientBuilderError::UnknownScheme);
        }

        if !inner.head.headers.contains_key(header::HOST) {
            inner.head.headers.insert(
                header::HOST,
                HeaderValue::from_str(uri.host().unwrap()).unwrap(),
            );
        }

        #[cfg(feature = "cookie")]
        {
            use percent_encoding::percent_encode;
            use std::fmt::Write as FmtWrite;

            // set cookies
            if let Some(ref mut jar) = self.cookies {
                let mut cookie = String::new();
                for c in jar.delta() {
                    let name =
                        percent_encode(c.name().as_bytes(), crate::http::helpers::USERINFO);
                    let value = percent_encode(
                        c.value().as_bytes(),
                        crate::http::helpers::USERINFO,
                    );
                    let _ = write!(cookie, "; {}={}", name, value);
                }
                inner.head.headers.insert(
                    header::COOKIE,
                    HeaderValue::from_str(&cookie.as_str()[2..]).unwrap(),
                );
            }
        }

        // origin
        if let Some(origin) = self.origin.take() {
            inner.head.headers.insert(header::ORIGIN, origin);
        }

        inner.head.set_connection_type(ConnectionType::Upgrade);
        inner
            .head
            .headers
            .insert(header::UPGRADE, HeaderValue::from_static("websocket"));
        inner.head.headers.insert(
            header::SEC_WEBSOCKET_VERSION,
            HeaderValue::from_static("13"),
        );

        if let Some(protocols) = self.protocols.take() {
            inner.head.headers.insert(
                header::SEC_WEBSOCKET_PROTOCOL,
                HeaderValue::try_from(protocols.as_str()).unwrap(),
            );
        }

        Ok(WsClient {
            connector: inner.connector,
            head: Rc::new(inner.head),
            addr: inner.addr,
            max_size: inner.max_size,
            server_mode: inner.server_mode,
            timeout: inner.timeout,
            keepalive_timeout: inner.keepalive_timeout,
            extra_headers: RefCell::new(None),
            _t: marker::PhantomData,
        })
    }
/// Complete building process and construct websockets client.
Complete building process and construct websockets client.
Complete building process and construct websockets client .
pub fn finish(&mut self) -> Result<WsClient<F, T>, WsClientBuilderError> {
        if let Some(e) = self.err.take() {
            return Err(WsClientBuilderError::Http(e));
        }
        // #[allow(unused_mut)]
        let mut inner = self.inner.take().expect("cannot reuse WsClient builder");
        // validate uri
        let uri = &inner.head.uri;
        if uri.host().is_none() {
            return Err(WsClientBuilderError::MissingHost);
        } else if uri.scheme().is_none() {
            return Err(WsClientBuilderError::MissingScheme);
        } else if let Some(scheme) = uri.scheme() {
            match scheme.as_str() {
                "http" | "ws" | "https" | "wss" => (),
                _ => return Err(WsClientBuilderError::UnknownScheme),
            }
        } else {
            return Err(WsClientBuilderError::UnknownScheme);
        }
        if !inner.head.headers.contains_key(header::HOST) {
            inner.head.headers.insert(
                header::HOST,
                HeaderValue::from_str(uri.host().unwrap()).unwrap(),
            );
        }
        #[cfg(feature = "cookie")]
        {
            use percent_encoding::percent_encode;
            use std::fmt::Write as FmtWrite;
            // set cookies
            if let Some(ref mut jar) = self.cookies {
                let mut cookie = String::new();
                for c in jar.delta() {
                    let name =
                        percent_encode(c.name().as_bytes(), crate::http::helpers::USERINFO);
                    let value = percent_encode(
                        c.value().as_bytes(),
                        crate::http::helpers::USERINFO,
                    );
                    let _ = write!(cookie, "; {}={}", name, value);
                }
                inner.head.headers.insert(
                    header::COOKIE,
                    HeaderValue::from_str(&cookie.as_str()[2..]).unwrap(),
                );
            }
        }
        // origin
        if let Some(origin) = self.origin.take() {
            inner.head.headers.insert(header::ORIGIN, origin);
        }
        inner.head.set_connection_type(ConnectionType::Upgrade);
        inner
            .head
            .headers
            .insert(header::UPGRADE, HeaderValue::from_static("websocket"));
        inner.head.headers.insert(
            header::SEC_WEBSOCKET_VERSION,
            HeaderValue::from_static("13"),
        );
        if let Some(protocols) = self.protocols.take() {
            inner.head.headers.insert(
                header::SEC_WEBSOCKET_PROTOCOL,
                HeaderValue::try_from(protocols.as_str()).unwrap(),
            );
        }
        Ok(WsClient {
            connector: inner.connector,
            head: Rc::new(inner.head),
            addr: inner.addr,
            max_size: inner.max_size,
            server_mode: inner.server_mode,
            timeout: inner.timeout,
            keepalive_timeout: inner.keepalive_timeout,
            extra_headers: RefCell::new(None),
            _t: marker::PhantomData,
        })
    }
pub fn finish ( & mut self ) -> Result < WsClient < F , T > , WsClientBuilderError > { if let Some ( e ) = self . err . take ( ) { return Err ( WsClientBuilderError :: Http ( e ) ) ; } // #[allow(unused_mut)] let mut inner = self . inner . take ( ) . expect ( " " ) ; // validate uri let uri = & inner . head . uri ; if uri . host ( ) . is_none ( ) { return Err ( WsClientBuilderError :: MissingHost ) ; } else if uri . scheme ( ) . is_none ( ) { return Err ( WsClientBuilderError :: MissingScheme ) ; } else if let Some ( scheme ) = uri . scheme ( ) { match scheme . as_str ( ) { " " | " " | " " | " " => ( ) , _ => return Err ( WsClientBuilderError :: UnknownScheme ) , } } else { return Err ( WsClientBuilderError :: UnknownScheme ) ; } if ! inner . head . headers . contains_key ( header :: HOST ) { inner . head . headers . insert ( header :: HOST , HeaderValue :: from_str ( uri . host ( ) . unwrap ( ) ) . unwrap ( ) , ) ; } # [ cfg ( feature " " ) ] { use percent_encoding :: percent_encode ; use std :: fmt :: Write as FmtWrite ; // set cookies if let Some ( ref mut jar ) = self . cookies { let mut cookie = String :: new ( ) ; for c in jar . delta ( ) { let name = percent_encode ( c . name ( ) . as_bytes ( ) , crate :: http :: helpers :: USERINFO ) ; let value = percent_encode ( c . value ( ) . as_bytes ( ) , crate :: http :: helpers :: USERINFO , ) ; let _ = write ! ( cookie " " name value ) ; } inner . head . headers . insert ( header :: COOKIE , HeaderValue :: from_str ( & cookie . as_str ( ) [ 2 .. ] ) . unwrap ( ) , ) ; } } // origin if let Some ( origin ) = self . origin . take ( ) { inner . head . headers . insert ( header :: ORIGIN , origin ) ; } inner . head . set_connection_type ( ConnectionType :: Upgrade ) ; inner . head . headers . insert ( header :: UPGRADE , HeaderValue :: from_static ( " " ) ) ; inner . head . headers . insert ( header :: SEC_WEBSOCKET_VERSION , HeaderValue :: from_static ( " " ) , ) ; if let Some ( protocols ) = self . protocols . take ( ) { inner . head . headers . insert ( header :: SEC_WEBSOCKET_PROTOCOL , HeaderValue :: try_from ( protocols . as_str ( ) ) . unwrap ( ) , ) ; } Ok ( WsClient { connector : inner . connector , head : Rc :: new ( inner . head ) , addr : inner . addr , max_size : inner . max_size , server_mode : inner . server_mode , timeout : inner . timeout , keepalive_timeout : inner . keepalive_timeout , extra_headers : RefCell :: new ( None ) , _t : marker :: PhantomData , } ) }
Complete building process and construct websockets client.
Complete building process and construct websockets client .
&mut self
&mut self