Skip to content

bpo-32628: support DirectoryIndex for http server #5308

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions Doc/library/http.server.rst
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,14 @@ of which this module provides three different variants:

If not specified, the directory to serve is the current working directory.

.. attribute:: directory_index

A list of files to potentially serve for a request for a directory,
rather than the set of files in the directory. The default list
is ``index.html`` and ``index.htm``.

.. versionadded:: 3.7

The :class:`SimpleHTTPRequestHandler` class defines the following methods:

.. method:: do_HEAD()
Expand Down
3 changes: 2 additions & 1 deletion Lib/http/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ def send_head(self):
self.send_header("Location", new_url)
self.end_headers()
return None
for index in "index.html", "index.htm":
for index in self.directory_index:
index = os.path.join(path, index)
if os.path.exists(index):
path = index
Expand Down Expand Up @@ -872,6 +872,7 @@ def guess_type(self, path):
'.c': 'text/plain',
'.h': 'text/plain',
})
directory_index = ["index.html", "index.htm"]


# Utilities for CGIHTTPRequestHandler
Expand Down
21 changes: 21 additions & 0 deletions Lib/test/test_httpservers.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ class SimpleHTTPServerTestCase(BaseTestCase):
class request_handler(NoLogRequestHandler, SimpleHTTPRequestHandler):
pass

request_handler.directory_index.append("index.test")
def setUp(self):
BaseTestCase.setUp(self)
self.cwd = os.getcwd()
Expand Down Expand Up @@ -440,6 +441,26 @@ def test_get(self):
f.write(data)
response = self.request(self.base_url + '/')
self.check_status_and_reason(response, HTTPStatus.OK, data)
os.remove(os.path.join(self.tempdir_name, 'index.html'))

# index.test was added at handler setup as a non-standard
# directory_index, check to make sure that if present we see
# an index file handled at the directory
with open(os.path.join(self.tempdir_name, 'index.test'), 'wb') as f:
f.write(data)
response = self.request(self.base_url + '/')
self.check_status_and_reason(response, HTTPStatus.OK, data)
os.remove(os.path.join(self.tempdir_name, 'index.test'))

# index.missing is not a standard index nor on the directory_index
# so we should see a list of files when asking for this directory
# and we should not see the contents of the index file
with open(os.path.join(self.tempdir_name, 'index.missing'), 'wb') as f:
f.write(data)
response = self.request(self.base_url + '/')
body = self.check_status_and_reason(response, HTTPStatus.OK)
self.assertIn(b'index.missing', body)
self.assertNotIn(b'Dummy', body)

# chmod() doesn't work as expected on Windows, and filesystem
# permissions are ignored by root on Unix.
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,7 @@ Gaël Pasgrimaud
Feanil Patel
Ashish Nitin Patil
Alecsandru Patrascu
Erik Paulson
Randy Pausch
Samuele Pedroni
Justin Peel
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add directory_index list to SimpleHTTPRequestHandler to support more than
index.html and index.htm