hyper/ext/
mod.rs

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