Module: ActionDispatch::Http::Cache::Request

Included in:
Request
Defined in:
lib/action_dispatch/http/cache.rb

Constant Summary collapse

HTTP_IF_MODIFIED_SINCE =
"HTTP_IF_MODIFIED_SINCE"
HTTP_IF_NONE_MATCH =
"HTTP_IF_NONE_MATCH"

Instance Method Summary collapse

Instance Method Details

#etag_matches?(etag) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
# File 'lib/action_dispatch/http/cache.rb', line 32

def etag_matches?(etag)
  if etag
    validators = if_none_match_etags
    validators.include?(etag) || validators.include?("*")
  end
end

#fresh?(response) ⇒ Boolean

Check response freshness (‘Last-Modified` and `ETag`) against request `If-Modified-Since` and `If-None-Match` conditions. If both headers are supplied, based on configuration, either `ETag` is preferred over `Last-Modified` or both are considered equally. You can adjust the preference with `config.action_dispatch.strict_freshness`. Reference: tools.ietf.org/html/rfc7232#section-6

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/action_dispatch/http/cache.rb', line 45

def fresh?(response)
  if Request.strict_freshness
    if if_none_match
      etag_matches?(response.etag)
    elsif if_modified_since
      not_modified?(response.last_modified)
    else
      false
    end
  else
    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

#if_modified_sinceObject



14
15
16
17
18
# File 'lib/action_dispatch/http/cache.rb', line 14

def if_modified_since
  if since = get_header(HTTP_IF_MODIFIED_SINCE)
    Time.rfc2822(since) rescue nil
  end
end

#if_none_matchObject



20
21
22
# File 'lib/action_dispatch/http/cache.rb', line 20

def if_none_match
  get_header HTTP_IF_NONE_MATCH
end

#if_none_match_etagsObject



24
25
26
# File 'lib/action_dispatch/http/cache.rb', line 24

def if_none_match_etags
  if_none_match ? if_none_match.split(",").each(&:strip!) : []
end

#not_modified?(modified_at) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/action_dispatch/http/cache.rb', line 28

def not_modified?(modified_at)
  if_modified_since && modified_at && if_modified_since >= modified_at
end