class WebAgent::CookieManager

Constants

SPECIAL_DOMAIN

Attributes

accept_domains[RW]
cookies_file[RW]
format[R]
jar[R]
netscape_rule[RW]
reject_domains[RW]

Public Class Methods

new(cookies_file = nil, format = WebAgentSaver, jar = HTTP::CookieJar.new) click to toggle source
# File lib/httpclient/cookie.rb, line 15
def initialize(cookies_file = nil, format = WebAgentSaver, jar = HTTP::CookieJar.new)
  @cookies_file = cookies_file
  @format = format
  @jar = jar
  load_cookies if @cookies_file
end

Public Instance Methods

add(cookie) click to toggle source
# File lib/httpclient/cookie.rb, line 69
def add(cookie)
  @jar.add(cookie)
end
check_expired_cookies() click to toggle source
# File lib/httpclient/webagent-cookie.rb, line 283
def check_expired_cookies
  @cookies.reject!{|cookie|
    is_expired = (cookie.expires && (cookie.expires < Time.now.gmtime))
    if is_expired && !cookie.discard?
      @is_saved = false
    end
    is_expired
  }
end
cookies(uri = nil) click to toggle source
# File lib/httpclient/cookie.rb, line 33
def cookies(uri = nil)
  cookies = @jar.cookies(uri)
  # TODO: return HTTP::Cookie in the future
  cookies.map { |cookie|
    WebAgent::Cookie.new(
      :name => cookie.name,
      :value => cookie.value,
      :domain => cookie.domain,
      :path => cookie.path,
      :origin => cookie.origin,
      :for_domain => cookie.for_domain,
      :expires => cookie.expires,
      :httponly => cookie.httponly,
      :secure => cookie.secure
    )
  }
end
cookies=(cookies) click to toggle source
# File lib/httpclient/cookie.rb, line 62
def cookies=(cookies)
  @jar.clear
  cookies.each do |cookie|
    add(cookie)
  end
end
find(uri) click to toggle source
# File lib/httpclient/cookie.rb, line 73
def find(uri)
  warning('CookieManager#find is deprecated and will be removed in near future. Use HTTP::Cookie.cookie_value(CookieManager#cookies) instead')
  if cookie = cookies(uri)
    HTTP::Cookie.cookie_value(cookie)
  end
end
Also aliased as: cookie_value
load_cookies() click to toggle source
# File lib/httpclient/cookie.rb, line 22
def load_cookies
  check_cookies_file
  @jar.clear
  @jar.load(@cookies_file, :format => @format)
end
parse(value, uri) click to toggle source
# File lib/httpclient/cookie.rb, line 58
def parse(value, uri)
  @jar.parse(value, uri)
end
save_all_cookies(force = nil, save_unused = true, save_discarded = true) click to toggle source
# File lib/httpclient/webagent-cookie.rb, line 255
def save_all_cookies(force = nil, save_unused = true, save_discarded = true)
  @cookies.synchronize do
    check_expired_cookies
    if @is_saved and !force
      return
    end
    File.open(@cookies_file, 'w') do |f|
      @cookies.each do |cookie|
        if (cookie.use? or save_unused) and
          (!cookie.discard? or save_discarded)
          f.print(cookie.url.to_s,"\t",
                  cookie.name,"\t",
                  cookie.value,"\t",
                  cookie.expires.to_i,"\t",
                  cookie.domain,"\t",
                  cookie.path,"\t",
                  cookie.flag,"\n")
        end
      end
    end
  end
  @is_saved = true
end
save_cookies(session = false) click to toggle source
# File lib/httpclient/cookie.rb, line 28
def save_cookies(session = false)
  check_cookies_file
  @jar.save(@cookies_file, :format => @format, :session => session)
end

Private Instance Methods

check_cookies_file() click to toggle source
# File lib/httpclient/cookie.rb, line 82
def check_cookies_file
  unless @cookies_file
    raise ArgumentError.new('Cookies file not specified')
  end
end
check_domain(domain, hostname, override) click to toggle source
# File lib/httpclient/webagent-cookie.rb, line 416
def check_domain(domain, hostname, override)
  return unless domain

  # [DRAFT 12] s. 4.2.2 (does not apply in the case that
  # host name is the same as domain attribute for version 0
  # cookie)
  # I think that this rule has almost the same effect as the
  # tail match of [NETSCAPE].
  if domain !~ /^\./ && hostname != domain
    domain = '.'+domain
  end
  # [NETSCAPE] rule
  if @netscape_rule
    n = domain.scan(/\./).length
    if n < 2
      cookie_error(SpecialError.new, override)
    elsif n == 2
      ## [NETSCAPE] rule
      ok = SPECIAL_DOMAIN.select{|sdomain|
        sdomain == domain[-(sdomain.length)..-1]
      }
      if ok.empty?
        cookie_error(SpecialError.new, override)
      end
    end
  end
  # this implementation does not check RFC2109 4.3.2 case 2;
  # the portion of host not in domain does not contain a dot.
  # according to nsCookieService.cpp in Firefox 3.0.4, Firefox 3.0.4
  # and IE does not check, too.
end