cpython: f180a9156cc8 (original) (raw)
Mercurial > cpython
changeset 92125:f180a9156cc8 3.4
Issue #22165: SimpleHTTPRequestHandler now supports undecodable file names. [#22165]
Serhiy Storchaka storchaka@gmail.com | |
---|---|
date | Sun, 17 Aug 2014 08:22:11 +0300 |
parents | d51e739004bc |
children | 3153a400b739 a894b629bbea |
files | Lib/http/server.py Lib/test/test_httpservers.py Misc/NEWS |
diffstat | 3 files changed, 36 insertions(+), 4 deletions(-)[+] [-] Lib/http/server.py 19 Lib/test/test_httpservers.py 19 Misc/NEWS 2 |
line wrap: on
line diff
--- a/Lib/http/server.py +++ b/Lib/http/server.py @@ -747,7 +747,12 @@ class SimpleHTTPRequestHandler(BaseHTTPR return None list.sort(key=lambda a: a.lower()) r = []
displaypath = html.escape(urllib.parse.unquote(self.path))[](#l1.7)
try:[](#l1.8)
displaypath = urllib.parse.unquote(self.path,[](#l1.9)
errors='surrogatepass')[](#l1.10)
except UnicodeDecodeError:[](#l1.11)
displaypath = urllib.parse.unquote(path)[](#l1.12)
displaypath = html.escape(displaypath)[](#l1.13) enc = sys.getfilesystemencoding()[](#l1.14) title = 'Directory listing for %s' % displaypath[](#l1.15) r.append('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" '[](#l1.16)
@@ -769,9 +774,11 @@ class SimpleHTTPRequestHandler(BaseHTTPR displayname = name + "@" # Note: a link to a directory displays with @ and links with / r.append('
% (urllib.parse.quote(linkname), html.escape(displayname)))[](#l1.21)
% (urllib.parse.quote(linkname,[](#l1.22)
errors='surrogatepass'),[](#l1.23)
html.escape(displayname)))[](#l1.24) r.append('</ul>\n<hr>\n</body>\n</html>\n')[](#l1.25)
encoded = '\n'.join(r).encode(enc)[](#l1.26)
encoded = '\n'.join(r).encode(enc, 'surrogateescape')[](#l1.27) f = io.BytesIO()[](#l1.28) f.write(encoded)[](#l1.29) f.seek(0)[](#l1.30)
@@ -794,7 +801,11 @@ class SimpleHTTPRequestHandler(BaseHTTPR 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))[](#l1.35)
try:[](#l1.36)
path = urllib.parse.unquote(path, errors='surrogatepass')[](#l1.37)
except UnicodeDecodeError:[](#l1.38)
path = urllib.parse.unquote(path)[](#l1.39)
path = posixpath.normpath(path)[](#l1.40) words = path.split('/')[](#l1.41) words = filter(None, words)[](#l1.42) path = os.getcwd()[](#l1.43)
--- a/Lib/test/test_httpservers.py +++ b/Lib/test/test_httpservers.py @@ -14,6 +14,7 @@ import re import base64 import shutil import urllib.parse +import html import http.client import tempfile from io import BytesIO @@ -266,6 +267,24 @@ class SimpleHTTPServerTestCase(BaseTestC self.assertIsNotNone(response.reason) if data: self.assertEqual(data, body)
return body[](#l2.15)
- @unittest.skipUnless(support.TESTFN_UNDECODABLE,
'need support.TESTFN_UNDECODABLE')[](#l2.18)
- def test_undecodable_filename(self):
filename = os.fsdecode(support.TESTFN_UNDECODABLE) + '.txt'[](#l2.20)
with open(os.path.join(self.tempdir, filename), 'wb') as f:[](#l2.21)
f.write(support.TESTFN_UNDECODABLE)[](#l2.22)
response = self.request(self.tempdir_name + '/')[](#l2.23)
body = self.check_status_and_reason(response, 200)[](#l2.24)
quotedname = urllib.parse.quote(filename, errors='surrogatepass')[](#l2.25)
self.assertIn(('href="%s"' % quotedname)[](#l2.26)
.encode('utf-8', 'surrogateescape'), body)[](#l2.27)
self.assertIn(('>%s<' % html.escape(filename))[](#l2.28)
.encode('utf-8', 'surrogateescape'), body)[](#l2.29)
response = self.request(self.tempdir_name + '/' + quotedname)[](#l2.30)
self.check_status_and_reason(response, 200,[](#l2.31)
data=support.TESTFN_UNDECODABLE)[](#l2.32)
def test_get(self): #constructs the path relative to the root directory of the HTTPServer
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -27,6 +27,8 @@ Core and Builtins Library ------- +- Issue #22165: SimpleHTTPRequestHandler now supports undecodable file names. +