use std::fmt::Write as FmtWrite; use std::io::Write; use std::rc::Rc; use std::time::Duration; use std::{fmt, net}; use bytes::{BufMut, Bytes, BytesMut}; use futures::Stream; use percent_encoding::percent_encode; use serde::Serialize; use actix_http::body::Body; use actix_http::cookie::{Cookie, CookieJar, USERINFO}; use actix_http::http::header::{self, Header, IntoHeaderValue}; use actix_http::http::{ uri, ConnectionType, Error as HttpError, HeaderMap, HeaderName, HeaderValue, HttpTryFrom, Method, Uri, Version, }; use actix_http::{Error, RequestHead}; use crate::error::{FreezeRequestError, InvalidUrl}; use crate::frozen::FrozenClientRequest; use crate::sender::{PrepForSendingError, RequestSender, SendClientRequest}; use crate::ClientConfig; #[cfg(any(feature = "brotli", feature = "flate2-zlib", feature = "flate2-rust"))] const HTTPS_ENCODING: &str = "br, gzip, deflate"; #[cfg(all( any(feature = "flate2-zlib", feature = "flate2-rust"), not(feature = "brotli") ))] const HTTPS_ENCODING: &str = "gzip, deflate"; /// An HTTP Client request builder /// /// This type can be used to construct an instance of `ClientRequest` through a /// builder-like pattern. /// /// ```rust /// use futures::future::{Future, lazy}; /// use actix_rt::System; /// /// fn main() { /// System::new("test").block_on(lazy(|| { /// awc::Client::new() /// .get("http://www.rust-lang.org") // <- Create request builder /// .header("User-Agent", "Actix-web") /// .send() // <- Send http request /// .map_err(|_| ()) /// .and_then(|response| { // <- server http response /// println!("Response: {:?}", response); /// Ok(()) /// }) /// })); /// } /// ``` pub struct ClientRequest { pub(crate) head: RequestHead, err: Option, addr: Option, cookies: Option, response_decompress: bool, timeout: Option, config: Rc, } impl ClientRequest { /// Create new client request builder. pub(crate) fn new(method: Method, uri: U, config: Rc) -> Self where Uri: HttpTryFrom, { ClientRequest { config, head: RequestHead::default(), err: None, addr: None, cookies: None, timeout: None, response_decompress: true, } .method(method) .uri(uri) } /// Set HTTP URI of request. #[inline] pub fn uri(mut self, uri: U) -> Self where Uri: HttpTryFrom, { match Uri::try_from(uri) { Ok(uri) => self.head.uri = uri, Err(e) => self.err = Some(e.into()), } self } /// Get HTTP URI of request. pub fn get_uri(&self) -> &Uri { &self.head.uri } /// Set socket address of the server. /// /// This address is used for connection. If address is not /// provided url's host name get resolved. pub fn address(mut self, addr: net::SocketAddr) -> Self { self.addr = Some(addr); self } /// Set HTTP method of this request. #[inline] pub fn method(mut self, method: Method) -> Self { self.head.method = method; self } /// Get HTTP method of this request pub fn get_method(&self) -> &Method { &self.head.method } #[doc(hidden)] /// Set HTTP version of this request. /// /// By default requests's HTTP version depends on network stream #[inline] pub fn version(mut self, version: Version) -> Self { self.head.version = version; self } /// Get HTTP version of this request. pub fn get_version(&self) -> &Version { &self.head.version } /// Get peer address of this request. pub fn get_peer_addr(&self) -> &Option { &self.head.peer_addr } #[inline] /// Returns request's headers. pub fn headers(&self) -> &HeaderMap { &self.head.headers } #[inline] /// Returns request's mutable headers. pub fn headers_mut(&mut self) -> &mut HeaderMap { &mut self.head.headers } /// Set a header. /// /// ```rust /// fn main() { /// # actix_rt::System::new("test").block_on(futures::future::lazy(|| { /// let req = awc::Client::new() /// .get("http://www.rust-lang.org") /// .set(awc::http::header::Date::now()) /// .set(awc::http::header::ContentType(mime::TEXT_HTML)); /// # Ok::<_, ()>(()) /// # })); /// } /// ``` pub fn set(mut self, hdr: H) -> Self { match hdr.try_into() { Ok(value) => { self.head.headers.insert(H::name(), value); } Err(e) => self.err = Some(e.into()), } self } /// Append a header. /// /// Header gets appended to existing header. /// To override header use `set_header()` method. /// /// ```rust /// use awc::{http, Client}; /// /// fn main() { /// # actix_rt::System::new("test").block_on(futures::future::lazy(|| { /// let req = Client::new() /// .get("http://www.rust-lang.org") /// .header("X-TEST", "value") /// .header(http::header::CONTENT_TYPE, "application/json"); /// # Ok::<_, ()>(()) /// # })); /// } /// ``` 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.err = Some(e.into()), }, Err(e) => self.err = Some(e.into()), } self } /// Insert a header, replaces existing header. pub fn set_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.insert(key, value), Err(e) => self.err = Some(e.into()), }, Err(e) => self.err = Some(e.into()), } self } /// Insert a header only if it is not yet set. pub fn set_header_if_none(mut self, key: K, value: V) -> Self where HeaderName: HttpTryFrom, V: IntoHeaderValue, { match HeaderName::try_from(key) { Ok(key) => { if !self.head.headers.contains_key(&key) { match value.try_into() { Ok(value) => self.head.headers.insert(key, value), Err(e) => self.err = Some(e.into()), } } } Err(e) => self.err = Some(e.into()), } self } /// Send headers in `Camel-Case` form. #[inline] pub fn camel_case(mut self) -> Self { self.head.set_camel_case_headers(true); self } /// Force close connection instead of returning it back to connections pool. /// This setting affect only http/1 connections. #[inline] pub fn force_close(mut self) -> Self { self.head.set_connection_type(ConnectionType::Close); self } /// Set request's content type #[inline] pub fn content_type(mut self, value: V) -> Self where HeaderValue: HttpTryFrom, { match HeaderValue::try_from(value) { Ok(value) => self.head.headers.insert(header::CONTENT_TYPE, value), Err(e) => self.err = Some(e.into()), } self } /// Set content length #[inline] pub fn content_length(self, len: u64) -> Self { let mut wrt = BytesMut::new().writer(); let _ = write!(wrt, "{}", len); self.header(header::CONTENT_LENGTH, wrt.get_mut().take().freeze()) } /// Set HTTP basic authorization header pub fn basic_auth(self, username: U, password: Option<&str>) -> Self where U: fmt::Display, { let auth = match password { Some(password) => format!("{}:{}", username, password), None => format!("{}:", username), }; self.header( header::AUTHORIZATION, format!("Basic {}", base64::encode(&auth)), ) } /// Set HTTP bearer authentication header pub fn bearer_auth(self, token: T) -> Self where T: fmt::Display, { self.header(header::AUTHORIZATION, format!("Bearer {}", token)) } /// Set a cookie /// /// ```rust /// # use actix_rt::System; /// # use futures::future::{lazy, Future}; /// fn main() { /// System::new("test").block_on(lazy(|| { /// awc::Client::new().get("https://www.rust-lang.org") /// .cookie( /// awc::http::Cookie::build("name", "value") /// .domain("www.rust-lang.org") /// .path("/") /// .secure(true) /// .http_only(true) /// .finish(), /// ) /// .send() /// .map_err(|_| ()) /// .and_then(|response| { /// println!("Response: {:?}", response); /// Ok(()) /// }) /// })); /// } /// ``` pub fn cookie(mut self, cookie: Cookie<'_>) -> Self { if self.cookies.is_none() { let mut jar = CookieJar::new(); jar.add(cookie.into_owned()); self.cookies = Some(jar) } else { self.cookies.as_mut().unwrap().add(cookie.into_owned()); } self } /// Disable automatic decompress of response's body pub fn no_decompress(mut self) -> Self { self.response_decompress = false; self } /// Set request timeout. Overrides client wide timeout setting. /// /// Request timeout is the total time before a response must be received. /// Default value is 5 seconds. pub fn timeout(mut self, timeout: Duration) -> Self { self.timeout = Some(timeout); self } /// This method calls provided closure with builder reference if /// value is `true`. pub fn if_true(self, value: bool, f: F) -> Self where F: FnOnce(ClientRequest) -> ClientRequest, { if value { f(self) } else { self } } /// This method calls provided closure with builder reference if /// value is `Some`. pub fn if_some(self, value: Option, f: F) -> Self where F: FnOnce(T, ClientRequest) -> ClientRequest, { if let Some(val) = value { f(val, self) } else { self } } /// Freeze request builder and construct `FrozenClientRequest`, /// which could be used for sending same request multiple times. pub fn freeze(self) -> Result { let slf = match self.prep_for_sending() { Ok(slf) => slf, Err(e) => return Err(e.into()), }; let request = FrozenClientRequest { head: Rc::new(slf.head), addr: slf.addr, response_decompress: slf.response_decompress, timeout: slf.timeout, config: slf.config, }; Ok(request) } /// Complete request construction and send body. pub fn send_body(self, body: B) -> SendClientRequest where B: Into, { let slf = match self.prep_for_sending() { Ok(slf) => slf, Err(e) => return e.into(), }; RequestSender::Owned(slf.head).send_body( slf.addr, slf.response_decompress, slf.timeout, slf.config.as_ref(), body, ) } /// Set a JSON body and generate `ClientRequest` pub fn send_json(self, value: &T) -> SendClientRequest { let slf = match self.prep_for_sending() { Ok(slf) => slf, Err(e) => return e.into(), }; RequestSender::Owned(slf.head).send_json( slf.addr, slf.response_decompress, slf.timeout, slf.config.as_ref(), value, ) } /// Set a urlencoded body and generate `ClientRequest` /// /// `ClientRequestBuilder` can not be used after this call. pub fn send_form(self, value: &T) -> SendClientRequest { let slf = match self.prep_for_sending() { Ok(slf) => slf, Err(e) => return e.into(), }; RequestSender::Owned(slf.head).send_form( slf.addr, slf.response_decompress, slf.timeout, slf.config.as_ref(), value, ) } /// Set an streaming body and generate `ClientRequest`. pub fn send_stream(self, stream: S) -> SendClientRequest where S: Stream + 'static, E: Into + 'static, { let slf = match self.prep_for_sending() { Ok(slf) => slf, Err(e) => return e.into(), }; RequestSender::Owned(slf.head).send_stream( slf.addr, slf.response_decompress, slf.timeout, slf.config.as_ref(), stream, ) } /// Set an empty body and generate `ClientRequest`. pub fn send(self) -> SendClientRequest { let slf = match self.prep_for_sending() { Ok(slf) => slf, Err(e) => return e.into(), }; RequestSender::Owned(slf.head).send( slf.addr, slf.response_decompress, slf.timeout, slf.config.as_ref(), ) } fn prep_for_sending(mut self) -> Result { if let Some(e) = self.err { return Err(e.into()); } // validate uri let uri = &self.head.uri; if uri.host().is_none() { return Err(InvalidUrl::MissingHost.into()); } else if uri.scheme_part().is_none() { return Err(InvalidUrl::MissingScheme.into()); } else if let Some(scheme) = uri.scheme_part() { match scheme.as_str() { "http" | "ws" | "https" | "wss" => (), _ => return Err(InvalidUrl::UnknownScheme.into()), } } else { return Err(InvalidUrl::UnknownScheme.into()); } // 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(), USERINFO); let value = percent_encode(c.value().as_bytes(), USERINFO); let _ = write!(&mut cookie, "; {}={}", name, value); } self.head.headers.insert( header::COOKIE, HeaderValue::from_str(&cookie.as_str()[2..]).unwrap(), ); } let mut slf = self; // enable br only for https #[cfg(any( feature = "brotli", feature = "flate2-zlib", feature = "flate2-rust" ))] { if slf.response_decompress { let https = slf .head .uri .scheme_part() .map(|s| s == &uri::Scheme::HTTPS) .unwrap_or(true); if https { slf = slf.set_header_if_none(header::ACCEPT_ENCODING, HTTPS_ENCODING) } else { #[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))] { slf = slf .set_header_if_none(header::ACCEPT_ENCODING, "gzip, deflate") } }; } } Ok(slf) } } impl fmt::Debug for ClientRequest { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { writeln!( f, "\nClientRequest {:?} {}:{}", self.head.version, self.head.method, self.head.uri )?; writeln!(f, " headers:")?; for (key, val) in self.head.headers.iter() { writeln!(f, " {:?}: {:?}", key, val)?; } Ok(()) } } #[cfg(test)] mod tests { use std::time::SystemTime; use super::*; use crate::Client; #[test] fn test_debug() { let request = Client::new().get("/").header("x-test", "111"); let repr = format!("{:?}", request); assert!(repr.contains("ClientRequest")); assert!(repr.contains("x-test")); } #[test] fn test_basics() { let mut req = Client::new() .put("/") .version(Version::HTTP_2) .set(header::Date(SystemTime::now().into())) .content_type("plain/text") .if_true(true, |req| req.header(header::SERVER, "awc")) .if_true(false, |req| req.header(header::EXPECT, "awc")) .if_some(Some("server"), |val, req| { req.header(header::USER_AGENT, val) }) .if_some(Option::<&str>::None, |_, req| { req.header(header::ALLOW, "1") }) .content_length(100); assert!(req.headers().contains_key(header::CONTENT_TYPE)); assert!(req.headers().contains_key(header::DATE)); assert!(req.headers().contains_key(header::SERVER)); assert!(req.headers().contains_key(header::USER_AGENT)); assert!(!req.headers().contains_key(header::ALLOW)); assert!(!req.headers().contains_key(header::EXPECT)); assert_eq!(req.head.version, Version::HTTP_2); let _ = req.headers_mut(); let _ = req.send_body(""); } #[test] fn test_client_header() { let req = Client::build() .header(header::CONTENT_TYPE, "111") .finish() .get("/"); assert_eq!( req.head .headers .get(header::CONTENT_TYPE) .unwrap() .to_str() .unwrap(), "111" ); } #[test] fn test_client_header_override() { let req = Client::build() .header(header::CONTENT_TYPE, "111") .finish() .get("/") .set_header(header::CONTENT_TYPE, "222"); assert_eq!( req.head .headers .get(header::CONTENT_TYPE) .unwrap() .to_str() .unwrap(), "222" ); } #[test] fn client_basic_auth() { let req = Client::new() .get("/") .basic_auth("username", Some("password")); assert_eq!( req.head .headers .get(header::AUTHORIZATION) .unwrap() .to_str() .unwrap(), "Basic dXNlcm5hbWU6cGFzc3dvcmQ=" ); let req = Client::new().get("/").basic_auth("username", None); assert_eq!( req.head .headers .get(header::AUTHORIZATION) .unwrap() .to_str() .unwrap(), "Basic dXNlcm5hbWU6" ); } #[test] fn client_bearer_auth() { let req = Client::new().get("/").bearer_auth("someS3cr3tAutht0k3n"); assert_eq!( req.head .headers .get(header::AUTHORIZATION) .unwrap() .to_str() .unwrap(), "Bearer someS3cr3tAutht0k3n" ); } }