1#[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#[derive(Clone, Eq, PartialEq)]
34pub struct Protocol {
35 inner: h2::ext::Protocol,
36}
37
38#[cfg(feature = "http2")]
39impl Protocol {
40 pub const fn from_static(value: &'static str) -> Self {
42 Self {
43 inner: h2::ext::Protocol::from_static(value),
44 }
45 }
46
47 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#[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 #[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 #[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")]
150pub(crate) struct OriginalHeaderOrder {
152 num_entries: HashMap<HeaderName, usize>,
155 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 }
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 pub(crate) fn get_in_order(&self) -> impl Iterator<Item = &(HeaderName, usize)> {
240 self.entry_order.iter()
241 }
242}