Takes URIs as 'TryInto<Origin>' in local client.

In particular, this allows passing in type-safe URIs constructed via the
'uri!' macro, which was not possible before.
This commit is contained in:
Sergio Benitez 2021-04-07 19:34:20 -07:00
parent cb4b8a3fef
commit 6ad14dc3f6
5 changed files with 33 additions and 33 deletions

View File

@ -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<Cow<'u, str>>
where U: TryInto<Origin<'u>> + fmt::Display
{
LocalRequest::new(self, method, uri.into())
LocalRequest::new(self, method, uri)
}
// Generates the public API methods, which call the private methods above.

View File

@ -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<u8>,
uri: Cow<'c, str>,
uri: Result<Origin<'c>, 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<Origin<'u>> + 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

View File

@ -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<Cow<'u, str>>
fn _req<'c, 'u: 'c, U>(&'c self, method: Method, uri: U) -> LocalRequest<'c>
where U: TryInto<Origin<'u>> + fmt::Display
{
LocalRequest::new(self, method, uri.into())
LocalRequest::new(self, method, uri)
}
// Generates the public API methods, which call the private methods above.

View File

@ -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<Origin<'u>> + fmt::Display
{
let inner = asynchronous::LocalRequest::new(client.inner(), method, uri);
Self { inner, client }
}

View File

@ -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<Cow<'u, str>>
where U: TryInto<Origin<'u>> + fmt::Display
{
self.req($method, uri)
}
@ -193,7 +193,7 @@ macro_rules! pub_client_impl {
method: Method,
uri: U
) -> LocalRequest<'c>
where U: Into<Cow<'u, str>>
where U: TryInto<Origin<'u>> + fmt::Display
{
self._req(method, uri)
}