cache.rb

Path: lib/atom/cache.rb
Last Update: Tue Aug 31 08:59:45 -0600 2010

portions of this ported from httplib2 <code.google.com/p/httplib2/> copyright 2006, Joe Gregorio

used under the terms of the MIT license

Required files

digest/md5  

Methods

Public Instance methods

[Source]

# 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

[Source]

# 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

[Source]

# 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

[Source]

# File lib/atom/cache.rb, line 8
def normalize_header_names _headers
  headers = {}
  _headers.each { |k,v| headers[k.downcase] = v }
  headers
end

[Validate]