1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#![deny(missing_docs)]
#![deny(missing_debug_implementations)]
#![cfg_attr(test, deny(warnings))]
extern crate bytes;
extern crate http;
use std::fmt;
pub use http::header::{self, HeaderName, HeaderValue};
pub mod decode;
pub mod encode;
pub trait Header {
const NAME: &'static HeaderName;
fn decode(values: &mut Values) -> Option<Self>
where
Self: Sized;
fn encode(&self, values: &mut ToValues);
}
#[derive(Debug)]
pub struct Values<'a> {
inner: http::header::ValueIter<'a, http::header::HeaderValue>,
should_exhaust: bool,
}
impl<'a> Values<'a> {
pub fn skip_exhaustive_iter_check(&mut self) {
self.should_exhaust = false;
}
}
impl<'a> Iterator for Values<'a> {
type Item = &'a HeaderValue;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.inner.next()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
}
impl<'a> DoubleEndedIterator for Values<'a> {
#[inline]
fn next_back(&mut self) -> Option<&'a HeaderValue> {
self.inner.next_back()
}
}
#[derive(Debug)]
pub struct ToValues<'a> {
state: State<'a>,
}
#[derive(Debug)]
enum State<'a> {
First(http::header::Entry<'a, HeaderValue>),
Latter(http::header::OccupiedEntry<'a, HeaderValue>),
Tmp,
}
impl<'a> ToValues<'a> {
pub fn append(&mut self, value: HeaderValue) {
let entry = match ::std::mem::replace(&mut self.state, State::Tmp) {
State::First(http::header::Entry::Occupied(mut e)) => {
e.insert(value);
e
},
State::First(http::header::Entry::Vacant(e)) => e.insert_entry(value),
State::Latter(mut e) => {
e.append(value);
e
},
State::Tmp => unreachable!("ToValues State::Tmp"),
};
self.state = State::Latter(entry);
}
pub fn append_fmt<T: fmt::Display>(&mut self, fmt: T) {
let s = fmt.to_string();
let value = match HeaderValue::from_shared(s.into()) {
Ok(val) => val,
Err(err) => panic!("illegal HeaderValue; error = {:?}, fmt = \"{}\"", err, fmt),
};
self.append(value);
}
}
pub trait HeaderMapExt: self::sealed::Sealed {
fn typed_insert<H>(&mut self, header: H)
where
H: Header;
fn typed_get<H>(&self) -> Option<H>
where
H: Header;
}
impl HeaderMapExt for http::HeaderMap {
fn typed_insert<H>(&mut self, header: H)
where
H: Header,
{
let entry = self
.entry(H::NAME)
.expect("HeaderName is always valid");
let mut values = ToValues {
state: State::First(entry),
};
header.encode(&mut values);
}
fn typed_get<H>(&self) -> Option<H>
where
H: Header,
{
let mut values = Values {
inner: self.get_all(H::NAME).iter(),
should_exhaust: true,
};
let header = H::decode(&mut values)?;
if !values.should_exhaust || values.next().is_none() {
Some(header)
} else {
None
}
}
}
mod sealed {
pub trait Sealed {}
impl Sealed for ::http::HeaderMap {}
}