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