tower_http/cors/
allow_headers.rs

1use std::{array, fmt};
2
3use http::{
4    header::{self, HeaderName, HeaderValue},
5    request::Parts as RequestParts,
6};
7
8use super::{separated_by_commas, Any, WILDCARD};
9
10/// Holds configuration for how to set the [`Access-Control-Allow-Headers`][mdn] header.
11///
12/// See [`CorsLayer::allow_headers`] for more details.
13///
14/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Headers
15/// [`CorsLayer::allow_headers`]: super::CorsLayer::allow_headers
16#[derive(Clone, Default)]
17#[must_use]
18pub struct AllowHeaders(AllowHeadersInner);
19
20impl AllowHeaders {
21    /// Allow any headers by sending a wildcard (`*`)
22    ///
23    /// See [`CorsLayer::allow_headers`] for more details.
24    ///
25    /// [`CorsLayer::allow_headers`]: super::CorsLayer::allow_headers
26    pub fn any() -> Self {
27        Self(AllowHeadersInner::Const(Some(WILDCARD)))
28    }
29
30    /// Set multiple allowed headers
31    ///
32    /// See [`CorsLayer::allow_headers`] for more details.
33    ///
34    /// [`CorsLayer::allow_headers`]: super::CorsLayer::allow_headers
35    pub fn list<I>(headers: I) -> Self
36    where
37        I: IntoIterator<Item = HeaderName>,
38    {
39        Self(AllowHeadersInner::Const(separated_by_commas(
40            headers.into_iter().map(Into::into),
41        )))
42    }
43
44    /// Allow any headers, by mirroring the preflight [`Access-Control-Request-Headers`][mdn]
45    /// header.
46    ///
47    /// See [`CorsLayer::allow_headers`] for more details.
48    ///
49    /// [`CorsLayer::allow_headers`]: super::CorsLayer::allow_headers
50    ///
51    /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Request-Headers
52    pub fn mirror_request() -> Self {
53        Self(AllowHeadersInner::MirrorRequest)
54    }
55
56    #[allow(clippy::borrow_interior_mutable_const)]
57    pub(super) fn is_wildcard(&self) -> bool {
58        matches!(&self.0, AllowHeadersInner::Const(Some(v)) if v == WILDCARD)
59    }
60
61    pub(super) fn to_header(&self, parts: &RequestParts) -> Option<(HeaderName, HeaderValue)> {
62        let allow_headers = match &self.0 {
63            AllowHeadersInner::Const(v) => v.clone()?,
64            AllowHeadersInner::MirrorRequest => parts
65                .headers
66                .get(header::ACCESS_CONTROL_REQUEST_HEADERS)?
67                .clone(),
68        };
69
70        Some((header::ACCESS_CONTROL_ALLOW_HEADERS, allow_headers))
71    }
72}
73
74impl fmt::Debug for AllowHeaders {
75    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
76        match &self.0 {
77            AllowHeadersInner::Const(inner) => f.debug_tuple("Const").field(inner).finish(),
78            AllowHeadersInner::MirrorRequest => f.debug_tuple("MirrorRequest").finish(),
79        }
80    }
81}
82
83impl From<Any> for AllowHeaders {
84    fn from(_: Any) -> Self {
85        Self::any()
86    }
87}
88
89impl<const N: usize> From<[HeaderName; N]> for AllowHeaders {
90    fn from(arr: [HeaderName; N]) -> Self {
91        #[allow(deprecated)] // Can be changed when MSRV >= 1.53
92        Self::list(array::IntoIter::new(arr))
93    }
94}
95
96impl From<Vec<HeaderName>> for AllowHeaders {
97    fn from(vec: Vec<HeaderName>) -> Self {
98        Self::list(vec)
99    }
100}
101
102#[derive(Clone)]
103enum AllowHeadersInner {
104    Const(Option<HeaderValue>),
105    MirrorRequest,
106}
107
108impl Default for AllowHeadersInner {
109    fn default() -> Self {
110        Self::Const(None)
111    }
112}