diff options
author | Vlastimil Zíma <vlastimil.zima@nic.cz> | 2018-03-12 09:23:07 +0100 |
---|---|---|
committer | Vlastimil Zíma <vlastimil.zima@nic.cz> | 2018-04-30 14:28:00 +0200 |
commit | cbc56b0104f29cb5cc344fa3f4d18e18cefb127d (patch) | |
tree | 0035167f77e696b6199f83a936548e9bd7fe3d34 /openid | |
parent | d95ddc43ab5adc221e3d7ea4215acc585934e300 (diff) | |
download | openid-cbc56b0104f29cb5cc344fa3f4d18e18cefb127d.tar.gz |
Refactor IRI to URI
Diffstat (limited to 'openid')
-rw-r--r-- | openid/test/test_xri.py | 17 | ||||
-rw-r--r-- | openid/yadis/xri.py | 71 |
2 files changed, 24 insertions, 64 deletions
diff --git a/openid/test/test_xri.py b/openid/test/test_xri.py index a5f0bfa..341472e 100644 --- a/openid/test/test_xri.py +++ b/openid/test/test_xri.py @@ -32,19 +32,10 @@ class XriTransformationTestCase(TestCase): def test_to_iri_normal(self): self.assertEqual(xri.toIRINormal('@example'), 'xri://@example') - try: - unichr(0x10000) - except ValueError: - # bleh narrow python build - def test_iri_to_url(self): - s = u'l\xa1m' - expected = 'l%C2%A1m' - self.assertEqual(xri.iriToURI(s), expected) - else: - def test_iri_to_url(self): - s = u'l\xa1m\U00101010n' - expected = 'l%C2%A1m%F4%81%80%90n' - self.assertEqual(xri.iriToURI(s), expected) + def test_iri_to_url(self): + s = u'l\xa1m\U00101010n' + expected = 'l%C2%A1m%F4%81%80%90n' + self.assertEqual(xri.iriToURI(s), expected) class CanonicalIDTest(TestCase): diff --git a/openid/yadis/xri.py b/openid/yadis/xri.py index 60e0675..73ad798 100644 --- a/openid/yadis/xri.py +++ b/openid/yadis/xri.py @@ -4,53 +4,15 @@ @see: XRI Syntax v2.0 at the U{OASIS XRI Technical Committee<http://www.oasis-open.org/committees/tc_home.php?wg_abbrev=xri>} """ - import re +import warnings +from urllib import quote -XRI_AUTHORITIES = ['!', '=', '@', '+', '$', '('] +import six -try: - unichr(0x10000) -except ValueError: - # narrow python build - UCSCHAR = [ - (0xA0, 0xD7FF), - (0xF900, 0xFDCF), - (0xFDF0, 0xFFEF), - ] - - IPRIVATE = [ - (0xE000, 0xF8FF), - ] -else: - UCSCHAR = [ - (0xA0, 0xD7FF), - (0xF900, 0xFDCF), - (0xFDF0, 0xFFEF), - (0x10000, 0x1FFFD), - (0x20000, 0x2FFFD), - (0x30000, 0x3FFFD), - (0x40000, 0x4FFFD), - (0x50000, 0x5FFFD), - (0x60000, 0x6FFFD), - (0x70000, 0x7FFFD), - (0x80000, 0x8FFFD), - (0x90000, 0x9FFFD), - (0xA0000, 0xAFFFD), - (0xB0000, 0xBFFFD), - (0xC0000, 0xCFFFD), - (0xD0000, 0xDFFFD), - (0xE1000, 0xEFFFD), - ] - - IPRIVATE = [ - (0xE000, 0xF8FF), - (0xF0000, 0xFFFFD), - (0x100000, 0x10FFFD), - ] - - -_escapeme_re = re.compile('[%s]' % ''.join(u'%s-%s' % (unichr(m_n[0]), unichr(m_n[1])) for m_n in UCSCHAR + IPRIVATE)) +from openid.urinorm import GEN_DELIMS, PERCENT_ENCODING_CHARACTER, SUB_DELIMS + +XRI_AUTHORITIES = ['!', '=', '@', '+', '$', '('] def identifierScheme(identifier): @@ -96,15 +58,22 @@ def toURINormal(xri): return iriToURI(toIRINormal(xri)) -def _percentEscapeUnicode(char_match): - c = char_match.group() - return ''.join(['%%%X' % (ord(octet),) for octet in c.encode('utf-8')]) +def iriToURI(iri): + """Transform an IRI to a URI by escaping unicode. + According to RFC 3987, section 3.1, "Mapping of IRIs to URIs" -def iriToURI(iri): - """Transform an IRI to a URI by escaping unicode.""" - # According to RFC 3987, section 3.1, "Mapping of IRIs to URIs" - return _escapeme_re.sub(_percentEscapeUnicode, iri) + @type iri: six.text_type, six.binary_type deprecated. + @rtype: six.text_type + """ + # Transform the input to the binary string. `quote` doesn't quote correctly unicode strings. + if isinstance(iri, six.text_type): + iri = iri.encode('utf-8') + else: + assert isinstance(iri, six.binary_type) + warnings.warn("Binary input for iriToURI is deprecated. Use text input instead.", DeprecationWarning) + + return quote(iri, (GEN_DELIMS + SUB_DELIMS + PERCENT_ENCODING_CHARACTER).encode('utf-8')).decode('utf-8') def providerIsAuthoritative(providerID, canonicalID): |