rama_http/service/redirect/
static.rs1use crate::Request;
2use crate::Response;
3use crate::service::web::response;
4use rama_core::Service;
5use rama_http_headers::{HeaderMapExt, Location};
6use rama_http_types::body::OptionalBody;
7use std::{convert::Infallible, fmt, marker::PhantomData};
8
9pub struct RedirectStatic<ResBody> {
11 resp: response::Redirect,
12 _marker: PhantomData<fn() -> ResBody>,
14}
15
16impl<ResBody> RedirectStatic<ResBody> {
17 pub fn to(loc: impl response::redirect::IntoRedirectLoc) -> Self {
27 Self {
28 resp: response::Redirect::to(loc),
29 _marker: PhantomData,
30 }
31 }
32
33 pub fn moved(loc: impl response::redirect::IntoRedirectLoc) -> Self {
39 Self {
40 resp: response::Redirect::moved(loc),
41 _marker: PhantomData,
42 }
43 }
44
45 pub fn found(loc: impl response::redirect::IntoRedirectLoc) -> Self {
50 Self {
51 resp: response::Redirect::found(loc),
52 _marker: PhantomData,
53 }
54 }
55
56 pub fn temporary(loc: impl response::redirect::IntoRedirectLoc) -> Self {
58 Self {
59 resp: response::Redirect::temporary(loc),
60 _marker: PhantomData,
61 }
62 }
63
64 pub fn permanent(loc: impl response::redirect::IntoRedirectLoc) -> Self {
66 Self {
67 resp: response::Redirect::permanent(loc),
68 _marker: PhantomData,
69 }
70 }
71}
72
73impl<Body, ResBody> Service<Request<Body>> for RedirectStatic<ResBody>
74where
75 Body: Send + 'static,
76 ResBody: Send + 'static,
77{
78 type Output = Response<OptionalBody<ResBody>>;
79 type Error = Infallible;
80
81 async fn serve(&self, _req: Request<Body>) -> Result<Self::Output, Self::Error> {
82 let mut res = Response::default();
83 *res.status_mut() = self.resp.status_code();
84 res.headers_mut()
85 .typed_insert(Location::new(self.resp.location().clone()));
86 Ok(res)
87 }
88}
89
90impl<ResBody> fmt::Debug for RedirectStatic<ResBody> {
91 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
92 f.debug_struct("RedirectStatic")
93 .field("response", &self.resp)
94 .finish()
95 }
96}
97
98impl<ResBody> Clone for RedirectStatic<ResBody> {
99 fn clone(&self) -> Self {
100 Self {
101 resp: self.resp.clone(),
102 _marker: PhantomData,
103 }
104 }
105}