From f056e9f44b0eecf49738254c29c15e4a2141e556 Mon Sep 17 00:00:00 2001 From: linguangming Date: Fri, 20 Dec 2019 01:39:03 -0500 Subject: [PATCH] set the actix-web client send the http2 packet without ssl. --- actix-http/src/client/connector.rs | 32 ++++++++++++++++++++++++++++++ awc/src/lib.rs | 11 ++++++++++ 2 files changed, 43 insertions(+) diff --git a/actix-http/src/client/connector.rs b/actix-http/src/client/connector.rs index 055d4276d..08bbc0efd 100644 --- a/actix-http/src/client/connector.rs +++ b/actix-http/src/client/connector.rs @@ -206,6 +206,38 @@ where self } + /// Finish configuration process and create connector service wich http2 set. + pub fn finish_for_h2( + self, + ) -> impl Service + + Clone { + let connector = TimeoutService::new( + self.timeout, + apply_fn(self.connector, |msg: Connect, srv| { + srv.call(TcpConnect::new(msg.uri).set_addr(msg.addr)) + }) + .map_err(ConnectError::from) + .map(|stream| { + let sock = stream.into_parts().0; + (Box::new(sock) as Box, Protocol::Http2) + }), + ) + .map_err(|e| match e { + TimeoutError::Service(e) => e, + TimeoutError::Timeout => ConnectError::Timeout, + }); + + connect_impl::InnerConnector { + tcp_pool: ConnectionPool::new( + connector, + self.conn_lifetime, + self.conn_keep_alive, + None, + self.limit, + ), + } + } + /// Finish configuration process and create connector service. /// The Connector builder always concludes by calling `finish()` last in /// its combinator chain. diff --git a/awc/src/lib.rs b/awc/src/lib.rs index 8944fe229..b1e0c5778 100644 --- a/awc/src/lib.rs +++ b/awc/src/lib.rs @@ -96,6 +96,17 @@ impl Client { Client::default() } + /// Create new client instance with http2 settings. + pub fn set_http2() -> Client { + Client(Rc::new(ClientConfig { + connector: RefCell::new(Box::new(ConnectorWrapper( + Connector::new().finish_for_h2(), + ))), + headers: HeaderMap::new(), + timeout: Some(Duration::from_secs(5)), + })) + } + /// Build client instance. pub fn build() -> ClientBuilder { ClientBuilder::new()