Parent

Class/Module Index [+]

Quicksearch

Irc::Utils::HttpUtil::CachedObject

Attributes

count[RW]
date[RW]
expires[RW]
first_used[RW]
last_used[RW]
response[RW]

Public Class Methods

maybe_new(resp) click to toggle source
# File lib/rbot/core/utils/httputil.rb, line 203
def self.maybe_new(resp)
  debug "maybe new #{resp}"
  return nil if resp.no_cache
  return nil unless Net::HTTPOK === resp ||
  Net::HTTPMovedPermanently === resp ||
  Net::HTTPFound === resp ||
  Net::HTTPPartialContent === resp

  cc = resp['cache-control']
  return nil if cc && (cc =~ /no-cache/)

  date = Time.now
  if d = resp['date']
    date = Time.httpdate(d)
  end

  return nil if resp['expires'] && (Time.httpdate(resp['expires']) < date)

  debug "creating cache obj"

  self.new(resp)
end
new(resp) click to toggle source
# File lib/rbot/core/utils/httputil.rb, line 277
def initialize(resp)
  @response = resp
  begin
    self.revalidate
    self.response.raw_body
  rescue Exception => e
    error e
    raise e
  end
end

Public Instance Methods

expired?() click to toggle source
# File lib/rbot/core/utils/httputil.rb, line 233
def expired?
  debug "checking expired?"
  if cc = self.response['cache-control'] && cc =~ /must-revalidate/
    return true
  end
  return self.expires < Time.now
end
revalidate(resp = self.response) click to toggle source
# File lib/rbot/core/utils/httputil.rb, line 252
def revalidate(resp = self.response)
  @count = 0
  self.use
  self.date = resp.key?('date') ? Time.httpdate(resp['date']) : Time.now

  cc = resp['cache-control']
  if cc && (cc =~ /max-age=(\d+)/)
    self.expires = self.date + $1.to_i
  elsif resp.key?('expires')
    self.expires = Time.httpdate(resp['expires'])
  elsif lm = resp['last-modified']
    delta = self.date - Time.httpdate(lm)
    delta = 10 if delta <= 0
    delta /= 5
    self.expires = self.date + delta
  else
    self.expires = self.date + 300
  end
  # self.expires = Time.now + 10 # DEBUG
  debug "expires on #{self.expires}"

  return true
end
setup_headers(hdr) click to toggle source
# File lib/rbot/core/utils/httputil.rb, line 241
def setup_headers(hdr)
  hdr['if-modified-since'] = self.date.rfc2822

  debug "ims == #{hdr['if-modified-since']}"

  if etag = self.response['etag']
    hdr['if-none-match'] = etag
    debug "etag: #{etag}"
  end
end
use() click to toggle source
# File lib/rbot/core/utils/httputil.rb, line 226
def use
  now = Time.now
  @first_used = now if @count == 0
  @last_used = now
  @count += 1
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.