1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use std::convert::TryInto;

use headers::HeaderValue;
use http::header::{self, InvalidHeaderValue};
use http::StatusCode;
use hyper::Body;

use crate::{IntoResponse, Response};

#[derive(Debug, Clone)]
pub struct Redirect {
    status: StatusCode,
    location: HeaderValue,
}

impl Redirect {
    pub fn see_other(
        location: impl TryInto<HeaderValue, Error = InvalidHeaderValue>,
    ) -> Result<Self, InvalidHeaderValue> {
        Ok(Redirect {
            status: StatusCode::SEE_OTHER,
            location: location.try_into()?,
        })
    }

    pub fn temporary(
        location: impl TryInto<HeaderValue, Error = InvalidHeaderValue>,
    ) -> Result<Self, InvalidHeaderValue> {
        Ok(Redirect {
            status: StatusCode::TEMPORARY_REDIRECT,
            location: location.try_into()?,
        })
    }

    pub fn permanent(
        location: impl TryInto<HeaderValue, Error = InvalidHeaderValue>,
    ) -> Result<Self, InvalidHeaderValue> {
        Ok(Redirect {
            status: StatusCode::PERMANENT_REDIRECT,
            location: location.try_into()?,
        })
    }
}

impl IntoResponse for Redirect {
    fn into_response(self) -> Response {
        let mut res = Response::new(Body::empty());
        *res.status_mut() = self.status;
        let headers = res.headers_mut();
        headers.insert(header::LOCATION, self.location);
        res
    }
}

impl IntoResponse for InvalidHeaderValue {
    fn into_response(self) -> Response {
        #[cfg(feature = "tracing")]
        tracing::error!(err = %self, "redirect location is not a valid header value");
        StatusCode::INTERNAL_SERVER_ERROR.into_response()
    }
}