Expand description
§Introduction
Library for parsing/editing/constructing SIP requests and responses.
This is the very first version where only simple parsing is support.
§Example
use sipmsg::*;
use unicase::Ascii;
let invite_msg_buf = "\
INVITE sip:bob@biloxi.com;user=phone?to=alice%40atlanta.com&priority=urgent SIP/2.0\r\n\
Via: SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bKkjshdyff\r\n\
Via: SIP/2.0/UDP 192.168.1.111\r\n\
To: Bob <sip:bob@biloxi.com>\r\n\
From: Alice <sip:alice@atlanta.com>;tag=88sja8x\r\n\
Contact: Caller <sip:alice@client.atlanta.example.com;transport=tcp>\r\n\
Max-Forwards: 70\r\n\
Call-ID: f81d4fae-7dec-11d0-a765-00a0c91e6bf6@foo.bar.com\r\n\
Extention-Header: extention header value;param=123;without_value\r\n\
CSeq: 986759 INVITE\r\n\r\nbody_stuff"
.as_bytes();
// First parameter not realized yet.
// It should consist be residue if Content-Length is less then actual body length.
let (_, request) = SipRequest::parse(invite_msg_buf).unwrap();
assert_eq!(request.rl.method, SipMethod::INVITE);
assert_eq!(request.rl.sip_version, SipVersion(2, 0));
// RURI
assert_eq!(request.rl.uri.scheme, SipRequestUriScheme::SIP);
assert_eq!(request.rl.uri.user_info().unwrap().value, "bob");
assert_eq!(request.rl.uri.hostport.host, "biloxi.com");
assert_eq!(request.rl.uri.params().unwrap().get(&"user"), Some(&Some("phone")));
assert_eq!(request.rl.uri.headers().unwrap().get(&"to"), Some(&"alice%40atlanta.com"));
assert_eq!(request.rl.uri.headers().unwrap().get(&"priority"), Some(&"urgent"));
let call_id_header = request.headers.get_rfc_s(SipRFCHeader::CallID).unwrap();
assert_eq!(call_id_header.value.vstr, "f81d4fae-7dec-11d0-a765-00a0c91e6bf6@foo.bar.com");
assert_eq!(call_id_header.value.tags().unwrap()[&SipHeaderTagType::ID],
"f81d4fae-7dec-11d0-a765-00a0c91e6bf6".as_bytes());
assert_eq!(call_id_header.value.tags().unwrap()[&SipHeaderTagType::Host], b"foo.bar.com");
// Via Header
let via_headers = request.headers.get_rfc(SipRFCHeader::Via).unwrap();
assert_eq!(via_headers[0].value.vstr, "SIP/2.0/UDP pc33.atlanta.com");
assert_eq!(
via_headers[0].params().unwrap().get(&"branch"),
Some(&Some("z9hG4bKkjshdyff"))
);
assert_eq!(
via_headers[0].value.tags().unwrap()[&SipHeaderTagType::ProtocolName],
b"SIP"
);
assert_eq!(
via_headers[0].value.tags().unwrap()[&SipHeaderTagType::ProtocolVersion],
b"2.0"
);
assert_eq!(
via_headers[0].value.tags().unwrap()[&SipHeaderTagType::ProtocolTransport],
b"UDP"
);
assert_eq!(
via_headers[0].value.tags().unwrap()[&SipHeaderTagType::Host],
b"pc33.atlanta.com"
);
assert_eq!(via_headers[1].value.vstr, "SIP/2.0/UDP 192.168.1.111");
assert_eq!(
via_headers[1].params(),
None
);
// Contact header
let contact_header = request.headers.get_rfc_s(SipRFCHeader::Contact).unwrap();
assert_eq!(
contact_header.value.tags().unwrap()[&SipHeaderTagType::DisplayName],
b"Caller"
);
assert_eq!(
contact_header.value.sip_uri().unwrap().user_info().unwrap().value,
"alice"
);
assert_eq!(
contact_header.value.sip_uri().unwrap().hostport.host,
"client.atlanta.example.com"
);
assert_eq!(
contact_header.value.sip_uri().unwrap().params().unwrap().get(&"transport"),
Some(&Some("tcp"))
);
assert_eq!(
contact_header.value.sip_uri().unwrap().params().unwrap().get(&"non-exists-param"),
None
);
// Extention Header
let extention_header = request.headers.get_ext_s("extention-header").unwrap();
assert_eq!(extention_header.name, "extention-header");
assert_eq!(extention_header.value.vstr, "extention header value;param=123;without_value");
// Body
assert_eq!(request.body.unwrap(), b"body_stuff");
Re-exports§
pub use common::errorparse;
pub use common::sip_method::SipMethod;
pub use common::traits::NomParser;
Modules§
Macros§
Structs§
- Generic
Params - SipAscii
- Case Insensitive wrapper of Ascii strings.
- SipHeader
- rfc3261 section-7.3
- SipHeaders
- SipRequest
- rfc3261 section-7.1
- SipRequest
Line - Ex:
INVITE sip:user@example.com SIP/2.0
The Request line and u8 buffer shoud have the same life time - SipResponse
- SipResponse
Status Line - Ex:
SIP/2.0 401 Unauthorized
- SipUri
- Its general form, in the case of a SIP URI, is: sip:user:password@host:port;uri-parameters?headers
- SipVersion
- SIP-Version
ex.
SIP/2.0 -> SipVersion(2, 0)
Enums§
- SipHeader
TagType - SipHeader
Value Type - SipMessage
Type - SipRFC
Header - Headers that defined in rfc3261
- SipRequest
UriScheme - SipResponse
Status Code
Functions§
- get_
sip_ message_ type - Fast determinates message type and minimal validate for further transmission to suitable parser. Does not validate full first line, just first 3 bytes.