feat(lib): remove extern Url type usage

BREAKING CHANGE: The `Url` type is no longer used. Any instance in the
  `Client` API has had it replaced with `hyper::Uri`.

  This also means `Error::Uri` has changed types to
  `hyper::error::UriError`.

  The type `hyper::header::parsing::HTTP_VALUE` has been made private,
  as an implementation detail. The function `http_percent_encoding`
  should be used instead.
This commit is contained in:
Sean McArthur
2017-03-21 10:35:31 -07:00
parent e81184e53c
commit 4fb7e6ebc6
12 changed files with 192 additions and 155 deletions

View File

@@ -7,7 +7,7 @@ use tokio_io::{AsyncRead, AsyncWrite};
use tokio::reactor::Handle;
use tokio::net::{TcpStream, TcpStreamNew};
use tokio_service::Service;
use Url;
use Uri;
use super::dns;
@@ -15,25 +15,25 @@ use super::dns;
///
/// This trait is not implemented directly, and only exists to make
/// the intent clearer. A connector should implement `Service` with
/// `Request=Url` and `Response: Io` instead.
pub trait Connect: Service<Request=Url, Error=io::Error> + 'static {
/// `Request=Uri` and `Response: Io` instead.
pub trait Connect: Service<Request=Uri, Error=io::Error> + 'static {
/// The connected Io Stream.
type Output: AsyncRead + AsyncWrite + 'static;
/// A Future that will resolve to the connected Stream.
type Future: Future<Item=Self::Output, Error=io::Error> + 'static;
/// Connect to a remote address.
fn connect(&self, Url) -> <Self as Connect>::Future;
fn connect(&self, Uri) -> <Self as Connect>::Future;
}
impl<T> Connect for T
where T: Service<Request=Url, Error=io::Error> + 'static,
where T: Service<Request=Uri, Error=io::Error> + 'static,
T::Response: AsyncRead + AsyncWrite,
T::Future: Future<Error=io::Error>,
{
type Output = T::Response;
type Future = T::Future;
fn connect(&self, url: Url) -> <Self as Connect>::Future {
fn connect(&self, url: Uri) -> <Self as Connect>::Future {
self.call(url)
}
}
@@ -66,21 +66,21 @@ impl fmt::Debug for HttpConnector {
}
impl Service for HttpConnector {
type Request = Url;
type Request = Uri;
type Response = TcpStream;
type Error = io::Error;
type Future = HttpConnecting;
fn call(&self, url: Url) -> Self::Future {
fn call(&self, url: Uri) -> Self::Future {
debug!("Http::connect({:?})", url);
let host = match url.host_str() {
let host = match url.host() {
Some(s) => s,
None => return HttpConnecting {
state: State::Error(Some(io::Error::new(io::ErrorKind::InvalidInput, "invalid url"))),
handle: self.handle.clone(),
},
};
let port = url.port_or_known_default().unwrap_or(80);
let port = url.port().unwrap_or(80);
HttpConnecting {
state: State::Resolving(self.dns.resolve(host.into(), port)),
@@ -185,13 +185,12 @@ impl<S: SslClient> HttpsConnector<S> {
mod tests {
use std::io;
use tokio::reactor::Core;
use Url;
use super::{Connect, HttpConnector};
#[test]
fn test_non_http_url() {
let mut core = Core::new().unwrap();
let url = Url::parse("file:///home/sean/foo.txt").unwrap();
let url = "/foo/bar?baz".parse().unwrap();
let connector = HttpConnector::new(1, &core.handle());
assert_eq!(core.run(connector.connect(url)).unwrap_err().kind(), io::ErrorKind::InvalidInput);

View File

@@ -10,7 +10,7 @@ use std::marker::PhantomData;
use std::rc::Rc;
use std::time::Duration;
use futures::{Poll, Async, Future, Stream};
use futures::{future, Poll, Async, Future, Stream};
use futures::unsync::oneshot;
use tokio_io::{AsyncRead, AsyncWrite};
use tokio::reactor::Handle;
@@ -24,7 +24,7 @@ use header::{Headers, Host};
use http::{self, TokioBody};
use method::Method;
use self::pool::{Pool, Pooled};
use Url;
use uri::{self, Uri};
pub use self::connect::{HttpConnector, Connect};
pub use self::request::Request;
@@ -95,7 +95,7 @@ where C: Connect,
{
/// Send a GET Request using this Client.
#[inline]
pub fn get(&self, url: Url) -> FutureResponse {
pub fn get(&self, url: Uri) -> FutureResponse {
self.request(Request::new(Method::Get, url))
}
@@ -135,18 +135,30 @@ where C: Connect,
type Future = FutureResponse;
fn call(&self, req: Self::Request) -> Self::Future {
let url = req.url().clone();
let url = req.uri().clone();
let domain = match uri::scheme_and_authority(&url) {
Some(uri) => uri,
None => {
return FutureResponse(Box::new(future::err(::Error::Io(
io::Error::new(
io::ErrorKind::InvalidInput,
"invalid URI for Client Request"
)
))));
}
};
let host = Host::new(domain.host().expect("authority implies host").to_owned(), domain.port());
let (mut head, body) = request::split(req);
let mut headers = Headers::new();
headers.set(Host::new(url.host_str().unwrap().to_owned(), url.port()));
headers.set(host);
headers.extend(head.headers.iter());
head.headers = headers;
let checkout = self.pool.checkout(&url[..::url::Position::BeforePath]);
let checkout = self.pool.checkout(domain.as_ref());
let connect = {
let handle = self.handle.clone();
let pool = self.pool.clone();
let pool_key = Rc::new(url[..::url::Position::BeforePath].to_owned());
let pool_key = Rc::new(domain.to_string());
self.connector.connect(url)
.map(move |io| {
let (tx, rx) = oneshot::channel();

View File

@@ -1,18 +1,15 @@
use std::fmt;
use Url;
use header::Headers;
use http::{Body, RequestHead};
use method::Method;
use uri::Uri;
use uri::{self, Uri};
use version::HttpVersion;
use std::str::FromStr;
/// A client request to a remote server.
pub struct Request<B = Body> {
method: Method,
url: Url,
uri: Uri,
version: HttpVersion,
headers: Headers,
body: Option<B>,
@@ -22,10 +19,10 @@ pub struct Request<B = Body> {
impl<B> Request<B> {
/// Construct a new Request.
#[inline]
pub fn new(method: Method, url: Url) -> Request<B> {
pub fn new(method: Method, uri: Uri) -> Request<B> {
Request {
method: method,
url: url,
uri: uri,
version: HttpVersion::default(),
headers: Headers::new(),
body: None,
@@ -33,9 +30,9 @@ impl<B> Request<B> {
}
}
/// Read the Request Url.
/// Read the Request Uri.
#[inline]
pub fn url(&self) -> &Url { &self.url }
pub fn uri(&self) -> &Uri { &self.uri }
/// Read the Request Version.
#[inline]
@@ -57,9 +54,9 @@ impl<B> Request<B> {
#[inline]
pub fn headers_mut(&mut self) -> &mut Headers { &mut self.headers }
/// Set the `Url` of this request.
/// Set the `Uri` of this request.
#[inline]
pub fn set_url(&mut self, url: Url) { self.url = url; }
pub fn set_uri(&mut self, uri: Uri) { self.uri = uri; }
/// Set the `HttpVersion` of this request.
#[inline]
@@ -81,7 +78,7 @@ impl<B> fmt::Debug for Request<B> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Request")
.field("method", &self.method)
.field("url", &self.url)
.field("uri", &self.uri)
.field("version", &self.version)
.field("headers", &self.headers)
.finish()
@@ -90,9 +87,9 @@ impl<B> fmt::Debug for Request<B> {
pub fn split<B>(req: Request<B>) -> (RequestHead, Option<B>) {
let uri = if req.is_proxy {
Uri::from(req.url)
req.uri
} else {
Uri::from_str(&req.url[::url::Position::BeforePath..::url::Position::AfterQuery]).expect("url is not uri")
uri::origin_form(&req.uri)
};
let head = RequestHead {
subject: ::http::RequestLine(req.method, uri),