//! Async functions that can be used to handle requests. use crate::{ body::{box_body, BoxBody}, extract::FromRequest, response::IntoResponse, routing::{EmptyRouter, MethodFilter}, service::HandleError, util::Either, }; use async_trait::async_trait; use bytes::Bytes; use http::{Request, Response}; use std::{ convert::Infallible, fmt, future::Future, marker::PhantomData, task::{Context, Poll}, }; use tower::{BoxError, Layer, Service, ServiceExt}; pub mod future; mod into_service; pub use self::into_service::IntoService; /// Route requests to the given handler regardless of the HTTP method of the /// request. /// /// # Example /// /// ```rust /// use axum::{ /// handler::any, /// Router, /// }; /// /// async fn handler() {} /// /// // All requests to `/` will go to `handler` regardless of the HTTP method. /// let app = Router::new().route("/", any(handler)); /// # async { /// # axum::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap(); /// # }; /// ``` pub fn any(handler: H) -> OnMethod where H: Handler, { on(MethodFilter::all(), handler) } /// Route `CONNECT` requests to the given handler. /// /// See [`get`] for an example. pub fn connect(handler: H) -> OnMethod where H: Handler, { on(MethodFilter::CONNECT, handler) } /// Route `DELETE` requests to the given handler. /// /// See [`get`] for an example. pub fn delete(handler: H) -> OnMethod where H: Handler, { on(MethodFilter::DELETE, handler) } /// Route `GET` requests to the given handler. /// /// # Example /// /// ```rust /// use axum::{ /// handler::get, /// Router, /// }; /// /// async fn handler() {} /// /// // Requests to `GET /` will go to `handler`. /// let app = Router::new().route("/", get(handler)); /// # async { /// # axum::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap(); /// # }; /// ``` /// /// Note that `get` routes will also be called for `HEAD` requests but will have /// the response body removed. Make sure to add explicit `HEAD` routes /// afterwards. pub fn get(handler: H) -> OnMethod where H: Handler, { on(MethodFilter::GET | MethodFilter::HEAD, handler) } /// Route `HEAD` requests to the given handler. /// /// See [`get`] for an example. pub fn head(handler: H) -> OnMethod where H: Handler, { on(MethodFilter::HEAD, handler) } /// Route `OPTIONS` requests to the given handler. /// /// See [`get`] for an example. pub fn options(handler: H) -> OnMethod where H: Handler, { on(MethodFilter::OPTIONS, handler) } /// Route `PATCH` requests to the given handler. /// /// See [`get`] for an example. pub fn patch(handler: H) -> OnMethod where H: Handler, { on(MethodFilter::PATCH, handler) } /// Route `POST` requests to the given handler. /// /// See [`get`] for an example. pub fn post(handler: H) -> OnMethod where H: Handler, { on(MethodFilter::POST, handler) } /// Route `PUT` requests to the given handler. /// /// See [`get`] for an example. pub fn put(handler: H) -> OnMethod where H: Handler, { on(MethodFilter::PUT, handler) } /// Route `TRACE` requests to the given handler. /// /// See [`get`] for an example. pub fn trace(handler: H) -> OnMethod where H: Handler, { on(MethodFilter::TRACE, handler) } /// Route requests with the given method to the handler. /// /// # Example /// /// ```rust /// use axum::{ /// handler::on, /// Router, /// routing::MethodFilter, /// }; /// /// async fn handler() {} /// /// // Requests to `POST /` will go to `handler`. /// let app = Router::new().route("/", on(MethodFilter::POST, handler)); /// # async { /// # axum::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap(); /// # }; /// ``` pub fn on(method: MethodFilter, handler: H) -> OnMethod where H: Handler, { OnMethod { method, handler, fallback: EmptyRouter::method_not_allowed(), _marker: PhantomData, } } pub(crate) mod sealed { #![allow(unreachable_pub, missing_docs, missing_debug_implementations)] pub trait HiddentTrait {} pub struct Hidden; impl HiddentTrait for Hidden {} } /// Trait for async functions that can be used to handle requests. /// /// You shouldn't need to depend on this trait directly. It is automatically /// implemented to closures of the right types. /// /// See the [module docs](crate::handler) for more details. #[async_trait] pub trait Handler: Clone + Send + Sized + 'static { // This seals the trait. We cannot use the regular "sealed super trait" // approach due to coherence. #[doc(hidden)] type Sealed: sealed::HiddentTrait; /// Call the handler with the given request. async fn call(self, req: Request) -> Response; /// Apply a [`tower::Layer`] to the handler. /// /// All requests to the handler will be processed by the layer's /// corresponding middleware. /// /// This can be used to add additional processing to a request for a single /// handler. /// /// Note this differs from [`routing::Layered`](crate::routing::Layered) /// which adds a middleware to a group of routes. /// /// # Example /// /// Adding the [`tower::limit::ConcurrencyLimit`] middleware to a handler /// can be done like so: /// /// ```rust /// use axum::{ /// handler::{get, Handler}, /// Router, /// }; /// use tower::limit::{ConcurrencyLimitLayer, ConcurrencyLimit}; /// /// async fn handler() { /* ... */ } /// /// let layered_handler = handler.layer(ConcurrencyLimitLayer::new(64)); /// let app = Router::new().route("/", get(layered_handler)); /// # async { /// # axum::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap(); /// # }; /// ``` /// /// When adding middleware that might fail its recommended to handle those /// errors. See [`Layered::handle_error`] for more details. fn layer(self, layer: L) -> Layered where L: Layer>, { Layered::new(layer.layer(any(self))) } /// Convert the handler into a [`Service`]. /// /// This allows you to serve a single handler if you don't need any routing: /// /// ```rust /// use axum::{ /// Server, handler::Handler, http::{Uri, Method}, response::IntoResponse, /// }; /// use tower::make::Shared; /// use std::net::SocketAddr; /// /// async fn handler(method: Method, uri: Uri, body: String) -> impl IntoResponse { /// format!("received `{} {}` with body `{:?}`", method, uri, body) /// } /// /// let service = handler.into_service(); /// /// # async { /// Server::bind(&SocketAddr::from(([127, 0, 0, 1], 3000))) /// .serve(Shared::new(service)) /// .await?; /// # Ok::<_, hyper::Error>(()) /// # }; /// ``` fn into_service(self) -> IntoService { IntoService::new(self) } } #[async_trait] impl Handler for F where F: FnOnce() -> Fut + Clone + Send + Sync + 'static, Fut: Future + Send, Res: IntoResponse, B: Send + 'static, { type Sealed = sealed::Hidden; async fn call(self, _req: Request) -> Response { self().await.into_response().map(box_body) } } macro_rules! impl_handler { () => { }; ( $head:ident, $($tail:ident),* $(,)? ) => { #[async_trait] #[allow(non_snake_case)] impl Handler for F where F: FnOnce($head, $($tail,)*) -> Fut + Clone + Send + Sync + 'static, Fut: Future + Send, B: Send + 'static, Res: IntoResponse, B: Send + 'static, $head: FromRequest + Send, $( $tail: FromRequest + Send,)* { type Sealed = sealed::Hidden; async fn call(self, req: Request) -> Response { let mut req = crate::extract::RequestParts::new(req); let $head = match $head::from_request(&mut req).await { Ok(value) => value, Err(rejection) => return rejection.into_response().map(box_body), }; $( let $tail = match $tail::from_request(&mut req).await { Ok(value) => value, Err(rejection) => return rejection.into_response().map(box_body), }; )* let res = self($head, $($tail,)*).await; res.into_response().map(crate::body::box_body) } } impl_handler!($($tail,)*); }; } impl_handler!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16); /// A [`Service`] created from a [`Handler`] by applying a Tower middleware. /// /// Created with [`Handler::layer`]. See that method for more details. pub struct Layered { svc: S, _input: PhantomData T>, } impl fmt::Debug for Layered where S: fmt::Debug, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("Layered").field("svc", &self.svc).finish() } } impl Clone for Layered where S: Clone, { fn clone(&self) -> Self { Self::new(self.svc.clone()) } } #[async_trait] impl Handler for Layered where S: Service, Response = Response> + Clone + Send + 'static, S::Error: IntoResponse, S::Future: Send, T: 'static, ReqBody: Send + 'static, ResBody: http_body::Body + Send + Sync + 'static, ResBody::Error: Into + Send + Sync + 'static, { type Sealed = sealed::Hidden; async fn call(self, req: Request) -> Response { match self .svc .oneshot(req) .await .map_err(IntoResponse::into_response) { Ok(res) => res.map(box_body), Err(res) => res.map(box_body), } } } impl Layered { pub(crate) fn new(svc: S) -> Self { Self { svc, _input: PhantomData, } } /// Create a new [`Layered`] handler where errors will be handled using the /// given closure. /// /// This is used to convert errors to responses rather than simply /// terminating the connection. /// /// It works similarly to [`routing::Router::handle_error`]. See that for more details. /// /// [`routing::Router::handle_error`]: crate::routing::Router::handle_error pub fn handle_error( self, f: F, ) -> Layered, T> where S: Service, Response = Response>, F: FnOnce(S::Error) -> Result, Res: IntoResponse, { let svc = HandleError::new(self.svc, f); Layered::new(svc) } } /// A handler [`Service`] that accepts requests based on a [`MethodFilter`] and /// allows chaining additional handlers. pub struct OnMethod { pub(crate) method: MethodFilter, pub(crate) handler: H, pub(crate) fallback: F, pub(crate) _marker: PhantomData (B, T)>, } impl fmt::Debug for OnMethod where T: fmt::Debug, F: fmt::Debug, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("OnMethod") .field("method", &self.method) .field("handler", &format_args!("{}", std::any::type_name::())) .field("fallback", &self.fallback) .finish() } } impl Clone for OnMethod where H: Clone, F: Clone, { fn clone(&self) -> Self { Self { method: self.method, handler: self.handler.clone(), fallback: self.fallback.clone(), _marker: PhantomData, } } } impl Copy for OnMethod where H: Copy, F: Copy, { } impl OnMethod { /// Chain an additional handler that will accept all requests regardless of /// its HTTP method. /// /// See [`OnMethod::get`] for an example. pub fn any(self, handler: H2) -> OnMethod where H2: Handler, { self.on(MethodFilter::all(), handler) } /// Chain an additional handler that will only accept `CONNECT` requests. /// /// See [`OnMethod::get`] for an example. pub fn connect(self, handler: H2) -> OnMethod where H2: Handler, { self.on(MethodFilter::CONNECT, handler) } /// Chain an additional handler that will only accept `DELETE` requests. /// /// See [`OnMethod::get`] for an example. pub fn delete(self, handler: H2) -> OnMethod where H2: Handler, { self.on(MethodFilter::DELETE, handler) } /// Chain an additional handler that will only accept `GET` requests. /// /// # Example /// /// ```rust /// use axum::{handler::post, Router}; /// /// async fn handler() {} /// /// async fn other_handler() {} /// /// // Requests to `GET /` will go to `handler` and `POST /` will go to /// // `other_handler`. /// let app = Router::new().route("/", post(handler).get(other_handler)); /// # async { /// # axum::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap(); /// # }; /// ``` /// /// Note that `get` routes will also be called for `HEAD` requests but will have /// the response body removed. Make sure to add explicit `HEAD` routes /// afterwards. pub fn get(self, handler: H2) -> OnMethod where H2: Handler, { self.on(MethodFilter::GET | MethodFilter::HEAD, handler) } /// Chain an additional handler that will only accept `HEAD` requests. /// /// See [`OnMethod::get`] for an example. pub fn head(self, handler: H2) -> OnMethod where H2: Handler, { self.on(MethodFilter::HEAD, handler) } /// Chain an additional handler that will only accept `OPTIONS` requests. /// /// See [`OnMethod::get`] for an example. pub fn options(self, handler: H2) -> OnMethod where H2: Handler, { self.on(MethodFilter::OPTIONS, handler) } /// Chain an additional handler that will only accept `PATCH` requests. /// /// See [`OnMethod::get`] for an example. pub fn patch(self, handler: H2) -> OnMethod where H2: Handler, { self.on(MethodFilter::PATCH, handler) } /// Chain an additional handler that will only accept `POST` requests. /// /// See [`OnMethod::get`] for an example. pub fn post(self, handler: H2) -> OnMethod where H2: Handler, { self.on(MethodFilter::POST, handler) } /// Chain an additional handler that will only accept `PUT` requests. /// /// See [`OnMethod::get`] for an example. pub fn put(self, handler: H2) -> OnMethod where H2: Handler, { self.on(MethodFilter::PUT, handler) } /// Chain an additional handler that will only accept `TRACE` requests. /// /// See [`OnMethod::get`] for an example. pub fn trace(self, handler: H2) -> OnMethod where H2: Handler, { self.on(MethodFilter::TRACE, handler) } /// Chain an additional handler that will accept requests matching the given /// `MethodFilter`. /// /// # Example /// /// ```rust /// use axum::{ /// handler::get, /// Router, /// routing::MethodFilter /// }; /// /// async fn handler() {} /// /// async fn other_handler() {} /// /// // Requests to `GET /` will go to `handler` and `DELETE /` will go to /// // `other_handler` /// let app = Router::new().route("/", get(handler).on(MethodFilter::DELETE, other_handler)); /// # async { /// # axum::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap(); /// # }; /// ``` pub fn on(self, method: MethodFilter, handler: H2) -> OnMethod where H2: Handler, { OnMethod { method, handler, fallback: self, _marker: PhantomData, } } } impl Service> for OnMethod where H: Handler, F: Service, Response = Response, Error = Infallible> + Clone, B: Send + 'static, { type Response = Response; type Error = Infallible; type Future = future::OnMethodFuture; fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll> { Poll::Ready(Ok(())) } fn call(&mut self, req: Request) -> Self::Future { let req_method = req.method().clone(); let fut = if self.method.matches(req.method()) { let fut = Handler::call(self.handler.clone(), req); Either::A { inner: fut } } else { let fut = self.fallback.clone().oneshot(req); Either::B { inner: fut } }; future::OnMethodFuture { inner: fut, req_method, } } }