Skip to content

Commit

Permalink
feat(client): remove Destination for http::Uri in connectors
Browse files Browse the repository at this point in the history
BREAKING CHANGE: All usage of `hyper::client::connect::Destination`
  should be replaced with `http::Uri`.
  • Loading branch information
seanmonstar committed Dec 5, 2019
1 parent 9645a12 commit 319e8ae
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 430 deletions.
5 changes: 2 additions & 3 deletions benches/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ extern crate test;

use std::net::SocketAddr;
use tokio::net::TcpListener;
use hyper::client::connect::{Destination, HttpConnector};
use hyper::client::connect::{HttpConnector};
use hyper::service::Service;
use http::Uri;

Expand All @@ -19,8 +19,7 @@ fn http_connector(b: &mut test::Bencher) {
.expect("rt build");
let mut listener = rt.block_on(TcpListener::bind(&SocketAddr::from(([127, 0, 0, 1], 0)))).expect("bind");
let addr = listener.local_addr().expect("local_addr");
let uri: Uri = format!("http://{}/", addr).parse().expect("uri parse");
let dst = Destination::try_from_uri(uri).expect("destination");
let dst: Uri = format!("http://{}/", addr).parse().expect("uri parse");
let mut connector = HttpConnector::new();

rt.spawn(async move {
Expand Down
5 changes: 2 additions & 3 deletions src/client/connect/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,11 +391,10 @@ mod tests {

#[test]
fn ip_addrs_try_parse_v6() {
let uri = ::http::Uri::from_static("http://[::1]:8080/");
let dst = super::super::Destination { uri };
let dst = ::http::Uri::from_static("http://[::1]:8080/");

let mut addrs =
IpAddrs::try_parse(dst.host(), dst.port().expect("port")).expect("try_parse");
IpAddrs::try_parse(dst.host().expect("host"), dst.port_u16().expect("port")).expect("try_parse");

let expected = "[::1]:8080".parse::<SocketAddr>().expect("expected");

Expand Down
52 changes: 16 additions & 36 deletions src/client/connect/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use tokio::net::TcpStream;
use tokio::time::Delay;

use super::dns::{self, resolve, GaiResolver, Resolve};
use super::{Connected, Destination};
use super::{Connected};
//#[cfg(feature = "runtime")] use super::dns::TokioThreadpoolGaiResolver;


Expand Down Expand Up @@ -229,7 +229,7 @@ impl<R: fmt::Debug> fmt::Debug for HttpConnector<R> {
}
}

impl<R> tower_service::Service<Destination> for HttpConnector<R>
impl<R> tower_service::Service<Uri> for HttpConnector<R>
where
R: Resolve + Clone + Send + Sync + 'static,
R::Future: Send,
Expand All @@ -243,7 +243,7 @@ where
Poll::Ready(Ok(()))
}

fn call(&mut self, dst: Destination) -> Self::Future {
fn call(&mut self, dst: Uri) -> Self::Future {
let mut self_ = self.clone();
HttpConnecting {
fut: Box::pin(async move { self_.call_async(dst).await }),
Expand All @@ -258,30 +258,30 @@ where
{
async fn call_async(
&mut self,
dst: Destination,
dst: Uri,
) -> Result<(TcpStream, Connected), ConnectError> {
trace!(
"Http::connect; scheme={}, host={}, port={:?}",
"Http::connect; scheme={:?}, host={:?}, port={:?}",
dst.scheme(),
dst.host(),
dst.port(),
);

if self.config.enforce_http {
if dst.uri.scheme() != Some(&Scheme::HTTP) {
if dst.scheme() != Some(&Scheme::HTTP) {
return Err(ConnectError {
msg: INVALID_NOT_HTTP.into(),
cause: None,
});
}
} else if dst.uri.scheme().is_none() {
} else if dst.scheme().is_none() {
return Err(ConnectError {
msg: INVALID_MISSING_SCHEME.into(),
cause: None,
});
}

let host = match dst.uri.host() {
let host = match dst.host() {
Some(s) => s,
None => {
return Err(ConnectError {
Expand All @@ -290,9 +290,9 @@ where
})
}
};
let port = match dst.uri.port() {
let port = match dst.port() {
Some(port) => port.as_u16(),
None => if dst.uri.scheme() == Some(&Scheme::HTTPS) { 443 } else { 80 },
None => if dst.scheme() == Some(&Scheme::HTTPS) { 443 } else { 80 },
};

let config = &self.config;
Expand Down Expand Up @@ -351,26 +351,6 @@ where
}
}

impl<R> tower_service::Service<Uri> for HttpConnector<R>
where
R: Resolve + Clone + Send + Sync + 'static,
R::Future: Send,
{
type Response = TcpStream;
type Error = ConnectError;
type Future =
Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send + 'static>>;

fn poll_ready(&mut self, cx: &mut task::Context<'_>) -> Poll<Result<(), Self::Error>> {
tower_service::Service::<Destination>::poll_ready(self, cx)
}

fn call(&mut self, uri: Uri) -> Self::Future {
let mut self_ = self.clone();
Box::pin(async move { self_.call_async(Destination { uri }).await.map(|(s, _)| s) })
}
}

impl HttpInfo {
/// Get the remote address of the transport used.
pub fn remote_addr(&self) -> SocketAddr {
Expand Down Expand Up @@ -661,12 +641,14 @@ impl ConnectingTcp {
mod tests {
use std::io;

use ::http::Uri;

use super::super::sealed::Connect;
use super::{Connected, Destination, HttpConnector};
use super::{Connected, HttpConnector};

async fn connect<C>(
connector: C,
dst: Destination,
dst: Uri,
) -> Result<(C::Transport, Connected), C::Error>
where
C: Connect,
Expand All @@ -676,8 +658,7 @@ mod tests {

#[tokio::test]
async fn test_errors_enforce_http() {
let uri = "https://example.domain/foo/bar?baz".parse().unwrap();
let dst = Destination { uri };
let dst = "https://example.domain/foo/bar?baz".parse().unwrap();
let connector = HttpConnector::new();

let err = connect(connector, dst).await.unwrap_err();
Expand All @@ -686,8 +667,7 @@ mod tests {

#[tokio::test]
async fn test_errors_missing_scheme() {
let uri = "example.domain".parse().unwrap();
let dst = Destination { uri };
let dst = "example.domain".parse().unwrap();
let mut connector = HttpConnector::new();
connector.enforce_http(false);

Expand Down
Loading

0 comments on commit 319e8ae

Please sign in to comment.