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