sip-header 0.3.0

SIP header field parsers: Via, Warning, Auth, Accept, Contact, Call-Info, History-Info, Geolocation, Security, and full IANA header catalog
Documentation
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//! SIP Warning header parser (RFC 3261 §20.43).

use std::fmt;

/// Error parsing a SIP Warning header.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum SipWarningError {
    /// Empty input.
    Empty,
    /// Invalid format.
    InvalidFormat(String),
}

impl fmt::Display for SipWarningError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            SipWarningError::Empty => write!(f, "empty Warning header"),
            SipWarningError::InvalidFormat(msg) => write!(f, "invalid Warning format: {}", msg),
        }
    }
}

impl std::error::Error for SipWarningError {}

/// A single Warning header entry.
///
/// RFC 3261 §20.43:
/// ```text
/// warning-value = warn-code SP warn-agent SP warn-text
/// warn-code = 3DIGIT
/// warn-agent = hostport / pseudonym
/// warn-text = quoted-string
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct SipWarningEntry {
    code: u16,
    agent: String,
    text: String,
}

impl SipWarningEntry {
    /// The 3-digit warning code.
    pub fn code(&self) -> u16 {
        self.code
    }

    /// The warn-agent (hostport or pseudonym).
    pub fn agent(&self) -> &str {
        &self.agent
    }

    /// The warn-text (unquoted).
    pub fn text(&self) -> &str {
        &self.text
    }

    fn parse(s: &str) -> Result<Self, SipWarningError> {
        let s = s.trim();
        if s.is_empty() {
            return Err(SipWarningError::InvalidFormat(
                "empty warning entry".to_string(),
            ));
        }

        // Parse warn-code (3DIGIT)
        let space_pos = s
            .find(' ')
            .ok_or_else(|| {
                SipWarningError::InvalidFormat("missing space after warn-code".to_string())
            })?;

        let code_str = &s[..space_pos];
        if code_str.len() != 3
            || !code_str
                .chars()
                .all(|c| c.is_ascii_digit())
        {
            return Err(SipWarningError::InvalidFormat(format!(
                "warn-code must be 3 digits, got '{}'",
                code_str
            )));
        }

        let code = code_str
            .parse::<u16>()
            .map_err(|_| {
                SipWarningError::InvalidFormat(format!("invalid warn-code '{}'", code_str))
            })?;

        let rest = s[space_pos..].trim_start();

        // Find the quoted warn-text
        let quote_pos = rest
            .find('"')
            .ok_or_else(|| {
                SipWarningError::InvalidFormat("missing quoted warn-text".to_string())
            })?;

        if quote_pos == 0 {
            return Err(SipWarningError::InvalidFormat(
                "missing warn-agent".to_string(),
            ));
        }

        let agent = rest[..quote_pos]
            .trim_end()
            .to_string();
        if agent.is_empty() {
            return Err(SipWarningError::InvalidFormat(
                "empty warn-agent".to_string(),
            ));
        }

        // Parse quoted string
        let text = parse_quoted_string(&rest[quote_pos..])?;

        Ok(SipWarningEntry { code, agent, text })
    }
}

impl fmt::Display for SipWarningEntry {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{:03} {} \"{}\"",
            self.code,
            self.agent,
            crate::escape_quoted_pair(&self.text)
        )
    }
}

/// Parse a quoted string starting with `"`, returning the unescaped content.
fn parse_quoted_string(s: &str) -> Result<String, SipWarningError> {
    let s = s.trim_start();
    if !s.starts_with('"') {
        return Err(SipWarningError::InvalidFormat(
            "quoted string must start with '\"'".to_string(),
        ));
    }

    // Find the closing quote, respecting backslash escapes.
    let content = &s[1..];
    let mut escaped = false;
    for (i, c) in content.char_indices() {
        if escaped {
            escaped = false;
        } else if c == '\\' {
            escaped = true;
        } else if c == '"' {
            return Ok(crate::unescape_quoted_pair(&content[..i]));
        }
    }

    Err(SipWarningError::InvalidFormat(
        "unterminated quoted string".to_string(),
    ))
}

/// SIP Warning header.
///
/// RFC 3261 §20.43:
/// ```text
/// Warning = "Warning" HCOLON warning-value *(COMMA warning-value)
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct SipWarning {
    entries: Vec<SipWarningEntry>,
}

impl SipWarning {
    /// Parse a Warning header value.
    pub fn parse(raw: &str) -> Result<Self, SipWarningError> {
        let raw = raw.trim();
        if raw.is_empty() {
            return Err(SipWarningError::Empty);
        }

        let entries = crate::split_comma_entries(raw)
            .into_iter()
            .map(SipWarningEntry::parse)
            .collect::<Result<Vec<_>, _>>()?;

        if entries.is_empty() {
            return Err(SipWarningError::Empty);
        }

        Ok(SipWarning { entries })
    }

    /// All warning entries.
    pub fn entries(&self) -> &[SipWarningEntry] {
        &self.entries
    }

    /// Consume self and return entries as a `Vec`.
    pub fn into_entries(self) -> Vec<SipWarningEntry> {
        self.entries
    }

    /// Number of warning entries.
    pub fn len(&self) -> usize {
        self.entries
            .len()
    }

    /// Whether there are no warning entries.
    pub fn is_empty(&self) -> bool {
        self.entries
            .is_empty()
    }
}

impl fmt::Display for SipWarning {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        crate::fmt_joined(f, &self.entries, ", ")
    }
}

impl_from_str_via_parse!(SipWarning, SipWarningError);

impl IntoIterator for SipWarning {
    type Item = SipWarningEntry;
    type IntoIter = std::vec::IntoIter<SipWarningEntry>;

    fn into_iter(self) -> Self::IntoIter {
        self.entries
            .into_iter()
    }
}

impl<'a> IntoIterator for &'a SipWarning {
    type Item = &'a SipWarningEntry;
    type IntoIter = std::slice::Iter<'a, SipWarningEntry>;

    fn into_iter(self) -> Self::IntoIter {
        self.entries
            .iter()
    }
}

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

    #[test]
    fn test_single_warning() {
        let input = r#"301 example.com "Incompatible network protocol""#;
        let warning = SipWarning::parse(input).unwrap();
        assert_eq!(warning.len(), 1);
        let entry = &warning.entries()[0];
        assert_eq!(entry.code(), 301);
        assert_eq!(entry.agent(), "example.com");
        assert_eq!(entry.text(), "Incompatible network protocol");
    }

    #[test]
    fn test_multiple_warnings() {
        let input = r#"301 example.com "Incompatible network protocol", 399 198.51.100.1:5060 "Miscellaneous warning""#;
        let warning = SipWarning::parse(input).unwrap();
        assert_eq!(warning.len(), 2);

        let entry1 = &warning.entries()[0];
        assert_eq!(entry1.code(), 301);
        assert_eq!(entry1.agent(), "example.com");
        assert_eq!(entry1.text(), "Incompatible network protocol");

        let entry2 = &warning.entries()[1];
        assert_eq!(entry2.code(), 399);
        assert_eq!(entry2.agent(), "198.51.100.1:5060");
        assert_eq!(entry2.text(), "Miscellaneous warning");
    }

    #[test]
    fn test_escaped_quotes_in_text() {
        let input = r#"399 example.org "Warning with \"quoted\" text""#;
        let warning = SipWarning::parse(input).unwrap();
        assert_eq!(warning.len(), 1);
        let entry = &warning.entries()[0];
        assert_eq!(entry.code(), 399);
        assert_eq!(entry.agent(), "example.org");
        assert_eq!(entry.text(), r#"Warning with "quoted" text"#);
    }

    #[test]
    fn test_common_warning_codes() {
        let input1 = r#"301 example.com "Incompatible network protocol""#;
        let warning1 = SipWarning::parse(input1).unwrap();
        assert_eq!(warning1.entries()[0].code(), 301);

        let input2 = r#"399 example.net "Miscellaneous warning""#;
        let warning2 = SipWarning::parse(input2).unwrap();
        assert_eq!(warning2.entries()[0].code(), 399);
    }

    #[test]
    fn test_display_roundtrip() {
        let input = r#"301 example.com "Incompatible network protocol", 399 198.51.100.1:5060 "Miscellaneous warning""#;
        let warning = SipWarning::parse(input).unwrap();
        let output = warning.to_string();
        let reparsed = SipWarning::parse(&output).unwrap();
        assert_eq!(warning, reparsed);
    }

    #[test]
    fn test_display_roundtrip_with_escaped_quotes() {
        let input = r#"399 example.org "Warning with \"quoted\" text""#;
        let warning = SipWarning::parse(input).unwrap();
        let output = warning.to_string();
        let reparsed = SipWarning::parse(&output).unwrap();
        assert_eq!(warning, reparsed);
    }

    #[test]
    fn test_empty_input() {
        let result = SipWarning::parse("");
        assert!(matches!(result, Err(SipWarningError::Empty)));

        let result = SipWarning::parse("   ");
        assert!(matches!(result, Err(SipWarningError::Empty)));
    }

    #[test]
    fn test_invalid_warn_code() {
        let result = SipWarning::parse(r#"30 example.com "Short code""#);
        assert!(matches!(result, Err(SipWarningError::InvalidFormat(_))));

        let result = SipWarning::parse(r#"3001 example.com "Long code""#);
        assert!(matches!(result, Err(SipWarningError::InvalidFormat(_))));

        let result = SipWarning::parse(r#"abc example.com "Non-numeric""#);
        assert!(matches!(result, Err(SipWarningError::InvalidFormat(_))));
    }

    #[test]
    fn test_missing_warn_agent() {
        let result = SipWarning::parse(r#"301 "Missing agent""#);
        assert!(matches!(result, Err(SipWarningError::InvalidFormat(_))));
    }

    #[test]
    fn test_missing_warn_text() {
        let result = SipWarning::parse("301 example.com");
        assert!(matches!(result, Err(SipWarningError::InvalidFormat(_))));
    }

    #[test]
    fn test_unterminated_quoted_string() {
        let result = SipWarning::parse(r#"301 example.com "Unterminated"#);
        assert!(matches!(result, Err(SipWarningError::InvalidFormat(_))));
    }

    #[test]
    fn test_into_iterator() {
        let input = r#"301 example.com "First", 399 example.org "Second""#;
        let warning = SipWarning::parse(input).unwrap();

        let codes: Vec<u16> = warning
            .into_iter()
            .map(|e| e.code())
            .collect();
        assert_eq!(codes, vec![301, 399]);
    }

    #[test]
    fn test_into_iterator_ref() {
        let input = r#"301 example.com "First", 399 example.org "Second""#;
        let warning = SipWarning::parse(input).unwrap();

        let codes: Vec<u16> = (&warning)
            .into_iter()
            .map(|e| e.code())
            .collect();
        assert_eq!(codes, vec![301, 399]);

        assert_eq!(warning.len(), 2);
    }

    #[test]
    fn test_is_empty() {
        let input = r#"301 example.com "Warning""#;
        let warning = SipWarning::parse(input).unwrap();
        assert!(!warning.is_empty());
    }

    #[test]
    fn test_into_entries() {
        let input = r#"301 example.com "First", 399 example.org "Second""#;
        let warning = SipWarning::parse(input).unwrap();
        let entries = warning.into_entries();
        assert_eq!(entries.len(), 2);
        assert_eq!(entries[0].code(), 301);
        assert_eq!(entries[1].code(), 399);
    }

    #[test]
    fn test_comma_in_warn_text() {
        let input = r#"301 example.com "text, with comma", 399 example.org "fine""#;
        let warning = SipWarning::parse(input).unwrap();
        assert_eq!(warning.len(), 2);
        assert_eq!(warning.entries()[0].text(), "text, with comma");
        assert_eq!(warning.entries()[1].text(), "fine");
    }

    #[test]
    fn test_from_str() {
        let input = r#"301 example.com "warning""#;
        let warning: SipWarning = input
            .parse()
            .unwrap();
        assert_eq!(warning.len(), 1);
    }

    #[test]
    fn test_ipv6_agent() {
        let input = r#"301 [2001:db8::1]:5060 "IPv6 warning""#;
        let warning = SipWarning::parse(input).unwrap();
        assert_eq!(warning.entries()[0].agent(), "[2001:db8::1]:5060");
    }

    #[test]
    fn test_escaped_backslash() {
        let input = r#"399 example.com "Path: C:\\temp\\file""#;
        let warning = SipWarning::parse(input).unwrap();
        assert_eq!(warning.entries()[0].text(), r#"Path: C:\temp\file"#);
    }
}