mirror of https://github.com/rwf2/Rocket.git
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:
parent
cb4b8a3fef
commit
6ad14dc3f6
|
@ -1,10 +1,11 @@
|
||||||
use std::borrow::Cow;
|
use std::fmt;
|
||||||
|
use std::convert::TryInto;
|
||||||
|
|
||||||
use parking_lot::RwLock;
|
use parking_lot::RwLock;
|
||||||
|
|
||||||
use crate::local::asynchronous::{LocalRequest, LocalResponse};
|
use crate::local::asynchronous::{LocalRequest, LocalResponse};
|
||||||
use crate::rocket::Rocket;
|
use crate::rocket::Rocket;
|
||||||
use crate::http::{private::cookie, Method};
|
use crate::http::{Method, uri::Origin, private::cookie};
|
||||||
use crate::error::Error;
|
use crate::error::Error;
|
||||||
|
|
||||||
/// An `async` client to construct and dispatch local requests.
|
/// An `async` client to construct and dispatch local requests.
|
||||||
|
@ -97,9 +98,9 @@ impl Client {
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn _req<'c, 'u: 'c, U>(&'c self, method: Method, uri: U) -> LocalRequest<'c>
|
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.
|
// Generates the public API methods, which call the private methods above.
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
use std::borrow::Cow;
|
use std::fmt;
|
||||||
|
use std::convert::TryInto;
|
||||||
|
|
||||||
use crate::{Request, Data};
|
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};
|
use super::{Client, LocalResponse};
|
||||||
|
|
||||||
|
@ -33,18 +35,18 @@ pub struct LocalRequest<'c> {
|
||||||
pub(in super) client: &'c Client,
|
pub(in super) client: &'c Client,
|
||||||
pub(in super) request: Request<'c>,
|
pub(in super) request: Request<'c>,
|
||||||
data: Vec<u8>,
|
data: Vec<u8>,
|
||||||
uri: Cow<'c, str>,
|
uri: Result<Origin<'c>, String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'c> LocalRequest<'c> {
|
impl<'c> LocalRequest<'c> {
|
||||||
pub(crate) fn new(
|
pub(crate) fn new<'u: 'c, U>(client: &'c Client, method: Method, uri: U) -> Self
|
||||||
client: &'c Client,
|
where U: TryInto<Origin<'u>> + fmt::Display
|
||||||
method: Method,
|
{
|
||||||
uri: Cow<'c, str>
|
|
||||||
) -> LocalRequest<'c> {
|
|
||||||
// We try to validate the URI now so that the inner `Request` contains a
|
// 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.
|
// 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());
|
let mut request = Request::new(client.rocket(), method, origin.into_owned());
|
||||||
|
|
||||||
// Add any cookies we know about.
|
// 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,
|
// from an error catcher) immediately if it's invalid. If it's valid,
|
||||||
// then `request` already contains the correct URI.
|
// then `request` already contains the correct URI.
|
||||||
let rocket = self.client.rocket();
|
let rocket = self.client.rocket();
|
||||||
if let Err(_) = Origin::parse(&self.uri) {
|
if let Err(malformed) = self.uri {
|
||||||
error!("Malformed request URI: {}", self.uri);
|
error!("Malformed request URI: {}", malformed);
|
||||||
return LocalResponse::new(self.request, move |req| {
|
return LocalResponse::new(self.request, move |req| {
|
||||||
rocket.handle_error(Status::BadRequest, req)
|
rocket.handle_error(Status::BadRequest, req)
|
||||||
}).await
|
}).await
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
use std::borrow::Cow;
|
use std::fmt;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
|
use std::convert::TryInto;
|
||||||
|
|
||||||
|
use crate::Rocket;
|
||||||
use crate::error::Error;
|
use crate::error::Error;
|
||||||
use crate::local::{asynchronous, blocking::{LocalRequest, LocalResponse}};
|
use crate::local::{asynchronous, blocking::{LocalRequest, LocalResponse}};
|
||||||
use crate::rocket::Rocket;
|
use crate::http::{Method, uri::Origin};
|
||||||
use crate::http::Method;
|
|
||||||
|
|
||||||
/// A `blocking` client to construct and dispatch local requests.
|
/// A `blocking` client to construct and dispatch local requests.
|
||||||
///
|
///
|
||||||
|
@ -78,14 +79,10 @@ impl Client {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub(crate) fn _req<'c, 'u: 'c, U>(
|
fn _req<'c, 'u: 'c, U>(&'c self, method: Method, uri: U) -> LocalRequest<'c>
|
||||||
&'c self,
|
where U: TryInto<Origin<'u>> + fmt::Display
|
||||||
method: Method,
|
|
||||||
uri: U
|
|
||||||
) -> LocalRequest<'c>
|
|
||||||
where U: Into<Cow<'u, str>>
|
|
||||||
{
|
{
|
||||||
LocalRequest::new(self, method, uri.into())
|
LocalRequest::new(self, method, uri)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generates the public API methods, which call the private methods above.
|
// Generates the public API methods, which call the private methods above.
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
use std::borrow::Cow;
|
use std::fmt;
|
||||||
|
use std::convert::TryInto;
|
||||||
|
|
||||||
use crate::{Request, http::Method, local::asynchronous};
|
use crate::{Request, http::Method, local::asynchronous};
|
||||||
|
use crate::http::uri::Origin;
|
||||||
|
|
||||||
use super::{Client, LocalResponse};
|
use super::{Client, LocalResponse};
|
||||||
|
|
||||||
|
@ -34,11 +36,9 @@ pub struct LocalRequest<'c> {
|
||||||
|
|
||||||
impl<'c> LocalRequest<'c> {
|
impl<'c> LocalRequest<'c> {
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(crate) fn new(
|
pub(crate) fn new<'u: 'c, U>(client: &'c Client, method: Method, uri: U) -> Self
|
||||||
client: &'c Client,
|
where U: TryInto<Origin<'u>> + fmt::Display
|
||||||
method: Method,
|
{
|
||||||
uri: Cow<'c, str>
|
|
||||||
) -> LocalRequest<'c> {
|
|
||||||
let inner = asynchronous::LocalRequest::new(client.inner(), method, uri);
|
let inner = asynchronous::LocalRequest::new(client.inner(), method, uri);
|
||||||
Self { inner, client }
|
Self { inner, client }
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,7 +31,7 @@ macro_rules! req_method {
|
||||||
/// ```
|
/// ```
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn $f<'c, 'u: 'c, U>(&'c self, uri: U) -> LocalRequest<'c>
|
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)
|
self.req($method, uri)
|
||||||
}
|
}
|
||||||
|
@ -193,7 +193,7 @@ macro_rules! pub_client_impl {
|
||||||
method: Method,
|
method: Method,
|
||||||
uri: U
|
uri: U
|
||||||
) -> LocalRequest<'c>
|
) -> LocalRequest<'c>
|
||||||
where U: Into<Cow<'u, str>>
|
where U: TryInto<Origin<'u>> + fmt::Display
|
||||||
{
|
{
|
||||||
self._req(method, uri)
|
self._req(method, uri)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue