2021-05-30 13:24:03 +02:00
|
|
|
use crate::{body::Body, Error};
|
2021-05-30 14:33:20 +02:00
|
|
|
use bytes::Bytes;
|
2021-05-30 13:24:03 +02:00
|
|
|
use futures_util::{future, ready};
|
2021-05-31 12:22:16 +02:00
|
|
|
use http::{header, Request, StatusCode};
|
2021-05-30 13:24:03 +02:00
|
|
|
use pin_project::pin_project;
|
|
|
|
|
use serde::de::DeserializeOwned;
|
|
|
|
|
use std::{
|
2021-05-30 15:44:26 +02:00
|
|
|
collections::HashMap,
|
2021-05-30 13:24:03 +02:00
|
|
|
future::Future,
|
|
|
|
|
pin::Pin,
|
2021-05-30 15:44:26 +02:00
|
|
|
str::FromStr,
|
2021-05-30 13:24:03 +02:00
|
|
|
task::{Context, Poll},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
pub trait FromRequest: Sized {
|
|
|
|
|
type Future: Future<Output = Result<Self, Error>> + Send;
|
|
|
|
|
|
|
|
|
|
fn from_request(req: &mut Request<Body>) -> Self::Future;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> FromRequest for Option<T>
|
|
|
|
|
where
|
|
|
|
|
T: FromRequest,
|
|
|
|
|
{
|
|
|
|
|
type Future = OptionFromRequestFuture<T::Future>;
|
|
|
|
|
|
|
|
|
|
fn from_request(req: &mut Request<Body>) -> Self::Future {
|
|
|
|
|
OptionFromRequestFuture(T::from_request(req))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[pin_project]
|
|
|
|
|
pub struct OptionFromRequestFuture<F>(#[pin] F);
|
|
|
|
|
|
|
|
|
|
impl<F, T> Future for OptionFromRequestFuture<F>
|
|
|
|
|
where
|
|
|
|
|
F: Future<Output = Result<T, Error>>,
|
|
|
|
|
{
|
|
|
|
|
type Output = Result<Option<T>, Error>;
|
|
|
|
|
|
|
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
|
|
|
let value = ready!(self.project().0.poll(cx));
|
|
|
|
|
Poll::Ready(Ok(value.ok()))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
|
pub struct Query<T>(T);
|
|
|
|
|
|
|
|
|
|
impl<T> Query<T> {
|
|
|
|
|
pub fn into_inner(self) -> T {
|
|
|
|
|
self.0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> FromRequest for Query<T>
|
|
|
|
|
where
|
|
|
|
|
T: DeserializeOwned + Send,
|
|
|
|
|
{
|
|
|
|
|
type Future = future::Ready<Result<Self, Error>>;
|
|
|
|
|
|
|
|
|
|
fn from_request(req: &mut Request<Body>) -> Self::Future {
|
|
|
|
|
let result = (|| {
|
|
|
|
|
let query = req.uri().query().ok_or(Error::QueryStringMissing)?;
|
|
|
|
|
let value = serde_urlencoded::from_str(query).map_err(Error::DeserializeQueryString)?;
|
|
|
|
|
Ok(Query(value))
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
future::ready(result)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
|
pub struct Json<T>(T);
|
|
|
|
|
|
|
|
|
|
impl<T> Json<T> {
|
|
|
|
|
pub fn into_inner(self) -> T {
|
|
|
|
|
self.0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> FromRequest for Json<T>
|
|
|
|
|
where
|
|
|
|
|
T: DeserializeOwned,
|
|
|
|
|
{
|
|
|
|
|
type Future = future::BoxFuture<'static, Result<Self, Error>>;
|
|
|
|
|
|
|
|
|
|
fn from_request(req: &mut Request<Body>) -> Self::Future {
|
2021-05-31 12:22:16 +02:00
|
|
|
if has_content_type(&req, "application/json") {
|
|
|
|
|
let body = std::mem::take(req.body_mut());
|
|
|
|
|
|
|
|
|
|
Box::pin(async move {
|
|
|
|
|
let bytes = hyper::body::to_bytes(body)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(Error::ConsumeRequestBody)?;
|
|
|
|
|
let value =
|
|
|
|
|
serde_json::from_slice(&bytes).map_err(Error::DeserializeRequestBody)?;
|
|
|
|
|
Ok(Json(value))
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
Box::pin(async { Err(Error::Status(StatusCode::BAD_REQUEST)) })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-30 13:24:03 +02:00
|
|
|
|
2021-05-31 12:22:16 +02:00
|
|
|
fn has_content_type<B>(req: &Request<B>, expected_content_type: &str) -> bool {
|
|
|
|
|
let content_type = if let Some(content_type) = req.headers().get(header::CONTENT_TYPE) {
|
|
|
|
|
content_type
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
};
|
2021-05-30 13:24:03 +02:00
|
|
|
|
2021-05-31 12:22:16 +02:00
|
|
|
let content_type = if let Ok(content_type) = content_type.to_str() {
|
|
|
|
|
content_type
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
content_type.starts_with(expected_content_type)
|
2021-05-30 13:24:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
|
pub struct Extension<T>(T);
|
|
|
|
|
|
|
|
|
|
impl<T> Extension<T> {
|
|
|
|
|
pub fn into_inner(self) -> T {
|
|
|
|
|
self.0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> FromRequest for Extension<T>
|
|
|
|
|
where
|
|
|
|
|
T: Clone + Send + Sync + 'static,
|
|
|
|
|
{
|
|
|
|
|
type Future = future::Ready<Result<Self, Error>>;
|
|
|
|
|
|
|
|
|
|
fn from_request(req: &mut Request<Body>) -> Self::Future {
|
|
|
|
|
let result = (|| {
|
|
|
|
|
let value = req
|
|
|
|
|
.extensions()
|
|
|
|
|
.get::<T>()
|
|
|
|
|
.ok_or_else(|| Error::MissingExtension {
|
|
|
|
|
type_name: std::any::type_name::<T>(),
|
|
|
|
|
})
|
|
|
|
|
.map(|x| x.clone())?;
|
|
|
|
|
Ok(Extension(value))
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
future::ready(result)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-30 14:33:20 +02:00
|
|
|
impl FromRequest for Bytes {
|
|
|
|
|
type Future = future::BoxFuture<'static, Result<Self, Error>>;
|
|
|
|
|
|
|
|
|
|
fn from_request(req: &mut Request<Body>) -> Self::Future {
|
|
|
|
|
let body = std::mem::take(req.body_mut());
|
|
|
|
|
|
|
|
|
|
Box::pin(async move {
|
|
|
|
|
let bytes = hyper::body::to_bytes(body)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(Error::ConsumeRequestBody)?;
|
|
|
|
|
Ok(bytes)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-31 12:22:16 +02:00
|
|
|
impl FromRequest for String {
|
|
|
|
|
type Future = future::BoxFuture<'static, Result<Self, Error>>;
|
|
|
|
|
|
|
|
|
|
fn from_request(req: &mut Request<Body>) -> Self::Future {
|
|
|
|
|
let body = std::mem::take(req.body_mut());
|
|
|
|
|
|
|
|
|
|
Box::pin(async move {
|
|
|
|
|
let bytes = hyper::body::to_bytes(body)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(Error::ConsumeRequestBody)?
|
|
|
|
|
.to_vec();
|
|
|
|
|
let string = String::from_utf8(bytes).map_err(|_| Error::InvalidUtf8)?;
|
|
|
|
|
Ok(string)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl FromRequest for Body {
|
|
|
|
|
type Future = future::Ready<Result<Self, Error>>;
|
|
|
|
|
|
|
|
|
|
fn from_request(req: &mut Request<Body>) -> Self::Future {
|
|
|
|
|
let body = std::mem::take(req.body_mut());
|
|
|
|
|
future::ok(body)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-30 13:24:03 +02:00
|
|
|
#[derive(Debug, Clone)]
|
2021-05-30 14:33:20 +02:00
|
|
|
pub struct BytesMaxLength<const N: u64>(Bytes);
|
2021-05-30 13:24:03 +02:00
|
|
|
|
2021-05-30 14:33:20 +02:00
|
|
|
impl<const N: u64> BytesMaxLength<N> {
|
|
|
|
|
pub fn into_inner(self) -> Bytes {
|
2021-05-30 13:24:03 +02:00
|
|
|
self.0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-30 14:33:20 +02:00
|
|
|
impl<const N: u64> FromRequest for BytesMaxLength<N> {
|
2021-05-30 13:24:03 +02:00
|
|
|
type Future = future::BoxFuture<'static, Result<Self, Error>>;
|
|
|
|
|
|
|
|
|
|
fn from_request(req: &mut Request<Body>) -> Self::Future {
|
2021-05-30 14:33:20 +02:00
|
|
|
let content_length = req.headers().get(http::header::CONTENT_LENGTH).cloned();
|
2021-05-30 13:24:03 +02:00
|
|
|
let body = std::mem::take(req.body_mut());
|
|
|
|
|
|
|
|
|
|
Box::pin(async move {
|
2021-05-30 14:33:20 +02:00
|
|
|
let content_length =
|
|
|
|
|
content_length.and_then(|value| value.to_str().ok()?.parse::<u64>().ok());
|
|
|
|
|
|
|
|
|
|
if let Some(length) = content_length {
|
|
|
|
|
if length > N {
|
|
|
|
|
return Err(Error::PayloadTooLarge);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return Err(Error::LengthRequired);
|
|
|
|
|
};
|
|
|
|
|
|
2021-05-30 13:24:03 +02:00
|
|
|
let bytes = hyper::body::to_bytes(body)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(Error::ConsumeRequestBody)?;
|
2021-05-30 14:33:20 +02:00
|
|
|
|
|
|
|
|
Ok(BytesMaxLength(bytes))
|
2021-05-30 13:24:03 +02:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-30 15:44:26 +02:00
|
|
|
|
2021-05-30 16:37:27 +02:00
|
|
|
pub struct UrlParamsMap(HashMap<String, String>);
|
2021-05-30 15:44:26 +02:00
|
|
|
|
2021-05-30 16:37:27 +02:00
|
|
|
impl UrlParamsMap {
|
2021-05-30 15:44:26 +02:00
|
|
|
pub fn get(&self, key: &str) -> Result<&str, Error> {
|
|
|
|
|
if let Some(value) = self.0.get(key) {
|
|
|
|
|
Ok(value)
|
|
|
|
|
} else {
|
|
|
|
|
Err(Error::UnknownUrlParam(key.to_string()))
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-30 16:37:27 +02:00
|
|
|
|
|
|
|
|
pub fn get_typed<T>(&self, key: &str) -> Result<T, Error>
|
|
|
|
|
where
|
2021-05-30 16:53:27 +02:00
|
|
|
T: FromStr,
|
2021-05-30 16:37:27 +02:00
|
|
|
{
|
|
|
|
|
self.get(key)?.parse().map_err(|_| Error::InvalidUrlParam {
|
|
|
|
|
type_name: std::any::type_name::<T>(),
|
|
|
|
|
})
|
|
|
|
|
}
|
2021-05-30 15:44:26 +02:00
|
|
|
}
|
|
|
|
|
|
2021-05-30 16:37:27 +02:00
|
|
|
impl FromRequest for UrlParamsMap {
|
2021-05-30 15:44:26 +02:00
|
|
|
type Future = future::Ready<Result<Self, Error>>;
|
|
|
|
|
|
|
|
|
|
fn from_request(req: &mut Request<Body>) -> Self::Future {
|
|
|
|
|
if let Some(params) = req
|
|
|
|
|
.extensions_mut()
|
|
|
|
|
.get_mut::<Option<crate::routing::UrlParams>>()
|
|
|
|
|
{
|
|
|
|
|
let params = params.take().expect("params already taken").0;
|
|
|
|
|
future::ok(Self(params.into_iter().collect()))
|
|
|
|
|
} else {
|
|
|
|
|
panic!("no url params found for matched route. This is a bug in tower-web")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-30 16:53:27 +02:00
|
|
|
|
|
|
|
|
pub struct UrlParams<T>(T);
|
|
|
|
|
|
|
|
|
|
impl<T> UrlParams<T> {
|
|
|
|
|
pub fn into_inner(self) -> T {
|
|
|
|
|
self.0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
macro_rules! impl_parse_url {
|
|
|
|
|
() => {};
|
|
|
|
|
|
|
|
|
|
( $head:ident, $($tail:ident),* $(,)? ) => {
|
|
|
|
|
impl<$head, $($tail,)*> FromRequest for UrlParams<($head, $($tail,)*)>
|
|
|
|
|
where
|
|
|
|
|
$head: FromStr + Send,
|
|
|
|
|
$( $tail: FromStr + Send, )*
|
|
|
|
|
{
|
|
|
|
|
type Future = future::Ready<Result<Self, Error>>;
|
|
|
|
|
|
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
|
fn from_request(req: &mut Request<Body>) -> Self::Future {
|
|
|
|
|
let params = if let Some(params) = req
|
|
|
|
|
.extensions_mut()
|
|
|
|
|
.get_mut::<Option<crate::routing::UrlParams>>()
|
|
|
|
|
{
|
|
|
|
|
params.take().expect("params already taken").0
|
|
|
|
|
} else {
|
|
|
|
|
panic!("no url params found for matched route. This is a bug in tower-web")
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if let [(_, $head), $((_, $tail),)*] = &*params {
|
|
|
|
|
let $head = if let Ok(x) = $head.parse::<$head>() {
|
|
|
|
|
x
|
|
|
|
|
} else {
|
|
|
|
|
return future::err(Error::InvalidUrlParam {
|
|
|
|
|
type_name: std::any::type_name::<$head>(),
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$(
|
|
|
|
|
let $tail = if let Ok(x) = $tail.parse::<$tail>() {
|
|
|
|
|
x
|
|
|
|
|
} else {
|
|
|
|
|
return future::err(Error::InvalidUrlParam {
|
|
|
|
|
type_name: std::any::type_name::<$tail>(),
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
)*
|
|
|
|
|
|
|
|
|
|
future::ok(UrlParams(($head, $($tail,)*)))
|
|
|
|
|
} else {
|
|
|
|
|
panic!("wrong number of url params found for matched route. This is a bug in tower-web")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl_parse_url!($($tail,)*);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl_parse_url!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16);
|