Skip to main content

trillium_http/headers/
header_value.rs

1use crate::compact_cow::CompactCow;
2use HeaderValueInner::{Bytes, Utf8};
3use compact_str::CompactString;
4use std::{
5    borrow::Cow,
6    fmt::{Debug, Display, Formatter, Write},
7    sync::Arc,
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(CompactCow::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(CompactCow::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(CompactCow<'static>),
84    Bytes(Arc<[u8]>),
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(CompactCow::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
143impl HeaderValue {
144    pub(crate) fn parse(bytes: &[u8]) -> Self {
145        match std::str::from_utf8(bytes) {
146            Ok(s) => Self::from_inner(Utf8(CompactCow::Owned(s.into()))),
147            Err(_) => Self::from_inner(Bytes(bytes.into())),
148        }
149    }
150}
151
152impl Display for HeaderValue {
153    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
154        match &self.inner {
155            Utf8(s) => f.write_str(s),
156            Bytes(b) => f.write_str(&String::from_utf8_lossy(b)),
157        }
158    }
159}
160
161impl From<Vec<u8>> for HeaderValue {
162    fn from(v: Vec<u8>) -> Self {
163        match String::from_utf8(v) {
164            Ok(s) => Self::from_inner(Utf8(CompactCow::Owned(s.into()))),
165            Err(e) => Self::from_inner(Bytes(e.into_bytes().into())),
166        }
167    }
168}
169
170impl From<Cow<'static, str>> for HeaderValue {
171    fn from(c: Cow<'static, str>) -> Self {
172        Self::from_inner(Utf8(CompactCow::from(c)))
173    }
174}
175
176impl From<&'static [u8]> for HeaderValue {
177    fn from(b: &'static [u8]) -> Self {
178        match std::str::from_utf8(b) {
179            Ok(s) => Self::from_inner(Utf8(CompactCow::Borrowed(s))),
180            Err(_) => Self::from_inner(Bytes(b.into())),
181        }
182    }
183}
184
185impl From<String> for HeaderValue {
186    fn from(s: String) -> Self {
187        Self::from_inner(Utf8(CompactCow::Owned(s.into())))
188    }
189}
190
191impl From<&'static str> for HeaderValue {
192    fn from(s: &'static str) -> Self {
193        Self::from_inner(Utf8(CompactCow::Borrowed(s)))
194    }
195}
196
197macro_rules! delegate_from_to_format {
198    ($($t:ty),*) => {
199        $(
200        impl From<$t> for HeaderValue {
201            fn from(value: $t) -> Self {
202                format_args!("{value}").into()
203            }
204        }
205        )*
206    };
207}
208
209delegate_from_to_format!(usize, u64, u16, u32, i32, i64);
210
211impl From<std::fmt::Arguments<'_>> for HeaderValue {
212    fn from(value: std::fmt::Arguments<'_>) -> Self {
213        let mut s = CompactString::default();
214        s.write_fmt(value).unwrap();
215        Self::from_inner(Utf8(CompactCow::Owned(s)))
216    }
217}
218
219impl AsRef<[u8]> for HeaderValueInner {
220    fn as_ref(&self) -> &[u8] {
221        match self {
222            Utf8(utf8) => utf8.as_bytes(),
223            Bytes(b) => b,
224        }
225    }
226}
227
228impl AsRef<[u8]> for HeaderValue {
229    fn as_ref(&self) -> &[u8] {
230        self.inner.as_ref()
231    }
232}
233
234impl PartialEq<&str> for HeaderValue {
235    fn eq(&self, other: &&str) -> bool {
236        self.as_str() == Some(*other)
237    }
238}
239
240impl PartialEq<&[u8]> for HeaderValue {
241    fn eq(&self, other: &&[u8]) -> bool {
242        self.as_ref() == *other
243    }
244}
245
246impl PartialEq<[u8]> for HeaderValue {
247    fn eq(&self, other: &[u8]) -> bool {
248        self.as_ref() == other
249    }
250}
251
252impl PartialEq<str> for HeaderValue {
253    fn eq(&self, other: &str) -> bool {
254        self.as_str() == Some(other)
255    }
256}
257
258impl PartialEq<String> for HeaderValue {
259    fn eq(&self, other: &String) -> bool {
260        self.as_str() == Some(other)
261    }
262}
263
264impl PartialEq<&String> for HeaderValue {
265    fn eq(&self, other: &&String) -> bool {
266        self.as_str() == Some(&**other)
267    }
268}
269
270impl PartialEq<[u8]> for &HeaderValue {
271    fn eq(&self, other: &[u8]) -> bool {
272        self.as_ref() == other
273    }
274}
275
276impl PartialEq<str> for &HeaderValue {
277    fn eq(&self, other: &str) -> bool {
278        self.as_str() == Some(other)
279    }
280}
281
282impl PartialEq<String> for &HeaderValue {
283    fn eq(&self, other: &String) -> bool {
284        self.as_str() == Some(other)
285    }
286}