Skip to content

Commit

Permalink
fix(headers): Allow IPv6 Addresses in Host header
Browse files Browse the repository at this point in the history
  • Loading branch information
untitaker committed Oct 31, 2016
1 parent 15fe605 commit 20f177a
Showing 1 changed file with 36 additions and 8 deletions.
44 changes: 36 additions & 8 deletions src/header/common/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,28 @@ impl FromStr for Host {
type Err = ::Error;

fn from_str(s: &str) -> ::Result<Host> {
let (host_port, res) = domain_to_unicode(s);
if res.is_err() {
return Err(::Error::Header)
}
let idx = host_port.rfind(':');
let idx = s.rfind(':');
let port = idx.and_then(
|idx| s[idx + 1..].parse().ok()
);
let hostname = match idx {
None => host_port,
Some(idx) => host_port[..idx].to_owned()
let hostname_encoded = match port {
None => s,
Some(_) => &s[..idx.unwrap()]
};

let hostname = if hostname_encoded.starts_with("[") {
if !hostname_encoded.ends_with("]") {
return Err(::Error::Header)
}
hostname_encoded.to_owned()
} else {
let (hostname, res) = domain_to_unicode(hostname_encoded);
if res.is_err() {
return Err(::Error::Header)
}
hostname
};

Ok(Host {
hostname: hostname,
port: port
Expand Down Expand Up @@ -111,6 +121,24 @@ mod tests {
hostname: "foo.com".to_owned(),
port: Some(8080)
}));

let host = Header::parse_header([b"foo.com".to_vec()].as_ref());
assert_eq!(host.ok(), Some(Host {
hostname: "foo.com".to_owned(),
port: None
}));

let host = Header::parse_header([b"[::1]:8080".to_vec()].as_ref());
assert_eq!(host.ok(), Some(Host {
hostname: "[::1]".to_owned(),
port: Some(8080)
}));

let host = Header::parse_header([b"[::1]".to_vec()].as_ref());
assert_eq!(host.ok(), Some(Host {
hostname: "[::1]".to_owned(),
port: None
}));
}
}

Expand Down

0 comments on commit 20f177a

Please sign in to comment.