2010-01-16 07:17:03 -05:00
|
|
|
module ActionDispatch
|
|
|
|
module Http
|
|
|
|
module Cache
|
|
|
|
module Request
|
2011-12-07 11:51:23 -05:00
|
|
|
|
|
|
|
HTTP_IF_MODIFIED_SINCE = 'HTTP_IF_MODIFIED_SINCE'.freeze
|
|
|
|
HTTP_IF_NONE_MATCH = 'HTTP_IF_NONE_MATCH'.freeze
|
|
|
|
|
2010-01-16 07:17:03 -05:00
|
|
|
def if_modified_since
|
2015-08-24 18:59:30 -04:00
|
|
|
if since = get_header(HTTP_IF_MODIFIED_SINCE)
|
2010-01-16 07:17:03 -05:00
|
|
|
Time.rfc2822(since) rescue nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def if_none_match
|
2015-08-24 18:59:30 -04:00
|
|
|
get_header HTTP_IF_NONE_MATCH
|
2010-01-16 07:17:03 -05:00
|
|
|
end
|
|
|
|
|
2011-08-13 22:23:27 -04:00
|
|
|
def if_none_match_etags
|
|
|
|
(if_none_match ? if_none_match.split(/\s*,\s*/) : []).collect do |etag|
|
|
|
|
etag.gsub(/^\"|\"$/, "")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-16 07:17:03 -05:00
|
|
|
def not_modified?(modified_at)
|
|
|
|
if_modified_since && modified_at && if_modified_since >= modified_at
|
|
|
|
end
|
|
|
|
|
|
|
|
def etag_matches?(etag)
|
2012-09-15 10:37:50 -04:00
|
|
|
if etag
|
|
|
|
etag = etag.gsub(/^\"|\"$/, "")
|
|
|
|
if_none_match_etags.include?(etag)
|
|
|
|
end
|
2010-01-16 07:17:03 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Check response freshness (Last-Modified and ETag) against request
|
|
|
|
# If-Modified-Since and If-None-Match conditions. If both headers are
|
|
|
|
# supplied, both must match, or the request is not considered fresh.
|
|
|
|
def fresh?(response)
|
|
|
|
last_modified = if_modified_since
|
|
|
|
etag = if_none_match
|
|
|
|
|
|
|
|
return false unless last_modified || etag
|
|
|
|
|
|
|
|
success = true
|
|
|
|
success &&= not_modified?(response.last_modified) if last_modified
|
|
|
|
success &&= etag_matches?(response.etag) if etag
|
|
|
|
success
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module Response
|
2015-08-27 15:02:33 -04:00
|
|
|
attr_reader :cache_control
|
2010-02-19 22:19:20 -05:00
|
|
|
|
2010-01-16 07:17:03 -05:00
|
|
|
def last_modified
|
2015-08-27 15:00:56 -04:00
|
|
|
if last = get_header(LAST_MODIFIED)
|
2010-01-16 07:17:03 -05:00
|
|
|
Time.httpdate(last)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def last_modified?
|
2015-10-03 22:14:37 -04:00
|
|
|
has_header? LAST_MODIFIED
|
2010-01-16 07:17:03 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def last_modified=(utc_time)
|
2015-08-27 15:00:56 -04:00
|
|
|
set_header LAST_MODIFIED, utc_time.httpdate
|
2010-01-16 07:17:03 -05:00
|
|
|
end
|
|
|
|
|
2011-10-31 05:26:05 -04:00
|
|
|
def date
|
2015-08-27 15:00:56 -04:00
|
|
|
if date_header = get_header(DATE)
|
2011-10-31 05:26:05 -04:00
|
|
|
Time.httpdate(date_header)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def date?
|
2015-10-03 22:14:37 -04:00
|
|
|
has_header? DATE
|
2011-10-31 05:26:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def date=(utc_time)
|
2015-08-27 15:00:56 -04:00
|
|
|
set_header DATE, utc_time.httpdate
|
2011-10-31 05:26:05 -04:00
|
|
|
end
|
|
|
|
|
2010-01-16 07:17:03 -05:00
|
|
|
def etag=(etag)
|
|
|
|
key = ActiveSupport::Cache.expand_cache_key(etag)
|
2015-10-06 16:15:39 -04:00
|
|
|
super %("#{Digest::MD5.hexdigest(key)}")
|
2015-08-27 15:02:33 -04:00
|
|
|
end
|
|
|
|
|
2015-10-06 16:15:39 -04:00
|
|
|
def etag?; etag; end
|
2010-01-16 07:17:03 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2015-01-14 02:07:58 -05:00
|
|
|
DATE = 'Date'.freeze
|
2011-12-07 11:51:23 -05:00
|
|
|
LAST_MODIFIED = "Last-Modified".freeze
|
2014-06-15 14:47:37 -04:00
|
|
|
SPECIAL_KEYS = Set.new(%w[extras no-cache max-age public must-revalidate])
|
Ensure that cache-control headers are merged
There are several aspects to this commit, that don't well fit into broken down
commits, so they are detailed here:
* When a user uses response.headers['Cache-Control'] = some_value, then the
documented convention in ConditionalGet is not adhered to, in this case,
response.cache_control is ignored due to `return if
self[CACHE_CONTROL].present?`
* When a middleware sets cache-control headers that would clobber, they're
converted to symbols directly, without underscores. This would lead to bugs.
* Items that would live in :extras if set through expires_in, are placed
directly in the @cache_control hash, and not respected in many cases
(somewhat adhering to the aforementioned documentation).
* Although quite useless, any directive named 'extras' would be ignored.
The general convention applied is that expires_* take precedence, but no longer
overwrite everything and expires_* are ALWAYS applied, even if the header is
set.
I am still unhappy about the contents of this commit, and the code in general.
Ideally it should be refactored to no longer use :extras. I'd likely recommend
expanding @cache_control into a class, and giving it the power to handle the
merge in a more efficient fashion. Such a commit would be a larger change that
could have additional semantic changes for other libraries unless they utilize
expires_in in very standard ways.
2012-06-18 19:49:03 -04:00
|
|
|
|
2012-07-30 01:40:13 -04:00
|
|
|
def cache_control_segments
|
2015-10-06 16:38:50 -04:00
|
|
|
if cache_control = _cache_control
|
2012-07-30 01:40:13 -04:00
|
|
|
cache_control.delete(' ').split(',')
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
Ensure that cache-control headers are merged
There are several aspects to this commit, that don't well fit into broken down
commits, so they are detailed here:
* When a user uses response.headers['Cache-Control'] = some_value, then the
documented convention in ConditionalGet is not adhered to, in this case,
response.cache_control is ignored due to `return if
self[CACHE_CONTROL].present?`
* When a middleware sets cache-control headers that would clobber, they're
converted to symbols directly, without underscores. This would lead to bugs.
* Items that would live in :extras if set through expires_in, are placed
directly in the @cache_control hash, and not respected in many cases
(somewhat adhering to the aforementioned documentation).
* Although quite useless, any directive named 'extras' would be ignored.
The general convention applied is that expires_* take precedence, but no longer
overwrite everything and expires_* are ALWAYS applied, even if the header is
set.
I am still unhappy about the contents of this commit, and the code in general.
Ideally it should be refactored to no longer use :extras. I'd likely recommend
expanding @cache_control into a class, and giving it the power to handle the
merge in a more efficient fashion. Such a commit would be a larger change that
could have additional semantic changes for other libraries unless they utilize
expires_in in very standard ways.
2012-06-18 19:49:03 -04:00
|
|
|
def cache_control_headers
|
|
|
|
cache_control = {}
|
2012-07-30 01:40:13 -04:00
|
|
|
|
|
|
|
cache_control_segments.each do |segment|
|
|
|
|
directive, argument = segment.split('=', 2)
|
|
|
|
|
2013-03-19 01:23:32 -04:00
|
|
|
if SPECIAL_KEYS.include? directive
|
2012-07-30 01:40:13 -04:00
|
|
|
key = directive.tr('-', '_')
|
|
|
|
cache_control[key.to_sym] = argument || true
|
|
|
|
else
|
|
|
|
cache_control[:extras] ||= []
|
|
|
|
cache_control[:extras] << segment
|
Ensure that cache-control headers are merged
There are several aspects to this commit, that don't well fit into broken down
commits, so they are detailed here:
* When a user uses response.headers['Cache-Control'] = some_value, then the
documented convention in ConditionalGet is not adhered to, in this case,
response.cache_control is ignored due to `return if
self[CACHE_CONTROL].present?`
* When a middleware sets cache-control headers that would clobber, they're
converted to symbols directly, without underscores. This would lead to bugs.
* Items that would live in :extras if set through expires_in, are placed
directly in the @cache_control hash, and not respected in many cases
(somewhat adhering to the aforementioned documentation).
* Although quite useless, any directive named 'extras' would be ignored.
The general convention applied is that expires_* take precedence, but no longer
overwrite everything and expires_* are ALWAYS applied, even if the header is
set.
I am still unhappy about the contents of this commit, and the code in general.
Ideally it should be refactored to no longer use :extras. I'd likely recommend
expanding @cache_control into a class, and giving it the power to handle the
merge in a more efficient fashion. Such a commit would be a larger change that
could have additional semantic changes for other libraries unless they utilize
expires_in in very standard ways.
2012-06-18 19:49:03 -04:00
|
|
|
end
|
|
|
|
end
|
2012-07-30 01:40:13 -04:00
|
|
|
|
Ensure that cache-control headers are merged
There are several aspects to this commit, that don't well fit into broken down
commits, so they are detailed here:
* When a user uses response.headers['Cache-Control'] = some_value, then the
documented convention in ConditionalGet is not adhered to, in this case,
response.cache_control is ignored due to `return if
self[CACHE_CONTROL].present?`
* When a middleware sets cache-control headers that would clobber, they're
converted to symbols directly, without underscores. This would lead to bugs.
* Items that would live in :extras if set through expires_in, are placed
directly in the @cache_control hash, and not respected in many cases
(somewhat adhering to the aforementioned documentation).
* Although quite useless, any directive named 'extras' would be ignored.
The general convention applied is that expires_* take precedence, but no longer
overwrite everything and expires_* are ALWAYS applied, even if the header is
set.
I am still unhappy about the contents of this commit, and the code in general.
Ideally it should be refactored to no longer use :extras. I'd likely recommend
expanding @cache_control into a class, and giving it the power to handle the
merge in a more efficient fashion. Such a commit would be a larger change that
could have additional semantic changes for other libraries unless they utilize
expires_in in very standard ways.
2012-06-18 19:49:03 -04:00
|
|
|
cache_control
|
|
|
|
end
|
2011-12-07 11:51:23 -05:00
|
|
|
|
2011-05-10 10:53:57 -04:00
|
|
|
def prepare_cache_control!
|
Ensure that cache-control headers are merged
There are several aspects to this commit, that don't well fit into broken down
commits, so they are detailed here:
* When a user uses response.headers['Cache-Control'] = some_value, then the
documented convention in ConditionalGet is not adhered to, in this case,
response.cache_control is ignored due to `return if
self[CACHE_CONTROL].present?`
* When a middleware sets cache-control headers that would clobber, they're
converted to symbols directly, without underscores. This would lead to bugs.
* Items that would live in :extras if set through expires_in, are placed
directly in the @cache_control hash, and not respected in many cases
(somewhat adhering to the aforementioned documentation).
* Although quite useless, any directive named 'extras' would be ignored.
The general convention applied is that expires_* take precedence, but no longer
overwrite everything and expires_* are ALWAYS applied, even if the header is
set.
I am still unhappy about the contents of this commit, and the code in general.
Ideally it should be refactored to no longer use :extras. I'd likely recommend
expanding @cache_control into a class, and giving it the power to handle the
merge in a more efficient fashion. Such a commit would be a larger change that
could have additional semantic changes for other libraries unless they utilize
expires_in in very standard ways.
2012-06-18 19:49:03 -04:00
|
|
|
@cache_control = cache_control_headers
|
2011-05-10 10:53:57 -04:00
|
|
|
end
|
|
|
|
|
2010-01-16 07:17:03 -05:00
|
|
|
def handle_conditional_get!
|
|
|
|
if etag? || last_modified? || !@cache_control.empty?
|
2015-08-27 15:09:04 -04:00
|
|
|
set_conditional_cache_control!(@cache_control)
|
2010-01-16 07:17:03 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-12-07 11:51:23 -05:00
|
|
|
DEFAULT_CACHE_CONTROL = "max-age=0, private, must-revalidate".freeze
|
|
|
|
NO_CACHE = "no-cache".freeze
|
|
|
|
PUBLIC = "public".freeze
|
|
|
|
PRIVATE = "private".freeze
|
|
|
|
MUST_REVALIDATE = "must-revalidate".freeze
|
2010-01-16 07:17:03 -05:00
|
|
|
|
2015-08-27 15:09:04 -04:00
|
|
|
def set_conditional_cache_control!(cache_control)
|
Ensure that cache-control headers are merged
There are several aspects to this commit, that don't well fit into broken down
commits, so they are detailed here:
* When a user uses response.headers['Cache-Control'] = some_value, then the
documented convention in ConditionalGet is not adhered to, in this case,
response.cache_control is ignored due to `return if
self[CACHE_CONTROL].present?`
* When a middleware sets cache-control headers that would clobber, they're
converted to symbols directly, without underscores. This would lead to bugs.
* Items that would live in :extras if set through expires_in, are placed
directly in the @cache_control hash, and not respected in many cases
(somewhat adhering to the aforementioned documentation).
* Although quite useless, any directive named 'extras' would be ignored.
The general convention applied is that expires_* take precedence, but no longer
overwrite everything and expires_* are ALWAYS applied, even if the header is
set.
I am still unhappy about the contents of this commit, and the code in general.
Ideally it should be refactored to no longer use :extras. I'd likely recommend
expanding @cache_control into a class, and giving it the power to handle the
merge in a more efficient fashion. Such a commit would be a larger change that
could have additional semantic changes for other libraries unless they utilize
expires_in in very standard ways.
2012-06-18 19:49:03 -04:00
|
|
|
control = {}
|
|
|
|
cc_headers = cache_control_headers
|
|
|
|
if extras = cc_headers.delete(:extras)
|
2015-08-27 15:09:04 -04:00
|
|
|
cache_control[:extras] ||= []
|
|
|
|
cache_control[:extras] += extras
|
|
|
|
cache_control[:extras].uniq!
|
Ensure that cache-control headers are merged
There are several aspects to this commit, that don't well fit into broken down
commits, so they are detailed here:
* When a user uses response.headers['Cache-Control'] = some_value, then the
documented convention in ConditionalGet is not adhered to, in this case,
response.cache_control is ignored due to `return if
self[CACHE_CONTROL].present?`
* When a middleware sets cache-control headers that would clobber, they're
converted to symbols directly, without underscores. This would lead to bugs.
* Items that would live in :extras if set through expires_in, are placed
directly in the @cache_control hash, and not respected in many cases
(somewhat adhering to the aforementioned documentation).
* Although quite useless, any directive named 'extras' would be ignored.
The general convention applied is that expires_* take precedence, but no longer
overwrite everything and expires_* are ALWAYS applied, even if the header is
set.
I am still unhappy about the contents of this commit, and the code in general.
Ideally it should be refactored to no longer use :extras. I'd likely recommend
expanding @cache_control into a class, and giving it the power to handle the
merge in a more efficient fashion. Such a commit would be a larger change that
could have additional semantic changes for other libraries unless they utilize
expires_in in very standard ways.
2012-06-18 19:49:03 -04:00
|
|
|
end
|
2010-02-19 22:19:20 -05:00
|
|
|
|
Ensure that cache-control headers are merged
There are several aspects to this commit, that don't well fit into broken down
commits, so they are detailed here:
* When a user uses response.headers['Cache-Control'] = some_value, then the
documented convention in ConditionalGet is not adhered to, in this case,
response.cache_control is ignored due to `return if
self[CACHE_CONTROL].present?`
* When a middleware sets cache-control headers that would clobber, they're
converted to symbols directly, without underscores. This would lead to bugs.
* Items that would live in :extras if set through expires_in, are placed
directly in the @cache_control hash, and not respected in many cases
(somewhat adhering to the aforementioned documentation).
* Although quite useless, any directive named 'extras' would be ignored.
The general convention applied is that expires_* take precedence, but no longer
overwrite everything and expires_* are ALWAYS applied, even if the header is
set.
I am still unhappy about the contents of this commit, and the code in general.
Ideally it should be refactored to no longer use :extras. I'd likely recommend
expanding @cache_control into a class, and giving it the power to handle the
merge in a more efficient fashion. Such a commit would be a larger change that
could have additional semantic changes for other libraries unless they utilize
expires_in in very standard ways.
2012-06-18 19:49:03 -04:00
|
|
|
control.merge! cc_headers
|
2015-08-27 15:09:04 -04:00
|
|
|
control.merge! cache_control
|
2010-08-11 17:54:15 -04:00
|
|
|
|
2010-01-16 07:17:03 -05:00
|
|
|
if control.empty?
|
2015-10-06 16:38:50 -04:00
|
|
|
self._cache_control = DEFAULT_CACHE_CONTROL
|
2010-10-17 17:08:41 -04:00
|
|
|
elsif control[:no_cache]
|
2015-10-06 16:38:50 -04:00
|
|
|
self._cache_control = NO_CACHE
|
Ensure that cache-control headers are merged
There are several aspects to this commit, that don't well fit into broken down
commits, so they are detailed here:
* When a user uses response.headers['Cache-Control'] = some_value, then the
documented convention in ConditionalGet is not adhered to, in this case,
response.cache_control is ignored due to `return if
self[CACHE_CONTROL].present?`
* When a middleware sets cache-control headers that would clobber, they're
converted to symbols directly, without underscores. This would lead to bugs.
* Items that would live in :extras if set through expires_in, are placed
directly in the @cache_control hash, and not respected in many cases
(somewhat adhering to the aforementioned documentation).
* Although quite useless, any directive named 'extras' would be ignored.
The general convention applied is that expires_* take precedence, but no longer
overwrite everything and expires_* are ALWAYS applied, even if the header is
set.
I am still unhappy about the contents of this commit, and the code in general.
Ideally it should be refactored to no longer use :extras. I'd likely recommend
expanding @cache_control into a class, and giving it the power to handle the
merge in a more efficient fashion. Such a commit would be a larger change that
could have additional semantic changes for other libraries unless they utilize
expires_in in very standard ways.
2012-06-18 19:49:03 -04:00
|
|
|
if control[:extras]
|
2015-10-06 16:38:50 -04:00
|
|
|
self._cache_control = _cache_control + ", #{control[:extras].join(', ')}"
|
Ensure that cache-control headers are merged
There are several aspects to this commit, that don't well fit into broken down
commits, so they are detailed here:
* When a user uses response.headers['Cache-Control'] = some_value, then the
documented convention in ConditionalGet is not adhered to, in this case,
response.cache_control is ignored due to `return if
self[CACHE_CONTROL].present?`
* When a middleware sets cache-control headers that would clobber, they're
converted to symbols directly, without underscores. This would lead to bugs.
* Items that would live in :extras if set through expires_in, are placed
directly in the @cache_control hash, and not respected in many cases
(somewhat adhering to the aforementioned documentation).
* Although quite useless, any directive named 'extras' would be ignored.
The general convention applied is that expires_* take precedence, but no longer
overwrite everything and expires_* are ALWAYS applied, even if the header is
set.
I am still unhappy about the contents of this commit, and the code in general.
Ideally it should be refactored to no longer use :extras. I'd likely recommend
expanding @cache_control into a class, and giving it the power to handle the
merge in a more efficient fashion. Such a commit would be a larger change that
could have additional semantic changes for other libraries unless they utilize
expires_in in very standard ways.
2012-06-18 19:49:03 -04:00
|
|
|
end
|
2010-01-16 07:17:03 -05:00
|
|
|
else
|
|
|
|
extras = control[:extras]
|
|
|
|
max_age = control[:max_age]
|
|
|
|
|
|
|
|
options = []
|
|
|
|
options << "max-age=#{max_age.to_i}" if max_age
|
2011-12-07 11:51:23 -05:00
|
|
|
options << (control[:public] ? PUBLIC : PRIVATE)
|
|
|
|
options << MUST_REVALIDATE if control[:must_revalidate]
|
2010-01-16 07:17:03 -05:00
|
|
|
options.concat(extras) if extras
|
|
|
|
|
2015-10-06 16:38:50 -04:00
|
|
|
self._cache_control = options.join(", ")
|
2010-01-16 07:17:03 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-06-28 02:42:13 -04:00
|
|
|
end
|