-
-
Notifications
You must be signed in to change notification settings - Fork 948
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
feat(static): implement Last-Modified header for static route #2426
base: master
Are you sure you want to change the base?
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #2426 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 64 64
Lines 7728 7743 +15
Branches 1071 1071
=========================================
+ Hits 7728 7743 +15 ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice improvement.
Some lines are not covered by tests, if you could add tests for them it would be great
falcon/routing/static.py
Outdated
try: | ||
stream, length, content_range = _open_range(file_path, req_range) | ||
resp.set_stream(stream, length) | ||
fh = io.open(file_path, 'rb') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's set fh to None above the try then in the except do if fn is not None: fh.close()
since in theory the io error could happen in os.fstat
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's set fh to None above the try then in the except do
if fn is not None: fh.close()
since in theory the io error could happen in os.fstat
I tried using os.stat(file_path)
instead to avoid handling fh outside _open_range()
. This approach can also avoid unneccesary io.open
when returning 304.
b1c90b4
to
cf105d5
Compare
falcon/routing/static.py
Outdated
try: | ||
stream, length, content_range = _open_range(file_path, req_range) | ||
resp.set_stream(stream, length) | ||
st = _stat(file_path) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice going overall so far 👍
I don't like this specific change though, please revert it if possible. We do want to stat()
an open fd instead of path, as this prevents a number of race conditions, for instance, if the file in question is deleted between stat()
and open()
, etc.
Summary of Changes
Last-Modified
header for static file responses.If-Modified-Since
header and return 304 if date in this header is not earlier than mtime.Related Issues
Fixes #2244
Pull Request Checklist
docs/
.docs/
.versionadded
,versionchanged
, ordeprecated
directives.docs/_newsfragments/
, with the file name format{issue_number}.{fragment_type}.rst
. (Runtowncrier --draft
to ensure it renders correctly.)