Skip to content

Commit

Permalink
Fix minor bugs, add support multiline extension header
Browse files Browse the repository at this point in the history
  • Loading branch information
kurotych committed Oct 17, 2020
1 parent 7dd55ef commit aaf11fd
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 deletions.
6 changes: 6 additions & 0 deletions crates/sipmsg/src/common/nom_wrappers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ mod tests {
"\r\nNextHeader: nextvalue\r\n\r\n",
);

take_quoted_string_case(
"\"Quoted string \\\"\\\"\" <sip:jdrosen@example.com>",
"Quoted string \\\"\\\"",
"<sip:jdrosen@example.com>",
);

take_quoted_string_case("\"\"", "", "");

let res = take_quoted_string(" \r\n \"value\" \r\nnext_value".as_bytes());
Expand Down
13 changes: 8 additions & 5 deletions crates/sipmsg/src/headers/header.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::{
common::{
bnfcore::*, errorparse::SipParseError, nom_wrappers::from_utf8_nom, take_sws_token,
bnfcore::*,
errorparse::SipParseError,
nom_wrappers::{from_utf8_nom, take_sws},
take_sws_token,
traits::NomParser,
},
headers::{
Expand Down Expand Up @@ -50,9 +53,9 @@ pub enum HeaderValueType {
RetryAfter, // tags: Seconds(R), Comment(O)
UserAgent, // haven't tags,

Via, // tags: ProtocolName(R),ProtocolVersion(R),ProtocolTransport(R), Host(R), Port(O)
Via, // tags: ProtocolName(R),ProtocolVersion(R),ProtocolTransport(R), Host(R), Port(O)
Warning, // tags: WarnCode(R), WarnAgent(R), WarnText(R)
ExtensionHeader // No tags
ExtensionHeader, // No tags
}

#[derive(PartialEq, Debug, Eq, PartialOrd, Ord)]
Expand Down Expand Up @@ -96,7 +99,7 @@ pub enum HeaderTagType {

WarnCode,
WarnAgent,
WarnText
WarnText,
}

pub type HeaderTags<'a> = BTreeMap<HeaderTagType, &'a [u8]>;
Expand Down Expand Up @@ -202,7 +205,7 @@ impl<'a> Header<'a> {
) -> nom::IResult<&'a [u8], (HeaderValue<'a>, Option<GenericParams<'a>>), SipParseError<'a>>
{
// skip whitespaces before take value
let (input, _) = complete::space0(input)?;
let (input, _) = take_sws(input)?;
if is_crlf(input) {
return Ok((input, (HeaderValue::create_empty_value(), None))); // This is header with empty value
}
Expand Down
8 changes: 3 additions & 5 deletions crates/sipmsg/src/headers/name_addr.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::{
common::{bnfcore::is_token_char, errorparse::SipParseError, take_sws_token},
common::{bnfcore::is_token_char, errorparse::SipParseError, take_sws_token, nom_wrappers},
headers::{
header::{HeaderTagType, HeaderTags},
},
};

use nom::{
bytes::complete::{take_while, take_while1},
bytes::complete::{take_while1},
character::complete,
sequence::tuple,
};
Expand Down Expand Up @@ -46,9 +46,7 @@ fn take_display_name(
display_name_type: NameAddrValueType,
) -> nom::IResult<&[u8], &[u8], SipParseError> {
if display_name_type == NameAddrValueType::QuotedDisplayName {
let (input, _) = take_sws_token::ldquot(source_input)?;
let (input, display_name) = take_while(|c: u8| c != b'"')(input)?;
let (input, _) = take_sws_token::rdquot(input)?;
let (input, (_, display_name, _)) = nom_wrappers::take_quoted_string(source_input)?;
return Ok((input, display_name));
} else if display_name_type == NameAddrValueType::TokenDisplayName {
let (input, display_name) = take_while1(is_token_char)(source_input)?;
Expand Down
24 changes: 20 additions & 4 deletions crates/sipmsg/src/headers/parsers/extension.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::common::bnfcore::is_wsp;
use crate::{
common::errorparse::SipParseError,
headers::{
Expand All @@ -10,9 +11,24 @@ use nom::bytes::complete::take_until;
pub struct ExtensionParser;

impl SipHeaderParser for ExtensionParser {
fn take_value(input: &[u8]) -> nom::IResult<&[u8], HeaderValue, SipParseError> {
let (inp, res_val) = take_until("\r\n")(input)?;
let (_, hdr_val) = HeaderValue::new(res_val, HeaderValueType::TokenValue, None, None)?;
Ok((inp, hdr_val))
fn take_value(source_input: &[u8]) -> nom::IResult<&[u8], HeaderValue, SipParseError> {
let mut taken_bytes = 0;
loop {
let (inp, res_val) = take_until("\r\n")(&source_input[taken_bytes..])?;
taken_bytes += res_val.len();
if inp.len() > 3 && is_wsp(inp[2]) {
taken_bytes += 3;
continue;
}
break;
}

let (_, hdr_val) = HeaderValue::new(
&source_input[..taken_bytes],
HeaderValueType::TokenValue,
None,
None,
)?;
Ok((&source_input[taken_bytes..], hdr_val))
}
}

0 comments on commit aaf11fd

Please sign in to comment.