Skip to main content

trillium_http/headers/
header_value.rs

1use HeaderValueInner::{Bytes, Utf8};
2use smallvec::SmallVec;
3use smartcow::SmartCow;
4use smartstring::SmartString;
5use std::{
6    borrow::Cow,
7    fmt::{Debug, Display, Formatter, Write},
8};
9
10/// A `HeaderValue` represents the right hand side of a single `name:
11/// value` pair.
12#[derive(Clone)]
13pub struct HeaderValue {
14    pub(crate) inner: HeaderValueInner,
15    /// HPACK / QPACK "Never-Indexed" bit, carried for proxy round-trip fidelity:
16    /// decoders set it from the wire bit and encoders re-emit a never-indexed
17    /// literal when it is set. Not part of value identity (`PartialEq` / `Hash`
18    /// ignore it).
19    pub(crate) never_indexed: bool,
20}
21
22impl HeaderValue {
23    pub(crate) const fn from_inner(inner: HeaderValueInner) -> Self {
24        Self {
25            inner,
26            never_indexed: false,
27        }
28    }
29
30    pub(crate) fn is_never_indexed(&self) -> bool {
31        self.never_indexed
32    }
33
34    pub(crate) fn set_never_indexed(&mut self, never_indexed: bool) {
35        self.never_indexed = never_indexed;
36    }
37}
38
39impl PartialEq for HeaderValue {
40    fn eq(&self, other: &Self) -> bool {
41        self.inner == other.inner
42    }
43}
44
45impl Eq for HeaderValue {}
46
47impl std::hash::Hash for HeaderValue {
48    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
49        self.inner.hash(state);
50    }
51}
52
53impl PartialOrd for HeaderValue {
54    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
55        Some(self.cmp(other))
56    }
57}
58
59impl Ord for HeaderValue {
60    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
61        self.inner.cmp(&other.inner)
62    }
63}
64
65impl From<Cow<'static, [u8]>> for HeaderValue {
66    fn from(value: Cow<'static, [u8]>) -> Self {
67        match value {
68            Cow::Borrowed(bytes) => match std::str::from_utf8(bytes) {
69                Ok(s) => Self::from_inner(Utf8(SmartCow::Borrowed(s))),
70                Err(_) => Self::from_inner(Bytes(bytes.into())),
71            },
72
73            Cow::Owned(bytes) => match String::from_utf8(bytes) {
74                Ok(s) => Self::from_inner(Utf8(SmartCow::Owned(s.into()))),
75                Err(e) => Self::from_inner(Bytes(e.into_bytes().into())),
76            },
77        }
78    }
79}
80
81#[derive(Eq, PartialEq, Clone, Hash)]
82pub(crate) enum HeaderValueInner {
83    Utf8(SmartCow<'static>),
84    Bytes(SmallVec<[u8; 32]>),
85}
86
87impl PartialOrd for HeaderValueInner {
88    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
89        Some(self.cmp(other))
90    }
91}
92impl Ord for HeaderValueInner {
93    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
94        let this: &[u8] = self.as_ref();
95        let that: &[u8] = other.as_ref();
96        this.cmp(that)
97    }
98}
99
100#[cfg(feature = "serde")]
101impl serde::Serialize for HeaderValue {
102    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
103    where
104        S: serde::Serializer,
105    {
106        match &self.inner {
107            Utf8(s) => serializer.serialize_str(s),
108            Bytes(bytes) => serializer.serialize_bytes(bytes),
109        }
110    }
111}
112
113impl Debug for HeaderValue {
114    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
115        match &self.inner {
116            Utf8(s) => Debug::fmt(s, f),
117            Bytes(b) => Debug::fmt(&String::from_utf8_lossy(b), f),
118        }
119    }
120}
121
122impl HeaderValue {
123    /// Build a new header value from a &'static str at compile time
124    pub const fn const_new(value: &'static str) -> Self {
125        Self::from_inner(Utf8(SmartCow::Borrowed(value)))
126    }
127
128    /// determine if this header contains no unsafe characters (\r, \n, \0)
129    pub fn is_valid(&self) -> bool {
130        memchr::memchr3(b'\r', b'\n', 0, self.as_ref()).is_none()
131    }
132
133    /// Returns this header value as a &str if it is utf8, `None` otherwise. For
134    /// a byte slice regardless of utf8-ness, use the `AsRef<[u8]>` impl.
135    pub fn as_str(&self) -> Option<&str> {
136        match &self.inner {
137            Utf8(utf8) => Some(utf8),
138            Bytes(_) => None,
139        }
140    }
141}
142
143#[cfg(feature = "parse")]
144impl HeaderValue {
145    pub(crate) fn parse(bytes: &[u8]) -> Self {
146        match std::str::from_utf8(bytes) {
147            Ok(s) => Self::from_inner(Utf8(SmartCow::Owned(s.into()))),
148            Err(_) => Self::from_inner(Bytes(bytes.into())),
149        }
150    }
151}
152
153impl Display for HeaderValue {
154    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
155        match &self.inner {
156            Utf8(s) => f.write_str(s),
157            Bytes(b) => f.write_str(&String::from_utf8_lossy(b)),
158        }
159    }
160}
161
162impl From<Vec<u8>> for HeaderValue {
163    fn from(v: Vec<u8>) -> Self {
164        match String::from_utf8(v) {
165            Ok(s) => Self::from_inner(Utf8(SmartCow::Owned(s.into()))),
166            Err(e) => Self::from_inner(Bytes(e.into_bytes().into())),
167        }
168    }
169}
170
171impl From<Cow<'static, str>> for HeaderValue {
172    fn from(c: Cow<'static, str>) -> Self {
173        Self::from_inner(Utf8(SmartCow::from(c)))
174    }
175}
176
177impl From<&'static [u8]> for HeaderValue {
178    fn from(b: &'static [u8]) -> Self {
179        match std::str::from_utf8(b) {
180            Ok(s) => Self::from_inner(Utf8(SmartCow::Borrowed(s))),
181            Err(_) => Self::from_inner(Bytes(b.into())),
182        }
183    }
184}
185
186impl From<String> for HeaderValue {
187    fn from(s: String) -> Self {
188        Self::from_inner(Utf8(SmartCow::Owned(s.into())))
189    }
190}
191
192impl From<&'static str> for HeaderValue {
193    fn from(s: &'static str) -> Self {
194        Self::from_inner(Utf8(SmartCow::Borrowed(s)))
195    }
196}
197
198macro_rules! delegate_from_to_format {
199    ($($t:ty),*) => {
200        $(
201        impl From<$t> for HeaderValue {
202            fn from(value: $t) -> Self {
203                format_args!("{value}").into()
204            }
205        }
206        )*
207    };
208}
209
210delegate_from_to_format!(usize, u64, u16, u32, i32, i64);
211
212impl From<std::fmt::Arguments<'_>> for HeaderValue {
213    fn from(value: std::fmt::Arguments<'_>) -> Self {
214        let mut s = SmartString::new();
215        s.write_fmt(value).unwrap();
216        Self::from_inner(Utf8(SmartCow::Owned(s)))
217    }
218}
219
220impl AsRef<[u8]> for HeaderValueInner {
221    fn as_ref(&self) -> &[u8] {
222        match self {
223            Utf8(utf8) => utf8.as_bytes(),
224            Bytes(b) => b,
225        }
226    }
227}
228
229impl AsRef<[u8]> for HeaderValue {
230    fn as_ref(&self) -> &[u8] {
231        self.inner.as_ref()
232    }
233}
234
235impl PartialEq<&str> for HeaderValue {
236    fn eq(&self, other: &&str) -> bool {
237        self.as_str() == Some(*other)
238    }
239}
240
241impl PartialEq<&[u8]> for HeaderValue {
242    fn eq(&self, other: &&[u8]) -> bool {
243        self.as_ref() == *other
244    }
245}
246
247impl PartialEq<[u8]> for HeaderValue {
248    fn eq(&self, other: &[u8]) -> bool {
249        self.as_ref() == other
250    }
251}
252
253impl PartialEq<str> for HeaderValue {
254    fn eq(&self, other: &str) -> bool {
255        self.as_str() == Some(other)
256    }
257}
258
259impl PartialEq<String> for HeaderValue {
260    fn eq(&self, other: &String) -> bool {
261        self.as_str() == Some(other)
262    }
263}
264
265impl PartialEq<&String> for HeaderValue {
266    fn eq(&self, other: &&String) -> bool {
267        self.as_str() == Some(&**other)
268    }
269}
270
271impl PartialEq<[u8]> for &HeaderValue {
272    fn eq(&self, other: &[u8]) -> bool {
273        self.as_ref() == other
274    }
275}
276
277impl PartialEq<str> for &HeaderValue {
278    fn eq(&self, other: &str) -> bool {
279        self.as_str() == Some(other)
280    }
281}
282
283impl PartialEq<String> for &HeaderValue {
284    fn eq(&self, other: &String) -> bool {
285        self.as_str() == Some(other)
286    }
287}