Skip to main content

axum/routing/
route.rs

1use crate::{
2    body::{Body, HttpBody},
3    response::Response,
4    util::MapIntoResponse,
5};
6use axum_core::{extract::Request, response::IntoResponse};
7use bytes::Bytes;
8use http::{
9    header::{self, CONTENT_LENGTH},
10    HeaderMap, HeaderValue, Method,
11};
12use pin_project_lite::pin_project;
13use std::{
14    convert::Infallible,
15    fmt,
16    future::Future,
17    pin::Pin,
18    task::{ready, Context, Poll},
19};
20use tower::{
21    util::{BoxCloneSyncService, MapErrLayer, Oneshot},
22    ServiceExt,
23};
24use tower_layer::Layer;
25use tower_service::Service;
26
27/// How routes are stored inside a [`Router`](super::Router).
28///
29/// You normally shouldn't need to care about this type. It's used in
30/// [`Router::layer`](super::Router::layer).
31pub struct Route<E = Infallible>(BoxCloneSyncService<Request, Response, E>);
32
33impl<E> Route<E> {
34    pub(crate) fn new<T>(svc: T) -> Self
35    where
36        T: Service<Request, Error = E> + Clone + Send + Sync + 'static,
37        T::Response: IntoResponse + 'static,
38        T::Future: Send + 'static,
39    {
40        Self(BoxCloneSyncService::new(MapIntoResponse::new(svc)))
41    }
42
43    /// Variant of [`Route::call`] that takes ownership of the route to avoid cloning.
44    pub(crate) fn call_owned(self, req: Request<Body>) -> RouteFuture<E> {
45        let req = req.map(Body::new);
46        self.oneshot_inner_owned(req).not_top_level()
47    }
48
49    pub(crate) fn oneshot_inner(&self, req: Request) -> RouteFuture<E> {
50        let method = req.method().clone();
51        RouteFuture::new(method, self.0.clone().oneshot(req))
52    }
53
54    /// Variant of [`Route::oneshot_inner`] that takes ownership of the route to avoid cloning.
55    pub(crate) fn oneshot_inner_owned(self, req: Request) -> RouteFuture<E> {
56        let method = req.method().clone();
57        RouteFuture::new(method, self.0.oneshot(req))
58    }
59
60    pub(crate) fn layer<L, NewError>(self, layer: L) -> Route<NewError>
61    where
62        L: Layer<Self> + Clone + Send + 'static,
63        L::Service: Service<Request> + Clone + Send + Sync + 'static,
64        <L::Service as Service<Request>>::Response: IntoResponse + 'static,
65        <L::Service as Service<Request>>::Error: Into<NewError> + 'static,
66        <L::Service as Service<Request>>::Future: Send + 'static,
67        NewError: 'static,
68    {
69        let layer = (MapErrLayer::new(Into::into), layer);
70
71        Route::new(layer.layer(self))
72    }
73}
74
75impl<E> Clone for Route<E> {
76    #[track_caller]
77    fn clone(&self) -> Self {
78        Self(self.0.clone())
79    }
80}
81
82impl<E> fmt::Debug for Route<E> {
83    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
84        f.debug_struct("Route").finish()
85    }
86}
87
88impl<B, E> Service<Request<B>> for Route<E>
89where
90    B: HttpBody<Data = bytes::Bytes> + Send + 'static,
91    B::Error: Into<axum_core::BoxError>,
92{
93    type Response = Response;
94    type Error = E;
95    type Future = RouteFuture<E>;
96
97    #[inline]
98    fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
99        Poll::Ready(Ok(()))
100    }
101
102    #[inline]
103    fn call(&mut self, req: Request<B>) -> Self::Future {
104        self.oneshot_inner(req.map(Body::new)).not_top_level()
105    }
106}
107
108pin_project! {
109    /// Response future for [`Route`].
110    pub struct RouteFuture<E> {
111        #[pin]
112        inner: Oneshot<BoxCloneSyncService<Request, Response, E>, Request>,
113        method: Method,
114        allow_header: Option<Bytes>,
115        top_level: bool,
116    }
117}
118
119impl<E> RouteFuture<E> {
120    fn new(
121        method: Method,
122        inner: Oneshot<BoxCloneSyncService<Request, Response, E>, Request>,
123    ) -> Self {
124        Self {
125            inner,
126            method,
127            allow_header: None,
128            top_level: true,
129        }
130    }
131
132    pub(crate) fn allow_header(mut self, allow_header: Bytes) -> Self {
133        self.allow_header = Some(allow_header);
134        self
135    }
136
137    pub(crate) fn not_top_level(mut self) -> Self {
138        self.top_level = false;
139        self
140    }
141}
142
143impl<E> Future for RouteFuture<E> {
144    type Output = Result<Response, E>;
145
146    #[inline]
147    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
148        let this = self.project();
149        let mut res = ready!(this.inner.poll(cx))?;
150
151        if *this.method == Method::CONNECT && res.status().is_success() {
152            // From https://httpwg.org/specs/rfc9110.html#CONNECT:
153            // > A server MUST NOT send any Transfer-Encoding or
154            // > Content-Length header fields in a 2xx (Successful)
155            // > response to CONNECT.
156            if res.headers().contains_key(&CONTENT_LENGTH)
157                || res.headers().contains_key(&header::TRANSFER_ENCODING)
158                || res.size_hint().lower() != 0
159            {
160                error!("response to CONNECT with nonempty body");
161                res = res.map(|_| Body::empty());
162                res.headers_mut().remove(&CONTENT_LENGTH);
163                res.headers_mut().remove(&header::TRANSFER_ENCODING);
164            }
165        } else if *this.top_level {
166            if res.status() == http::StatusCode::METHOD_NOT_ALLOWED {
167                // From https://httpwg.org/specs/rfc9110.html#field.allow:
168                // An origin server MUST generate an `Allow` header field in a 405 (Method Not Allowed) response and MAY do so in any other response.
169                set_allow_header(res.headers_mut(), this.allow_header);
170            }
171
172            // make sure to set content-length before removing the body
173            set_content_length(&res.size_hint(), res.headers_mut());
174
175            if *this.method == Method::HEAD {
176                *res.body_mut() = Body::empty();
177            }
178        }
179
180        Poll::Ready(Ok(res))
181    }
182}
183
184fn set_allow_header(headers: &mut HeaderMap, allow_header: &mut Option<Bytes>) {
185    match allow_header.take() {
186        Some(allow_header) if !headers.contains_key(header::ALLOW) => {
187            headers.insert(
188                header::ALLOW,
189                HeaderValue::from_maybe_shared(allow_header).expect("invalid `Allow` header"),
190            );
191        }
192        _ => {}
193    }
194}
195
196fn set_content_length(size_hint: &http_body::SizeHint, headers: &mut HeaderMap) {
197    if headers.contains_key(CONTENT_LENGTH) {
198        return;
199    }
200
201    if let Some(size) = size_hint.exact() {
202        let header_value = if size == 0 {
203            #[allow(clippy::declare_interior_mutable_const)]
204            const ZERO: HeaderValue = HeaderValue::from_static("0");
205
206            ZERO
207        } else {
208            let mut buffer = itoa::Buffer::new();
209            HeaderValue::from_str(buffer.format(size)).unwrap()
210        };
211
212        headers.insert(CONTENT_LENGTH, header_value);
213    }
214}
215
216pin_project! {
217    /// A [`RouteFuture`] that always yields a [`Response`].
218    pub struct InfallibleRouteFuture {
219        #[pin]
220        future: RouteFuture<Infallible>,
221    }
222}
223
224impl InfallibleRouteFuture {
225    pub(crate) fn new(future: RouteFuture<Infallible>) -> Self {
226        Self { future }
227    }
228}
229
230impl Future for InfallibleRouteFuture {
231    type Output = Response;
232
233    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
234        match ready!(self.project().future.poll(cx)) {
235            Ok(response) => Poll::Ready(response),
236            Err(err) => match err {},
237        }
238    }
239}
240
241#[cfg(test)]
242mod tests {
243    use super::*;
244    use crate::{routing::get, test_helpers::*, Router};
245
246    #[test]
247    fn traits() {
248        use crate::test_helpers::*;
249        assert_send::<Route<()>>();
250    }
251
252    #[crate::test]
253    async fn regression_3741() {
254        const BODY: &str = "Very expensive body.";
255        let content_length: HeaderValue = HeaderValue::from_str(&BODY.len().to_string()).unwrap();
256
257        async fn handler(method: http::Method) -> Response {
258            if method == http::Method::HEAD {
259                ().into_response()
260            } else {
261                BODY.into_response()
262            }
263        }
264
265        let client = TestClient::new(Router::new().route("/", get(handler)));
266
267        let get = client.get("/").await;
268        assert_eq!(get.status(), http::StatusCode::OK);
269        assert_eq!(get.headers().get(CONTENT_LENGTH), Some(&content_length));
270
271        let head = client.head("/").await;
272        assert_eq!(get.status(), http::StatusCode::OK);
273        assert_eq!(head.headers().get(CONTENT_LENGTH), None);
274    }
275
276    #[crate::test]
277    async fn head_content_length_default() {
278        const BODY: &str = "Hello world!";
279        let content_length: HeaderValue = HeaderValue::from_str(&BODY.len().to_string()).unwrap();
280
281        async fn handler() -> Response {
282            BODY.into_response()
283        }
284
285        let client = TestClient::new(Router::new().route("/", get(handler)));
286
287        let get = client.get("/").await;
288        assert_eq!(get.status(), http::StatusCode::OK);
289        assert_eq!(get.headers().get(CONTENT_LENGTH), Some(&content_length));
290
291        let head = client.head("/").await;
292        assert_eq!(head.status(), http::StatusCode::OK);
293        assert_eq!(head.headers().get(CONTENT_LENGTH), Some(&content_length));
294    }
295
296    #[crate::test]
297    async fn unit_content_length_zero() {
298        let content_length: HeaderValue = HeaderValue::from_static("0");
299
300        async fn handler() -> Response {
301            ().into_response()
302        }
303
304        let client = TestClient::new(Router::new().route("/", get(handler)));
305
306        let get = client.get("/").await;
307        assert_eq!(get.status(), http::StatusCode::OK);
308        assert_eq!(get.headers().get(CONTENT_LENGTH), Some(&content_length));
309    }
310}