diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2014-08-17 05:22:11 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2014-08-17 05:22:11 (GMT) |
commit | cb5bc408ad525c695b00bf78fdf69c24028fed96 (patch) | |
tree | ab375d8444a09cb88f37162dd1730f2662c643b7 /Lib/http/server.py | |
parent | 402df0975c1adacdc3673b9308ec759a557f6b4b (diff) | |
download | cpython-cb5bc408ad525c695b00bf78fdf69c24028fed96.zip cpython-cb5bc408ad525c695b00bf78fdf69c24028fed96.tar.gz cpython-cb5bc408ad525c695b00bf78fdf69c24028fed96.tar.bz2 |
Issue #22165: SimpleHTTPRequestHandler now supports undecodable file names.
Diffstat (limited to 'Lib/http/server.py')
-rw-r--r-- | Lib/http/server.py | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/Lib/http/server.py b/Lib/http/server.py index 2a95028..f916fdd 100644 --- a/Lib/http/server.py +++ b/Lib/http/server.py @@ -747,7 +747,12 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler): return None list.sort(key=lambda a: a.lower()) r = [] - displaypath = html.escape(urllib.parse.unquote(self.path)) + try: + displaypath = urllib.parse.unquote(self.path, + errors='surrogatepass') + except UnicodeDecodeError: + displaypath = urllib.parse.unquote(path) + displaypath = html.escape(displaypath) enc = sys.getfilesystemencoding() title = 'Directory listing for %s' % displaypath r.append('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" ' @@ -769,9 +774,11 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler): displayname = name + "@" # Note: a link to a directory displays with @ and links with / r.append('<li><a href="%s">%s</a></li>' - % (urllib.parse.quote(linkname), html.escape(displayname))) + % (urllib.parse.quote(linkname, + errors='surrogatepass'), + html.escape(displayname))) r.append('</ul>\n<hr>\n</body>\n</html>\n') - encoded = '\n'.join(r).encode(enc) + encoded = '\n'.join(r).encode(enc, 'surrogateescape') f = io.BytesIO() f.write(encoded) f.seek(0) @@ -794,7 +801,11 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler): path = path.split('#',1)[0] # Don't forget explicit trailing slash when normalizing. Issue17324 trailing_slash = path.rstrip().endswith('/') - path = posixpath.normpath(urllib.parse.unquote(path)) + try: + path = urllib.parse.unquote(path, errors='surrogatepass') + except UnicodeDecodeError: + path = urllib.parse.unquote(path) + path = posixpath.normpath(path) words = path.split('/') words = filter(None, words) path = os.getcwd() |