-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for parsing
CSeq
headers
- Loading branch information
Showing
5 changed files
with
76 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# | ||
# Copyright (C) Spacinov SAS | ||
# Distributed under the 2-clause BSD license | ||
# | ||
|
||
import dataclasses | ||
import re | ||
|
||
from . import grammar | ||
|
||
CSEQ_PATTERN = re.compile( | ||
f"^(?P<sequence>{grammar.DIGIT}+){grammar.LWS}(?P<method>{grammar.TOKEN})$" | ||
) | ||
|
||
|
||
@dataclasses.dataclass | ||
class CSeq: | ||
""" | ||
A `CSeq` header, used to identity and order transactions. | ||
""" | ||
|
||
sequence: int | ||
"The sequence number." | ||
|
||
method: str | ||
"The request method." | ||
|
||
@classmethod | ||
def parse(cls, value: str) -> "CSeq": | ||
""" | ||
Parse the given string into an :class:`Address` instance. | ||
If parsing fails, a :class:`ValueError` is raised. | ||
""" | ||
pass | ||
value = grammar.simplify_whitespace(value) | ||
|
||
m = CSEQ_PATTERN.match(value) | ||
if m is None: | ||
raise ValueError("CSeq is not valid") | ||
|
||
return cls(sequence=int(m.group("sequence")), method=m.group("method")) | ||
|
||
def __str__(self) -> str: | ||
return f"{self.sequence} {self.method}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# | ||
# Copyright (C) Spacinov SAS | ||
# Distributed under the 2-clause BSD license | ||
# | ||
|
||
import unittest | ||
|
||
from sipmessage import CSeq | ||
|
||
|
||
class ViaTest(unittest.TestCase): | ||
def test_empty(self) -> None: | ||
with self.assertRaises(ValueError) as cm: | ||
CSeq.parse("") | ||
self.assertEqual(str(cm.exception), "CSeq is not valid") | ||
|
||
def test_rfc4475_wsinv(self) -> None: | ||
cseq = CSeq.parse(" 0009\r\n INVITE ") | ||
self.assertEqual(cseq, CSeq(sequence=9, method="INVITE")) | ||
self.assertEqual(str(cseq), "9 INVITE") |