//! Http client request use std::str; #[cfg(feature = "cookies")] use cookie::Cookie; use http::header::{HeaderName, HeaderValue}; use http::{Error as HttpError, HttpTryFrom, Uri}; use super::ClientError; use crate::header::IntoHeaderValue; use crate::message::RequestHead; /// `WebSocket` connection pub struct Connect { pub(super) head: RequestHead, pub(super) err: Option, pub(super) http_err: Option, pub(super) origin: Option, pub(super) protocols: Option, pub(super) max_size: usize, pub(super) server_mode: bool, } impl Connect { /// Create new websocket connection pub fn new>(uri: S) -> Connect { let mut cl = Connect { head: RequestHead::default(), err: None, http_err: None, origin: None, protocols: None, max_size: 65_536, server_mode: false, }; match Uri::try_from(uri.as_ref()) { Ok(uri) => cl.head.uri = uri, Err(e) => cl.http_err = Some(e.into()), } cl } /// Set supported websocket protocols pub fn protocols(mut self, protos: U) -> Self where U: IntoIterator + 'static, V: AsRef, { let mut protos = protos .into_iter() .fold(String::new(), |acc, s| acc + s.as_ref() + ","); protos.pop(); self.protocols = Some(protos); self } // #[cfg(feature = "cookies")] // /// Set cookie for handshake request // pub fn cookie(mut self, cookie: Cookie) -> Self { // self.request.cookie(cookie); // self // } /// Set request Origin pub fn origin(mut self, origin: V) -> Self where HeaderValue: HttpTryFrom, { match HeaderValue::try_from(origin) { Ok(value) => self.origin = Some(value), Err(e) => self.http_err = Some(e.into()), } self } /// Set max frame size /// /// By default max size is set to 64kb pub fn max_frame_size(mut self, size: usize) -> Self { self.max_size = size; self } /// Disable payload masking. By default ws client masks frame payload. pub fn server_mode(mut self) -> Self { self.server_mode = true; self } /// Set request header pub fn header(mut self, key: K, value: V) -> Self where HeaderName: HttpTryFrom, V: IntoHeaderValue, { match HeaderName::try_from(key) { Ok(key) => match value.try_into() { Ok(value) => { self.head.headers.append(key, value); } Err(e) => self.http_err = Some(e.into()), }, Err(e) => self.http_err = Some(e.into()), } self } }