1
0
Fork 0
forked from Deuxfleurs/garage

Rewrite for clarity

This commit is contained in:
Quentin 2020-11-10 15:48:40 +01:00
parent ab62c59acb
commit d1b2fcc1e7

View file

@ -66,21 +66,24 @@ async fn handler(
/// ///
/// The HTTP host contains both a host and a port. /// The HTTP host contains both a host and a port.
/// Extracting the port is more complex than just finding the colon (:) symbol due to IPv6 /// Extracting the port is more complex than just finding the colon (:) symbol due to IPv6
/// we do not use the collect pattern as there is no way in std rust to collect over a stack allocated value /// We do not use the collect pattern as there is no way in std rust to collect over a stack allocated value
/// check here: https://docs.rs/collect_slice/1.2.0/collect_slice/ /// check here: https://docs.rs/collect_slice/1.2.0/collect_slice/
fn authority_to_host(authority: &str) -> Result<&str, Error> { fn authority_to_host(authority: &str) -> Result<&str, Error> {
let mut iter = authority.chars().enumerate(); let mut iter = authority.chars().enumerate();
let split = match iter.next() { let (_, first_char) = iter
Some((_, '[')) => { .next()
let mut niter = iter.skip_while(|(_, c)| c != &']'); .ok_or(Error::BadRequest(format!("Authority is empty")))?;
niter.next().ok_or(Error::BadRequest(format!(
let split = match first_char {
'[' => {
let mut iter = iter.skip_while(|(_, c)| c != &']');
iter.next().ok_or(Error::BadRequest(format!(
"Authority {} has an illegal format", "Authority {} has an illegal format",
authority authority
)))?; )))?;
niter.next() iter.next()
} }
Some((_, _)) => iter.skip_while(|(_, c)| c != &':').next(), _ => iter.skip_while(|(_, c)| c != &':').next(),
None => return Err(Error::BadRequest(format!("Authority is empty"))),
}; };
match split { match split {