From 622ed285b029dfa3b65cc3f13b51c1e0b83262b9 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Sat, 22 Feb 2025 12:25:32 +0100 Subject: fix things --- sip/src/encoding/mod.rs | 7 +++++++ sip/src/encoding/request.rs | 3 ++- sip/src/lib.rs | 4 ++-- sip/src/transport/tcp.rs | 14 ++++++++++++-- 4 files changed, 23 insertions(+), 5 deletions(-) (limited to 'sip/src') diff --git a/sip/src/encoding/mod.rs b/sip/src/encoding/mod.rs index 816aa01..3c2f3fd 100644 --- a/sip/src/encoding/mod.rs +++ b/sip/src/encoding/mod.rs @@ -1,5 +1,6 @@ use std::{fmt::Display, str::FromStr}; +use headers::ContentLength; use request::Request; use response::Response; @@ -43,4 +44,10 @@ impl Message { Message::Response(r) => &mut r.body, } } + pub fn content_length(&self) -> Option> { + match self { + Message::Request(r) => r.headers.get::(), + Message::Response(r) => r.headers.get::(), + } + } } diff --git a/sip/src/encoding/request.rs b/sip/src/encoding/request.rs index ab41b7c..06f9f9d 100644 --- a/sip/src/encoding/request.rs +++ b/sip/src/encoding/request.rs @@ -16,10 +16,11 @@ impl Display for Request { headers, method, uri, - .. + body, } = self; write!(f, "{method} {uri} SIP/2.0\r\n")?; write!(f, "{headers}\r\n")?; + write!(f, "{body}")?; Ok(()) } } diff --git a/sip/src/lib.rs b/sip/src/lib.rs index 6c6cc3b..52f7fa7 100644 --- a/sip/src/lib.rs +++ b/sip/src/lib.rs @@ -1,4 +1,4 @@ -#![feature(iterator_try_collect)] +#![feature(iterator_try_collect, string_from_utf8_lossy_owned)] pub mod encoding; -pub mod transport; pub mod transaction; +pub mod transport; diff --git a/sip/src/transport/tcp.rs b/sip/src/transport/tcp.rs index efe433d..58dfb7e 100644 --- a/sip/src/transport/tcp.rs +++ b/sip/src/transport/tcp.rs @@ -4,7 +4,7 @@ use anyhow::Result; use log::debug; use std::str::FromStr; use tokio::{ - io::{AsyncBufReadExt, AsyncWriteExt, BufReader, BufWriter}, + io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader, BufWriter}, net::{ tcp::{OwnedReadHalf, OwnedWriteHalf}, TcpStream, @@ -34,8 +34,18 @@ impl Transport for TcpTransport { while !message.ends_with("\r\n\r\n") { g.read_line(&mut message).await?; } - let mesg = Message::from_str(message.trim())?; + let mut mesg = Message::from_str(message.trim())?; debug!("<- {mesg}"); + + let cl = mesg + .content_length() + .transpose()? + .map(|e| e.0) + .unwrap_or_default(); + let mut body = vec![0u8; cl]; + g.read_exact(&mut body).await?; + *mesg.body_mut() = String::from_utf8_lossy_owned(body); + Ok(mesg) } async fn send(&self, request: Message) -> Result<()> { -- cgit v1.2.3-70-g09d2