-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add cache control headers to http static file handler + a few more mi… #2470
Changes from all commits
71b9ac4
fb5b8f2
131cccb
73c4289
2ff58b8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,6 +64,23 @@ class HTTP::StaticFileHandler | |
context.response.content_type = "text/html" | ||
directory_listing(context.response, request_path, file_path) | ||
elsif is_file | ||
last_modified = File.stat(file_path).mtime | ||
context.response.headers["Last-Modified"] = HTTP.rfc1123_date(last_modified) | ||
|
||
if if_modified_since = context.request.headers["If-Modified-Since"]? | ||
# TODO: Use a more generalized time format parser for better compatibility to RFC 7232 | ||
header_time = Time.parse(if_modified_since, "%a, %d %b %Y %H:%M:%S GMT") | ||
|
||
# File mtime probably has a higher resolution than the header value. | ||
# An exact comparison might be slightly off, so we add 1s padding. | ||
# Static files should generally not be modified in subsecond intervals, so this is perfectly safe. | ||
# This might replaced by a more sophisticated time comparison when it becomes available. | ||
if last_modified <= header_time + 1.second | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we can use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if this could lead to failing edge cases because of rounding errors. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would certainly not have the expected results because a time at There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While it's not super critical here, it doesn't make sense to sacrifice precision for performance since the whole point is to improve efficiency if the time stamp is pretty recent. |
||
context.response.status_code = 304 | ||
return | ||
end | ||
end | ||
|
||
context.response.content_type = mime_type(file_path) | ||
context.response.content_length = File.size(file_path) | ||
File.open(file_path) do |file| | ||
|
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.
Argh, more stat usages to fix in #5584! :)