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
use std::borrow::Cow;
#[derive(Clone, Debug, Default)]
pub struct Headers<'a>(Vec<Header<'a>>);
impl<'a> Headers<'a> {
pub fn set(&mut self, header: Header<'a>) {
self.0.retain(|old_header| header.name != old_header.name);
self.0.push(header);
}
pub fn add(&mut self, header: Header<'a>) {
self.0.push(header);
}
}
impl<'a> IntoIterator for Headers<'a> {
type Item = Header<'a>;
type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
#[derive(Clone, Debug)]
pub struct Header<'a> {
pub(crate) name: Cow<'a, str>,
pub(crate) value: Cow<'a, str>,
}
impl<'a> Header<'a> {
pub fn content_type(value: impl Into<Cow<'a, str>>) -> Header<'a> {
Self::custom("Content-Type", value)
}
pub fn authorization(value: impl Into<Cow<'a, str>>) -> Header<'a> {
Self::custom("Authorization", value)
}
pub fn bearer(token: impl Into<Cow<'a, str>>) -> Header<'a> {
Self::custom("Authorization", format!("Bearer {}", token.into()))
}
pub fn custom(name: impl Into<Cow<'a, str>>, value: impl Into<Cow<'a, str>>) -> Header<'a> {
Header {
name: name.into(),
value: value.into(),
}
}
}