diff options
| author | blob8108 <blob8108@gmail.com> | 2014-01-05 23:12:25 +0000 |
|---|---|---|
| committer | blob8108 <blob8108@gmail.com> | 2014-01-05 23:12:25 +0000 |
| commit | 192bc9617b09fc584980191c14b02f6bdae2e583 (patch) | |
| tree | e16d7d4063b883ccf0fbebf611134459a2bad3f2 | |
| parent | a49a17bf5a60ea40117fd3e9fa7121e0be550126 (diff) | |
| download | blockext-192bc9617b09fc584980191c14b02f6bdae2e583.tar.gz blockext-192bc9617b09fc584980191c14b02f6bdae2e583.zip | |
Decode Unicode from Scratch inputs correctly
Fixes #3.
| -rw-r--r-- | blockext/__init__.py | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/blockext/__init__.py b/blockext/__init__.py index 9b2e4e5..776d447 100644 --- a/blockext/__init__.py +++ b/blockext/__init__.py @@ -7,12 +7,13 @@ import re import threading import time try: + from urllib import quote from urllib import unquote as _unquote_str def unquote(part): part = str(part) return _unquote_str(part).decode("utf-8") except ImportError: - from urllib.parse import unquote + from urllib.parse import unquote, quote try: from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler from SocketServer import ThreadingMixIn @@ -28,10 +29,21 @@ except ImportError: # TODO I'm sure this isn't right try: unicode + unichr except NameError: unicode = str + unichr = chr +def unquote_unicode(text): + def unicode_unquoter(match): + c = unichr(int(match.group(1), 16)) + # urlib.unquote doesn't like Unicode. + # So, we simply URL-encode the character again -- but this time, the + # "correct" way! + return quote(c.encode("utf-8")) + return re.sub(r'%u([0-9a-fA-F]{4})', unicode_unquoter, text) + class ThreadedHTTPServer(ThreadingMixIn, HTTPServer): """Handle requests in a separate thread.""" @@ -58,7 +70,7 @@ class Blockext(BaseHTTPRequestHandler): mime_type = "text/plain" path = self.path.split("/")[1:] - path = [unquote(p) for p in path] + path = [unquote(unquote_unicode(p)) for p in path] name = path[0] args = path[1:] |
