sipmessage 0.2.1__tar.gz → 0.2.3__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 (29) hide show
  1. {sipmessage-0.2.1/src/sipmessage.egg-info → sipmessage-0.2.3}/PKG-INFO +1 -1
  2. {sipmessage-0.2.1 → sipmessage-0.2.3}/pyproject.toml +1 -1
  3. {sipmessage-0.2.1 → sipmessage-0.2.3}/src/sipmessage/address.py +30 -20
  4. {sipmessage-0.2.1 → sipmessage-0.2.3}/src/sipmessage/uri.py +11 -1
  5. {sipmessage-0.2.1 → sipmessage-0.2.3}/src/sipmessage/via.py +2 -0
  6. {sipmessage-0.2.1 → sipmessage-0.2.3/src/sipmessage.egg-info}/PKG-INFO +1 -1
  7. sipmessage-0.2.3/tests/test_address.py +69 -0
  8. {sipmessage-0.2.1 → sipmessage-0.2.3}/tests/test_uri.py +22 -0
  9. sipmessage-0.2.1/tests/test_address.py +0 -43
  10. {sipmessage-0.2.1 → sipmessage-0.2.3}/LICENSE +0 -0
  11. {sipmessage-0.2.1 → sipmessage-0.2.3}/MANIFEST.in +0 -0
  12. {sipmessage-0.2.1 → sipmessage-0.2.3}/Makefile +0 -0
  13. {sipmessage-0.2.1 → sipmessage-0.2.3}/README.rst +0 -0
  14. {sipmessage-0.2.1 → sipmessage-0.2.3}/docs/Makefile +0 -0
  15. {sipmessage-0.2.1 → sipmessage-0.2.3}/docs/conf.py +0 -0
  16. {sipmessage-0.2.1 → sipmessage-0.2.3}/docs/index.rst +0 -0
  17. {sipmessage-0.2.1 → sipmessage-0.2.3}/setup.cfg +0 -0
  18. {sipmessage-0.2.1 → sipmessage-0.2.3}/setup.py +0 -0
  19. {sipmessage-0.2.1 → sipmessage-0.2.3}/src/sipmessage/__init__.py +0 -0
  20. {sipmessage-0.2.1 → sipmessage-0.2.3}/src/sipmessage/parameters.py +0 -0
  21. {sipmessage-0.2.1 → sipmessage-0.2.3}/src/sipmessage/py.typed +0 -0
  22. {sipmessage-0.2.1 → sipmessage-0.2.3}/src/sipmessage.egg-info/SOURCES.txt +0 -0
  23. {sipmessage-0.2.1 → sipmessage-0.2.3}/src/sipmessage.egg-info/dependency_links.txt +0 -0
  24. {sipmessage-0.2.1 → sipmessage-0.2.3}/src/sipmessage.egg-info/requires.txt +0 -0
  25. {sipmessage-0.2.1 → sipmessage-0.2.3}/src/sipmessage.egg-info/top_level.txt +0 -0
  26. {sipmessage-0.2.1 → sipmessage-0.2.3}/tests/__init__.py +0 -0
  27. {sipmessage-0.2.1 → sipmessage-0.2.3}/tests/test_parameters.py +0 -0
  28. {sipmessage-0.2.1 → sipmessage-0.2.3}/tests/test_public_api.py +0 -0
  29. {sipmessage-0.2.1 → sipmessage-0.2.3}/tests/test_via.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: sipmessage
3
- Version: 0.2.1
3
+ Version: 0.2.3
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.1"
8
+ version = "0.2.3"
9
9
 
10
10
  # Dependencies
11
11
  dependencies = []
@@ -9,27 +9,19 @@ import re
9
9
  from .parameters import Parameters
10
10
  from .uri import URI
11
11
 
12
+ TOKEN_LWS = r"[a-zA-Z0-9\-.!%*_+`'~ ]*"
13
+ QUOTED_STRING = '"(?:[^"]|\\")*"'
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
+ "^(?P<name>" + TOKEN_LWS + "|" + QUOTED_STRING + ")"
30
20
  # LAQUOT addr-spec RAQUOT
21
+ "[ ]*"
31
22
  "<(?P<uri>[^>]+)>"
32
- "[ \t]*"
23
+ # *(SEMI contact-params)
24
+ "[ ]*"
33
25
  r"(?:;(?P<parameters>[^\?]*))?"
34
26
  ),
35
27
  # addr-spec *(SEMI contact-params)
@@ -37,13 +29,25 @@ ADDRESS_PATTERNS = [
37
29
  # no name
38
30
  "^(?P<name>)"
39
31
  # addr-spec
40
- "(?P<uri>[^ \t;]+)"
41
- "[ \t]*"
32
+ "(?P<uri>[^ ;]+)"
33
+ # *(SEMI contact-params)
34
+ "[ ]*"
42
35
  r"(?:;(?P<parameters>[^\?]*))?"
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
  """
@@ -63,13 +67,19 @@ class Address:
63
67
  def parse(cls, value: str) -> "Address":
64
68
  """
65
69
  Parse the given string into an :class:`Address` instance.
70
+
71
+ If parsing fails, a :class:`ValueError` is raised.
66
72
  """
73
+
74
+ # All linear white space, including folding, has the same semantics as SP.
75
+ value = re.sub(r"\s+", " ", value).strip()
76
+
67
77
  for pattern in ADDRESS_PATTERNS:
68
78
  m = pattern.match(value)
69
79
  if m:
70
80
  return cls(
71
81
  uri=URI.parse(m.group("uri")),
72
- name=m.group("name"),
82
+ name=unquote(m.group("name").strip()),
73
83
  parameters=Parameters.parse(m.group("parameters")),
74
84
  )
75
85
  else:
@@ -78,7 +88,7 @@ class Address:
78
88
  def __str__(self) -> str:
79
89
  s = ""
80
90
  if self.name:
81
- s += '"%s" ' % self.name
91
+ s += quote(self.name) + " "
82
92
  s += "<%s>" % self.uri
83
93
  if self.parameters:
84
94
  s += ";" + str(self.parameters)
@@ -37,8 +37,15 @@ class URI:
37
37
  def parse(cls, value: str) -> "URI":
38
38
  """
39
39
  Parse the given string into a :class:`URI` instance.
40
+
41
+ If parsing fails, a :class:`ValueError` is raised.
40
42
  """
41
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
+
42
49
  if "@" in parsed.path:
43
50
  user_password, host_port = parsed.path.split("@")
44
51
  if ":" in user_password:
@@ -52,7 +59,10 @@ class URI:
52
59
  password = None
53
60
  if ":" in host_port:
54
61
  host, port_ = host_port.split(":")
55
- port = int(port_)
62
+ try:
63
+ port = int(port_)
64
+ except ValueError:
65
+ raise ValueError("URI port must be an integer")
56
66
  else:
57
67
  host = host_port
58
68
  port = None
@@ -28,6 +28,8 @@ class Via:
28
28
  def parse(cls, value: str) -> "Via":
29
29
  """
30
30
  Parse the given string into a :class:`Via` instance.
31
+
32
+ If parsing fails, a :class:`ValueError` is raised.
31
33
  """
32
34
  m = re.match(r"SIP/2\.0/([A-Z]+) (.+)", value)
33
35
  if m is None:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: sipmessage
3
- Version: 0.2.1
3
+ Version: 0.2.3
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
@@ -0,0 +1,69 @@
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_display_name_no_quotes(self) -> None:
30
+ contact = Address.parse(
31
+ "Anonymous User\t! <sip:c8oqz84zk7z@privacy.org>;tag=hyh8"
32
+ )
33
+ self.assertEqual(contact.name, "Anonymous User !")
34
+ self.assertEqual(str(contact.uri), "sip:c8oqz84zk7z@privacy.org")
35
+ self.assertEqual(contact.parameters, {"tag": "hyh8"})
36
+ self.assertEqual(
37
+ str(contact), '"Anonymous User !" <sip:c8oqz84zk7z@privacy.org>;tag=hyh8'
38
+ )
39
+
40
+ def test_display_name_with_quotes(self) -> None:
41
+ contact = Address.parse('"Bob" <sips:bob@biloxi.com> ;tag=a48s')
42
+ self.assertEqual(contact.name, "Bob")
43
+ self.assertEqual(str(contact.uri), "sips:bob@biloxi.com")
44
+ self.assertEqual(contact.parameters, {"tag": "a48s"})
45
+ self.assertEqual(str(contact), '"Bob" <sips:bob@biloxi.com>;tag=a48s')
46
+
47
+ def test_display_name_with_quotes_no_space(self) -> None:
48
+ contact = Address.parse('"Bob"<sips:bob@biloxi.com>;tag=a48s')
49
+ self.assertEqual(contact.name, "Bob")
50
+ self.assertEqual(str(contact.uri), "sips:bob@biloxi.com")
51
+ self.assertEqual(contact.parameters, {"tag": "a48s"})
52
+ self.assertEqual(str(contact), '"Bob" <sips:bob@biloxi.com>;tag=a48s')
53
+
54
+ def test_display_name_with_quotes_escape(self) -> None:
55
+ contact = Address.parse(
56
+ '"Bob \\"foo\\" \\\\backslashes \\\\\\\\ <bar>" <sips:bob@biloxi.com> ;tag=a48s'
57
+ )
58
+ self.assertEqual(contact.name, 'Bob "foo" \\backslashes \\\\ <bar>')
59
+ self.assertEqual(str(contact.uri), "sips:bob@biloxi.com")
60
+ self.assertEqual(contact.parameters, {"tag": "a48s"})
61
+ self.assertEqual(
62
+ str(contact),
63
+ '"Bob \\"foo\\" \\\\backslashes \\\\\\\\ <bar>" <sips:bob@biloxi.com>;tag=a48s',
64
+ )
65
+
66
+ def test_with_parameter_without_value(self) -> None:
67
+ contact = Address.parse("<sip:1.2.3.4;lr>")
68
+ self.assertEqual(contact.name, "")
69
+ self.assertEqual(str(contact.uri), "sip:1.2.3.4;lr")
@@ -9,6 +9,28 @@ from sipmessage import URI
9
9
 
10
10
 
11
11
  class URITest(unittest.TestCase):
12
+ def test_invalid_port(self) -> None:
13
+ # No port.
14
+ with self.assertRaises(ValueError) as cm:
15
+ URI.parse("sip:atlanta.com:")
16
+ self.assertEqual(str(cm.exception), "URI port must be an integer")
17
+
18
+ # Invalid port.
19
+ with self.assertRaises(ValueError) as cm:
20
+ URI.parse("sip:atlanta.com:bob")
21
+ self.assertEqual(str(cm.exception), "URI port must be an integer")
22
+
23
+ def test_invalid_scheme(self) -> None:
24
+ # No scheme.
25
+ with self.assertRaises(ValueError) as cm:
26
+ URI.parse("atlanta.com")
27
+ self.assertEqual(str(cm.exception), "URI scheme must be 'sip' or 'sips'")
28
+
29
+ # Invalid scheme.
30
+ with self.assertRaises(ValueError) as cm:
31
+ URI.parse("bogus:atlanta.com")
32
+ self.assertEqual(str(cm.exception), "URI scheme must be 'sip' or 'sips'")
33
+
12
34
  def test_host(self) -> None:
13
35
  uri = URI.parse("sip:atlanta.com")
14
36
 
@@ -1,43 +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_no_brackets(self) -> None:
18
- contact = Address.parse("sip:+12125551212@phone2net.com;tag=887s")
19
- self.assertEqual(contact.name, "")
20
- self.assertEqual(str(contact.uri), "sip:+12125551212@phone2net.com")
21
- self.assertEqual(contact.parameters, {"tag": "887s"})
22
- self.assertEqual(str(contact), "<sip:+12125551212@phone2net.com>;tag=887s")
23
-
24
- def test_no_quotes(self) -> None:
25
- contact = Address.parse("Anonymous <sip:c8oqz84zk7z@privacy.org>;tag=hyh8")
26
- self.assertEqual(contact.name, "Anonymous")
27
- self.assertEqual(str(contact.uri), "sip:c8oqz84zk7z@privacy.org")
28
- self.assertEqual(contact.parameters, {"tag": "hyh8"})
29
- self.assertEqual(
30
- str(contact), '"Anonymous" <sip:c8oqz84zk7z@privacy.org>;tag=hyh8'
31
- )
32
-
33
- def test_with_parameter_without_value(self) -> None:
34
- contact = Address.parse("<sip:1.2.3.4;lr>")
35
- self.assertEqual(contact.name, "")
36
- self.assertEqual(str(contact.uri), "sip:1.2.3.4;lr")
37
-
38
- def test_with_quotes(self) -> None:
39
- contact = Address.parse('"Bob" <sips:bob@biloxi.com> ;tag=a48s')
40
- self.assertEqual(contact.name, "Bob")
41
- self.assertEqual(str(contact.uri), "sips:bob@biloxi.com")
42
- self.assertEqual(contact.parameters, {"tag": "a48s"})
43
- self.assertEqual(str(contact), '"Bob" <sips:bob@biloxi.com>;tag=a48s')
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
File without changes