class HTTPX::Plugins::Cookies::Cookie

  1. lib/httpx/plugins/cookies/cookie.rb
  2. lib/httpx/plugins/cookies/set_cookie_parser.rb
  3. show all
Superclass: Object

The HTTP Cookie.

Contains the single cookie info: name, value and attributes.

Included modules

  1. Comparable

Constants

MAX_LENGTH = 4096  

Maximum number of bytes per cookie (RFC 6265 6.1 requires 4096 at least)

Public Instance Aliases

to_s -> cookie_value

Attributes

created_at [R]
domain [R]
name [R]
path [R]
value [R]

Public Class methods

new(cookie, *args)
[show source]
   # File lib/httpx/plugins/cookies/cookie.rb
71 def new(cookie, *args)
72   case cookie
73   when self
74     cookie
75   when Array, Hash
76     options = Hash[cookie] #: cookie_attributes
77     super(options[:name], options[:value], options)
78   else
79 
80     super
81   end
82 end
new(arg, value, attrs = nil)
[show source]
    # File lib/httpx/plugins/cookies/cookie.rb
114 def initialize(arg, value, attrs = nil)
115   @created_at = Time.now
116 
117   @name = arg
118   @value = value
119   attr_hash = Hash.try_convert(attrs)
120 
121   attr_hash.each do |key, val|
122     key = key.downcase.tr("-", "_").to_sym unless key.is_a?(Symbol)
123 
124     case key
125     when :domain, :path
126       __send__(:"#{key}=", val)
127     else
128       instance_variable_set(:"@#{key}", val)
129     end
130   end if attr_hash
131 
132   @path ||= "/"
133   raise ArgumentError, "name must be specified" if @name.nil?
134 
135   @name = @name.to_s
136 end
path_match?(base_path, target_path)

Tests if target_path is under base_path as described in RFC 6265 5.1.4. base_path must be an absolute path. target_path may be empty, in which case it is treated as the root path.

e.g.

path_match?('/admin/', '/admin/index') == true
path_match?('/admin/', '/Admin/index') == false
path_match?('/admin/', '/admin/') == true
path_match?('/admin/', '/admin') == false

path_match?('/admin', '/admin') == true
path_match?('/admin', '/Admin') == false
path_match?('/admin', '/admins') == false
path_match?('/admin', '/admin/') == true
path_match?('/admin', '/admin/index') == true
[show source]
    # File lib/httpx/plugins/cookies/cookie.rb
101 def path_match?(base_path, target_path)
102   base_path.start_with?("/") || (return false)
103   # RFC 6265 5.1.4
104   bsize = base_path.size
105   tsize = target_path.size
106   return bsize == 1 if tsize.zero? # treat empty target_path as "/"
107   return false unless target_path.start_with?(base_path)
108   return true if bsize == tsize || base_path.end_with?("/")
109 
110   target_path[bsize] == "/"
111 end

Public Instance methods

<=>(other)

Compares the cookie with another. When there are many cookies with the same name for a URL, the value of the smallest must be used.

[show source]
   # File lib/httpx/plugins/cookies/cookie.rb
51 def <=>(other)
52   # RFC 6265 5.4
53   # Precedence: 1. longer path  2. older creation
54   (@name <=> other.name).nonzero? ||
55     (other.path.length <=> @path.length).nonzero? ||
56     (@created_at <=> other.created_at).nonzero? || 0
57 end
==(other)

checks whether other is the same cookie, i.e. name, value, domain and path are the same.

[show source]
   # File lib/httpx/plugins/cookies/cookie.rb
44 def ==(other)
45   @name == other.name && @value == other.value &&
46     @path == other.path && @domain == other.domain
47 end
domain=(domain)

assigns a new domain to this cookie.

[show source]
   # File lib/httpx/plugins/cookies/cookie.rb
25 def domain=(domain)
26   domain = String(domain)
27 
28   if domain.start_with?(".")
29     @for_domain = true
30     domain = domain[1..-1]
31   end
32 
33   return if domain.empty?
34 
35   @domain_name = DomainName.new(domain)
36   # RFC 6265 5.3 5.
37   @for_domain = false if @domain_name.domain.nil? # a public suffix or IP address
38 
39   @domain = @domain_name.hostname
40 end
expired?(time = Time.now)
[show source]
    # File lib/httpx/plugins/cookies/cookie.rb
142 def expired?(time = Time.now)
143   return false unless expires
144 
145   expires <= time
146 end
expires()
[show source]
    # File lib/httpx/plugins/cookies/cookie.rb
138 def expires
139   @expires || (@created_at && @max_age ? @created_at + @max_age : nil)
140 end
match?(name_or_options)
[show source]
   # File lib/httpx/plugins/cookies/cookie.rb
59 def match?(name_or_options)
60   case name_or_options
61   when String
62     @name == name_or_options
63   when Hash, Array
64     name_or_options.all? { |k, v| respond_to?(k) && send(k) == v }
65   else
66     false
67   end
68 end
path=(path)

assigns a new path to this cookie.

[show source]
   # File lib/httpx/plugins/cookies/cookie.rb
18 def path=(path)
19   path = String(path)
20   @for_domain = false
21   @path = path.start_with?("/") ? path : "/"
22 end
valid_for_uri?(uri)

Tests if it is OK to send this cookie to a given ‘uri`. A RuntimeError is raised if the cookie’s domain is unknown.

[show source]
    # File lib/httpx/plugins/cookies/cookie.rb
157 def valid_for_uri?(uri)
158   uri = URI(uri)
159   # RFC 6265 5.4
160 
161   return false if @secure && uri.scheme != "https"
162 
163   acceptable_from_uri?(uri) && Cookie.path_match?(@path, uri.path)
164 end