8000 gh-100474: Fix handling of dirs named index.html in http.server (GH-1… · python/cpython@46e6a28 · GitHub
[go: up one dir, main page]

Skip to content

Commit 46e6a28

Browse files
authored
gh-100474: Fix handling of dirs named index.html in http.server (GH-100475)
If you had a directory called index.html or index.htm within a directory, it would cause http.server to return a 404 Not Found error instead of the directory listing. This came about due to not checking that the index was a regular file. I have also added a test case for this situation. Automerge-Triggered-By: GH:merwok
1 parent 00afa50 commit 46e6a28

File tree

3 files changed

+6
-1
lines changed

3 files changed

+6
-1
lines changed

Lib/http/server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ def send_head(self):
711711
return None
712712
for index in self.index_pages:
713713
index = os.path.join(path, index)
714-
if os.path.exists(index):
714+
if os.path.isfile(index):
715715
path = index
716716
break
717717
else:

Lib/test/test_httpservers.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,9 @@ def test_get(self):
489489
self.check_status_and_reason(response, HTTPStatus.NOT_FOUND)
490490
response = self.request('/' + 'ThisDoesNotExist' + '/')
491491
self.check_status_and_reason(response, HTTPStatus.NOT_FOUND)
492+
os.makedirs(os.path.join(self.tempdir, 'spam', 'index.html'))
493+
response = self.request(self.base_url + '/spam/')
494+
self.check_status_and_reason(response, HTTPStatus.OK)
492495

493496
data = b"Dummy index file\r\n"
494497
with open(os.path.join(self.tempdir_name, 'index.html'), 'wb') as f:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:mod:`http.server` now checks that an index page is actually a regular file before trying
2+
to serve it. This avoids issues with directories named ``index.html``.

0 commit comments

Comments
 (0)
0