tower_http/services/
redirect.rs

1//! Service that redirects all requests.
2//!
3//! # Example
4//!
5//! Imagine that we run `example.com` and want to redirect all requests using `HTTP` to `HTTPS`.
6//! That can be done like so:
7//!
8//! ```rust
9//! use http::{Request, Uri, StatusCode};
10//! use http_body_util::Full;
11//! use bytes::Bytes;
12//! use tower::{Service, ServiceExt};
13//! use tower_http::services::Redirect;
14//!
15//! # #[tokio::main]
16//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
17//! let uri: Uri = "https://example.com/".parse().unwrap();
18//! let mut service: Redirect<Full<Bytes>> = Redirect::permanent(uri);
19//!
20//! let request = Request::builder()
21//!     .uri("http://example.com")
22//!     .body(Full::<Bytes>::default())
23//!     .unwrap();
24//!
25//! let response = service.oneshot(request).await?;
26//!
27//! assert_eq!(response.status(), StatusCode::PERMANENT_REDIRECT);
28//! assert_eq!(response.headers()["location"], "https://example.com/");
29//! #
30//! # Ok(())
31//! # }
32//! ```
33
34use http::{header, HeaderValue, Response, StatusCode, Uri};
35use std::{
36    convert::{Infallible, TryFrom},
37    fmt,
38    future::Future,
39    marker::PhantomData,
40    pin::Pin,
41    task::{Context, Poll},
42};
43use tower_service::Service;
44
45/// Service that redirects all requests.
46///
47/// See the [module docs](crate::services::redirect) for more details.
48pub struct Redirect<ResBody> {
49    status_code: StatusCode,
50    location: HeaderValue,
51    // Covariant over ResBody, no dropping of ResBody
52    _marker: PhantomData<fn() -> ResBody>,
53}
54
55impl<ResBody> Redirect<ResBody> {
56    /// Create a new [`Redirect`] that uses a [`307 Temporary Redirect`][mdn] status code.
57    ///
58    /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/307
59    pub fn temporary(uri: Uri) -> Self {
60        Self::with_status_code(StatusCode::TEMPORARY_REDIRECT, uri)
61    }
62
63    /// Create a new [`Redirect`] that uses a [`308 Permanent Redirect`][mdn] status code.
64    ///
65    /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/308
66    pub fn permanent(uri: Uri) -> Self {
67        Self::with_status_code(StatusCode::PERMANENT_REDIRECT, uri)
68    }
69
70    /// Create a new [`Redirect`] that uses the given status code.
71    ///
72    /// # Panics
73    ///
74    /// - If `status_code` isn't a [redirection status code][mdn] (3xx).
75    /// - If `uri` isn't a valid [`HeaderValue`].
76    ///
77    /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#redirection_messages
78    pub fn with_status_code(status_code: StatusCode, uri: Uri) -> Self {
79        assert!(
80            status_code.is_redirection(),
81            "not a redirection status code"
82        );
83
84        Self {
85            status_code,
86            location: HeaderValue::try_from(uri.to_string())
87                .expect("URI isn't a valid header value"),
88            _marker: PhantomData,
89        }
90    }
91}
92
93impl<R, ResBody> Service<R> for Redirect<ResBody>
94where
95    ResBody: Default,
96{
97    type Response = Response<ResBody>;
98    type Error = Infallible;
99    type Future = ResponseFuture<ResBody>;
100
101    #[inline]
102    fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
103        Poll::Ready(Ok(()))
104    }
105
106    fn call(&mut self, _req: R) -> Self::Future {
107        ResponseFuture {
108            status_code: self.status_code,
109            location: Some(self.location.clone()),
110            _marker: PhantomData,
111        }
112    }
113}
114
115impl<ResBody> fmt::Debug for Redirect<ResBody> {
116    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
117        f.debug_struct("Redirect")
118            .field("status_code", &self.status_code)
119            .field("location", &self.location)
120            .finish()
121    }
122}
123
124impl<ResBody> Clone for Redirect<ResBody> {
125    fn clone(&self) -> Self {
126        Self {
127            status_code: self.status_code,
128            location: self.location.clone(),
129            _marker: PhantomData,
130        }
131    }
132}
133
134/// Response future of [`Redirect`].
135#[derive(Debug)]
136pub struct ResponseFuture<ResBody> {
137    location: Option<HeaderValue>,
138    status_code: StatusCode,
139    // Covariant over ResBody, no dropping of ResBody
140    _marker: PhantomData<fn() -> ResBody>,
141}
142
143impl<ResBody> Future for ResponseFuture<ResBody>
144where
145    ResBody: Default,
146{
147    type Output = Result<Response<ResBody>, Infallible>;
148
149    fn poll(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
150        let mut res = Response::default();
151
152        *res.status_mut() = self.status_code;
153
154        res.headers_mut()
155            .insert(header::LOCATION, self.location.take().unwrap());
156
157        Poll::Ready(Ok(res))
158    }
159}