From 7177367ae41a5e2d6ed401f60ee1455812dd8ffb Mon Sep 17 00:00:00 2001 From: metamuffin Date: Sat, 6 Jul 2024 15:43:45 +0200 Subject: phone is ringing --- src/encoding/headermap.rs | 3 +++ src/encoding/headers.rs | 36 +++++++++++++++++++++++++++++++++--- 2 files changed, 36 insertions(+), 3 deletions(-) (limited to 'src/encoding') diff --git a/src/encoding/headermap.rs b/src/encoding/headermap.rs index 5d1fa0a..01e1962 100644 --- a/src/encoding/headermap.rs +++ b/src/encoding/headermap.rs @@ -25,6 +25,9 @@ impl HeaderMap { pub fn get(&self) -> Option> { self.get_raw(H::NAME).map(H::from_str) } + pub fn get_res(&self) -> Result { + self.get().ok_or(anyhow!("{} header missing", H::NAME))? + } pub fn insert_raw(&mut self, key: String, value: String) { self.0.push((key, value)) } diff --git a/src/encoding/headers.rs b/src/encoding/headers.rs index 9e785f0..afcfef1 100644 --- a/src/encoding/headers.rs +++ b/src/encoding/headers.rs @@ -1,4 +1,4 @@ -use super::{headermap::HeaderMap, method::Method}; +use super::{headermap::HeaderMap, method::Method, uri::Uri}; use anyhow::{anyhow, bail, Result}; use std::{fmt::Display, str::FromStr}; @@ -31,7 +31,6 @@ header!("Content-Length", struct ContentLength(usize)); header!("Content-Type", struct ContentType(String)); header!("Call-ID", struct CallID(String)); header!("Via", struct Via(String)); -header!("Contact", struct Contact(String)); header!("Max-Forwards", struct MaxForwards(usize)); header!("From", struct From(String)); header!("To", struct To(String)); @@ -100,7 +99,7 @@ impl FromStr for WWWAuthenticate { } } -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Authorization { pub username: String, pub realm: String, @@ -140,3 +139,34 @@ pub fn unquote(v: &str) -> Result { .ok_or(anyhow!("end quote missing"))? .to_string()) } + +#[derive(Debug)] +pub struct Contact { + pub display_name: Option, + pub uri: Uri, + pub params: String, +} + +impl Header for Contact { + const NAME: &'static str = "Contact"; +} +impl Display for Contact { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let Self { + display_name, + uri, + params, + } = self; + if let Some(display_name) = display_name { + write!(f, "{display_name} <{uri}>{params}") + } else { + write!(f, "<{uri}>{params}") + } + } +} +impl FromStr for Contact { + type Err = anyhow::Error; + fn from_str(_s: &str) -> Result { + todo!() + } +} -- cgit v1.2.3-70-g09d2