class Pincers::Http::CookieJar
Constants
- BAD_VALUE_CHARS
Attributes
Public Class Methods
new(_cookies=nil)
click to toggle source
# File lib/pincers/http/cookie_jar.rb, line 11 def initialize(_cookies=nil) @cookies = _cookies || [] end
Public Instance Methods
copy()
click to toggle source
# File lib/pincers/http/cookie_jar.rb, line 15 def copy self.class.new @cookies.clone end
for_origin(_uri)
click to toggle source
# File lib/pincers/http/cookie_jar.rb, line 51 def for_origin(_uri) # RFC 6265 5.4.1 @cookies.select do |c| # TODO: add scheme and host only checks domains_match c.domain, _uri.host and paths_match c.path, _uri.path end end
for_origin_as_header(_uri)
click to toggle source
# File lib/pincers/http/cookie_jar.rb, line 59 def for_origin_as_header(_uri) for_origin(_uri).map { |c| "#{c.name}=#{quote(c.value)}" }.join('; ') end
get(_url, _name)
click to toggle source
# File lib/pincers/http/cookie_jar.rb, line 19 def get(_url, _name) for_origin(Utils.parse_uri(_url)).find { |c| c.name == _name } end
set(_cookie)
click to toggle source
# File lib/pincers/http/cookie_jar.rb, line 23 def set(_cookie) if _cookie.name.nil? or _cookie.value.nil? or _cookie.domain.nil? or _cookie.path.nil? raise ArgumentError, 'Invalid cookie' end @cookies.each_with_index do |cookie, i| if equivalent(cookie, _cookie) @cookies[i] = _cookie return _cookie end end @cookies << _cookie _cookie end
set_from_header(_uri, _header)
click to toggle source
# File lib/pincers/http/cookie_jar.rb, line 45 def set_from_header(_uri, _header) _header.split(/, (?=\w+=)/).map do |raw_cookie| set_raw _uri, raw_cookie.strip end end
set_raw(_request_uri, _raw)
click to toggle source
# File lib/pincers/http/cookie_jar.rb, line 39 def set_raw(_request_uri, _raw) cookie = decode_cookie _request_uri, _raw set cookie unless cookie.nil? cookie end
Private Instance Methods
default_path(_request_path)
click to toggle source
# File lib/pincers/http/cookie_jar.rb, line 128 def default_path(_request_path) # RFC 6265 - 5.1.4 return '/' unless _request_path[0] === '/' ls_idx = _request_path.rindex('/') return '/' unless ls_idx > 0 _request_path[0..ls_idx] end
dequote(_str)
click to toggle source
# File lib/pincers/http/cookie_jar.rb, line 136 def dequote(_str) # taken from WEBrick implementation ret = (/\A"(.*)"\Z/ =~ _str) ? $1 : _str.dup ret.gsub!(/\\(.)/, "\\1") ret end
domains_match(_cookie_domain, _request_domain)
click to toggle source
# File lib/pincers/http/cookie_jar.rb, line 108 def domains_match(_cookie_domain, _request_domain) # RFC 6265 - 5.1.3 # TODO: ensure request domain is not an IP return true if _cookie_domain == _request_domain if _request_domain.end_with? "#{_cookie_domain}" return true if _cookie_domain[0] == '.' or _request_domain.end_with? ".#{_cookie_domain}" end return false end
equivalent(_cookie_a, _cookie_b)
click to toggle source
# File lib/pincers/http/cookie_jar.rb, line 149 def equivalent(_cookie_a, _cookie_b) return false unless _cookie_a.domain == _cookie_b.domain return false unless _cookie_a.path == _cookie_b.path return false unless _cookie_a.name == _cookie_b.name return true end
paths_match(_cookie_path, _request_path)
click to toggle source
# File lib/pincers/http/cookie_jar.rb, line 118 def paths_match(_cookie_path, _request_path) # RFC 6265 - 5.1.4 _request_path = '/' if _request_path.empty? return true if _cookie_path == _request_path if _request_path.start_with? _cookie_path return true if _cookie_path[-1] == '/' or _request_path.start_with? "#{_cookie_path}/" end return false end
quote(_str)
click to toggle source
# File lib/pincers/http/cookie_jar.rb, line 143 def quote(_str) # taken from WEBrick implementation and the http-cookie gem return _str unless BAD_VALUE_CHARS === _str '"' << _str.gsub(/[\\\"]/o, "\\\1") << '"' end