rama_hyper/ext/
mod.rs

1//! HTTP extensions.
2
3use bytes::Bytes;
4#[cfg(any(
5    all(any(feature = "client", feature = "server"), feature = "http1"),
6    feature = "ffi"
7))]
8use http::header::HeaderName;
9#[cfg(all(any(feature = "client", feature = "server"), feature = "http1"))]
10use http::header::{IntoHeaderName, ValueIter};
11use http::HeaderMap;
12#[cfg(feature = "ffi")]
13use std::collections::HashMap;
14#[cfg(feature = "http2")]
15use std::fmt;
16
17#[cfg(any(feature = "http1", feature = "ffi"))]
18mod h1_reason_phrase;
19#[cfg(any(feature = "http1", feature = "ffi"))]
20pub use h1_reason_phrase::ReasonPhrase;
21
22#[cfg(feature = "http2")]
23/// Represents the `:protocol` pseudo-header used by
24/// the [Extended CONNECT Protocol].
25///
26/// [Extended CONNECT Protocol]: https://datatracker.ietf.org/doc/html/rfc8441#section-4
27#[derive(Clone, Eq, PartialEq)]
28pub struct Protocol {
29    inner: h2::ext::Protocol,
30}
31
32#[cfg(feature = "http2")]
33impl Protocol {
34    /// Converts a static string to a protocol name.
35    pub const fn from_static(value: &'static str) -> Self {
36        Self {
37            inner: h2::ext::Protocol::from_static(value),
38        }
39    }
40
41    /// Returns a str representation of the header.
42    pub fn as_str(&self) -> &str {
43        self.inner.as_str()
44    }
45
46    #[cfg(feature = "server")]
47    pub(crate) fn from_inner(inner: h2::ext::Protocol) -> Self {
48        Self { inner }
49    }
50
51    #[cfg(all(feature = "client", feature = "http2"))]
52    pub(crate) fn into_inner(self) -> h2::ext::Protocol {
53        self.inner
54    }
55}
56
57#[cfg(feature = "http2")]
58impl<'a> From<&'a str> for Protocol {
59    fn from(value: &'a str) -> Self {
60        Self {
61            inner: h2::ext::Protocol::from(value),
62        }
63    }
64}
65
66#[cfg(feature = "http2")]
67impl AsRef<[u8]> for Protocol {
68    fn as_ref(&self) -> &[u8] {
69        self.inner.as_ref()
70    }
71}
72
73#[cfg(feature = "http2")]
74impl fmt::Debug for Protocol {
75    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
76        self.inner.fmt(f)
77    }
78}
79
80/// A map from header names to their original casing as received in an HTTP message.
81///
82/// If an HTTP/1 response `res` is parsed on a connection whose option
83/// [`preserve_header_case`] was set to true and the response included
84/// the following headers:
85///
86/// ```ignore
87/// x-Bread: Baguette
88/// X-BREAD: Pain
89/// x-bread: Ficelle
90/// ```
91///
92/// Then `res.extensions().get::<HeaderCaseMap>()` will return a map with:
93///
94/// ```ignore
95/// HeaderCaseMap({
96///     "x-bread": ["x-Bread", "X-BREAD", "x-bread"],
97/// })
98/// ```
99///
100/// [`preserve_header_case`]: /client/struct.Client.html#method.preserve_header_case
101#[derive(Clone, Debug)]
102pub(crate) struct HeaderCaseMap(HeaderMap<Bytes>);
103
104#[cfg(all(any(feature = "client", feature = "server"), feature = "http1"))]
105impl HeaderCaseMap {
106    /// Returns a view of all spellings associated with that header name,
107    /// in the order they were found.
108    #[cfg(feature = "client")]
109    pub(crate) fn get_all<'a>(
110        &'a self,
111        name: &HeaderName,
112    ) -> impl Iterator<Item = impl AsRef<[u8]> + 'a> + 'a {
113        self.get_all_internal(name)
114    }
115
116    /// Returns a view of all spellings associated with that header name,
117    /// in the order they were found.
118    #[cfg(any(feature = "client", feature = "server"))]
119    pub(crate) fn get_all_internal(&self, name: &HeaderName) -> ValueIter<'_, Bytes> {
120        self.0.get_all(name).into_iter()
121    }
122
123    #[cfg(any(feature = "client", feature = "server"))]
124    pub(crate) fn default() -> Self {
125        Self(Default::default())
126    }
127
128    #[cfg(any(test, feature = "ffi"))]
129    pub(crate) fn insert(&mut self, name: HeaderName, orig: Bytes) {
130        self.0.insert(name, orig);
131    }
132
133    #[cfg(any(feature = "client", feature = "server"))]
134    pub(crate) fn append<N>(&mut self, name: N, orig: Bytes)
135    where
136        N: IntoHeaderName,
137    {
138        self.0.append(name, orig);
139    }
140}
141
142#[cfg(feature = "ffi")]
143#[derive(Clone, Debug)]
144/// Hashmap<Headername, numheaders with that name>
145pub(crate) struct OriginalHeaderOrder {
146    /// Stores how many entries a Headername maps to. This is used
147    /// for accounting.
148    num_entries: HashMap<HeaderName, usize>,
149    /// Stores the ordering of the headers. ex: `vec[i] = (headerName, idx)`,
150    /// The vector is ordered such that the ith element
151    /// represents the ith header that came in off the line.
152    /// The `HeaderName` and `idx` are then used elsewhere to index into
153    /// the multi map that stores the header values.
154    entry_order: Vec<(HeaderName, usize)>,
155}
156
157#[cfg(all(feature = "http1", feature = "ffi"))]
158impl OriginalHeaderOrder {
159    pub(crate) fn default() -> Self {
160        OriginalHeaderOrder {
161            num_entries: HashMap::new(),
162            entry_order: Vec::new(),
163        }
164    }
165
166    pub(crate) fn insert(&mut self, name: HeaderName) {
167        if !self.num_entries.contains_key(&name) {
168            let idx = 0;
169            self.num_entries.insert(name.clone(), 1);
170            self.entry_order.push((name, idx));
171        }
172        // Replacing an already existing element does not
173        // change ordering, so we only care if its the first
174        // header name encountered
175    }
176
177    pub(crate) fn append<N>(&mut self, name: N)
178    where
179        N: IntoHeaderName + Into<HeaderName> + Clone,
180    {
181        let name: HeaderName = name.into();
182        let idx;
183        if self.num_entries.contains_key(&name) {
184            idx = self.num_entries[&name];
185            *self.num_entries.get_mut(&name).unwrap() += 1;
186        } else {
187            idx = 0;
188            self.num_entries.insert(name.clone(), 1);
189        }
190        self.entry_order.push((name, idx));
191    }
192
193    // No doc test is run here because `RUSTFLAGS='--cfg hyper_unstable_ffi'`
194    // is needed to compile. Once ffi is stablized `no_run` should be removed
195    // here.
196    /// This returns an iterator that provides header names and indexes
197    /// in the original order received.
198    ///
199    /// # Examples
200    /// ```no_run
201    /// use hyper::ext::OriginalHeaderOrder;
202    /// use hyper::header::{HeaderName, HeaderValue, HeaderMap};
203    ///
204    /// let mut h_order = OriginalHeaderOrder::default();
205    /// let mut h_map = Headermap::new();
206    ///
207    /// let name1 = b"Set-CookiE";
208    /// let value1 = b"a=b";
209    /// h_map.append(name1);
210    /// h_order.append(name1);
211    ///
212    /// let name2 = b"Content-Encoding";
213    /// let value2 = b"gzip";
214    /// h_map.append(name2, value2);
215    /// h_order.append(name2);
216    ///
217    /// let name3 = b"SET-COOKIE";
218    /// let value3 = b"c=d";
219    /// h_map.append(name3, value3);
220    /// h_order.append(name3)
221    ///
222    /// let mut iter = h_order.get_in_order()
223    ///
224    /// let (name, idx) = iter.next();
225    /// assert_eq!(b"a=b", h_map.get_all(name).nth(idx).unwrap());
226    ///
227    /// let (name, idx) = iter.next();
228    /// assert_eq!(b"gzip", h_map.get_all(name).nth(idx).unwrap());
229    ///
230    /// let (name, idx) = iter.next();
231    /// assert_eq!(b"c=d", h_map.get_all(name).nth(idx).unwrap());
232    /// ```
233    pub(crate) fn get_in_order(&self) -> impl Iterator<Item = &(HeaderName, usize)> {
234        self.entry_order.iter()
235    }
236}