aboutsummaryrefslogtreecommitdiff
path: root/src/encoding/request.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/encoding/request.rs')
-rw-r--r--src/encoding/request.rs34
1 files changed, 33 insertions, 1 deletions
diff --git a/src/encoding/request.rs b/src/encoding/request.rs
index aecd006..c62bab3 100644
--- a/src/encoding/request.rs
+++ b/src/encoding/request.rs
@@ -1,5 +1,6 @@
use super::{headermap::HeaderMap, method::Method, uri::Uri};
-use std::fmt::Display;
+use anyhow::{anyhow, bail};
+use std::{fmt::Display, str::FromStr};
#[derive(Debug, Clone)]
pub struct Request {
@@ -20,3 +21,34 @@ impl Display for Request {
Ok(())
}
}
+impl FromStr for Request {
+ type Err = anyhow::Error;
+ fn from_str(s: &str) -> Result<Self, Self::Err> {
+ let mut lines = s.lines();
+ let statusline = lines.next().ok_or(anyhow!("status line missing"))?;
+ let (method, rest) = statusline
+ .split_once(" ")
+ .ok_or(anyhow!("status line malformed"))?;
+ let (uri, sipver) = rest
+ .split_once(" ")
+ .ok_or(anyhow!("status line malformed"))?;
+
+ let Some(ver) = sipver.strip_prefix("SIP/") else {
+ bail!("sip version malformed");
+ };
+ if ver != "2.0" {
+ bail!("sip version {ver:?} is not supported");
+ }
+
+ let uri = Uri::from_str(uri)?;
+
+ let headers = HeaderMap::parse(&mut lines)?;
+ let method = Method::from_str(method)?;
+
+ Ok(Self {
+ headers,
+ method,
+ uri,
+ })
+ }
+}