diff --git a/core/lib/src/local/asynchronous/client.rs b/core/lib/src/local/asynchronous/client.rs index 4cda65fc..4bf7dad2 100644 --- a/core/lib/src/local/asynchronous/client.rs +++ b/core/lib/src/local/asynchronous/client.rs @@ -1,10 +1,11 @@ -use std::borrow::Cow; +use std::fmt; +use std::convert::TryInto; use parking_lot::RwLock; use crate::local::asynchronous::{LocalRequest, LocalResponse}; use crate::rocket::Rocket; -use crate::http::{private::cookie, Method}; +use crate::http::{Method, uri::Origin, private::cookie}; use crate::error::Error; /// An `async` client to construct and dispatch local requests. @@ -97,9 +98,9 @@ impl Client { #[inline(always)] fn _req<'c, 'u: 'c, U>(&'c self, method: Method, uri: U) -> LocalRequest<'c> - where U: Into> + where U: TryInto> + fmt::Display { - LocalRequest::new(self, method, uri.into()) + LocalRequest::new(self, method, uri) } // Generates the public API methods, which call the private methods above. diff --git a/core/lib/src/local/asynchronous/request.rs b/core/lib/src/local/asynchronous/request.rs index 4d6c12d4..a6333e43 100644 --- a/core/lib/src/local/asynchronous/request.rs +++ b/core/lib/src/local/asynchronous/request.rs @@ -1,7 +1,9 @@ -use std::borrow::Cow; +use std::fmt; +use std::convert::TryInto; use crate::{Request, Data}; -use crate::http::{Status, Method, uri::Origin, ext::IntoOwned}; +use crate::http::{Status, Method, ext::IntoOwned}; +use crate::http::uri::Origin; use super::{Client, LocalResponse}; @@ -33,18 +35,18 @@ pub struct LocalRequest<'c> { pub(in super) client: &'c Client, pub(in super) request: Request<'c>, data: Vec, - uri: Cow<'c, str>, + uri: Result, String>, } impl<'c> LocalRequest<'c> { - pub(crate) fn new( - client: &'c Client, - method: Method, - uri: Cow<'c, str> - ) -> LocalRequest<'c> { + pub(crate) fn new<'u: 'c, U>(client: &'c Client, method: Method, uri: U) -> Self + where U: TryInto> + fmt::Display + { // We try to validate the URI now so that the inner `Request` contains a // valid URI. If it doesn't, we set a dummy one. - let origin = Origin::parse(&uri).unwrap_or_else(|_| Origin::dummy()); + let uri_string = uri.to_string(); + let uri = uri.try_into().map_err(move |_| uri_string); + let origin = uri.clone().unwrap_or_else(|_| Origin::dummy()); let mut request = Request::new(client.rocket(), method, origin.into_owned()); // Add any cookies we know about. @@ -77,8 +79,8 @@ impl<'c> LocalRequest<'c> { // from an error catcher) immediately if it's invalid. If it's valid, // then `request` already contains the correct URI. let rocket = self.client.rocket(); - if let Err(_) = Origin::parse(&self.uri) { - error!("Malformed request URI: {}", self.uri); + if let Err(malformed) = self.uri { + error!("Malformed request URI: {}", malformed); return LocalResponse::new(self.request, move |req| { rocket.handle_error(Status::BadRequest, req) }).await diff --git a/core/lib/src/local/blocking/client.rs b/core/lib/src/local/blocking/client.rs index 3a3f33ad..4dfd17c9 100644 --- a/core/lib/src/local/blocking/client.rs +++ b/core/lib/src/local/blocking/client.rs @@ -1,10 +1,11 @@ -use std::borrow::Cow; +use std::fmt; use std::cell::RefCell; +use std::convert::TryInto; +use crate::Rocket; use crate::error::Error; use crate::local::{asynchronous, blocking::{LocalRequest, LocalResponse}}; -use crate::rocket::Rocket; -use crate::http::Method; +use crate::http::{Method, uri::Origin}; /// A `blocking` client to construct and dispatch local requests. /// @@ -78,14 +79,10 @@ impl Client { } #[inline(always)] - pub(crate) fn _req<'c, 'u: 'c, U>( - &'c self, - method: Method, - uri: U - ) -> LocalRequest<'c> - where U: Into> + fn _req<'c, 'u: 'c, U>(&'c self, method: Method, uri: U) -> LocalRequest<'c> + where U: TryInto> + fmt::Display { - LocalRequest::new(self, method, uri.into()) + LocalRequest::new(self, method, uri) } // Generates the public API methods, which call the private methods above. diff --git a/core/lib/src/local/blocking/request.rs b/core/lib/src/local/blocking/request.rs index c9d9815e..32489118 100644 --- a/core/lib/src/local/blocking/request.rs +++ b/core/lib/src/local/blocking/request.rs @@ -1,6 +1,8 @@ -use std::borrow::Cow; +use std::fmt; +use std::convert::TryInto; use crate::{Request, http::Method, local::asynchronous}; +use crate::http::uri::Origin; use super::{Client, LocalResponse}; @@ -34,11 +36,9 @@ pub struct LocalRequest<'c> { impl<'c> LocalRequest<'c> { #[inline] - pub(crate) fn new( - client: &'c Client, - method: Method, - uri: Cow<'c, str> - ) -> LocalRequest<'c> { + pub(crate) fn new<'u: 'c, U>(client: &'c Client, method: Method, uri: U) -> Self + where U: TryInto> + fmt::Display + { let inner = asynchronous::LocalRequest::new(client.inner(), method, uri); Self { inner, client } } diff --git a/core/lib/src/local/client.rs b/core/lib/src/local/client.rs index a1eaa9cf..6c059dad 100644 --- a/core/lib/src/local/client.rs +++ b/core/lib/src/local/client.rs @@ -31,7 +31,7 @@ macro_rules! req_method { /// ``` #[inline(always)] pub fn $f<'c, 'u: 'c, U>(&'c self, uri: U) -> LocalRequest<'c> - where U: Into> + where U: TryInto> + fmt::Display { self.req($method, uri) } @@ -193,7 +193,7 @@ macro_rules! pub_client_impl { method: Method, uri: U ) -> LocalRequest<'c> - where U: Into> + where U: TryInto> + fmt::Display { self._req(method, uri) }