tower_async_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_async::{Service, ServiceExt};
13//! use tower_async_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 marker::PhantomData,
39};
40use tower_async_service::Service;
41
42/// Service that redirects all requests.
43///
44/// See the [module docs](crate::services::redirect) for more details.
45pub struct Redirect<ResBody> {
46 status_code: StatusCode,
47 location: HeaderValue,
48 // Covariant over ResBody, no dropping of ResBody
49 _marker: PhantomData<fn() -> ResBody>,
50}
51
52impl<ResBody> Redirect<ResBody> {
53 /// Create a new [`Redirect`] that uses a [`307 Temporary Redirect`][mdn] status code.
54 ///
55 /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/307
56 pub fn temporary(uri: Uri) -> Self {
57 Self::with_status_code(StatusCode::TEMPORARY_REDIRECT, uri)
58 }
59
60 /// Create a new [`Redirect`] that uses a [`308 Permanent Redirect`][mdn] status code.
61 ///
62 /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/308
63 pub fn permanent(uri: Uri) -> Self {
64 Self::with_status_code(StatusCode::PERMANENT_REDIRECT, uri)
65 }
66
67 /// Create a new [`Redirect`] that uses the given status code.
68 ///
69 /// # Panics
70 ///
71 /// - If `status_code` isn't a [redirection status code][mdn] (3xx).
72 /// - If `uri` isn't a valid [`HeaderValue`].
73 ///
74 /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#redirection_messages
75 pub fn with_status_code(status_code: StatusCode, uri: Uri) -> Self {
76 assert!(
77 status_code.is_redirection(),
78 "not a redirection status code"
79 );
80
81 Self {
82 status_code,
83 location: HeaderValue::try_from(uri.to_string())
84 .expect("URI isn't a valid header value"),
85 _marker: PhantomData,
86 }
87 }
88}
89
90impl<R, ResBody> Service<R> for Redirect<ResBody>
91where
92 ResBody: Default,
93{
94 type Response = Response<ResBody>;
95 type Error = Infallible;
96
97 async fn call(&self, _req: R) -> Result<Self::Response, Self::Error> {
98 let mut res = Response::default();
99 *res.status_mut() = self.status_code;
100 res.headers_mut()
101 .insert(header::LOCATION, self.location.clone());
102 Ok(res)
103 }
104}
105
106impl<ResBody> fmt::Debug for Redirect<ResBody> {
107 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
108 f.debug_struct("Redirect")
109 .field("status_code", &self.status_code)
110 .field("location", &self.location)
111 .finish()
112 }
113}
114
115impl<ResBody> Clone for Redirect<ResBody> {
116 fn clone(&self) -> Self {
117 Self {
118 status_code: self.status_code,
119 location: self.location.clone(),
120 _marker: PhantomData,
121 }
122 }
123}