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
62
63
64
65
66
67
68
use std::convert::Infallible;

use actix_utils::future::{ok, Ready};
use actix_web::{dev::Payload, FromRequest, HttpRequest};

/// Host information.
///
/// See [`ConnectionInfo::host()`](actix_web::dev::ConnectionInfo::host) for more.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Host(String);

impl_more::impl_as_ref!(Host => String);
impl_more::impl_into!(Host => String);
impl_more::forward_display!(Host);

impl Host {
    /// Unwraps into inner string value.
    pub fn into_inner(self) -> String {
        self.0
    }
}

impl FromRequest for Host {
    type Error = Infallible;
    type Future = Ready<Result<Self, Self::Error>>;

    #[inline]
    fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
        ok(Host(req.connection_info().host().to_owned()))
    }
}

#[cfg(test)]
mod tests {
    use actix_web::{
        http::StatusCode,
        test::{self, TestRequest},
        web, App, HttpResponse,
    };

    use super::*;

    #[actix_web::test]
    async fn extracts_host() {
        let app =
            test::init_service(App::new().default_service(web::to(|host: Host| async move {
                HttpResponse::Ok().body(host.to_string())
            })))
            .await;

        let req = TestRequest::default()
            .insert_header(("host", "in-header.com"))
            .to_request();
        let res = test::call_service(&app, req).await;
        assert_eq!(res.status(), StatusCode::OK);
        assert_eq!(test::read_body(res).await, b"in-header.com".as_ref());

        let req = TestRequest::default().uri("http://in-url.com").to_request();
        let res = test::call_service(&app, req).await;
        assert_eq!(res.status(), StatusCode::OK);
        assert_eq!(test::read_body(res).await, b"in-url.com".as_ref());

        let req = TestRequest::default().to_request();
        let res = test::call_service(&app, req).await;
        assert_eq!(res.status(), StatusCode::OK);
        assert_eq!(test::read_body(res).await, b"localhost:8080".as_ref());
    }
}