forked from http-rs/surf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheaders.rs
62 lines (51 loc) · 1.5 KB
/
headers.rs
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
//! HTTP Headers.
use std::iter::{IntoIterator, Iterator};
/// A collection of HTTP Headers.
#[derive(Debug)]
pub struct Headers<'a> {
headers: &'a mut http::HeaderMap,
}
impl<'a> Headers<'a> {
/// Create a new instance.
pub(crate) fn new(headers: &'a mut http::HeaderMap) -> Self {
Self { headers }
}
/// Get a header.
pub fn get<K>(&self, key: K) -> Option<&'_ str>
where
K: http::header::AsHeaderName,
{
self.headers.get(key).map(|h| h.to_str().unwrap())
}
/// Set a header.
pub fn insert<K>(&mut self, key: K, value: impl AsRef<str>) -> Option<String>
where
K: http::header::IntoHeaderName,
{
let value = value.as_ref();
let res = self.headers.insert(key, value.parse().unwrap());
res.as_ref().map(|h| h.to_str().unwrap().to_owned())
}
/// Iterate over all headers.
pub fn iter(&self) -> Iter<'_> {
Iter(self.headers.iter())
}
}
impl<'a> IntoIterator for Headers<'a> {
type Item = (&'a str, &'a str);
type IntoIter = Iter<'a>;
fn into_iter(self) -> Self::IntoIter {
Iter(self.headers.iter())
}
}
/// An iterator over headers in `Headers`.
#[derive(Debug)]
pub struct Iter<'a>(http::header::Iter<'a, http::header::HeaderValue>);
impl<'a> Iterator for Iter<'a> {
type Item = (&'a str, &'a str);
fn next(&mut self) -> Option<Self::Item> {
self.0
.next()
.map(|(key, value)| (key.as_str(), value.to_str().unwrap()))
}
}