sipmsg/lib.rs
1#![no_std]
2
3//! # Introduction
4//!
5//! Library for parsing/editing/constructing SIP requests and responses.
6//!
7//! This is the very first version where only simple parsing is support.
8//!
9//! ## Example
10//! ```rust
11//!
12//! use sipmsg::*;
13//! use unicase::Ascii;
14//!
15//! let invite_msg_buf = "\
16//! INVITE sip:bob@biloxi.com;user=phone?to=alice%40atlanta.com&priority=urgent SIP/2.0\r\n\
17//! Via: SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bKkjshdyff\r\n\
18//! Via: SIP/2.0/UDP 192.168.1.111\r\n\
19//! To: Bob <sip:bob@biloxi.com>\r\n\
20//! From: Alice <sip:alice@atlanta.com>;tag=88sja8x\r\n\
21//! Contact: Caller <sip:alice@client.atlanta.example.com;transport=tcp>\r\n\
22//! Max-Forwards: 70\r\n\
23//! Call-ID: f81d4fae-7dec-11d0-a765-00a0c91e6bf6@foo.bar.com\r\n\
24//! Extention-Header: extention header value;param=123;without_value\r\n\
25//! CSeq: 986759 INVITE\r\n\r\nbody_stuff"
26//! .as_bytes();
27//!
28//! // First parameter not realized yet.
29//! // It should consist be residue if Content-Length is less then actual body length.
30//! let (_, request) = SipRequest::parse(invite_msg_buf).unwrap();
31//! assert_eq!(request.rl.method, SipMethod::INVITE);
32//! assert_eq!(request.rl.sip_version, SipVersion(2, 0));
33//!
34//! // RURI
35//! assert_eq!(request.rl.uri.scheme, SipRequestUriScheme::SIP);
36//! assert_eq!(request.rl.uri.user_info().unwrap().value, "bob");
37//! assert_eq!(request.rl.uri.hostport.host, "biloxi.com");
38//! assert_eq!(request.rl.uri.params().unwrap().get(&"user"), Some(&Some("phone")));
39//! assert_eq!(request.rl.uri.headers().unwrap().get(&"to"), Some(&"alice%40atlanta.com"));
40//! assert_eq!(request.rl.uri.headers().unwrap().get(&"priority"), Some(&"urgent"));
41//!
42//! let call_id_header = request.headers.get_rfc_s(SipRFCHeader::CallID).unwrap();
43//! assert_eq!(call_id_header.value.vstr, "f81d4fae-7dec-11d0-a765-00a0c91e6bf6@foo.bar.com");
44//! assert_eq!(call_id_header.value.tags().unwrap()[&SipHeaderTagType::ID],
45//! "f81d4fae-7dec-11d0-a765-00a0c91e6bf6".as_bytes());
46//! assert_eq!(call_id_header.value.tags().unwrap()[&SipHeaderTagType::Host], b"foo.bar.com");
47//!
48//! // Via Header
49//! let via_headers = request.headers.get_rfc(SipRFCHeader::Via).unwrap();
50//! assert_eq!(via_headers[0].value.vstr, "SIP/2.0/UDP pc33.atlanta.com");
51//! assert_eq!(
52//! via_headers[0].params().unwrap().get(&"branch"),
53//! Some(&Some("z9hG4bKkjshdyff"))
54//! );
55//! assert_eq!(
56//! via_headers[0].value.tags().unwrap()[&SipHeaderTagType::ProtocolName],
57//! b"SIP"
58//! );
59//! assert_eq!(
60//! via_headers[0].value.tags().unwrap()[&SipHeaderTagType::ProtocolVersion],
61//! b"2.0"
62//! );
63//! assert_eq!(
64//! via_headers[0].value.tags().unwrap()[&SipHeaderTagType::ProtocolTransport],
65//! b"UDP"
66//! );
67//! assert_eq!(
68//! via_headers[0].value.tags().unwrap()[&SipHeaderTagType::Host],
69//! b"pc33.atlanta.com"
70//! );
71//! assert_eq!(via_headers[1].value.vstr, "SIP/2.0/UDP 192.168.1.111");
72//! assert_eq!(
73//! via_headers[1].params(),
74//! None
75//! );
76//!
77//! // Contact header
78//! let contact_header = request.headers.get_rfc_s(SipRFCHeader::Contact).unwrap();
79//! assert_eq!(
80//! contact_header.value.tags().unwrap()[&SipHeaderTagType::DisplayName],
81//! b"Caller"
82//! );
83//! assert_eq!(
84//! contact_header.value.sip_uri().unwrap().user_info().unwrap().value,
85//! "alice"
86//! );
87//! assert_eq!(
88//! contact_header.value.sip_uri().unwrap().hostport.host,
89//! "client.atlanta.example.com"
90//! );
91//!
92//! assert_eq!(
93//! contact_header.value.sip_uri().unwrap().params().unwrap().get(&"transport"),
94//! Some(&Some("tcp"))
95//! );
96//! assert_eq!(
97//! contact_header.value.sip_uri().unwrap().params().unwrap().get(&"non-exists-param"),
98//! None
99//! );
100//!
101//! // Extention Header
102//! let extention_header = request.headers.get_ext_s("extention-header").unwrap();
103//! assert_eq!(extention_header.name, "extention-header");
104//! assert_eq!(extention_header.value.vstr, "extention header value;param=123;without_value");
105//!
106//! // Body
107//! assert_eq!(request.body.unwrap(), b"body_stuff");
108//! ```
109//!
110extern crate alloc;
111extern crate nom;
112
113#[macro_use]
114pub mod common;
115pub use common::errorparse;
116pub use common::sip_method::SipMethod;
117pub use common::traits::NomParser;
118
119mod message;
120pub use message::get_message_type as get_sip_message_type;
121pub use message::MessageType as SipMessageType;
122pub use message::SipVersion;
123
124mod userinfo;
125
126mod request;
127pub use request::Request as SipRequest;
128pub use request::RequestLine as SipRequestLine;
129
130mod response;
131pub use response::Response as SipResponse;
132pub use response::StatusCode as SipResponseStatusCode;
133pub use response::StatusLine as SipResponseStatusLine;
134
135mod headers;
136pub use headers::sipuri::RequestUriScheme as SipRequestUriScheme;
137pub use headers::*;
138
139pub use unicase::Ascii as SipAscii;