diff options
Diffstat (limited to 'src/encoding')
-rw-r--r-- | src/encoding/headers.rs | 41 |
1 files changed, 22 insertions, 19 deletions
diff --git a/src/encoding/headers.rs b/src/encoding/headers.rs index b45cfd5..60dca7f 100644 --- a/src/encoding/headers.rs +++ b/src/encoding/headers.rs @@ -1,5 +1,5 @@ use super::{headermap::HeaderMap, method::Method}; -use anyhow::{anyhow, bail}; +use anyhow::{anyhow, bail, Result}; use std::{fmt::Display, str::FromStr}; macro_rules! header { @@ -76,30 +76,25 @@ impl FromStr for WWWAuthenticate { let kvs = s .strip_prefix("Digest ") .ok_or(anyhow!("type not digest"))? - .split(", ") + .split(",") .map(|e| { let Some((k, v)) = e.split_once("=") else { bail!("not a KV-pair") }; - Ok(( - k.to_string(), - v.strip_prefix("\"") - .ok_or(anyhow!("start quote missing"))? - .strip_suffix("\"") - .ok_or(anyhow!("end quote missing"))? - .to_string(), - )) + Ok((k.trim().to_string(), v.trim().to_string())) }) .try_collect::<HeaderMap>()?; Ok(WWWAuthenticate { - realm: kvs - .get_raw("realm") - .ok_or(anyhow!("realm missing"))? - .to_string(), - nonce: kvs - .get_raw("nonce") - .ok_or(anyhow!("nonce missing"))? - .to_string(), + realm: unquote( + &kvs.get_raw("realm") + .ok_or(anyhow!("realm missing"))? + .to_string(), + )?, + nonce: unquote( + &kvs.get_raw("nonce") + .ok_or(anyhow!("nonce missing"))? + .to_string(), + )?, }) } } @@ -126,7 +121,7 @@ impl Display for Authorization { } = self; write!( f, - "Digest username={username:?} realm={realm:?}, nonce={nonce:?}, uri={uri:?}, response={response:?}" + "Digest username={username:?},\r\n realm={realm:?},\r\n nonce={nonce:?},\r\n uri={uri:?},\r\n response={response:?},\r\n algorithm=MD5" ) } } @@ -136,3 +131,11 @@ impl FromStr for Authorization { todo!() } } + +pub fn unquote(v: &str) -> Result<String> { + Ok(v.strip_prefix("\"") + .ok_or(anyhow!("start quote missing"))? + .strip_suffix("\"") + .ok_or(anyhow!("end quote missing"))? + .to_string()) +} |