gh-91219: Add an index_pages default list and parameter to SimpleHTTPRequestHandler (GH-31985)

* Add an index_pages default list to SimpleHTTPRequestHandler and an
optional constructor parameter that allows the default indexes pages
list to be overridden.  This makes it easy to set a new index page name
without having to override send_head.
This commit is contained in:
Myron Walker 2022-06-23 16:30:44 -04:00 committed by GitHub
parent 4e796f5646
commit 9a95fa9267
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 2 deletions

View file

@ -642,6 +642,7 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
""" """
index_pages = ["index.html", "index.htm"]
server_version = "SimpleHTTP/" + __version__ server_version = "SimpleHTTP/" + __version__
extensions_map = _encodings_map_default = { extensions_map = _encodings_map_default = {
'.gz': 'application/gzip', '.gz': 'application/gzip',
@ -650,9 +651,11 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
'.xz': 'application/x-xz', '.xz': 'application/x-xz',
} }
def __init__(self, *args, directory=None, **kwargs): def __init__(self, *args, directory=None, index_pages=None, **kwargs):
if directory is None: if directory is None:
directory = os.getcwd() directory = os.getcwd()
if index_pages is not None:
self.index_pages = index_pages
self.directory = os.fspath(directory) self.directory = os.fspath(directory)
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
@ -696,7 +699,7 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
self.send_header("Content-Length", "0") self.send_header("Content-Length", "0")
self.end_headers() self.end_headers()
return None return None
for index in "index.html", "index.htm": for index in self.index_pages:
index = os.path.join(path, index) index = os.path.join(path, index)
if os.path.exists(index): if os.path.exists(index):
path = index path = index

View file

@ -0,0 +1 @@
Add an index_pages parameter to support using non-default index page names.