From 5b4105e1e6e5c69e7f71efaae20923fe7746f792 Mon Sep 17 00:00:00 2001 From: fakeshadow <24548779@qq.com> Date: Sun, 7 Mar 2021 15:57:32 -0800 Subject: [PATCH] Refactor/client builder (#2053) --- awc/CHANGES.md | 1 - awc/src/builder.rs | 38 +++++++++++++++++++++++++++++++++- awc/src/lib.rs | 10 ++------- awc/src/middleware/redirect.rs | 3 ++- 4 files changed, 41 insertions(+), 11 deletions(-) diff --git a/awc/CHANGES.md b/awc/CHANGES.md index 211db5a80..b11d64628 100644 --- a/awc/CHANGES.md +++ b/awc/CHANGES.md @@ -12,7 +12,6 @@ ### Removed * `ClientBuilder::default` function [#2008] -* `ClientBuilder::disable_redirects` and `ClientBuilder::max_redirects` method [#2008] [#1931]: https://github.com/actix/actix-web/pull/1931 [#1981]: https://github.com/actix/actix-web/pull/1981 diff --git a/awc/src/builder.rs b/awc/src/builder.rs index 363056c02..72a0f4f04 100644 --- a/awc/src/builder.rs +++ b/awc/src/builder.rs @@ -14,7 +14,7 @@ use actix_service::{boxed, Service}; use crate::connect::DefaultConnector; use crate::error::SendRequestError; -use crate::middleware::{NestTransform, Transform}; +use crate::middleware::{NestTransform, Redirect, Transform}; use crate::{Client, ClientConfig, ConnectRequest, ConnectResponse, ConnectorService}; /// An HTTP Client builder @@ -31,6 +31,7 @@ pub struct ClientBuilder { connector: Connector, middleware: M, local_address: Option, + max_redirects: u8, } impl ClientBuilder { @@ -54,6 +55,7 @@ impl ClientBuilder { max_http_version: None, stream_window_size: None, conn_window_size: None, + max_redirects: 10, } } } @@ -86,6 +88,7 @@ where max_http_version: self.max_http_version, stream_window_size: self.stream_window_size, conn_window_size: self.conn_window_size, + max_redirects: self.max_redirects, } } @@ -118,6 +121,22 @@ where self } + /// Do not follow redirects. + /// + /// Redirects are allowed by default. + pub fn disable_redirects(mut self) -> Self { + self.max_redirects = 0; + self + } + + /// Set max number of redirects. + /// + /// Max redirects is set to 10 by default. + pub fn max_redirects(mut self, num: u8) -> Self { + self.max_redirects = num; + self + } + /// Indicates the initial window size (in octets) for /// HTTP2 stream-level flow control for received data. /// @@ -209,11 +228,28 @@ where timeout: self.timeout, connector: self.connector, local_address: self.local_address, + max_redirects: self.max_redirects, } } /// Finish build process and create `Client` instance. pub fn finish(self) -> Client + where + M: Transform + 'static, + M::Transform: + Service, + { + let redirect_time = self.max_redirects; + + if redirect_time > 0 { + self.wrap(Redirect::new().max_redirect_times(redirect_time)) + ._finish() + } else { + self._finish() + } + } + + fn _finish(self) -> Client where M: Transform + 'static, M::Transform: diff --git a/awc/src/lib.rs b/awc/src/lib.rs index 2f48dca79..4cd1d5bb2 100644 --- a/awc/src/lib.rs +++ b/awc/src/lib.rs @@ -107,7 +107,7 @@ use actix_http::{ RequestHead, }; use actix_rt::net::TcpStream; -use actix_service::{boxed, Service}; +use actix_service::Service; mod builder; mod connect; @@ -157,13 +157,7 @@ pub(crate) struct ClientConfig { impl Default for Client { fn default() -> Self { - Client(Rc::new(ClientConfig { - connector: boxed::service(self::connect::DefaultConnector::new( - Connector::new().finish(), - )), - headers: HeaderMap::new(), - timeout: Some(Duration::from_secs(5)), - })) + ClientBuilder::new().finish() } } diff --git a/awc/src/middleware/redirect.rs b/awc/src/middleware/redirect.rs index 1d0ace166..f8bdd2def 100644 --- a/awc/src/middleware/redirect.rs +++ b/awc/src/middleware/redirect.rs @@ -292,7 +292,7 @@ mod tests { #[actix_rt::test] async fn test_basic_redirect() { let client = ClientBuilder::new() - .connector(crate::Connector::new()) + .disable_redirects() .wrap(Redirect::new().max_redirect_times(10)) .finish(); @@ -318,6 +318,7 @@ mod tests { #[actix_rt::test] async fn test_redirect_limit() { let client = ClientBuilder::new() + .disable_redirects() .wrap(Redirect::new().max_redirect_times(1)) .connector(crate::Connector::new()) .finish();