Skip to content
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

Fix Staticfiles 404.html in HTML mode #1314

Merged
merged 15 commits into from Oct 19, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 16 additions & 15 deletions starlette/staticfiles.py
Expand Up @@ -111,20 +111,6 @@ async def get_response(self, path: str, scope: Scope) -> Response:
full_path, stat_result = await anyio.to_thread.run_sync(
self.lookup_path, path
)
except (FileNotFoundError, NotADirectoryError):
if self.html:
# Check for '404.html' if we're in HTML mode.
full_path, stat_result = await anyio.to_thread.run_sync(
self.lookup_path, "404.html"
)
if stat_result and stat.S_ISREG(stat_result.st_mode):
return FileResponse(
full_path,
stat_result=stat_result,
method=scope["method"],
status_code=404,
)
raise HTTPException(status_code=404)
aminalaee marked this conversation as resolved.
Show resolved Hide resolved
except PermissionError:
raise HTTPException(status_code=401)
except OSError:
Expand All @@ -149,6 +135,18 @@ async def get_response(self, path: str, scope: Scope) -> Response:
return RedirectResponse(url=url)
return self.file_response(full_path, stat_result, scope)

if self.html:
# Check for '404.html' if we're in HTML mode.
full_path, stat_result = await anyio.to_thread.run_sync(
self.lookup_path, "404.html"
)
if stat_result and stat.S_ISREG(stat_result.st_mode):
return FileResponse(
full_path,
stat_result=stat_result,
method=scope["method"],
status_code=404,
)
raise HTTPException(status_code=404)

def lookup_path(
Expand All @@ -161,7 +159,10 @@ def lookup_path(
# Don't allow misbehaving clients to break out of the static files
# directory.
continue
return full_path, os.stat(full_path)
try:
return full_path, os.stat(full_path)
except (FileNotFoundError, NotADirectoryError):
continue
aminalaee marked this conversation as resolved.
Show resolved Hide resolved
return "", None

def file_response(
Expand Down