headers/common/
host.rs
1use std::convert::TryFrom;
2use std::fmt;
3
4use http::uri::Authority;
5
6#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd)]
8pub struct Host(Authority);
9
10impl Host {
11 pub fn hostname(&self) -> &str {
13 self.0.host()
14 }
15
16 pub fn port(&self) -> Option<u16> {
18 self.0.port_u16()
19 }
20}
21
22impl ::Header for Host {
23 fn name() -> &'static ::HeaderName {
24 &::http::header::HOST
25 }
26
27 fn decode<'i, I: Iterator<Item = &'i ::HeaderValue>>(values: &mut I) -> Result<Self, ::Error> {
28 values
29 .next()
30 .cloned()
31 .and_then(|val| Authority::try_from(val.as_bytes()).ok())
32 .map(Host)
33 .ok_or_else(::Error::invalid)
34 }
35
36 fn encode<E: Extend<::HeaderValue>>(&self, values: &mut E) {
37 let bytes = self.0.as_str().as_bytes();
38 let val = ::HeaderValue::from_bytes(bytes).expect("Authority is a valid HeaderValue");
39
40 values.extend(::std::iter::once(val));
41 }
42}
43
44impl From<Authority> for Host {
45 fn from(auth: Authority) -> Host {
46 Host(auth)
47 }
48}
49
50impl fmt::Display for Host {
51 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
52 fmt::Display::fmt(&self.0, f)
53 }
54}