tower_http/cors/
allow_headers.rs1use 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#[derive(Clone, Default)]
17#[must_use]
18pub struct AllowHeaders(AllowHeadersInner);
19
20impl AllowHeaders {
21 pub fn any() -> Self {
27 Self(AllowHeadersInner::Const(Some(WILDCARD)))
28 }
29
30 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 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)] 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}