Only normalize URIs when comparing
diff --git a/asn1crypto/_iri.py b/asn1crypto/_iri.py
index 57ddd40..7394b4d 100644
--- a/asn1crypto/_iri.py
+++ b/asn1crypto/_iri.py
@@ -34,13 +34,16 @@
)
-def iri_to_uri(value):
+def iri_to_uri(value, normalize=False):
"""
- Normalizes and encodes a unicode IRI into an ASCII byte string URI
+ Encodes a unicode IRI into an ASCII byte string URI
:param value:
A unicode string of an IRI
+ :param normalize:
+ A bool that controls URI normalization
+
:return:
A byte string of the ASCII-encoded URI
"""
@@ -91,7 +94,7 @@
if port is not None:
default_http = scheme == b'http' and port == b'80'
default_https = scheme == b'https' and port == b'443'
- if not default_http and not default_https:
+ if not normalize or (not default_http and not default_https):
netloc += b':' + port
# RFC 3986 allows a path to contain sub-delims, plus "@" and ":"
@@ -101,7 +104,7 @@
# RFC 3986 allows the fragment to contain sub-delims, plus "@", ":" , "/" and "?"
fragment = _urlquote(parsed.fragment, safe='/?!$&\'()*+,;=@:')
- if query is None and fragment is None and path == b'/':
+ if normalize and query is None and fragment is None and path == b'/':
path = None
# Python 2.7 compat
diff --git a/asn1crypto/x509.py b/asn1crypto/x509.py
index 428404c..938bb41 100644
--- a/asn1crypto/x509.py
+++ b/asn1crypto/x509.py
@@ -163,7 +163,7 @@
if not isinstance(other, URI):
return False
- return iri_to_uri(self.native) == iri_to_uri(other.native)
+ return iri_to_uri(self.native, True) == iri_to_uri(other.native, True)
def __unicode__(self):
"""
diff --git a/tests/test_x509.py b/tests/test_x509.py
index ded3b16..273cf12 100644
--- a/tests/test_x509.py
+++ b/tests/test_x509.py
@@ -176,6 +176,20 @@
self.assertEqual('https://example.com', u.__unicode__())
self.assertEqual(b'\x16\x13https://example.com', u.dump())
+ def test_uri_no_normalization(self):
+ u = x509.URI('https://example.com/')
+ self.assertEqual('https://example.com/', u.native)
+ self.assertEqual('https://example.com/', u.__unicode__())
+ self.assertEqual(b'\x16\x14https://example.com/', u.dump())
+ u2 = x509.URI('https://example.com')
+ self.assertEqual('https://example.com', u2.native)
+ self.assertEqual('https://example.com', u2.__unicode__())
+ self.assertEqual(b'\x16\x13https://example.com', u2.dump())
+ u3 = x509.URI('https://example.com:443/')
+ self.assertEqual('https://example.com:443/', u3.native)
+ self.assertEqual('https://example.com:443/', u3.__unicode__())
+ self.assertEqual(b'\x16\x18https://example.com:443/', u3.dump())
+
def test_indef_uri(self):
u = x509.URI.load(b'\x36\x80\x16\x07https:/\x16\x07/exampl\x16\x05e.com\x00\x00')
self.assertEqual('https://example.com', u.native)