Skip to content

Commit

Permalink
close file handler before raising exception
Browse files Browse the repository at this point in the history
  • Loading branch information
Cycloctane committed Jan 14, 2025
1 parent 6165bef commit 613fce6
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions falcon/routing/static.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,16 +218,21 @@ def __call__(self, req: Request, resp: Response, **kw: Any) -> None:
if '..' in file_path or not file_path.startswith(self._directory):
raise falcon.HTTPNotFound()

fh: Optional[io.BufferedReader] = None
try:
fh = io.open(file_path, 'rb')
st = os.fstat(fh.fileno())
except IOError:
if self._fallback_filename is None:
if fh is not None:
fh.close()
raise falcon.HTTPNotFound()
try:
fh = io.open(self._fallback_filename, 'rb')
st = os.fstat(fh.fileno())
except IOError:
if fh is not None:
fh.close()
raise falcon.HTTPNotFound()
else:
file_path = self._fallback_filename
Expand All @@ -243,6 +248,7 @@ def __call__(self, req: Request, resp: Response, **kw: Any) -> None:
stream, length, content_range = _open_range(fh, st, req_range)
resp.set_stream(stream, length)
except IOError:
fh.close()
raise falcon.HTTPNotFound()

suffix = os.path.splitext(file_path)[1]
Expand Down

0 comments on commit 613fce6

Please sign in to comment.