actix_quick_extract/headers/
origin.rs

1use actix_web::{http::header::ORIGIN, FromRequest};
2use derive_more::{AsRef, Deref, Display, Into};
3
4use super::HeaderType;
5use crate::ExtractError;
6/// The `Origin` header.
7///
8/// ```no_run
9/// use actix_quick_extract::headers::Origin;
10/// use actix_web::get;
11/// #[get("/")]
12/// pub async fn index(origin: Origin) -> String {
13///     format!("Your Origin Header is: {}", origin)
14/// }
15/// ```
16#[derive(Debug, Clone, PartialEq, Eq, Hash, Display, Into, AsRef, Deref)]
17#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
18#[display("{url}")]
19pub struct Origin {
20    #[into]
21    #[as_ref(str)]
22    #[deref(forward)]
23    pub url: String,
24    pub is_https: bool,
25}
26
27impl HeaderType for Origin {
28    #[inline]
29    fn from_request(req: &actix_web::HttpRequest) -> Result<Self, crate::ExtractError>
30    where
31        Self: Sized,
32    {
33        let (url, https) = if let Some(value) = req.headers().get(ORIGIN) {
34            value
35                .to_str()
36                .map(|value| (value.to_string(), value.starts_with("https")))
37                .map_err(|v| ExtractError::ToStrError("Origin", v))?
38        } else {
39            log::debug!("No Origin Header Found");
40            return Err(ExtractError::MissingHeader("Origin"));
41        };
42
43        Ok(Origin {
44            url,
45            is_https: https,
46        })
47    }
48}
49
50crate::ready_impl_from_request!(Origin as Header);