Update to hyper 0.13

This commit is contained in:
Gleb Pomykalov
2019-12-11 03:24:05 +03:00
committed by Sean McArthur
parent db2de90e42
commit 0f32c4a01a
23 changed files with 469 additions and 219 deletions

View File

@@ -1,24 +1,31 @@
use futures_util::FutureExt;
use hyper::service::Service;
use http::uri::{Scheme, Authority};
use hyper::client::connect::{Connect, Connected, Destination};
use http::Uri;
use hyper::client::connect::{Connected, Connection};
use tokio::io::{AsyncRead, AsyncWrite};
#[cfg(feature = "default-tls")]
use native_tls::{TlsConnector, TlsConnectorBuilder};
#[cfg(feature = "tls")]
use http::header::HeaderValue;
use bytes::{Buf, BufMut};
use std::future::Future;
use std::io;
use std::net::IpAddr;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
use std::time::Duration;
use std::mem::MaybeUninit;
use pin_project_lite::pin_project;
//#[cfg(feature = "trust-dns")]
//use crate::dns::TrustDnsResolver;
use crate::proxy::{Proxy, ProxyScheme};
use tokio::future::FutureExt as _;
use crate::error::BoxError;
#[cfg(feature = "default-tls")]
use self::native_tls_conn::NativeTlsConn;
//#[cfg(feature = "trust-dns")]
//type HttpConnector = hyper::client::HttpConnector<TrustDnsResolver>;
@@ -198,24 +205,27 @@ impl Connector {
async fn connect_with_maybe_proxy(
self,
dst: Destination,
dst: Uri,
is_proxy: bool,
) -> Result<(Conn, Connected), io::Error> {
) -> Result<Conn, BoxError> {
match self.inner {
#[cfg(not(feature = "tls"))]
Inner::Http(http) => {
let (io, connected) = http.connect(dst).await?;
Ok((Box::new(io) as Conn, connected.proxy(is_proxy)))
Inner::Http(mut http) => {
let io = http.call(dst).await?;
Ok(Conn {
inner: Box::new(io),
is_proxy,
})
}
#[cfg(feature = "default-tls")]
Inner::DefaultTls(http, tls) => {
let mut http = http.clone();
http.set_nodelay(self.nodelay || (dst.scheme() == "https"));
http.set_nodelay(self.nodelay || (dst.scheme() == Some(&Scheme::HTTPS)));
let tls_connector = tokio_tls::TlsConnector::from(tls.clone());
let http = hyper_tls::HttpsConnector::from((http, tls_connector));
let (io, connected) = http.connect(dst).await?;
let mut http = hyper_tls::HttpsConnector::from((http, tls_connector));
let io = http.call(dst).await?;
//TODO: where's this at now?
//if let hyper_tls::MaybeHttpsStream::Https(_stream) = &io {
// if !no_delay {
@@ -223,7 +233,10 @@ impl Connector {
// }
//}
Ok((Box::new(io) as Conn, connected.proxy(is_proxy)))
Ok(Conn {
inner: Box::new(io),
is_proxy,
})
}
#[cfg(feature = "rustls-tls")]
Inner::RustlsTls { http, tls, .. } => {
@@ -232,10 +245,10 @@ impl Connector {
// Disable Nagle's algorithm for TLS handshake
//
// https://www.openssl.org/docs/man1.1.1/man3/SSL_connect.html#NOTES
http.set_nodelay(no_delay || (dst.scheme() == "https"));
http.set_nodelay(no_delay || (dst.scheme() == Some(&Scheme::HTTPS)));
let http = hyper_rustls::HttpsConnector::from((http, tls.clone()));
let (io, connected) = http.connect(dst).await?;
let io = http.connect(dst).await?;
if let hyper_rustls::MaybeHttpsStream::Https(stream) = &io {
if !no_delay {
let (io, _) = stream.get_ref();
@@ -243,21 +256,24 @@ impl Connector {
}
}
Ok((Box::new(io) as Conn, connected.proxy(is_proxy)))
Ok(Conn {
inner: Box::new(io),
is_proxy,
})
}
}
}
async fn connect_via_proxy(
self,
dst: Destination,
dst: Uri,
proxy_scheme: ProxyScheme,
) -> Result<(Conn, Connected), io::Error> {
) -> Result<Conn, BoxError> {
log::trace!("proxy({:?}) intercepts {:?}", proxy_scheme, dst);
let (proxy_dst, _auth) = match proxy_scheme {
ProxyScheme::Http { host, auth } => (into_dst(Scheme::HTTP, host), auth),
ProxyScheme::Https { host, auth } => (into_dst(Scheme::HTTPS, host), auth),
ProxyScheme::Http { host, auth } => (into_uri(Scheme::HTTP, host), auth),
ProxyScheme::Https { host, auth } => (into_uri(Scheme::HTTPS, host), auth),
#[cfg(feature = "socks")]
ProxyScheme::Socks5 { .. } => return this.connect_socks(dst, proxy_scheme),
};
@@ -269,22 +285,33 @@ impl Connector {
match &self.inner {
#[cfg(feature = "default-tls")]
Inner::DefaultTls(http, tls) => {
if dst.scheme() == "https" {
if dst.scheme() == Some(&Scheme::HTTPS) {
let host = dst.host().to_owned();
let port = dst.port().unwrap_or(443);
let port = dst.port().map(|p| p.as_u16()).unwrap_or(443);
let mut http = http.clone();
http.set_nodelay(self.nodelay);
let tls_connector = tokio_tls::TlsConnector::from(tls.clone());
let http = hyper_tls::HttpsConnector::from((http, tls_connector));
let (conn, connected) = http.connect(proxy_dst).await?;
let mut http = hyper_tls::HttpsConnector::from((http, tls_connector));
let conn = http.call(proxy_dst).await?;
log::trace!("tunneling HTTPS over proxy");
let tunneled = tunnel(conn, host.clone(), port, self.user_agent.clone(), auth).await?;
let tunneled = tunnel(
conn,
host
.ok_or(io::Error::new(io::ErrorKind::Other, "no host in url"))?
.to_string(),
port,
self.user_agent.clone(),
auth
).await?;
let tls_connector = tokio_tls::TlsConnector::from(tls.clone());
let io = tls_connector
.connect(&host, tunneled)
.connect(&host.ok_or(io::Error::new(io::ErrorKind::Other, "no host in url"))?, tunneled)
.await
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
return Ok((Box::new(io) as Conn, connected.proxy(true)));
return Ok(Conn {
inner: Box::new(NativeTlsConn { inner: io }),
is_proxy: false,
});
}
}
#[cfg(feature = "rustls-tls")]
@@ -293,7 +320,7 @@ impl Connector {
tls,
tls_proxy,
} => {
if dst.scheme() == "https" {
if dst.scheme() == Some(&Scheme::HTTPS) {
use rustls::Session;
use tokio_rustls::webpki::DNSNameRef;
use tokio_rustls::TlsConnector as RustlsConnector;
@@ -320,7 +347,10 @@ impl Connector {
} else {
connected
};
return Ok((Box::new(io) as Conn, connected.proxy(true)));
return Ok(Conn {
inner: Box::new(io),
connected: Connected::new(),
});
}
}
#[cfg(not(feature = "tls"))]
@@ -331,9 +361,7 @@ impl Connector {
}
}
fn into_dst(scheme: Scheme, host: Authority) -> Destination {
use std::convert::TryInto;
fn into_uri(scheme: Scheme, host: Authority) -> Uri {
// TODO: Should the `http` crate get `From<(Scheme, Authority)> for Uri`?
http::Uri::builder()
.scheme(scheme)
@@ -341,8 +369,6 @@ fn into_dst(scheme: Scheme, host: Authority) -> Destination {
.path_and_query(http::uri::PathAndQuery::from_static("/"))
.build()
.expect("scheme and authority is valid Uri")
.try_into()
.expect("scheme and authority is valid Destination")
}
//#[cfg(feature = "trust-dns")]
@@ -358,26 +384,32 @@ fn http_connector() -> crate::Result<HttpConnector> {
}
async fn with_timeout<T, F>(f: F, timeout: Option<Duration>) -> Result<T, io::Error>
async fn with_timeout<T, F>(f: F, timeout: Option<Duration>) -> Result<T, BoxError>
where
F: Future<Output = Result<T, io::Error>>,
F: Future<Output = Result<T, BoxError>>,
{
if let Some(to) = timeout {
match f.timeout(to).await {
Err(_elapsed) => Err(io::Error::new(io::ErrorKind::TimedOut, "connect timed out")),
Ok(try_res) => try_res,
match tokio::time::timeout(to, f).await {
Err(_elapsed) => Err(Box::new(io::Error::new(io::ErrorKind::TimedOut, "connect timed out")) as BoxError),
Ok(Ok(try_res)) => Ok(try_res),
Ok(Err(e)) => Err(e),
}
} else {
f.await
}
}
impl Connect for Connector {
type Transport = Conn;
type Error = io::Error;
impl Service<Uri> for Connector
{
type Response = Conn;
type Error = BoxError;
type Future = Connecting;
fn connect(&self, dst: Destination) -> Self::Future {
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, dst: Uri) -> Self::Future {
let timeout = self.timeout;
for prox in self.proxies.iter() {
if let Some(proxy_scheme) = prox.intercept(&dst) {
@@ -397,12 +429,115 @@ impl Connect for Connector {
}
}
pub(crate) trait AsyncConn: AsyncRead + AsyncWrite {}
impl<T: AsyncRead + AsyncWrite> AsyncConn for T {}
pub(crate) type Conn = Box<dyn AsyncConn + Send + Sync + Unpin + 'static>;
//impl Connect for Connector {
// type Transport = Conn;
// type Error = BoxError;
// type Future = Connecting;
//
// fn connect(&self, dst: Uri) -> Self::Future {
// let timeout = self.timeout;
// for prox in self.proxies.iter() {
// if let Some(proxy_scheme) = prox.intercept(&dst) {
// return with_timeout(
// self.clone().connect_via_proxy(dst, proxy_scheme),
// timeout,
// )
// .boxed();
// }
// }
//
// with_timeout(
// self.clone().connect_with_maybe_proxy(dst, false),
// timeout,
// )
// .boxed()
// }
//}
pub(crate) trait AsyncConn: AsyncRead + AsyncWrite + Connection {}
impl<T: AsyncRead + AsyncWrite + Connection> AsyncConn for T {}
pin_project! {
pub(crate) struct Conn {
#[pin]
inner: Box<dyn AsyncConn + Send + Sync + Unpin + 'static>,
is_proxy: bool,
}
}
impl Connection for Conn {
fn connected(&self) -> Connected {
self.inner.connected().proxy(self.is_proxy)
}
}
impl AsyncRead for Conn {
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context,
buf: &mut [u8]
) -> Poll<tokio::io::Result<usize>> {
let this = self.project();
AsyncRead::poll_read(this.inner, cx, buf)
}
unsafe fn prepare_uninitialized_buffer(
&self,
buf: &mut [MaybeUninit<u8>]
) -> bool {
self.inner.prepare_uninitialized_buffer(buf)
}
fn poll_read_buf<B: BufMut>(
self: Pin<&mut Self>,
cx: &mut Context,
buf: &mut B
) -> Poll<tokio::io::Result<usize>>
where
Self: Sized
{
let this = self.project();
AsyncRead::poll_read_buf(this.inner, cx, buf)
}
}
impl AsyncWrite for Conn {
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context,
buf: &[u8]
) -> Poll<Result<usize, tokio::io::Error>> {
let this = self.project();
AsyncWrite::poll_write(this.inner, cx, buf)
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), tokio::io::Error>> {
let this = self.project();
AsyncWrite::poll_flush(this.inner, cx)
}
fn poll_shutdown(
self: Pin<&mut Self>,
cx: &mut Context
) -> Poll<Result<(), tokio::io::Error>> {
let this = self.project();
AsyncWrite::poll_shutdown(this.inner, cx)
}
fn poll_write_buf<B: Buf>(
self: Pin<&mut Self>,
cx: &mut Context,
buf: &mut B
) -> Poll<Result<usize, tokio::io::Error>> where
Self: Sized {
let this = self.project();
AsyncWrite::poll_write_buf(this.inner, cx, buf)
}
}
pub(crate) type Connecting =
Pin<Box<dyn Future<Output = Result<(Conn, Connected), io::Error>> + Send>>;
Pin<Box<dyn Future<Output = Result<Conn, BoxError>> + Send>>;
#[cfg(feature = "tls")]
async fn tunnel<T>(
@@ -488,6 +623,94 @@ fn tunnel_eof() -> io::Error {
)
}
#[cfg(feature = "default-tls")]
mod native_tls_conn {
use std::mem::MaybeUninit;
use std::{pin::Pin, task::{Context, Poll}};
use bytes::{Buf, BufMut};
use hyper::client::connect::{Connected, Connection};
use pin_project_lite::pin_project;
use tokio::io::{AsyncRead, AsyncWrite};
use tokio_tls::TlsStream;
pin_project! {
pub(super) struct NativeTlsConn<T> {
#[pin] pub(super) inner: TlsStream<T>,
}
}
impl<T: Connection + AsyncRead + AsyncWrite + Unpin> Connection for NativeTlsConn<T> {
fn connected(&self) -> Connected {
self.inner.get_ref().connected()
}
}
impl<T: AsyncRead + AsyncWrite + Unpin> AsyncRead for NativeTlsConn<T> {
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context,
buf: &mut [u8]
) -> Poll<tokio::io::Result<usize>> {
let this = self.project();
AsyncRead::poll_read(this.inner, cx, buf)
}
unsafe fn prepare_uninitialized_buffer(
&self,
buf: &mut [MaybeUninit<u8>]
) -> bool {
self.inner.prepare_uninitialized_buffer(buf)
}
fn poll_read_buf<B: BufMut>(
self: Pin<&mut Self>,
cx: &mut Context,
buf: &mut B
) -> Poll<tokio::io::Result<usize>>
where
Self: Sized
{
let this = self.project();
AsyncRead::poll_read_buf(this.inner, cx, buf)
}
}
impl<T: AsyncRead + AsyncWrite + Unpin> AsyncWrite for NativeTlsConn<T> {
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context,
buf: &[u8]
) -> Poll<Result<usize, tokio::io::Error>> {
let this = self.project();
AsyncWrite::poll_write(this.inner, cx, buf)
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), tokio::io::Error>> {
let this = self.project();
AsyncWrite::poll_flush(this.inner, cx)
}
fn poll_shutdown(
self: Pin<&mut Self>,
cx: &mut Context
) -> Poll<Result<(), tokio::io::Error>> {
let this = self.project();
AsyncWrite::poll_shutdown(this.inner, cx)
}
fn poll_write_buf<B: Buf>(
self: Pin<&mut Self>,
cx: &mut Context,
buf: &mut B
) -> Poll<Result<usize, tokio::io::Error>> where
Self: Sized {
let this = self.project();
AsyncWrite::poll_write_buf(this.inner, cx, buf)
}
}
}
#[cfg(feature = "socks")]
mod socks {
use std::io;
@@ -510,8 +733,8 @@ mod socks {
proxy: ProxyScheme,
dst: Destination,
dns: DnsResolve,
) -> Result<(super::Conn, Connected), io::Error> {
let https = dst.scheme() == "https";
) -> Result<super::Conn, BoxError> {
let https = dst.scheme() == Some(&Scheme::HTTPS);
let original_host = dst.host().to_owned();
let mut host = original_host.clone();
let port = dst.port().unwrap_or_else(|| if https { 443 } else { 80 });
@@ -555,8 +778,8 @@ mod tests {
use std::io::{Read, Write};
use std::net::TcpListener;
use std::thread;
use tokio::net::tcp::TcpStream;
use tokio::runtime::current_thread::Runtime;
use tokio::net::TcpStream;
use tokio::runtime;
static TUNNEL_UA: &'static str = "tunnel-test/x.y";
static TUNNEL_OK: &[u8] = b"\
@@ -609,7 +832,7 @@ mod tests {
fn test_tunnel() {
let addr = mock_tunnel!();
let mut rt = Runtime::new().unwrap();
let mut rt = runtime::Builder::new().basic_scheduler().enable_all().build().expect("new rt");
let f = async move {
let tcp = TcpStream::connect(&addr).await?;
let host = addr.ip().to_string();
@@ -624,7 +847,7 @@ mod tests {
fn test_tunnel_eof() {
let addr = mock_tunnel!(b"HTTP/1.1 200 OK");
let mut rt = Runtime::new().unwrap();
let mut rt = runtime::Builder::new().basic_scheduler().enable_all().build().expect("new rt");
let f = async move {
let tcp = TcpStream::connect(&addr).await?;
let host = addr.ip().to_string();
@@ -639,7 +862,7 @@ mod tests {
fn test_tunnel_non_http_response() {
let addr = mock_tunnel!(b"foo bar baz hallo");
let mut rt = Runtime::new().unwrap();
let mut rt = runtime::Builder::new().basic_scheduler().enable_all().build().expect("new rt");
let f = async move {
let tcp = TcpStream::connect(&addr).await?;
let host = addr.ip().to_string();
@@ -660,7 +883,7 @@ mod tests {
"
);
let mut rt = Runtime::new().unwrap();
let mut rt = runtime::Builder::new().basic_scheduler().enable_all().build().expect("new rt");
let f = async move {
let tcp = TcpStream::connect(&addr).await?;
let host = addr.ip().to_string();
@@ -679,7 +902,7 @@ mod tests {
"Proxy-Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==\r\n"
);
let mut rt = Runtime::new().unwrap();
let mut rt = runtime::Builder::new().basic_scheduler().enable_all().build().expect("new rt");
let f = async move {
let tcp = TcpStream::connect(&addr).await?;
let host = addr.ip().to_string();