tower_http/services/
redirect.rs1use 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
45pub struct Redirect<ResBody> {
49 status_code: StatusCode,
50 location: HeaderValue,
51 _marker: PhantomData<fn() -> ResBody>,
53}
54
55impl<ResBody> Redirect<ResBody> {
56 pub fn temporary(uri: Uri) -> Self {
60 Self::with_status_code(StatusCode::TEMPORARY_REDIRECT, uri)
61 }
62
63 pub fn permanent(uri: Uri) -> Self {
67 Self::with_status_code(StatusCode::PERMANENT_REDIRECT, uri)
68 }
69
70 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#[derive(Debug)]
136pub struct ResponseFuture<ResBody> {
137 location: Option<HeaderValue>,
138 status_code: StatusCode,
139 _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}