@@ -291,10 +291,9 @@ impl Client {
|
|||||||
///
|
///
|
||||||
/// This method fails whenever supplied `Url` cannot be parsed.
|
/// This method fails whenever supplied `Url` cannot be parsed.
|
||||||
pub fn request<U: IntoUrl>(&self, method: Method, url: U) -> RequestBuilder {
|
pub fn request<U: IntoUrl>(&self, method: Method, url: U) -> RequestBuilder {
|
||||||
let req = match url.into_url() {
|
let req = url
|
||||||
Ok(url) => Ok(Request::new(method, url)),
|
.into_url()
|
||||||
Err(err) => Err(::error::from(err)),
|
.map(move |url| Request::new(method, url));
|
||||||
};
|
|
||||||
RequestBuilder::new(self.clone(), req)
|
RequestBuilder::new(self.clone(), req)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -339,10 +339,9 @@ impl Client {
|
|||||||
///
|
///
|
||||||
/// This method fails whenever supplied `Url` cannot be parsed.
|
/// This method fails whenever supplied `Url` cannot be parsed.
|
||||||
pub fn request<U: IntoUrl>(&self, method: Method, url: U) -> RequestBuilder {
|
pub fn request<U: IntoUrl>(&self, method: Method, url: U) -> RequestBuilder {
|
||||||
let req = match url.into_url() {
|
let req = url
|
||||||
Ok(url) => Ok(Request::new(method, url)),
|
.into_url()
|
||||||
Err(err) => Err(::error::from(err)),
|
.map(move |url| Request::new(method, url));
|
||||||
};
|
|
||||||
RequestBuilder::new(self.clone(), req)
|
RequestBuilder::new(self.clone(), req)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
12
src/error.rs
12
src/error.rs
@@ -119,6 +119,7 @@ impl Error {
|
|||||||
Kind::Io(ref e) => Some(e),
|
Kind::Io(ref e) => Some(e),
|
||||||
Kind::UrlEncoded(ref e) => Some(e),
|
Kind::UrlEncoded(ref e) => Some(e),
|
||||||
Kind::Json(ref e) => Some(e),
|
Kind::Json(ref e) => Some(e),
|
||||||
|
Kind::UrlBadScheme |
|
||||||
Kind::TooManyRedirects |
|
Kind::TooManyRedirects |
|
||||||
Kind::RedirectLoop |
|
Kind::RedirectLoop |
|
||||||
Kind::ClientError(_) |
|
Kind::ClientError(_) |
|
||||||
@@ -196,6 +197,7 @@ impl fmt::Display for Error {
|
|||||||
Kind::Hyper(ref e) => fmt::Display::fmt(e, f),
|
Kind::Hyper(ref e) => fmt::Display::fmt(e, f),
|
||||||
Kind::Mime(ref e) => fmt::Display::fmt(e, f),
|
Kind::Mime(ref e) => fmt::Display::fmt(e, f),
|
||||||
Kind::Url(ref e) => fmt::Display::fmt(e, f),
|
Kind::Url(ref e) => fmt::Display::fmt(e, f),
|
||||||
|
Kind::UrlBadScheme => f.write_str("URL scheme is not allowed"),
|
||||||
Kind::Tls(ref e) => fmt::Display::fmt(e, f),
|
Kind::Tls(ref e) => fmt::Display::fmt(e, f),
|
||||||
Kind::Io(ref e) => fmt::Display::fmt(e, f),
|
Kind::Io(ref e) => fmt::Display::fmt(e, f),
|
||||||
Kind::UrlEncoded(ref e) => fmt::Display::fmt(e, f),
|
Kind::UrlEncoded(ref e) => fmt::Display::fmt(e, f),
|
||||||
@@ -221,6 +223,7 @@ impl StdError for Error {
|
|||||||
Kind::Hyper(ref e) => e.description(),
|
Kind::Hyper(ref e) => e.description(),
|
||||||
Kind::Mime(ref e) => e.description(),
|
Kind::Mime(ref e) => e.description(),
|
||||||
Kind::Url(ref e) => e.description(),
|
Kind::Url(ref e) => e.description(),
|
||||||
|
Kind::UrlBadScheme => "URL scheme is not allowed",
|
||||||
Kind::Tls(ref e) => e.description(),
|
Kind::Tls(ref e) => e.description(),
|
||||||
Kind::Io(ref e) => e.description(),
|
Kind::Io(ref e) => e.description(),
|
||||||
Kind::UrlEncoded(ref e) => e.description(),
|
Kind::UrlEncoded(ref e) => e.description(),
|
||||||
@@ -242,6 +245,7 @@ impl StdError for Error {
|
|||||||
Kind::Io(ref e) => e.cause(),
|
Kind::Io(ref e) => e.cause(),
|
||||||
Kind::UrlEncoded(ref e) => e.cause(),
|
Kind::UrlEncoded(ref e) => e.cause(),
|
||||||
Kind::Json(ref e) => e.cause(),
|
Kind::Json(ref e) => e.cause(),
|
||||||
|
Kind::UrlBadScheme |
|
||||||
Kind::TooManyRedirects |
|
Kind::TooManyRedirects |
|
||||||
Kind::RedirectLoop |
|
Kind::RedirectLoop |
|
||||||
Kind::ClientError(_) |
|
Kind::ClientError(_) |
|
||||||
@@ -258,6 +262,7 @@ pub(crate) enum Kind {
|
|||||||
Hyper(::hyper::Error),
|
Hyper(::hyper::Error),
|
||||||
Mime(::mime::FromStrError),
|
Mime(::mime::FromStrError),
|
||||||
Url(::url::ParseError),
|
Url(::url::ParseError),
|
||||||
|
UrlBadScheme,
|
||||||
Tls(::native_tls::Error),
|
Tls(::native_tls::Error),
|
||||||
Io(io::Error),
|
Io(io::Error),
|
||||||
UrlEncoded(::serde_urlencoded::ser::Error),
|
UrlEncoded(::serde_urlencoded::ser::Error),
|
||||||
@@ -438,6 +443,13 @@ pub(crate) fn server_error(url: Url, status: StatusCode) -> Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn url_bad_scheme(url: Url) -> Error {
|
||||||
|
Error {
|
||||||
|
kind: Kind::UrlBadScheme,
|
||||||
|
url: Some(url),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use url::{Url, ParseError};
|
use url::Url;
|
||||||
|
|
||||||
/// A trait to try to convert some type into a `Url`.
|
/// A trait to try to convert some type into a `Url`.
|
||||||
///
|
///
|
||||||
@@ -12,27 +12,47 @@ impl<T: PolyfillTryInto> IntoUrl for T {}
|
|||||||
// pub(crate)
|
// pub(crate)
|
||||||
|
|
||||||
pub trait PolyfillTryInto {
|
pub trait PolyfillTryInto {
|
||||||
fn into_url(self) -> Result<Url, ParseError>;
|
// Besides parsing as a valid `Url`, the `Url` must be a valid
|
||||||
|
// `http::Uri`, in that it makes sense to use in a network request.
|
||||||
|
fn into_url(self) -> ::Result<Url>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PolyfillTryInto for Url {
|
impl PolyfillTryInto for Url {
|
||||||
fn into_url(self) -> Result<Url, ParseError> {
|
fn into_url(self) -> ::Result<Url> {
|
||||||
Ok(self)
|
if self.has_host() {
|
||||||
|
Ok(self)
|
||||||
|
} else {
|
||||||
|
Err(::error::url_bad_scheme(self))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> PolyfillTryInto for &'a str {
|
impl<'a> PolyfillTryInto for &'a str {
|
||||||
fn into_url(self) -> Result<Url, ParseError> {
|
fn into_url(self) -> ::Result<Url> {
|
||||||
Url::parse(self)
|
try_!(Url::parse(self))
|
||||||
|
.into_url()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> PolyfillTryInto for &'a String {
|
impl<'a> PolyfillTryInto for &'a String {
|
||||||
fn into_url(self) -> Result<Url, ParseError> {
|
fn into_url(self) -> ::Result<Url> {
|
||||||
Url::parse(self)
|
(&**self).into_url()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_uri(url: &Url) -> ::hyper::Uri {
|
pub(crate) fn to_uri(url: &Url) -> ::hyper::Uri {
|
||||||
url.as_str().parse().expect("a parsed Url should always be a valid Uri")
|
url.as_str().parse().expect("a parsed Url should always be a valid Uri")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn into_url_file_scheme() {
|
||||||
|
let err = "file:///etc/hosts"
|
||||||
|
.into_url()
|
||||||
|
.unwrap_err();
|
||||||
|
assert_eq!(err.to_string(), "file:///etc/hosts: URL scheme is not allowed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ impl Proxy {
|
|||||||
/// # fn main() {}
|
/// # fn main() {}
|
||||||
/// ```
|
/// ```
|
||||||
pub fn http<U: IntoUrl>(url: U) -> ::Result<Proxy> {
|
pub fn http<U: IntoUrl>(url: U) -> ::Result<Proxy> {
|
||||||
let uri = ::into_url::to_uri(&try_!(url.into_url()));
|
let uri = ::into_url::to_uri(&url.into_url()?);
|
||||||
Ok(Proxy::new(Intercept::Http(uri)))
|
Ok(Proxy::new(Intercept::Http(uri)))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,7 +68,7 @@ impl Proxy {
|
|||||||
/// # fn main() {}
|
/// # fn main() {}
|
||||||
/// ```
|
/// ```
|
||||||
pub fn https<U: IntoUrl>(url: U) -> ::Result<Proxy> {
|
pub fn https<U: IntoUrl>(url: U) -> ::Result<Proxy> {
|
||||||
let uri = ::into_url::to_uri(&try_!(url.into_url()));
|
let uri = ::into_url::to_uri(&url.into_url()?);
|
||||||
Ok(Proxy::new(Intercept::Https(uri)))
|
Ok(Proxy::new(Intercept::Https(uri)))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,7 +87,7 @@ impl Proxy {
|
|||||||
/// # fn main() {}
|
/// # fn main() {}
|
||||||
/// ```
|
/// ```
|
||||||
pub fn all<U: IntoUrl>(url: U) -> ::Result<Proxy> {
|
pub fn all<U: IntoUrl>(url: U) -> ::Result<Proxy> {
|
||||||
let uri = ::into_url::to_uri(&try_!(url.into_url()));
|
let uri = ::into_url::to_uri(&url.into_url()?);
|
||||||
Ok(Proxy::new(Intercept::All(uri)))
|
Ok(Proxy::new(Intercept::All(uri)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user