1# -*- coding: utf-8 -*-
2"""Converts an IRI to a URI."""
3
4__author__ = "Joe Gregorio (joe@bitworking.org)"
5__copyright__ = "Copyright 2006, Joe Gregorio"
6__contributors__ = []
7__version__ = "1.0.0"
8__license__ = "MIT"
9
10import urllib.parse
11
12# Convert an IRI to a URI following the rules in RFC 3987
13#
14# The characters we need to enocde and escape are defined in the spec:
15#
16# iprivate = %xE000-F8FF / %xF0000-FFFFD / %x100000-10FFFD
17# ucschar = %xA0-D7FF / %xF900-FDCF / %xFDF0-FFEF
18# / %x10000-1FFFD / %x20000-2FFFD / %x30000-3FFFD
19# / %x40000-4FFFD / %x50000-5FFFD / %x60000-6FFFD
20# / %x70000-7FFFD / %x80000-8FFFD / %x90000-9FFFD
21# / %xA0000-AFFFD / %xB0000-BFFFD / %xC0000-CFFFD
22# / %xD0000-DFFFD / %xE1000-EFFFD
23
24escape_range = [
25 (0xA0, 0xD7FF),
26 (0xE000, 0xF8FF),
27 (0xF900, 0xFDCF),
28 (0xFDF0, 0xFFEF),
29 (0x10000, 0x1FFFD),
30 (0x20000, 0x2FFFD),
31 (0x30000, 0x3FFFD),
32 (0x40000, 0x4FFFD),
33 (0x50000, 0x5FFFD),
34 (0x60000, 0x6FFFD),
35 (0x70000, 0x7FFFD),
36 (0x80000, 0x8FFFD),
37 (0x90000, 0x9FFFD),
38 (0xA0000, 0xAFFFD),
39 (0xB0000, 0xBFFFD),
40 (0xC0000, 0xCFFFD),
41 (0xD0000, 0xDFFFD),
42 (0xE1000, 0xEFFFD),
43 (0xF0000, 0xFFFFD),
44 (0x100000, 0x10FFFD),
45]
46
47
48def encode(c):
49 retval = c
50 i = ord(c)
51 for low, high in escape_range:
52 if i < low:
53 break
54 if i >= low and i <= high:
55 retval = "".join(["%%%2X" % o for o in c.encode("utf-8")])
56 break
57 return retval
58
59
60def iri2uri(uri):
61 """Convert an IRI to a URI. Note that IRIs must be
62 passed in a unicode strings. That is, do not utf-8 encode
63 the IRI before passing it into the function."""
64 if isinstance(uri, str):
65 (scheme, authority, path, query, fragment) = urllib.parse.urlsplit(uri)
66 authority = authority.encode("idna").decode("utf-8")
67 # For each character in 'ucschar' or 'iprivate'
68 # 1. encode as utf-8
69 # 2. then %-encode each octet of that utf-8
70 uri = urllib.parse.urlunsplit((scheme, authority, path, query, fragment))
71 uri = "".join([encode(c) for c in uri])
72 return uri
73
74
75if __name__ == "__main__":
76 import unittest
77
78 class Test(unittest.TestCase):
79 def test_uris(self):
80 """Test that URIs are invariant under the transformation."""
81 invariant = [
82 "ftp://ftp.is.co.za/rfc/rfc1808.txt",
83 "http://www.ietf.org/rfc/rfc2396.txt",
84 "ldap://[2001:db8::7]/c=GB?objectClass?one",
85 "mailto:John.Doe@example.com",
86 "news:comp.infosystems.www.servers.unix",
87 "tel:+1-816-555-1212",
88 "telnet://192.0.2.16:80/",
89 "urn:oasis:names:specification:docbook:dtd:xml:4.1.2",
90 ]
91 for uri in invariant:
92 self.assertEqual(uri, iri2uri(uri))
93
94 def test_iri(self):
95 """Test that the right type of escaping is done for each part of the URI."""
96 self.assertEqual(
97 "http://xn--o3h.com/%E2%98%84",
98 iri2uri("http://\N{COMET}.com/\N{COMET}"),
99 )
100 self.assertEqual(
101 "http://bitworking.org/?fred=%E2%98%84",
102 iri2uri("http://bitworking.org/?fred=\N{COMET}"),
103 )
104 self.assertEqual(
105 "http://bitworking.org/#%E2%98%84",
106 iri2uri("http://bitworking.org/#\N{COMET}"),
107 )
108 self.assertEqual("#%E2%98%84", iri2uri("#\N{COMET}"))
109 self.assertEqual(
110 "/fred?bar=%E2%98%9A#%E2%98%84",
111 iri2uri("/fred?bar=\N{BLACK LEFT POINTING INDEX}#\N{COMET}"),
112 )
113 self.assertEqual(
114 "/fred?bar=%E2%98%9A#%E2%98%84",
115 iri2uri(iri2uri("/fred?bar=\N{BLACK LEFT POINTING INDEX}#\N{COMET}")),
116 )
117 self.assertNotEqual(
118 "/fred?bar=%E2%98%9A#%E2%98%84",
119 iri2uri(
120 "/fred?bar=\N{BLACK LEFT POINTING INDEX}#\N{COMET}".encode("utf-8")
121 ),
122 )
123
124 unittest.main()