use std::any::type_name;
use std::convert::Infallible;
use std::fmt;
use std::future::Future;
use std::marker::PhantomData;
use std::pin::Pin;
use std::task::{Context, Poll};
use navree_kernel::BoxFuture;
use tower::{Layer, Service};
use crate::AppRouter;
use crate::extract::{FromRequest, FromRequestParts, Request};
use crate::response::{IntoResponse, Response};
pub fn from_fn<F, T>(f: F) -> FromFnLayer<F, (), T> {
from_fn_with_state((), f)
}
pub fn from_fn_with_state<F, S, T>(state: S, f: F) -> FromFnLayer<F, S, T> {
FromFnLayer {
f,
state,
_arguments: PhantomData,
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct State<S>(pub S);
pub trait Middleware<S, T>: Clone + Send + 'static {
fn call(self, state: S, request: Request, next: Next) -> BoxFuture<'static, Response>;
}
#[doc(hidden)]
pub mod with {
#[derive(Debug)]
pub enum NoState {}
#[derive(Debug)]
pub enum State {}
}
macro_rules! middleware {
($($arg:ident),* ; $last:ident) => {
#[allow(non_snake_case, unused_mut)]
impl<F, Fut, Out, M, $($arg,)* $last>
Middleware<(), (with::NoState, M, $($arg,)* $last,)> for F
where
F: FnMut($($arg,)* $last, Next) -> Fut + Clone + Send + 'static,
Fut: Future<Output = Out> + Send + 'static,
Out: IntoResponse + 'static,
$($arg: FromRequestParts + 'static,)*
$last: FromRequest<M> + 'static,
M: 'static,
{
fn call(
mut self,
(): (),
request: Request,
next: Next,
) -> BoxFuture<'static, Response> {
Box::pin(async move {
middleware!(@extract request; $($arg),* ; $last);
self($($arg,)* $last, next).await.into_response()
})
}
}
#[allow(non_snake_case, unused_mut)]
impl<F, Fut, Out, S, M, $($arg,)* $last>
Middleware<S, (with::State, M, $($arg,)* $last,)> for F
where
F: FnMut(State<S>, $($arg,)* $last, Next) -> Fut + Clone + Send + 'static,
Fut: Future<Output = Out> + Send + 'static,
Out: IntoResponse + 'static,
S: Send + 'static,
$($arg: FromRequestParts + 'static,)*
$last: FromRequest<M> + 'static,
M: 'static,
{
fn call(
mut self,
state: S,
request: Request,
next: Next,
) -> BoxFuture<'static, Response> {
Box::pin(async move {
middleware!(@extract request; $($arg),* ; $last);
self(State(state), $($arg,)* $last, next).await.into_response()
})
}
}
};
(@extract $request:ident; $($arg:ident),* ; $last:ident) => {
let (mut parts, body) = $request.into_parts();
$(
let $arg = match $arg::from_request_parts(&mut parts).await {
Ok(value) => value,
Err(rejection) => return rejection.into_response(),
};
)*
let $last = match $last::from_request(Request::from_parts(parts, body)).await {
Ok(value) => value,
Err(rejection) => return rejection.into_response(),
};
};
}
middleware!(; T1);
middleware!(T1; T2);
middleware!(T1, T2; T3);
middleware!(T1, T2, T3; T4);
middleware!(T1, T2, T3, T4; T5);
middleware!(T1, T2, T3, T4, T5; T6);
middleware!(T1, T2, T3, T4, T5, T6; T7);
middleware!(T1, T2, T3, T4, T5, T6, T7; T8);
#[must_use]
pub struct FromFnLayer<F, S, T> {
f: F,
state: S,
_arguments: PhantomData<fn() -> T>,
}
impl<F: Clone, S: Clone, T> Clone for FromFnLayer<F, S, T> {
fn clone(&self) -> Self {
Self {
f: self.f.clone(),
state: self.state.clone(),
_arguments: PhantomData,
}
}
}
impl<I, F: Clone, S: Clone, T> Layer<I> for FromFnLayer<F, S, T> {
type Service = FromFn<F, S, I, T>;
fn layer(&self, inner: I) -> Self::Service {
FromFn {
f: self.f.clone(),
state: self.state.clone(),
inner,
_arguments: PhantomData,
}
}
}
impl<F, S: fmt::Debug, T> fmt::Debug for FromFnLayer<F, S, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("FromFnLayer")
.field("f", &format_args!("{}", type_name::<F>()))
.field("state", &self.state)
.finish()
}
}
pub struct FromFn<F, S, I, T> {
f: F,
state: S,
inner: I,
_arguments: PhantomData<fn() -> T>,
}
impl<F: Clone, S: Clone, I: Clone, T> Clone for FromFn<F, S, I, T> {
fn clone(&self) -> Self {
Self {
f: self.f.clone(),
state: self.state.clone(),
inner: self.inner.clone(),
_arguments: PhantomData,
}
}
}
impl<F, S: fmt::Debug, I: fmt::Debug, T> fmt::Debug for FromFn<F, S, I, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("FromFn")
.field("f", &format_args!("{}", type_name::<F>()))
.field("state", &self.state)
.field("inner", &self.inner)
.finish()
}
}
impl<F, S, I, T> Service<Request> for FromFn<F, S, I, T>
where
F: Middleware<S, T>,
S: Clone,
I: Service<Request, Error = Infallible> + Clone + Send + Sync + 'static,
I::Response: IntoResponse + 'static,
I::Future: Send + 'static,
{
type Response = Response;
type Error = Infallible;
type Future = ResponseFuture;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Infallible>> {
self.inner.poll_ready(cx)
}
fn call(&mut self, request: Request) -> ResponseFuture {
let not_ready = self.inner.clone();
let ready = std::mem::replace(&mut self.inner, not_ready);
let next = Next {
inner: AppRouter::new(ready),
};
ResponseFuture {
inner: self.f.clone().call(self.state.clone(), request, next),
}
}
}
pub struct ResponseFuture {
inner: BoxFuture<'static, Response>,
}
impl Future for ResponseFuture {
type Output = Result<Response, Infallible>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self.inner.as_mut().poll(cx).map(Ok)
}
}
impl fmt::Debug for ResponseFuture {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ResponseFuture").finish_non_exhaustive()
}
}
#[derive(Clone, Debug)]
pub struct Next {
inner: AppRouter,
}
impl Next {
pub async fn run(self, request: Request) -> Response {
match self.inner.call_owned(request).await {
Ok(response) => response,
Err(never) => match never {},
}
}
}
impl Service<Request> for Next {
type Response = Response;
type Error = Infallible;
type Future = crate::app::ResponseFuture;
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Infallible>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, request: Request) -> Self::Future {
self.inner.clone().call_owned(request)
}
}
#[cfg(test)]
mod tests {
use http::{HeaderMap, Method, StatusCode, Uri};
use http_body_util::BodyExt;
use tower::ServiceExt;
use super::*;
use crate::body::Body;
use crate::routing::Router;
use crate::{Json, Path, Query};
fn routes() -> AppRouter {
Router::new()
.route(Method::GET, "/{id}", |Path(id): Path<u32>| async move {
format!("{id}")
})
.into()
}
fn get(uri: &str) -> Request {
http::Request::get(uri).body(Body::empty()).unwrap()
}
async fn run(app: AppRouter, request: Request) -> (StatusCode, HeaderMap, String) {
let response = app.oneshot(request).await.unwrap();
let (parts, body) = response.into_parts();
let body = body.collect().await.unwrap().to_bytes();
(
parts.status,
parts.headers,
String::from_utf8_lossy(&body).into_owned(),
)
}
#[tokio::test]
async fn a_middleware_changes_the_request_and_the_response() {
async fn rewrite(mut request: Request, next: Next) -> Response {
*request.uri_mut() = Uri::from_static("/7");
let mut response = next.run(request).await;
response
.headers_mut()
.insert("x-seen", "yes".parse().unwrap());
response
}
let (status, headers, body) = run(routes().layer(from_fn(rewrite)), get("/1")).await;
assert_eq!(status, StatusCode::OK);
assert_eq!(headers["x-seen"], "yes");
assert_eq!(body, "7");
}
#[tokio::test]
async fn a_middleware_answers_without_the_rest() {
async fn refuse(_request: Request, _next: Next) -> impl IntoResponse {
(StatusCode::IM_A_TEAPOT, "no")
}
let (status, _, body) = run(routes().layer(from_fn(refuse)), get("/1")).await;
assert_eq!((status, body.as_str()), (StatusCode::IM_A_TEAPOT, "no"));
}
#[tokio::test]
async fn state_is_the_first_argument() {
async fn add(State(name): State<String>, request: Request, next: Next) -> Response {
let mut response = next.run(request).await;
response
.headers_mut()
.insert("x-name", name.parse().unwrap());
response
}
let app = routes().layer(from_fn_with_state("value".to_owned(), add));
let (_, headers, _) = run(app, get("/1")).await;
assert_eq!(headers["x-name"], "value");
}
#[tokio::test]
async fn arguments_are_extractors_and_the_last_may_read_the_body() {
async fn head(
Query(query): Query<std::collections::HashMap<String, String>>,
request: Request,
next: Next,
) -> Response {
let mut response = next.run(request).await;
let value = query["a"].parse().unwrap();
response.headers_mut().insert("x-a", value);
response
}
let (_, headers, _) = run(routes().layer(from_fn(head)), get("/1?a=b")).await;
assert_eq!(headers["x-a"], "b");
async fn body(Json(value): Json<u8>, _next: Next) -> String {
format!("got {value}")
}
let request = http::Request::post("/1")
.header("content-type", "application/json")
.body(Body::from("5"))
.unwrap();
let (_, _, text) = run(routes().layer(from_fn(body)), request).await;
assert_eq!(text, "got 5");
}
#[tokio::test]
async fn a_failed_extractor_answers_with_its_rejection() {
async fn needs_json(Json(_): Json<u8>, next: Next) -> Response {
next.run(get("/1")).await
}
let (status, _, _) = run(routes().layer(from_fn(needs_json)), get("/1")).await;
assert_eq!(status, StatusCode::UNSUPPORTED_MEDIA_TYPE);
}
#[tokio::test]
async fn the_body_reaches_the_handler_unread() {
async fn pass(request: Request, next: Next) -> Response {
next.run(request).await
}
let app = AppRouter::from_handler(|body: String| async move { body })
.layer(from_fn(pass))
.layer(from_fn(pass));
let chunks = futures_util::stream::iter(["a", "b"].map(Ok::<_, std::io::Error>));
let request = http::Request::post("/")
.body(Body::from_stream(chunks))
.unwrap();
let (_, _, text) = run(app, request).await;
assert_eq!(text, "ab");
}
#[tokio::test]
async fn a_closure_can_capture_what_it_needs() {
let name = "closure".to_owned();
let app = routes().layer(from_fn(move |request: Request, next: Next| {
let name = name.clone();
async move {
let mut response = next.run(request).await;
response
.headers_mut()
.insert("x-name", name.parse().unwrap());
response
}
}));
let (_, headers, _) = run(app, get("/1")).await;
assert_eq!(headers["x-name"], "closure");
}
#[tokio::test]
async fn it_layers_a_route_as_well() {
async fn stamp(request: Request, next: Next) -> Response {
let mut response = next.run(request).await;
response
.headers_mut()
.insert("x-route", "yes".parse().unwrap());
response
}
let router = Router::new()
.route(Method::GET, "/", || async { "ok" })
.route_layer(from_fn(stamp));
let response = router.oneshot(get("/")).await.unwrap();
assert_eq!(response.headers()["x-route"], "yes");
}
#[tokio::test]
async fn a_panic_in_a_middleware_reaches_whoever_is_outside_it() {
async fn panics(_request: Request, _next: Next) -> Response {
panic!("in the middleware")
}
let app = routes().layer(from_fn(panics));
let outcome = futures_util::FutureExt::catch_unwind(std::panic::AssertUnwindSafe(
app.oneshot(get("/1")),
))
.await;
let payload = outcome.unwrap_err();
assert_eq!(
payload.downcast_ref::<&str>(),
Some(&"in the middleware"),
"the panic is not swallowed or changed on its way out"
);
}
#[tokio::test]
async fn next_is_a_service() {
async fn twice(request: Request, next: Next) -> Response {
let first = next.clone().oneshot(get("/1")).await.unwrap();
assert_eq!(first.status(), StatusCode::OK);
next.run(request).await
}
let (_, _, body) = run(routes().layer(from_fn(twice)), get("/2")).await;
assert_eq!(body, "2");
}
}