Skip to content

Commit

Permalink
add SIP headers and response codes from RFC 6665
Browse files Browse the repository at this point in the history
  • Loading branch information
utsl42 authored and kbalt committed Jul 9, 2024
1 parent 0c6acc0 commit ff924ad
Show file tree
Hide file tree
Showing 6 changed files with 390 additions and 0 deletions.
4 changes: 4 additions & 0 deletions crates/sip-types/src/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,10 @@ codes! {
/// 488 Not Acceptable Here
[488 => NOT_ACCEPTABLE_HERE, "Not Acceptable Here"];

/// [[RFC6665, Section 8.3.2](https://datatracker.ietf.org/doc/html/rfc6665#section-8.3.2)]
/// 489 Bad Event
[489 => BAD_EVENT, "Bad Event"];

/// [[RFC3621, Section 21.4.27](https://tools.ietf.org/html/rfc3261#section-21.4.27)]
/// 491 Request Pending
[491 => REQUEST_PENDING, "Request Pending"];
Expand Down
9 changes: 9 additions & 0 deletions crates/sip-types/src/header/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ header_names! {
/// [[RFC3621, Section 20.5](https://tools.ietf.org/html/rfc3261#section-20.5)]
"Allow", Allow, ["allow"], ALLOW;

/// [[RFC6665, Section 8.2.2](https://datatracker.ietf.org/doc/html/rfc6665#section-8.2.2)])]
"Allow-Events", AllowEvents, ["allow-events", "u"], ALLOW_EVENTS;

/// [[RFC3621, Section 20.6](https://tools.ietf.org/html/rfc3261#section-20.6)]
"Authentication-Info", AuthenticationInfo, ["authentication-info"], AUTHENTICATION_INFO;

Expand Down Expand Up @@ -172,6 +175,9 @@ header_names! {
/// [[RFC3621, Section 20.18](https://tools.ietf.org/html/rfc3261#section-20.18)]
"Error-Info", ErrorInfo, ["error-info"], ERROR_INFO;

/// [[RFC6665, Section 8.2.1](https://datatracker.ietf.org/doc/html/rfc6665#section-8.2.1)]
"Event", Event, ["event", "o"], EVENT;

/// [[RFC3621, Section 20.19](https://tools.ietf.org/html/rfc3261#section-20.19)]
"Expires", Expires, ["expires"], EXPIRES;

Expand Down Expand Up @@ -268,6 +274,9 @@ header_names! {
/// [[RFC3621, Section 20.36](https://tools.ietf.org/html/rfc3261#section-20.36)]
"Subject", Subject, ["subject", "s"], SUBJECT;

/// [[RFC6665, Section 8.2.3](https://datatracker.ietf.org/doc/html/rfc6665#section-8.2.3)]
"Subscription-State", SubscriptionState, ["subscription-state"], SUBSCRIPTION_STATE;

/// [[RFC3621, Section 20.37](https://tools.ietf.org/html/rfc3261#section-20.37)]
"Supported", Supported, ["supported", "k"], SUPPORTED;

Expand Down
71 changes: 71 additions & 0 deletions crates/sip-types/src/header/typed/allow_events.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use crate::header::name::Name;
use bytesstr::BytesStr;

csv_header! {
/// `Allow-Events` header, contains only one event.
/// To get all allowed events use [`Vec`].
AllowEvents,
BytesStr,
Name::ALLOW_EVENTS
}

#[cfg(test)]
mod test {
use super::*;
use crate::Headers;

const ALLOW_EVENTS_DIALOG: AllowEvents = AllowEvents(BytesStr::from_static("dialog"));
const ALLOW_EVENTS_MESSAGE_SUMMARY: AllowEvents =
AllowEvents(BytesStr::from_static("message-summary"));

#[test]
fn print_allow_single() {
let mut headers = Headers::new();
headers.insert_named(&ALLOW_EVENTS_DIALOG);
let headers = headers.to_string();

assert_eq!(headers, "Allow-Events: dialog\r\n");
}

#[test]
fn print_allow_multiple_vec() {
let allow = vec![ALLOW_EVENTS_DIALOG, ALLOW_EVENTS_MESSAGE_SUMMARY];

let mut headers = Headers::new();
headers.insert_named(&allow);
let headers = headers.to_string();

assert_eq!(headers, "Allow-Events: dialog, message-summary\r\n");
}

#[test]
fn print_allow_multiple_insert() {
let mut headers = Headers::new();
headers.insert_named(&ALLOW_EVENTS_DIALOG);
headers.insert_named(&ALLOW_EVENTS_MESSAGE_SUMMARY);
let headers = headers.to_string();

assert_eq!(headers, "Allow-Events: dialog, message-summary\r\n");
}

#[test]
fn parse_allow_single() {
let mut headers = Headers::new();
headers.insert(Name::ALLOW_EVENTS, "dialog");

let allow_events: AllowEvents = headers.get_named().unwrap();
assert_eq!(allow_events, ALLOW_EVENTS_DIALOG)
}

#[test]
fn parse_allow_multiple_vec() {
let mut headers = Headers::new();
headers.insert(Name::ALLOW_EVENTS, "dialog, message-summary");

let allow: Vec<AllowEvents> = headers.get_named().unwrap();
assert_eq!(
allow,
vec![ALLOW_EVENTS_DIALOG, ALLOW_EVENTS_MESSAGE_SUMMARY]
)
}
}
68 changes: 68 additions & 0 deletions crates/sip-types/src/header/typed/event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use crate::header::headers::OneOrMore;
use crate::header::name::Name;
use crate::header::{ConstNamed, ExtendValues, HeaderParse};
use crate::parse::ParseCtx;
use crate::print::PrintCtx;
use bytesstr::BytesStr;
use internal::{identity, IResult};
use nom::combinator::map;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Event(pub BytesStr);

impl Event {
pub fn new<B>(ev: B) -> Self
where
B: Into<BytesStr>,
{
Event(ev.into())
}
}

impl ConstNamed for Event {
const NAME: Name = Name::EVENT;
}

impl HeaderParse for Event {
fn parse<'i>(ctx: ParseCtx, i: &'i str) -> IResult<&'i str, Self> {
map(identity(), |i| {
Self(BytesStr::from_parse(ctx.src, i.trim()))
})(i)
}
}

impl ExtendValues for Event {
fn extend_values(&self, ctx: PrintCtx<'_>, values: &mut OneOrMore) {
*values = self.create_values(ctx)
}

fn create_values(&self, _: PrintCtx<'_>) -> OneOrMore {
OneOrMore::One(self.0.as_str().into())
}
}

#[cfg(test)]
mod test {
use super::*;
use crate::Headers;

const EVENT_DIALOG: Event = Event(BytesStr::from_static("dialog"));

#[test]
fn print_event_single() {
let mut headers = Headers::new();
headers.insert_named(&EVENT_DIALOG);
let headers = headers.to_string();

assert_eq!(headers, "Event: dialog\r\n");
}

#[test]
fn parse_event() {
let mut headers = Headers::new();
headers.insert(Name::EVENT, "dialog");

let event: Event = headers.get_named().unwrap();
assert_eq!(event, EVENT_DIALOG)
}
}
6 changes: 6 additions & 0 deletions crates/sip-types/src/header/typed/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

mod accept;
mod allow;
mod allow_events;
mod auth;
mod call_id;
mod contact;
mod content;
mod cseq;
mod event;
mod expires;
mod extensions;
mod from_to;
Expand All @@ -15,16 +17,19 @@ mod prack;
mod replaces;
mod retry_after;
mod routing;
mod subscription_state;
mod timer;
mod via;

pub use accept::Accept;
pub use allow::Allow;
pub use allow_events::AllowEvents;
pub use auth::*;
pub use call_id::CallID;
pub use contact::Contact;
pub use content::{ContentLength, ContentType};
pub use cseq::CSeq;
pub use event::Event;
pub use expires::{Expires, MinExpires};
pub use extensions::{Require, Supported};
pub use from_to::FromTo;
Expand All @@ -33,5 +38,6 @@ pub use prack::{RAck, RSeq};
pub use replaces::Replaces;
pub use retry_after::RetryAfter;
pub use routing::Routing;
pub use subscription_state::{SubscriptionState, EventReasonValue, SubStateValue};
pub use timer::{MinSe, Refresher, SessionExpires};
pub use via::Via;
Loading

0 comments on commit ff924ad

Please sign in to comment.