Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions Lib/http/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -846,12 +846,11 @@ def list_directory(self, path):
except UnicodeDecodeError:
displaypath = urllib.parse.unquote(self.path)
displaypath = html.escape(displaypath, quote=False)
enc = sys.getfilesystemencoding()
title = f'Directory listing for {displaypath}'
r.append('<!DOCTYPE HTML>')
r.append('<html lang="en">')
r.append('<head>')
r.append(f'<meta charset="{enc}">')
r.append('<meta charset="utf-8">')
r.append('<style type="text/css">\n:root {\ncolor-scheme: light dark;\n}\n</style>')
r.append(f'<title>{title}</title>\n</head>')
r.append(f'<body>\n<h1>{title}</h1>')
Expand All @@ -871,12 +870,12 @@ def list_directory(self, path):
errors='surrogatepass'),
html.escape(displayname, quote=False)))
r.append('</ul>\n<hr>\n</body>\n</html>\n')
encoded = '\n'.join(r).encode(enc, 'surrogateescape')
encoded = '\n'.join(r).encode('utf-8', 'surrogateescape')
f = io.BytesIO()
f.write(encoded)
f.seek(0)
self.send_response(HTTPStatus.OK)
self.send_header("Content-type", "text/html; charset=%s" % enc)
self.send_header("Content-type", "text/html; charset=utf-8")
self.send_header("Content-Length", str(len(encoded)))
self.end_headers()
return f
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Encode file paths with utf-8 in :meth:`http.SimpleHTTPRequestHandler.list_directory`.
Loading