diff options
author | metamuffin <metamuffin@disroot.org> | 2024-07-05 23:37:39 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2024-07-05 23:37:39 +0200 |
commit | a4c52bedef04cfb927f3d7809680fed0425a5125 (patch) | |
tree | 96e11f458049a3944fbfd1bd30fe9826d385c80d | |
parent | b5e408b02cd99a7833faab9b46d33042786c568c (diff) | |
download | sip-rs-a4c52bedef04cfb927f3d7809680fed0425a5125.tar sip-rs-a4c52bedef04cfb927f3d7809680fed0425a5125.tar.bz2 sip-rs-a4c52bedef04cfb927f3d7809680fed0425a5125.tar.zst |
udp transport
-rw-r--r-- | src/encoding/headers.rs | 41 | ||||
-rw-r--r-- | src/transport/mod.rs | 1 | ||||
-rw-r--r-- | src/transport/tcp.rs | 3 | ||||
-rw-r--r-- | src/transport/udp.rs | 30 |
4 files changed, 54 insertions, 21 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()) +} diff --git a/src/transport/mod.rs b/src/transport/mod.rs index 3fa82df..cf49d31 100644 --- a/src/transport/mod.rs +++ b/src/transport/mod.rs @@ -2,6 +2,7 @@ use crate::encoding::{request::Request, response::Response}; use anyhow::Result; pub mod tcp; +pub mod udp; #[allow(async_fn_in_trait)] pub trait Transport { diff --git a/src/transport/tcp.rs b/src/transport/tcp.rs index f5986b9..8c0a024 100644 --- a/src/transport/tcp.rs +++ b/src/transport/tcp.rs @@ -1,3 +1,4 @@ +use super::Transport; use crate::encoding::{request::Request, response::Response}; use anyhow::{anyhow, Result}; use log::debug; @@ -11,8 +12,6 @@ use tokio::{ }, }; -use super::Transport; - pub struct TcpTransport { write: Mutex<BufWriter<OwnedWriteHalf>>, read: Mutex<Receiver<Response>>, diff --git a/src/transport/udp.rs b/src/transport/udp.rs new file mode 100644 index 0000000..c86ce50 --- /dev/null +++ b/src/transport/udp.rs @@ -0,0 +1,30 @@ +use super::Transport; +use crate::encoding::{request::Request, response::Response}; +use anyhow::Result; +use log::debug; +use std::str::FromStr; +use tokio::net::UdpSocket; + +pub struct UdpTransport { + sock: UdpSocket, +} + +impl UdpTransport { + pub async fn new(sock: UdpSocket) -> Result<Self> { + Ok(Self { sock }) + } +} +impl Transport for UdpTransport { + async fn recv(&self) -> Result<Response> { + let mut buf = [0; 1024]; + let size = self.sock.recv(&mut buf).await?; + let message = String::from_utf8(buf[..size].to_vec())?; + debug!("{message}"); + Response::from_str(message.trim_end()) + } + async fn send(&self, request: Request) -> Result<()> { + debug!("-> {request}"); + self.sock.send(format!("{request}").as_bytes()).await?; + Ok(()) + } +} |