| 1 |
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 2 |
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 3 |
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 4 |
// option. This file may not be copied, modified, or distributed |
| 5 |
// except according to those terms. |
| 6 |
|
| 7 |
#[derive(Debug, PartialEq, PartialOrd, Eq, Ord, Clone)] |
| 8 |
pub struct Header { |
| 9 |
name: String, |
| 10 |
value: String, |
| 11 |
} |
| 12 |
|
| 13 |
impl Header { |
| 14 |
pub fn new<N, V>(name: N, value: V) -> Self |
| 15 |
where |
| 16 |
N: Into<String>, |
| 17 |
V: Into<String>, |
| 18 |
{ |
| 19 |
Self { |
| 20 |
name: name.into(), |
| 21 |
value: value.into(), |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
#[must_use] |
| 26 |
pub fn is_allowed_for_response(&self) -> bool { |
| 27 |
!matches!( |
| 28 |
self.name.as_str(), |
| 29 |
"connection" |
| 30 |
| "host" |
| 31 |
| "keep-alive" |
| 32 |
| "proxy-connection" |
| 33 |
| "te" |
| 34 |
| "transfer-encoding" |
| 35 |
| "upgrade" |
| 36 |
) |
| 37 |
} |
| 38 |
|
| 39 |
#[must_use] |
| 40 |
pub fn name(&self) -> &str { |
| 41 |
&self.name |
| 42 |
} |
| 43 |
|
| 44 |
#[must_use] |
| 45 |
pub fn value(&self) -> &str { |
| 46 |
&self.value |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
impl<T: AsRef<str>, U: AsRef<str>> PartialEq<(T, U)> for Header { |
| 51 |
fn eq(&self, other: &(T, U)) -> bool { |
| 52 |
self.name == other.0.as_ref() && self.value == other.1.as_ref() |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
pub trait HeadersExt<'h> { |
| 57 |
fn contains_header<T: AsRef<str>, U: AsRef<str>>(self, name: T, value: U) -> bool; |
| 58 |
fn find_header<T: AsRef<str> + 'h>(self, name: T) -> Option<&'h Header>; |
| 59 |
} |
| 60 |
|
| 61 |
impl<'h, H> HeadersExt<'h> for H |
| 62 |
where |
| 63 |
H: IntoIterator<Item = &'h Header> + 'h, |
| 64 |
{ |
| 65 |
fn contains_header<T: AsRef<str>, U: AsRef<str>>(self, name: T, value: U) -> bool { |
| 66 |
let (name, value) = (name.as_ref(), value.as_ref()); |
| 67 |
self.into_iter().any(|h| h == &(name, value)) |
| 68 |
} |
| 69 |
|
| 70 |
fn find_header<T: AsRef<str> + 'h>(self, name: T) -> Option<&'h Header> { |
| 71 |
let name = name.as_ref(); |
| 72 |
self.into_iter().find(|h| h.name == name) |
| 73 |
} |
| 74 |
} |