sipmessage 0.2.2__tar.gz → 0.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. {sipmessage-0.2.2/src/sipmessage.egg-info → sipmessage-0.3.0}/PKG-INFO +1 -1
  2. {sipmessage-0.2.2 → sipmessage-0.3.0}/pyproject.toml +1 -1
  3. {sipmessage-0.2.2 → sipmessage-0.3.0}/src/sipmessage/address.py +30 -23
  4. sipmessage-0.3.0/src/sipmessage/grammar.py +69 -0
  5. {sipmessage-0.2.2 → sipmessage-0.3.0}/src/sipmessage/parameters.py +19 -5
  6. {sipmessage-0.2.2 → sipmessage-0.3.0}/src/sipmessage/uri.py +32 -35
  7. sipmessage-0.3.0/src/sipmessage/via.py +68 -0
  8. {sipmessage-0.2.2 → sipmessage-0.3.0/src/sipmessage.egg-info}/PKG-INFO +1 -1
  9. {sipmessage-0.2.2 → sipmessage-0.3.0}/src/sipmessage.egg-info/SOURCES.txt +1 -0
  10. sipmessage-0.3.0/tests/test_address.py +197 -0
  11. sipmessage-0.3.0/tests/test_parameters.py +31 -0
  12. {sipmessage-0.2.2 → sipmessage-0.3.0}/tests/test_uri.py +77 -8
  13. sipmessage-0.3.0/tests/test_via.py +113 -0
  14. sipmessage-0.2.2/src/sipmessage/via.py +0 -50
  15. sipmessage-0.2.2/tests/test_address.py +0 -48
  16. sipmessage-0.2.2/tests/test_parameters.py +0 -20
  17. sipmessage-0.2.2/tests/test_via.py +0 -36
  18. {sipmessage-0.2.2 → sipmessage-0.3.0}/LICENSE +0 -0
  19. {sipmessage-0.2.2 → sipmessage-0.3.0}/MANIFEST.in +0 -0
  20. {sipmessage-0.2.2 → sipmessage-0.3.0}/Makefile +0 -0
  21. {sipmessage-0.2.2 → sipmessage-0.3.0}/README.rst +0 -0
  22. {sipmessage-0.2.2 → sipmessage-0.3.0}/docs/Makefile +0 -0
  23. {sipmessage-0.2.2 → sipmessage-0.3.0}/docs/conf.py +0 -0
  24. {sipmessage-0.2.2 → sipmessage-0.3.0}/docs/index.rst +0 -0
  25. {sipmessage-0.2.2 → sipmessage-0.3.0}/setup.cfg +0 -0
  26. {sipmessage-0.2.2 → sipmessage-0.3.0}/setup.py +0 -0
  27. {sipmessage-0.2.2 → sipmessage-0.3.0}/src/sipmessage/__init__.py +0 -0
  28. {sipmessage-0.2.2 → sipmessage-0.3.0}/src/sipmessage/py.typed +0 -0
  29. {sipmessage-0.2.2 → sipmessage-0.3.0}/src/sipmessage.egg-info/dependency_links.txt +0 -0
  30. {sipmessage-0.2.2 → sipmessage-0.3.0}/src/sipmessage.egg-info/requires.txt +0 -0
  31. {sipmessage-0.2.2 → sipmessage-0.3.0}/src/sipmessage.egg-info/top_level.txt +0 -0
  32. {sipmessage-0.2.2 → sipmessage-0.3.0}/tests/__init__.py +0 -0
  33. {sipmessage-0.2.2 → sipmessage-0.3.0}/tests/test_public_api.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: sipmessage
3
- Version: 0.2.2
3
+ Version: 0.3.0
4
4
  Summary: Pure python parsing of SIP messages and headers
5
5
  Author-email: Spacinov Engineering <opensource@spacinov.com>
6
6
  License: BSD License
@@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta"
5
5
  [project]
6
6
  # Version
7
7
  name = "sipmessage"
8
- version = "0.2.2"
8
+ version = "0.3.0"
9
9
 
10
10
  # Dependencies
11
11
  dependencies = []
@@ -6,44 +6,48 @@
6
6
  import dataclasses
7
7
  import re
8
8
 
9
+ from . import grammar
9
10
  from .parameters import Parameters
10
11
  from .uri import URI
11
12
 
13
+ TOKEN_LWS = grammar.cset(grammar.C_TOKEN + " ")
14
+
12
15
  ADDRESS_PATTERNS = [
13
16
  # name-addr *(SEMI contact-params)
14
17
  re.compile(
15
- # token
16
- r"^(?P<name>[a-zA-Z0-9\-\._\+~]*)"
17
- # LWS
18
- "[ \t]*"
19
- # LAQUOT addr-spec RAQUOT
20
- "<(?P<uri>[^>]+)>"
21
- "[ \t]*"
22
- r"(?:;(?P<parameters>[^\?]*))?"
23
- ),
24
- # name-addr *(SEMI contact-params)
25
- re.compile(
26
- # quoted-string
27
- '^(?:"(?P<name>[^"]+)")'
28
- # LWS
29
- "[ \t]*"
18
+ # display-name
19
+ f"^(?P<name>{TOKEN_LWS}*|{grammar.QUOTED_STRING})"
30
20
  # LAQUOT addr-spec RAQUOT
21
+ "[ ]*"
31
22
  "<(?P<uri>[^>]+)>"
32
- "[ \t]*"
33
- r"(?:;(?P<parameters>[^\?]*))?"
23
+ # *(SEMI contact-params)
24
+ f"(?P<parameters>(?:{grammar.SEMI}{grammar.GENERIC_PARAM})*)"
25
+ "$"
34
26
  ),
35
27
  # addr-spec *(SEMI contact-params)
36
28
  re.compile(
37
29
  # no name
38
30
  "^(?P<name>)"
39
31
  # addr-spec
40
- "(?P<uri>[^ \t;]+)"
41
- "[ \t]*"
42
- r"(?:;(?P<parameters>[^\?]*))?"
32
+ "(?P<uri>[^ ;]+)"
33
+ # *(SEMI contact-params)
34
+ f"(?P<parameters>(?:{grammar.SEMI}{grammar.GENERIC_PARAM})*)"
35
+ "$"
43
36
  ),
44
37
  ]
45
38
 
46
39
 
40
+ def quote(value: str) -> str:
41
+ return '"' + value.replace("\\", "\\\\").replace('"', '\\"') + '"'
42
+
43
+
44
+ def unquote(value: str) -> str:
45
+ if value.startswith('"'):
46
+ return value[1:-1].replace('\\"', '"').replace("\\\\", "\\")
47
+ else:
48
+ return value
49
+
50
+
47
51
  @dataclasses.dataclass
48
52
  class Address:
49
53
  """
@@ -66,21 +70,24 @@ class Address:
66
70
 
67
71
  If parsing fails, a :class:`ValueError` is raised.
68
72
  """
73
+
74
+ value = grammar.simplify_whitespace(value)
75
+
69
76
  for pattern in ADDRESS_PATTERNS:
70
77
  m = pattern.match(value)
71
78
  if m:
72
79
  return cls(
73
80
  uri=URI.parse(m.group("uri")),
74
- name=m.group("name"),
81
+ name=unquote(m.group("name").strip()),
75
82
  parameters=Parameters.parse(m.group("parameters")),
76
83
  )
77
84
  else:
78
- raise ValueError("Not a valid address")
85
+ raise ValueError("Address is not valid")
79
86
 
80
87
  def __str__(self) -> str:
81
88
  s = ""
82
89
  if self.name:
83
- s += '"%s" ' % self.name
90
+ s += quote(self.name) + " "
84
91
  s += "<%s>" % self.uri
85
92
  if self.parameters:
86
93
  s += ";" + str(self.parameters)
@@ -0,0 +1,69 @@
1
+ #
2
+ # Copyright (C) Spacinov SAS
3
+ # Distributed under the 2-clause BSD license
4
+ #
5
+
6
+ import re
7
+ import string
8
+
9
+
10
+ def cset(chars: str) -> str:
11
+ """
12
+ Transform a collection of characters into a regular expression
13
+ character set.
14
+ """
15
+ return "[" + chars.replace("-", "\\-").replace("[", "\\[").replace("]", "\\]") + "]"
16
+
17
+
18
+ def simplify_whitespace(value: str) -> str:
19
+ """
20
+ Replace any whitespace by a single space.
21
+
22
+ According to RFC 3261, all linear white space, including folding,
23
+ has the same semantics as SP.
24
+ """
25
+ return re.sub(r"\s+", " ", value).strip()
26
+
27
+
28
+ # Character collections.
29
+ C_ALPHA = string.ascii_letters
30
+ C_ALPHANUM = string.ascii_letters + string.digits
31
+ C_MARK = "-_.!~*/'()"
32
+ C_TOKEN = C_ALPHANUM + "-.!%*_+`'~"
33
+ C_UNRESERVED = C_ALPHANUM + C_MARK
34
+ C_PASSWORD_SAFE = C_UNRESERVED + "&=+$,"
35
+ C_USER_SAFE = C_UNRESERVED + "&=+$,;?/"
36
+ C_URI_PARAM_SAFE = C_UNRESERVED + "[]/:&+$"
37
+
38
+ # Regular expression fragments.
39
+ ALPHA = cset(C_ALPHA)
40
+ ALPHANUM = cset(C_ALPHANUM)
41
+ HEXDIG = cset(string.hexdigits)
42
+ ESCAPED = f"%{HEXDIG}{{2}}"
43
+ TOKEN = f"{cset(C_TOKEN)}+"
44
+ QUOTED_STRING = '"(?:[^"]|\\")*"'
45
+
46
+ EQUAL = "[ ]*=[ ]*"
47
+ SEMI = "[ ]*;[ ]*"
48
+ SLASH = "[ ]*/[ ]*"
49
+
50
+ HEXSEQ = f"{HEXDIG}{{1,4}}(?::{HEXDIG}{{1,4}})*"
51
+ HEXPART = f"(?:{HEXSEQ}|{HEXSEQ}::(?:{HEXSEQ})?|::(?:{HEXSEQ})?)"
52
+ IPV4ADDRESS = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}"
53
+ IPV6ADDRESS = f"{HEXPART}(?::{IPV4ADDRESS})?"
54
+ IPV6REFERENCE = f"\\[{IPV6ADDRESS}\\]"
55
+
56
+ DOMAINLABEL = f"{ALPHANUM}+([\\-]+{ALPHANUM}+)*"
57
+ TOPLABEL = f"{ALPHA}+([\\-]+{ALPHANUM}+)*"
58
+ HOSTNAME = f"(?:{DOMAINLABEL}\\.)*{TOPLABEL}[\\.]?"
59
+
60
+ HOST = f"(?:{HOSTNAME}|{IPV4ADDRESS}|{IPV6REFERENCE})"
61
+ USER = f"(?:{cset(C_USER_SAFE)}|{ESCAPED})+"
62
+ PASSWORD = f"(?:{cset(C_PASSWORD_SAFE)}|{ESCAPED})+"
63
+ PORT = "\\d+"
64
+
65
+ URI_PARAMCHAR = f"(?:{cset(C_URI_PARAM_SAFE)}|{ESCAPED})"
66
+ URI_PARAM = f"{URI_PARAMCHAR}+(?:={URI_PARAMCHAR}+)?"
67
+
68
+ GENERIC_VALUE = f"(?:{TOKEN}|{HOST}|{QUOTED_STRING})"
69
+ GENERIC_PARAM = f"{TOKEN}(?:{EQUAL}{GENERIC_VALUE})?"
@@ -3,6 +3,14 @@
3
3
  # Distributed under the 2-clause BSD license
4
4
  #
5
5
 
6
+ import re
7
+ from urllib.parse import quote, unquote
8
+
9
+ from . import grammar
10
+
11
+ EQUAL_PATTERN = re.compile(grammar.EQUAL)
12
+ SEMI_PATTERN = re.compile(grammar.SEMI)
13
+
6
14
 
7
15
  class Parameters(dict[str, str | None]):
8
16
  """
@@ -15,14 +23,20 @@ class Parameters(dict[str, str | None]):
15
23
  Parse the given string into a :class:`Parameters` instance.
16
24
  """
17
25
  p = cls()
18
- if val:
19
- for bit in val.split(";"):
26
+ for bit in SEMI_PATTERN.split(val):
27
+ if bit:
20
28
  if "=" in bit:
21
- k, v = bit.split("=", 1)
29
+ k, v = EQUAL_PATTERN.split(bit, 1)
30
+ v = unquote(v)
22
31
  else:
23
32
  k, v = bit, None
24
- p[k] = v
33
+ p[unquote(k)] = v
25
34
  return p
26
35
 
27
36
  def __str__(self) -> str:
28
- return ";".join([k if v is None else (k + "=" + v) for k, v in self.items()])
37
+ return ";".join(
38
+ [
39
+ quote(k) if v is None else (quote(k) + "=" + quote(v))
40
+ for k, v in self.items()
41
+ ]
42
+ )
@@ -4,10 +4,22 @@
4
4
  #
5
5
 
6
6
  import dataclasses
7
+ import re
7
8
  import urllib.parse
8
9
 
10
+ from . import grammar
9
11
  from .parameters import Parameters
10
12
 
13
+ URI_PATTERN = re.compile(
14
+ "^"
15
+ "(?P<scheme>sip|sips):"
16
+ f"(?:(?P<user>{grammar.USER})(?::(?P<password>{grammar.PASSWORD}))?@)?"
17
+ f"(?P<host>{grammar.HOST})"
18
+ f"(?::(?P<port>{grammar.PORT}))?"
19
+ f"(?P<parameters>(?:;{grammar.URI_PARAM})*)"
20
+ "$"
21
+ )
22
+
11
23
 
12
24
  @dataclasses.dataclass
13
25
  class URI:
@@ -40,39 +52,22 @@ class URI:
40
52
 
41
53
  If parsing fails, a :class:`ValueError` is raised.
42
54
  """
43
- parsed = urllib.parse.urlparse(value)
44
-
45
- # Check the URI scheme is valid.
46
- if parsed.scheme not in ("sip", "sips"):
47
- raise ValueError("URI scheme must be 'sip' or 'sips'")
48
-
49
- if "@" in parsed.path:
50
- user_password, host_port = parsed.path.split("@")
51
- if ":" in user_password:
52
- user, password = user_password.split(":")
53
- else:
54
- user = user_password
55
- password = None
56
- else:
57
- host_port = parsed.path
58
- user = None
59
- password = None
60
- if ":" in host_port:
61
- host, port_ = host_port.split(":")
62
- try:
63
- port = int(port_)
64
- except ValueError:
65
- raise ValueError("URI port must be an integer")
66
- else:
67
- host = host_port
68
- port = None
55
+ m = URI_PATTERN.match(value)
56
+ if not m:
57
+ raise ValueError("URI is not valid")
58
+
59
+ port = m.group("port")
60
+ user = m.group("user")
61
+ password = m.group("password")
62
+ parameters = m.group("parameters")
63
+
69
64
  return cls(
70
- scheme=parsed.scheme,
71
- host=host,
72
- port=port,
73
- user=user,
74
- password=password,
75
- parameters=Parameters.parse(parsed.params),
65
+ scheme=m.group("scheme"),
66
+ host=m.group("host"),
67
+ port=int(port) if port else None,
68
+ user=urllib.parse.unquote(user) if user else None,
69
+ password=urllib.parse.unquote(password) if password else None,
70
+ parameters=Parameters.parse(parameters),
76
71
  )
77
72
 
78
73
  @property
@@ -96,13 +91,15 @@ class URI:
96
91
  def __str__(self) -> str:
97
92
  s = self.scheme + ":"
98
93
  if self.user is not None:
99
- s += self.user
94
+ s += urllib.parse.quote(self.user, safe=grammar.C_USER_SAFE)
100
95
  if self.password is not None:
101
- s += ":" + self.password
96
+ s += ":" + urllib.parse.quote(
97
+ self.password, safe=grammar.C_PASSWORD_SAFE
98
+ )
102
99
  s += "@"
103
100
  s += self.host
104
101
  if self.port is not None:
105
- s += ":" + str(self.port)
102
+ s += f":{self.port}"
106
103
  if self.parameters:
107
104
  s += ";" + str(self.parameters)
108
105
  return s
@@ -0,0 +1,68 @@
1
+ #
2
+ # Copyright (C) Spacinov SAS
3
+ # Distributed under the 2-clause BSD license
4
+ #
5
+
6
+ import dataclasses
7
+ import re
8
+
9
+ from . import grammar
10
+ from .parameters import Parameters
11
+
12
+ VIA_PATTERN = re.compile(
13
+ f"^SIP{grammar.SLASH}2\\.0{grammar.SLASH}(?P<transport>{grammar.TOKEN}) "
14
+ f"(?P<host>{grammar.HOST})"
15
+ f"(?::(?P<port>{grammar.PORT}))?"
16
+ f"(?P<parameters>(?:{grammar.SEMI}{grammar.GENERIC_PARAM})*)"
17
+ "$"
18
+ )
19
+
20
+
21
+ @dataclasses.dataclass
22
+ class Via:
23
+ """
24
+ A `Via` header, indicating a reponse location for a transaction.
25
+ """
26
+
27
+ transport: str
28
+ 'The transport for the transaction, e.g `"UDP"`, `"TCP"`, `"TLS"`.'
29
+
30
+ host: str
31
+ "The host to which responses should to be sent."
32
+
33
+ port: int | None = None
34
+ "The port to which responses should to be sent."
35
+
36
+ parameters: Parameters = dataclasses.field(default_factory=Parameters)
37
+ "The parameters of the address."
38
+
39
+ @classmethod
40
+ def parse(cls, value: str) -> "Via":
41
+ """
42
+ Parse the given string into a :class:`Via` instance.
43
+
44
+ If parsing fails, a :class:`ValueError` is raised.
45
+ """
46
+
47
+ value = grammar.simplify_whitespace(value)
48
+
49
+ m = VIA_PATTERN.match(value)
50
+ if m is None:
51
+ raise ValueError("Via is not valid")
52
+
53
+ port = m.group("port")
54
+
55
+ return cls(
56
+ transport=m.group("transport"),
57
+ host=m.group("host"),
58
+ port=int(port) if port else None,
59
+ parameters=Parameters.parse(m.group("parameters")),
60
+ )
61
+
62
+ def __str__(self) -> str:
63
+ s = f"SIP/2.0/{self.transport} {self.host}"
64
+ if self.port is not None:
65
+ s += f":{self.port}"
66
+ if self.parameters:
67
+ s += ";" + str(self.parameters)
68
+ return s
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: sipmessage
3
- Version: 0.2.2
3
+ Version: 0.3.0
4
4
  Summary: Pure python parsing of SIP messages and headers
5
5
  Author-email: Spacinov Engineering <opensource@spacinov.com>
6
6
  License: BSD License
@@ -9,6 +9,7 @@ docs/conf.py
9
9
  docs/index.rst
10
10
  src/sipmessage/__init__.py
11
11
  src/sipmessage/address.py
12
+ src/sipmessage/grammar.py
12
13
  src/sipmessage/parameters.py
13
14
  src/sipmessage/py.typed
14
15
  src/sipmessage/uri.py
@@ -0,0 +1,197 @@
1
+ #
2
+ # Copyright (C) Spacinov SAS
3
+ # Distributed under the 2-clause BSD license
4
+ #
5
+
6
+ import unittest
7
+
8
+ from sipmessage import URI, Address, Parameters
9
+
10
+
11
+ class AddressTest(unittest.TestCase):
12
+ def test_empty(self) -> None:
13
+ with self.assertRaises(ValueError) as cm:
14
+ Address.parse("")
15
+ self.assertEqual(str(cm.exception), "Address is not valid")
16
+
17
+ def test_invalid_uri(self) -> None:
18
+ with self.assertRaises(ValueError) as cm:
19
+ Address.parse("atlanta.com")
20
+ self.assertEqual(str(cm.exception), "URI is not valid")
21
+
22
+ def test_no_brackets(self) -> None:
23
+ contact = Address.parse("sip:+12125551212@phone2net.com;tag=887s")
24
+ self.assertEqual(
25
+ contact,
26
+ Address(
27
+ uri=URI(
28
+ scheme="sip",
29
+ user="+12125551212",
30
+ host="phone2net.com",
31
+ ),
32
+ parameters=Parameters({"tag": "887s"}),
33
+ ),
34
+ )
35
+ self.assertEqual(str(contact), "<sip:+12125551212@phone2net.com>;tag=887s")
36
+
37
+ def test_display_name_no_quotes(self) -> None:
38
+ contact = Address.parse(
39
+ "Anonymous User\t! <sip:c8oqz84zk7z@privacy.org>;tag=hyh8"
40
+ )
41
+ self.assertEqual(
42
+ contact,
43
+ Address(
44
+ name="Anonymous User !",
45
+ uri=URI(
46
+ scheme="sip",
47
+ user="c8oqz84zk7z",
48
+ host="privacy.org",
49
+ ),
50
+ parameters=Parameters({"tag": "hyh8"}),
51
+ ),
52
+ )
53
+ self.assertEqual(
54
+ str(contact), '"Anonymous User !" <sip:c8oqz84zk7z@privacy.org>;tag=hyh8'
55
+ )
56
+
57
+ def test_display_name_with_quotes(self) -> None:
58
+ contact = Address.parse('"Bob" <sips:bob@biloxi.com> ;tag=a48s')
59
+ self.assertEqual(
60
+ contact,
61
+ Address(
62
+ name="Bob",
63
+ uri=URI(
64
+ scheme="sips",
65
+ user="bob",
66
+ host="biloxi.com",
67
+ ),
68
+ parameters=Parameters({"tag": "a48s"}),
69
+ ),
70
+ )
71
+ self.assertEqual(str(contact), '"Bob" <sips:bob@biloxi.com>;tag=a48s')
72
+
73
+ def test_display_name_with_quotes_no_space(self) -> None:
74
+ contact = Address.parse('"Bob"<sips:bob@biloxi.com>;tag=a48s')
75
+ self.assertEqual(
76
+ contact,
77
+ Address(
78
+ name="Bob",
79
+ uri=URI(
80
+ scheme="sips",
81
+ user="bob",
82
+ host="biloxi.com",
83
+ ),
84
+ parameters=Parameters({"tag": "a48s"}),
85
+ ),
86
+ )
87
+ self.assertEqual(str(contact), '"Bob" <sips:bob@biloxi.com>;tag=a48s')
88
+
89
+ def test_display_name_with_quotes_escape(self) -> None:
90
+ contact = Address.parse(
91
+ '"Bob \\"foo\\" \\\\backslashes \\\\\\\\ <bar>" '
92
+ "<sips:bob@biloxi.com> ;tag=a48s"
93
+ )
94
+ self.assertEqual(
95
+ contact,
96
+ Address(
97
+ name='Bob "foo" \\backslashes \\\\ <bar>',
98
+ uri=URI(
99
+ scheme="sips",
100
+ user="bob",
101
+ host="biloxi.com",
102
+ ),
103
+ parameters=Parameters({"tag": "a48s"}),
104
+ ),
105
+ )
106
+ self.assertEqual(
107
+ str(contact),
108
+ '"Bob \\"foo\\" \\\\backslashes \\\\\\\\ <bar>" '
109
+ "<sips:bob@biloxi.com>;tag=a48s",
110
+ )
111
+
112
+ def test_with_parameter_without_value(self) -> None:
113
+ contact = Address.parse("<sip:1.2.3.4;lr>")
114
+ self.assertEqual(
115
+ contact,
116
+ Address(
117
+ uri=URI(
118
+ scheme="sip", host="1.2.3.4", parameters=Parameters({"lr": None})
119
+ )
120
+ ),
121
+ )
122
+ self.assertEqual(str(contact), "<sip:1.2.3.4;lr>")
123
+
124
+ def test_rfc4475_esc01_contact(self) -> None:
125
+ contact = Address.parse(
126
+ "<sip:cal%6Cer@host5.example.net;%6C%72;n%61me=v%61lue%25%34%31>"
127
+ )
128
+ self.assertEqual(
129
+ contact,
130
+ Address(
131
+ uri=URI(
132
+ scheme="sip",
133
+ user="caller",
134
+ host="host5.example.net",
135
+ parameters=Parameters({"lr": None, "name": "value%41"}),
136
+ ),
137
+ ),
138
+ )
139
+ self.assertEqual(
140
+ str(contact),
141
+ "<sip:caller@host5.example.net;lr;name=value%2541>",
142
+ )
143
+
144
+ def test_rfc4475_esc02_from(self) -> None:
145
+ contact = Address.parse('"%Z%45" <sip:resource@example.com>;tag=f232jadfj23')
146
+ self.assertEqual(
147
+ contact,
148
+ Address(
149
+ name="%Z%45",
150
+ uri=URI(
151
+ scheme="sip",
152
+ user="resource",
153
+ host="example.com",
154
+ ),
155
+ parameters=Parameters({"tag": "f232jadfj23"}),
156
+ ),
157
+ )
158
+ self.assertEqual(
159
+ str(contact), '"%Z%45" <sip:resource@example.com>;tag=f232jadfj23'
160
+ )
161
+
162
+ def test_rfc4475_escnull_from(self) -> None:
163
+ contact = Address.parse("sip:null-%00-null@example.com;tag=839923423")
164
+ self.assertEqual(
165
+ contact,
166
+ Address(
167
+ uri=URI(
168
+ scheme="sip",
169
+ host="example.com",
170
+ user="null-\x00-null",
171
+ ),
172
+ parameters=Parameters({"tag": "839923423"}),
173
+ ),
174
+ )
175
+ self.assertEqual(str(contact), "<sip:null-%00-null@example.com>;tag=839923423")
176
+
177
+ def test_rfc4475_intmeth_to(self) -> None:
178
+ contact = Address.parse(
179
+ '"BEL:\x07 NUL:\x00 DEL:\x7f" '
180
+ "<sip:1_unusual.URI~(to-be!sure)&isn't+it$/crazy?,/;;*@example.com>"
181
+ )
182
+ self.assertEqual(
183
+ contact,
184
+ Address(
185
+ name="BEL:\x07 NUL:\x00 DEL:\x7f",
186
+ uri=URI(
187
+ scheme="sip",
188
+ user="1_unusual.URI~(to-be!sure)&isn't+it$/crazy?,/;;*",
189
+ host="example.com",
190
+ ),
191
+ ),
192
+ )
193
+ self.assertEqual(
194
+ str(contact),
195
+ '"BEL:\x07 NUL:\x00 DEL:\x7f" '
196
+ "<sip:1_unusual.URI~(to-be!sure)&isn't+it$/crazy?,/;;*@example.com>",
197
+ )
@@ -0,0 +1,31 @@
1
+ #
2
+ # Copyright (C) Spacinov SAS
3
+ # Distributed under the 2-clause BSD license
4
+ #
5
+
6
+ import unittest
7
+
8
+ from sipmessage import Parameters
9
+
10
+
11
+ class ParametersTest(unittest.TestCase):
12
+ def test_empty(self) -> None:
13
+ for s in ["", ";;;", " ; ; ;"]:
14
+ parameters = Parameters.parse(s)
15
+ self.assertEqual(parameters, {})
16
+ self.assertEqual(str(parameters), "")
17
+
18
+ def test_escaped(self) -> None:
19
+ parameters = Parameters.parse("%6C%72;n%61me=v%61lue%25%34%31")
20
+ self.assertEqual(parameters, {"lr": None, "name": "value%41"})
21
+ self.assertEqual(str(parameters), "lr;name=value%2541")
22
+
23
+ def test_simple(self) -> None:
24
+ parameters = Parameters.parse("foo=1;bar")
25
+ self.assertEqual(parameters, {"foo": "1", "bar": None})
26
+ self.assertEqual(str(parameters), "foo=1;bar")
27
+
28
+ def test_spaces(self) -> None:
29
+ parameters = Parameters.parse("foo = 1 ; bar")
30
+ self.assertEqual(parameters, {"foo": "1", "bar": None})
31
+ self.assertEqual(str(parameters), "foo=1;bar")
@@ -13,27 +13,28 @@ class URITest(unittest.TestCase):
13
13
  # No port.
14
14
  with self.assertRaises(ValueError) as cm:
15
15
  URI.parse("sip:atlanta.com:")
16
- self.assertEqual(str(cm.exception), "URI port must be an integer")
16
+ self.assertEqual(str(cm.exception), "URI is not valid")
17
17
 
18
18
  # Invalid port.
19
19
  with self.assertRaises(ValueError) as cm:
20
20
  URI.parse("sip:atlanta.com:bob")
21
- self.assertEqual(str(cm.exception), "URI port must be an integer")
21
+ self.assertEqual(str(cm.exception), "URI is not valid")
22
22
 
23
23
  def test_invalid_scheme(self) -> None:
24
24
  # No scheme.
25
25
  with self.assertRaises(ValueError) as cm:
26
26
  URI.parse("atlanta.com")
27
- self.assertEqual(str(cm.exception), "URI scheme must be 'sip' or 'sips'")
27
+ self.assertEqual(str(cm.exception), "URI is not valid")
28
28
 
29
29
  # Invalid scheme.
30
30
  with self.assertRaises(ValueError) as cm:
31
31
  URI.parse("bogus:atlanta.com")
32
- self.assertEqual(str(cm.exception), "URI scheme must be 'sip' or 'sips'")
32
+ self.assertEqual(str(cm.exception), "URI is not valid")
33
33
 
34
34
  def test_host(self) -> None:
35
35
  uri = URI.parse("sip:atlanta.com")
36
36
 
37
+ self.assertEqual(uri.scheme, "sip")
37
38
  self.assertEqual(uri.host, "atlanta.com")
38
39
  self.assertEqual(uri.port, None)
39
40
  self.assertEqual(uri.user, None)
@@ -46,6 +47,7 @@ class URITest(unittest.TestCase):
46
47
  def test_host_and_parameter_without_value(self) -> None:
47
48
  uri = URI.parse("sip:1.2.3.4;lr")
48
49
 
50
+ self.assertEqual(uri.scheme, "sip")
49
51
  self.assertEqual(uri.host, "1.2.3.4")
50
52
  self.assertEqual(uri.port, None)
51
53
  self.assertEqual(uri.user, None)
@@ -58,9 +60,10 @@ class URITest(unittest.TestCase):
58
60
  self.assertEqual(uri.global_phone_number, None)
59
61
  self.assertEqual(str(uri), "sip:1.2.3.4;lr")
60
62
 
61
- def test_user_and_host(self) -> None:
63
+ def test_user(self) -> None:
62
64
  uri = URI.parse("sip:alice@atlanta.com")
63
65
 
66
+ self.assertEqual(uri.scheme, "sip")
64
67
  self.assertEqual(uri.host, "atlanta.com")
65
68
  self.assertEqual(uri.port, None)
66
69
  self.assertEqual(uri.user, "alice")
@@ -70,9 +73,35 @@ class URITest(unittest.TestCase):
70
73
  self.assertEqual(uri.global_phone_number, None)
71
74
  self.assertEqual(str(uri), "sip:alice@atlanta.com")
72
75
 
73
- def test_user_and_host_and_parameters(self) -> None:
76
+ def test_user_escaped(self) -> None:
77
+ uri = URI.parse("sip:sips%3Auser%40example.com@example.net")
78
+
79
+ self.assertEqual(uri.scheme, "sip")
80
+ self.assertEqual(uri.host, "example.net")
81
+ self.assertEqual(uri.port, None)
82
+ self.assertEqual(uri.user, "sips:user@example.com")
83
+ self.assertEqual(uri.password, None)
84
+ self.assertEqual(uri.parameters, {})
85
+
86
+ self.assertEqual(str(uri), "sip:sips%3Auser%40example.com@example.net")
87
+
88
+ def test_user_question_mark(self) -> None:
89
+ uri = URI.parse("sip:alice?@atlanta.com")
90
+
91
+ self.assertEqual(uri.scheme, "sip")
92
+ self.assertEqual(uri.host, "atlanta.com")
93
+ self.assertEqual(uri.port, None)
94
+ self.assertEqual(uri.user, "alice?")
95
+ self.assertEqual(uri.password, None)
96
+ self.assertEqual(uri.parameters, {})
97
+
98
+ self.assertEqual(uri.global_phone_number, None)
99
+ self.assertEqual(str(uri), "sip:alice?@atlanta.com")
100
+
101
+ def test_user_and_parameters(self) -> None:
74
102
  uri = URI.parse("sip:alice@atlanta.com;maddr=239.255.255.1;ttl=15")
75
103
 
104
+ self.assertEqual(uri.scheme, "sip")
76
105
  self.assertEqual(uri.host, "atlanta.com")
77
106
  self.assertEqual(uri.port, None)
78
107
  self.assertEqual(uri.user, "alice")
@@ -89,8 +118,9 @@ class URITest(unittest.TestCase):
89
118
  self.assertEqual(str(uri), "sip:alice@atlanta.com;maddr=239.255.255.1;ttl=15")
90
119
 
91
120
  def test_phone_identity(self) -> None:
92
- uri = URI.parse("sip:+123456789@1.2.3.4")
121
+ uri = URI.parse("sips:+123456789@1.2.3.4")
93
122
 
123
+ self.assertEqual(uri.scheme, "sips")
94
124
  self.assertEqual(uri.host, "1.2.3.4")
95
125
  self.assertEqual(uri.port, None)
96
126
  self.assertEqual(uri.user, "+123456789")
@@ -98,11 +128,12 @@ class URITest(unittest.TestCase):
98
128
  self.assertEqual(uri.parameters, {})
99
129
 
100
130
  self.assertEqual(uri.global_phone_number, "+123456789")
101
- self.assertEqual(str(uri), "sip:+123456789@1.2.3.4")
131
+ self.assertEqual(str(uri), "sips:+123456789@1.2.3.4")
102
132
 
103
133
  def test_full(self) -> None:
104
134
  uri = URI.parse("sip:alice:secret@atlanta.com:5060;maddr=239.255.255.1;ttl=15")
105
135
 
136
+ self.assertEqual(uri.scheme, "sip")
106
137
  self.assertEqual(uri.host, "atlanta.com")
107
138
  self.assertEqual(uri.port, 5060)
108
139
  self.assertEqual(uri.user, "alice")
@@ -119,3 +150,41 @@ class URITest(unittest.TestCase):
119
150
  self.assertEqual(
120
151
  str(uri), "sip:alice:secret@atlanta.com:5060;maddr=239.255.255.1;ttl=15"
121
152
  )
153
+
154
+ def test_password(self) -> None:
155
+ uri = URI.parse("sip:alice:secret@atlanta.com")
156
+
157
+ self.assertEqual(uri.scheme, "sip")
158
+ self.assertEqual(uri.host, "atlanta.com")
159
+ self.assertEqual(uri.port, None)
160
+ self.assertEqual(uri.user, "alice")
161
+ self.assertEqual(uri.password, "secret")
162
+ self.assertEqual(uri.parameters, {})
163
+
164
+ self.assertEqual(str(uri), "sip:alice:secret@atlanta.com")
165
+
166
+ def test_password_escaped(self) -> None:
167
+ uri = URI.parse("sip:alice:sips%3Auser%40example.com@atlanta.com")
168
+
169
+ self.assertEqual(uri.scheme, "sip")
170
+ self.assertEqual(uri.host, "atlanta.com")
171
+ self.assertEqual(uri.port, None)
172
+ self.assertEqual(uri.user, "alice")
173
+ self.assertEqual(uri.password, "sips:user@example.com")
174
+ self.assertEqual(uri.parameters, {})
175
+
176
+ self.assertEqual(str(uri), "sip:alice:sips%3Auser%40example.com@atlanta.com")
177
+
178
+ def test_rfc4475_wide_range_of_valid_characters(self) -> None:
179
+ uri = URI.parse(
180
+ "sip:1_unusual.URI~(to-be!sure)&isn't+it$/crazy?,/;;*:&it+has=1,weird!*pas$wo~d_too.(doesn't-it)@example.com"
181
+ )
182
+ self.assertEqual(uri.host, "example.com")
183
+ self.assertEqual(uri.port, None)
184
+ self.assertEqual(uri.user, "1_unusual.URI~(to-be!sure)&isn't+it$/crazy?,/;;*")
185
+ self.assertEqual(uri.password, "&it+has=1,weird!*pas$wo~d_too.(doesn't-it)")
186
+ self.assertEqual(uri.parameters, {})
187
+ self.assertEqual(
188
+ str(uri),
189
+ "sip:1_unusual.URI~(to-be!sure)&isn't+it$/crazy?,/;;*:&it+has=1,weird!*pas$wo~d_too.(doesn't-it)@example.com",
190
+ )
@@ -0,0 +1,113 @@
1
+ #
2
+ # Copyright (C) Spacinov SAS
3
+ # Distributed under the 2-clause BSD license
4
+ #
5
+
6
+ import unittest
7
+
8
+ from sipmessage import Parameters, Via
9
+
10
+
11
+ class ViaTest(unittest.TestCase):
12
+ def test_empty(self) -> None:
13
+ with self.assertRaises(ValueError) as cm:
14
+ Via.parse("")
15
+ self.assertEqual(str(cm.exception), "Via is not valid")
16
+
17
+ def test_hostname(self) -> None:
18
+ via = Via.parse(
19
+ "SIP/2.0/WSS T8trJdbBz7r6.invalid;branch=z9hG4bKYJHC9fb;"
20
+ "received=80.200.136.90;rport=49940"
21
+ )
22
+ self.assertEqual(
23
+ via,
24
+ Via(
25
+ transport="WSS",
26
+ host="T8trJdbBz7r6.invalid",
27
+ parameters=Parameters(
28
+ branch="z9hG4bKYJHC9fb",
29
+ received="80.200.136.90",
30
+ rport="49940",
31
+ ),
32
+ ),
33
+ )
34
+ self.assertEqual(
35
+ str(via),
36
+ "SIP/2.0/WSS T8trJdbBz7r6.invalid;branch=z9hG4bKYJHC9fb;"
37
+ "received=80.200.136.90;rport=49940",
38
+ )
39
+
40
+ def test_hostname_and_port(self) -> None:
41
+ via = Via.parse(
42
+ "SIP/2.0/WSS T8trJdbBz7r6.invalid:5060;branch=z9hG4bKYJHC9fb;"
43
+ "received=80.200.136.90;rport=49940"
44
+ )
45
+ self.assertEqual(
46
+ via,
47
+ Via(
48
+ transport="WSS",
49
+ host="T8trJdbBz7r6.invalid",
50
+ port=5060,
51
+ parameters=Parameters(
52
+ branch="z9hG4bKYJHC9fb",
53
+ received="80.200.136.90",
54
+ rport="49940",
55
+ ),
56
+ ),
57
+ )
58
+ self.assertEqual(
59
+ str(via),
60
+ "SIP/2.0/WSS T8trJdbBz7r6.invalid:5060;branch=z9hG4bKYJHC9fb;"
61
+ "received=80.200.136.90;rport=49940",
62
+ )
63
+
64
+ def test_ipv4(self) -> None:
65
+ via = Via.parse(
66
+ "SIP/2.0/WSS 1.2.3.4;branch=z9hG4bKYJHC9fb;"
67
+ "received=80.200.136.90;rport=49940"
68
+ )
69
+ self.assertEqual(
70
+ via,
71
+ Via(
72
+ transport="WSS",
73
+ host="1.2.3.4",
74
+ parameters=Parameters(
75
+ branch="z9hG4bKYJHC9fb",
76
+ received="80.200.136.90",
77
+ rport="49940",
78
+ ),
79
+ ),
80
+ )
81
+ self.assertEqual(
82
+ str(via),
83
+ "SIP/2.0/WSS 1.2.3.4;branch=z9hG4bKYJHC9fb;"
84
+ "received=80.200.136.90;rport=49940",
85
+ )
86
+
87
+ def test_rfc4475_intmeth(self) -> None:
88
+ via = Via.parse("SIP/2.0/TCP host1.example.com;branch=z9hG4bK-.!%66*_+`'~")
89
+ self.assertEqual(
90
+ via,
91
+ Via(
92
+ transport="TCP",
93
+ host="host1.example.com",
94
+ parameters=Parameters(branch="z9hG4bK-.!f*_+`'~"),
95
+ ),
96
+ )
97
+ # FIXME: this is unnecessarily quoted
98
+ self.assertEqual(
99
+ str(via), "SIP/2.0/TCP host1.example.com;branch=z9hG4bK-.%21f%2A_%2B%60%27~"
100
+ )
101
+
102
+ def test_rfc4475_wsinv(self) -> None:
103
+ via = Via.parse(
104
+ "SIP / 2.0 / UDP 192.168.255.111 ; branch = z9hG4bK30239"
105
+ )
106
+ self.assertEqual(
107
+ via,
108
+ Via(
109
+ transport="UDP",
110
+ host="192.168.255.111",
111
+ parameters=Parameters(branch="z9hG4bK30239"),
112
+ ),
113
+ )
@@ -1,50 +0,0 @@
1
- #
2
- # Copyright (C) Spacinov SAS
3
- # Distributed under the 2-clause BSD license
4
- #
5
-
6
- import dataclasses
7
- import re
8
-
9
- from .parameters import Parameters
10
-
11
-
12
- @dataclasses.dataclass
13
- class Via:
14
- """
15
- A `Via` header, indicating a reponse location for a transaction.
16
- """
17
-
18
- transport: str
19
- 'The transport for the transaction, e.g `"UDP"`, `"TCP"`, `"TLS"`.'
20
-
21
- address: str
22
- "The location where the response is to be sent."
23
-
24
- parameters: Parameters = dataclasses.field(default_factory=Parameters)
25
- "The parameters of the address."
26
-
27
- @classmethod
28
- def parse(cls, value: str) -> "Via":
29
- """
30
- Parse the given string into a :class:`Via` instance.
31
-
32
- If parsing fails, a :class:`ValueError` is raised.
33
- """
34
- m = re.match(r"SIP/2\.0/([A-Z]+) (.+)", value)
35
- if m is None:
36
- raise ValueError("Malformed Via header")
37
-
38
- transport = m.group(1)
39
- address, _, rest = m.group(2).partition(";")
40
- return cls(
41
- transport=transport,
42
- address=address,
43
- parameters=Parameters.parse(rest),
44
- )
45
-
46
- def __str__(self) -> str:
47
- s = "SIP/2.0/%s %s" % (self.transport, self.address)
48
- if self.parameters:
49
- s += ";" + str(self.parameters)
50
- return s
@@ -1,48 +0,0 @@
1
- #
2
- # Copyright (C) Spacinov SAS
3
- # Distributed under the 2-clause BSD license
4
- #
5
-
6
- import unittest
7
-
8
- from sipmessage import Address
9
-
10
-
11
- class AddressTest(unittest.TestCase):
12
- def test_empty(self) -> None:
13
- with self.assertRaises(ValueError) as cm:
14
- Address.parse("")
15
- self.assertEqual(str(cm.exception), "Not a valid address")
16
-
17
- def test_invalid_uri(self) -> None:
18
- with self.assertRaises(ValueError) as cm:
19
- Address.parse("atlanta.com")
20
- self.assertEqual(str(cm.exception), "URI scheme must be 'sip' or 'sips'")
21
-
22
- def test_no_brackets(self) -> None:
23
- contact = Address.parse("sip:+12125551212@phone2net.com;tag=887s")
24
- self.assertEqual(contact.name, "")
25
- self.assertEqual(str(contact.uri), "sip:+12125551212@phone2net.com")
26
- self.assertEqual(contact.parameters, {"tag": "887s"})
27
- self.assertEqual(str(contact), "<sip:+12125551212@phone2net.com>;tag=887s")
28
-
29
- def test_no_quotes(self) -> None:
30
- contact = Address.parse("Anonymous <sip:c8oqz84zk7z@privacy.org>;tag=hyh8")
31
- self.assertEqual(contact.name, "Anonymous")
32
- self.assertEqual(str(contact.uri), "sip:c8oqz84zk7z@privacy.org")
33
- self.assertEqual(contact.parameters, {"tag": "hyh8"})
34
- self.assertEqual(
35
- str(contact), '"Anonymous" <sip:c8oqz84zk7z@privacy.org>;tag=hyh8'
36
- )
37
-
38
- def test_with_parameter_without_value(self) -> None:
39
- contact = Address.parse("<sip:1.2.3.4;lr>")
40
- self.assertEqual(contact.name, "")
41
- self.assertEqual(str(contact.uri), "sip:1.2.3.4;lr")
42
-
43
- def test_with_quotes(self) -> None:
44
- contact = Address.parse('"Bob" <sips:bob@biloxi.com> ;tag=a48s')
45
- self.assertEqual(contact.name, "Bob")
46
- self.assertEqual(str(contact.uri), "sips:bob@biloxi.com")
47
- self.assertEqual(contact.parameters, {"tag": "a48s"})
48
- self.assertEqual(str(contact), '"Bob" <sips:bob@biloxi.com>;tag=a48s')
@@ -1,20 +0,0 @@
1
- #
2
- # Copyright (C) Spacinov SAS
3
- # Distributed under the 2-clause BSD license
4
- #
5
-
6
- import unittest
7
-
8
- from sipmessage import Parameters
9
-
10
-
11
- class ParametersTest(unittest.TestCase):
12
- def test_empty(self) -> None:
13
- parameters = Parameters.parse("")
14
- self.assertEqual(parameters, {})
15
- self.assertEqual(str(parameters), "")
16
-
17
- def test_not_empty(self) -> None:
18
- parameters = Parameters.parse("foo=1;bar")
19
- self.assertEqual(parameters, {"foo": "1", "bar": None})
20
- self.assertEqual(str(parameters), "foo=1;bar")
@@ -1,36 +0,0 @@
1
- #
2
- # Copyright (C) Spacinov SAS
3
- # Distributed under the 2-clause BSD license
4
- #
5
-
6
- import unittest
7
-
8
- from sipmessage import Via
9
-
10
-
11
- class ViaTest(unittest.TestCase):
12
- def test_empty(self) -> None:
13
- with self.assertRaises(ValueError) as cm:
14
- Via.parse("")
15
- self.assertEqual(str(cm.exception), "Malformed Via header")
16
-
17
- def test_parse(self) -> None:
18
- via = Via.parse(
19
- "SIP/2.0/WSS T8trJdbBz7r6.invalid;branch=z9hG4bKYJHC9fb;received=80.200.136.90;rport=49940"
20
- )
21
-
22
- self.assertEqual(via.transport, "WSS")
23
- self.assertEqual(via.address, "T8trJdbBz7r6.invalid")
24
- self.assertEqual(
25
- via.parameters,
26
- {
27
- "branch": "z9hG4bKYJHC9fb",
28
- "received": "80.200.136.90",
29
- "rport": "49940",
30
- },
31
- )
32
-
33
- self.assertEqual(
34
- str(via),
35
- "SIP/2.0/WSS T8trJdbBz7r6.invalid;branch=z9hG4bKYJHC9fb;received=80.200.136.90;rport=49940",
36
- )
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes