diff --git a/Doc/library/http.server.rst b/Doc/library/http.server.rst index c98843de02cba3..6c3848bce4eb6d 100644 --- a/Doc/library/http.server.rst +++ b/Doc/library/http.server.rst @@ -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() diff --git a/Lib/http/server.py b/Lib/http/server.py index 502bce0c7a40da..8ab6bd0c3fa552 100644 --- a/Lib/http/server.py +++ b/Lib/http/server.py @@ -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 @@ -872,6 +872,7 @@ def guess_type(self, path): '.c': 'text/plain', '.h': 'text/plain', }) + directory_index = ["index.html", "index.htm"] # Utilities for CGIHTTPRequestHandler diff --git a/Lib/test/test_httpservers.py b/Lib/test/test_httpservers.py index cc829a522b88bc..f78a7415517cda 100644 --- a/Lib/test/test_httpservers.py +++ b/Lib/test/test_httpservers.py @@ -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() @@ -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. diff --git a/Misc/ACKS b/Misc/ACKS index 900604ca4c6a13..c6c4d208375036 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -1189,6 +1189,7 @@ Gaƫl Pasgrimaud Feanil Patel Ashish Nitin Patil Alecsandru Patrascu +Erik Paulson Randy Pausch Samuele Pedroni Justin Peel diff --git a/Misc/NEWS.d/next/Library/2018-01-25-03-55-28.bpo-32628.ize0Jq.rst b/Misc/NEWS.d/next/Library/2018-01-25-03-55-28.bpo-32628.ize0Jq.rst new file mode 100644 index 00000000000000..fb623bf0579bd4 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-01-25-03-55-28.bpo-32628.ize0Jq.rst @@ -0,0 +1,2 @@ +Add directory_index list to SimpleHTTPRequestHandler to support more than +index.html and index.htm