1#[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#[derive(Clone, Eq, PartialEq)]
37pub struct Protocol {
38 inner: h2::ext::Protocol,
39}
40
41#[cfg(feature = "http2")]
42impl Protocol {
43 pub const fn from_static(value: &'static str) -> Self {
45 Self {
46 inner: h2::ext::Protocol::from_static(value),
47 }
48 }
49
50 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#[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 #[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 #[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)]
154pub(crate) struct OriginalHeaderOrder {
156 num_entries: HashMap<HeaderName, usize>,
159 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 }
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 pub(crate) fn get_in_order(&self) -> impl Iterator<Item = &(HeaderName, usize)> {
244 self.entry_order.iter()
245 }
246}