1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-12-18 06:06:36 +00:00
actix-web/awc/src/builder.rs

238 lines
6.9 KiB
Rust
Raw Normal View History

2019-12-05 17:35:43 +00:00
use std::convert::TryFrom;
2019-03-26 04:58:01 +00:00
use std::fmt;
use std::rc::Rc;
2019-03-29 05:33:41 +00:00
use std::time::Duration;
2019-03-26 04:58:01 +00:00
use actix_http::client::{Connect as HttpConnect, ConnectError, Connection, Connector};
2020-03-08 07:42:45 +00:00
use actix_http::http::{self, header, Error as HttpError, HeaderMap, HeaderName};
use actix_service::Service;
2019-03-26 04:58:01 +00:00
2020-03-08 07:42:45 +00:00
use crate::connect::{Connect, ConnectorWrapper};
use crate::{Client, ClientConfig};
2019-03-26 04:58:01 +00:00
/// An HTTP Client builder
///
/// This type can be used to construct an instance of `Client` through a
/// builder-like pattern.
pub struct ClientBuilder {
default_headers: bool,
allow_redirects: bool,
max_redirects: usize,
max_http_version: Option<http::Version>,
stream_window_size: Option<u32>,
conn_window_size: Option<u32>,
headers: HeaderMap,
timeout: Option<Duration>,
connector: Option<Box<dyn Connect>>,
2019-03-26 04:58:01 +00:00
}
2019-07-17 09:08:30 +00:00
impl Default for ClientBuilder {
fn default() -> Self {
Self::new()
}
}
2019-03-26 04:58:01 +00:00
impl ClientBuilder {
pub fn new() -> Self {
ClientBuilder {
default_headers: true,
allow_redirects: true,
max_redirects: 10,
headers: HeaderMap::new(),
timeout: Some(Duration::from_secs(5)),
connector: None,
max_http_version: None,
stream_window_size: None,
conn_window_size: None,
2019-03-26 04:58:01 +00:00
}
}
/// Use custom connector service.
pub fn connector<T>(mut self, connector: T) -> Self
where
T: Service<HttpConnect, Error = ConnectError> + 'static,
T::Response: Connection,
<T::Response as Connection>::Future: 'static,
T::Future: 'static,
{
self.connector = Some(Box::new(ConnectorWrapper(connector)));
2019-03-29 05:33:41 +00:00
self
}
/// Set request timeout
///
/// 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);
2019-03-29 05:33:41 +00:00
self
}
/// Disable request timeout.
pub fn disable_timeout(mut self) -> Self {
self.timeout = None;
self
}
2019-03-26 04:58:01 +00:00
/// Do not follow redirects.
///
/// Redirects are allowed by default.
pub fn disable_redirects(mut self) -> Self {
self.allow_redirects = false;
self
}
/// Maximum supported http major version
/// Supported versions http/1.1, http/2
pub fn max_http_version(mut self, val: http::Version) -> Self {
self.max_http_version = Some(val);
self
}
/// Indicates the initial window size (in octets) for
/// HTTP2 stream-level flow control for received data.
///
/// The default value is 65,535 and is good for APIs, but not for big objects.
pub fn initial_window_size(mut self, size: u32) -> Self {
self.stream_window_size = Some(size);
self
}
/// Indicates the initial window size (in octets) for
/// HTTP2 connection-level flow control for received data.
///
/// The default value is 65,535 and is good for APIs, but not for big objects.
pub fn initial_connection_window_size(mut self, size: u32) -> Self {
self.conn_window_size = Some(size);
self
}
2019-03-26 04:58:01 +00:00
/// Set max number of redirects.
///
/// Max redirects is set to 10 by default.
pub fn max_redirects(mut self, num: usize) -> Self {
self.max_redirects = num;
self
}
/// Do not add default request headers.
2019-03-27 05:33:01 +00:00
/// By default `Date` and `User-Agent` headers are set.
pub fn no_default_headers(mut self) -> Self {
2019-03-26 04:58:01 +00:00
self.default_headers = false;
self
}
2019-03-29 21:07:37 +00:00
/// Add default header. Headers added by this method
2019-03-27 05:33:01 +00:00
/// get added to every request.
2019-03-26 04:58:01 +00:00
pub fn header<K, V>(mut self, key: K, value: V) -> Self
where
2019-12-05 17:35:43 +00:00
HeaderName: TryFrom<K>,
<HeaderName as TryFrom<K>>::Error: fmt::Debug + Into<HttpError>,
V: header::IntoHeaderValue,
2019-03-26 04:58:01 +00:00
V::Error: fmt::Debug,
{
match HeaderName::try_from(key) {
2021-01-15 02:11:10 +00:00
Ok(key) => match value.try_into_value() {
2019-03-26 04:58:01 +00:00
Ok(value) => {
self.headers.append(key, value);
2019-03-26 04:58:01 +00:00
}
Err(e) => log::error!("Header value error: {:?}", e),
},
Err(e) => log::error!("Header name error: {:?}", e),
}
self
}
/// Set client wide HTTP basic authorization header
pub fn basic_auth<U>(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 client wide HTTP bearer authentication header
pub fn bearer_auth<T>(self, token: T) -> Self
where
T: fmt::Display,
{
self.header(header::AUTHORIZATION, format!("Bearer {}", token))
}
2019-03-27 05:33:01 +00:00
/// Finish build process and create `Client` instance.
2019-03-26 04:58:01 +00:00
pub fn finish(self) -> Client {
let connector = if let Some(connector) = self.connector {
connector
} else {
let mut connector = Connector::new();
if let Some(val) = self.max_http_version {
connector = connector.max_http_version(val)
};
if let Some(val) = self.conn_window_size {
connector = connector.initial_connection_window_size(val)
};
if let Some(val) = self.stream_window_size {
connector = connector.initial_window_size(val)
};
Box::new(ConnectorWrapper(connector.finish())) as _
};
let config = ClientConfig {
headers: self.headers,
timeout: self.timeout,
connector,
};
Client(Rc::new(config))
2019-03-26 04:58:01 +00:00
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn client_basic_auth() {
2019-05-12 15:34:51 +00:00
let client = ClientBuilder::new().basic_auth("username", Some("password"));
assert_eq!(
client
.headers
.get(header::AUTHORIZATION)
.unwrap()
.to_str()
.unwrap(),
"Basic dXNlcm5hbWU6cGFzc3dvcmQ="
);
let client = ClientBuilder::new().basic_auth("username", None);
assert_eq!(
client
.headers
.get(header::AUTHORIZATION)
.unwrap()
.to_str()
.unwrap(),
"Basic dXNlcm5hbWU6"
2019-05-12 15:34:51 +00:00
);
}
#[test]
fn client_bearer_auth() {
2019-05-12 15:34:51 +00:00
let client = ClientBuilder::new().bearer_auth("someS3cr3tAutht0k3n");
assert_eq!(
client
.headers
.get(header::AUTHORIZATION)
.unwrap()
.to_str()
.unwrap(),
"Bearer someS3cr3tAutht0k3n"
);
}
}