Top Level Namespace

Defined Under Namespace

Modules: Atom, URI, XHTML Classes: Object, String

Instance Method Summary collapse

Instance Method Details

#_entry_disposition(response_headers, request_headers) ⇒ Object

Determine freshness from the Date, Expires and Cache-Control headers.

We don't handle the following:

1. Cache-Control: max-stale
2. Age: headers are not used in the calculations.

Not that this algorithm is simpler than you might think
because we are operating as a private (non-shared) cache.
This lets us ignore 's-maxage'. We can also ignore
'proxy-invalidate' since we aren't a proxy.
We will never return a stale document as
fresh as a design decision, and thus the non-implementation
of 'max-stale'. This also lets us safely ignore 'must-revalidate'
since we operate as if every server has sent 'must-revalidate'.
Since we are private we get to ignore both 'public' and
'private' parameters. We also ignore 'no-transform' since
we don't do any transformations.
The 'no-store' parameter is handled at a higher level.
So the only Cache-Control parameters we look at are:

no-cache
only-if-cached
max-age
min-fresh


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/atom/cache.rb', line 79

def _entry_disposition(response_headers, request_headers)
  request_headers = normalize_header_names(request_headers)

  cc = _parse_cache_control(request_headers)
  cc_response = _parse_cache_control(response_headers)

  if request_headers['pragma'] and request_headers['pragma'].downcase.match(/no-cache/)
    unless request_headers.key? 'cache-control'
      request_headers['cache-control'] = 'no-cache'
    end
    :TRANSPARENT
  elsif cc.key? 'no-cache'
    :TRANSPARENT
  elsif cc_response.key? 'no-cache'
    :STALE
  elsif cc.key? 'only-if-cached'
    :FRESH
  elsif response_headers.key? 'date'
    date = Time.rfc2822(response_headers['date'])
    diff = Time.now - date
    current_age = (diff > 0) ? diff : 0
    if cc_response.key? 'max-age'
      freshness_lifetime = cc_response['max-age'].to_i
    elsif response_headers.key? 'expires'
      expires = Time.rfc2822(response_headers['expires'])
      diff = expires - date
      freshness_lifetime = (diff > 0) ? diff : 0
    else
      freshness_lifetime = 0
    end

    if cc.key? 'max-age'
      freshness_lifetime = cc['max-age'].to_i
    end

    if cc.key? 'min-fresh'
      min_fresh = cc['min-fresh'].to_i
      current_age += min_fresh
    end

    if freshness_lifetime > current_age
      :FRESH
    else
      :STALE
    end
  end
end

#_parse_cache_control(headers) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/atom/cache.rb', line 14

def _parse_cache_control headers
  retval = {}
  headers = normalize_header_names(headers) if headers.is_a? Hash

  if headers['cache-control']
    parts = headers['cache-control'].split(',')
    parts.each do |part|
      if part.match(/=/)
        k, v = part.split('=').map { |p| p.strip }
        retval[k] = v
      else
        retval[part.strip] = 1
      end
    end
  end

  retval
end

#_updateCache(request_headers, response, cache, cachekey) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/atom/cache.rb', line 33

def _updateCache request_headers, response, cache, cachekey
  cc = _parse_cache_control request_headers
  cc_response = _parse_cache_control response
  if cc['no-store'] or cc_response['no-store']
    cache.delete cachekey
  else
    result = "HTTP/#{response.http_version} #{response.code} #{response.message}\r\n"

    response.each_capitalized_name do |field|
      next if ['status', 'content-encoding', 'transfer-encoding'].member? field.downcase
      response.get_fields(field).each do |value|
        result += "#{field}: #{value}\r\n"
      end
    end

    cache[cachekey] = result + "\r\n" + response.body
  end
end

#normalize_header_names(_headers) ⇒ Object



8
9
10
11
12
# File 'lib/atom/cache.rb', line 8

def normalize_header_names _headers
  headers = {}
  _headers.each { |k,v| headers[k.downcase] = v }
  headers
end