1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use crate::errorparse::SipParseError;

pub use crate::traits::NomParser;

use nom::{
    bytes::complete::{tag, take_while1},
    character::{complete, is_alphabetic},
    sequence::tuple,
};

use crate::header::*;
use crate::message::*;
use crate::sipuri::*;

use alloc::vec::Vec;

use core::{str, u8};

/// [rfc3261 section-7.1](https://tools.ietf.org/html/rfc3261#section-7.1)
pub struct Request<'a> {
    /// The request line. Example: `OPTIONS sip:user@example.com SIP/2.0`
    pub rl: RequestLine<'a>,
    /// The request headers.
    pub headers: Vec<Header<'a>>,
    /// The body of message
    pub body: Option<&'a [u8]>,
}

impl<'a> Request<'a> {
    fn new(rl: RequestLine<'a>, headers: Vec<Header<'a>>, body: Option<&'a [u8]>) -> Request<'a> {
        Request {
            rl: rl,
            headers: headers,
            body: body,
        }
    }
}

impl<'a> NomParser<'a> for Request<'a> {
    type ParseResult = Request<'a>;

    fn parse(buf_input: &'a [u8]) -> nom::IResult<&[u8], Request, SipParseError> {
        let (input, rl) = RequestLine::parse(buf_input)?;

        let (input, headers) = parse_headers(input)?;
        // TODO check header Content-Length and fix buf_input return
        let (body, _) = tag("\r\n")(input)?;
        Ok((buf_input, Request::new(rl, headers, Some(body))))
    }
}

/// Ex: `INVITE sip:user@example.com SIP/2.0`
/// The Request line and u8 buffer shoud have the same life time
pub struct RequestLine<'a> {
    pub method: Method,
    pub uri: SipUri<'a>,
    pub sip_version: SipVersion,
}

impl<'a> NomParser<'a> for RequestLine<'a> {
    type ParseResult = RequestLine<'a>;

    fn parse(rl: &[u8]) -> nom::IResult<&[u8], RequestLine, SipParseError> {
        let method = take_while1(is_alphabetic);
        let uri = take_while1(|c| c != b' ' as u8);
        let (input, (method, _, uri, _, _, major_version, _, minor_version, _)) = tuple((
            method,
            complete::space1,
            uri,
            complete::space1,
            tag("SIP/"),
            complete::digit1,
            complete::char('.'),
            complete::digit1,
            complete::crlf,
        ))(rl)?;

        let (_, sip_uri) = SipUri::parse(uri)?;

        let sip_version = SipVersion(
            u8::from_str_radix(str::from_utf8(major_version).unwrap(), 10).unwrap(),
            u8::from_str_radix(str::from_utf8(minor_version).unwrap(), 10).unwrap(),
        );

        match RequestLine::parse_method(method) {
            Some(m) => Ok((
                input,
                RequestLine {
                    method: m,
                    uri: sip_uri,
                    sip_version: sip_version,
                },
            )),
            None => return sip_parse_error!(1, "Error cast from_utf8")
        }
    }
}

impl<'a> RequestLine<'a> {
    fn parse_method(method: &[u8]) -> Option<Method> {
        match str::from_utf8(method) {
            Ok(s) => Method::from_str(s),
            Err(_) => None,
        }
    }
}

#[derive(Copy, Clone, PartialEq, Debug)]
pub enum Method {
    ACK,
    BYE,
    CANCEL,
    INFO,
    INVITE,
    MESSAGE,
    NOTIFY,
    OPTIONS,
    PRACK,
    PUBLISH,
    REFER,
    REGISTER,
    SUBSCRIBE,
    UPDATE,
}

impl Method {
    pub fn as_str(&self) -> &str {
        match self {
            &Method::ACK => "ACK",
            &Method::BYE => "BYE",
            &Method::CANCEL => "CANCEL",
            &Method::INFO => "INFO",
            &Method::INVITE => "INVITE",
            &Method::MESSAGE => "MESSAGE",
            &Method::NOTIFY => "NOTIFY",
            &Method::OPTIONS => "OPTIONS",
            &Method::PRACK => "PRACK",
            &Method::PUBLISH => "PUBLISH",
            &Method::REFER => "REFER",
            &Method::REGISTER => "REGISTER",
            &Method::SUBSCRIBE => "SUBSCRIBE",
            &Method::UPDATE => "UPDATE",
        }
    }

    pub fn from_str(s: &str) -> Option<Method> {
        match s {
            "ACK" => Some(Method::ACK),
            "BYE" => Some(Method::BYE),
            "CANCEL" => Some(Method::CANCEL),
            "INFO" => Some(Method::INFO),
            "INVITE" => Some(Method::INVITE),
            "MESSAGE" => Some(Method::MESSAGE),
            "NOTIFY" => Some(Method::NOTIFY),
            "OPTIONS" => Some(Method::OPTIONS),
            "PRACK" => Some(Method::PRACK),
            "PUBLISH" => Some(Method::PUBLISH),
            "REFER" => Some(Method::REFER),
            "REGISTER" => Some(Method::REGISTER),
            "SUBSCRIBE" => Some(Method::SUBSCRIBE),
            "UPDATE" => Some(Method::UPDATE),
            _ => None,
        }
    }
}