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
50 def new(cookie, *args)
51   return cookie if cookie.is_a?(self)
52 
53   super
54 end
new(arg, *attrs)
[show source]
    # File lib/httpx/plugins/cookies/cookie.rb
 86 def initialize(arg, *attrs)
 87   @created_at = Time.now
 88 
 89   if attrs.empty?
 90     attr_hash = Hash.try_convert(arg)
 91   else
 92     @name = arg
 93     @value, attr_hash = attrs
 94     attr_hash = Hash.try_convert(attr_hash)
 95   end
 96 
 97   attr_hash.each do |key, val|
 98     key = key.downcase.tr("-", "_").to_sym unless key.is_a?(Symbol)
 99 
100     case key
101     when :domain, :path
102       __send__(:"#{key}=", val)
103     else
104       instance_variable_set(:"@#{key}", val)
105     end
106   end if attr_hash
107 
108   @path ||= "/"
109   raise ArgumentError, "name must be specified" if @name.nil?
110 
111   @name = @name.to_s
112 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
73 def path_match?(base_path, target_path)
74   base_path.start_with?("/") || (return false)
75   # RFC 6265 5.1.4
76   bsize = base_path.size
77   tsize = target_path.size
78   return bsize == 1 if tsize.zero? # treat empty target_path as "/"
79   return false unless target_path.start_with?(base_path)
80   return true if bsize == tsize || base_path.end_with?("/")
81 
82   target_path[bsize] == "/"
83 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
41 def <=>(other)
42   # RFC 6265 5.4
43   # Precedence: 1. longer path  2. older creation
44   (@name <=> other.name).nonzero? ||
45     (other.path.length <=> @path.length).nonzero? ||
46     (@created_at <=> other.created_at).nonzero? || 0
47 end
domain=(domain)

See domain.

[show source]
   # File lib/httpx/plugins/cookies/cookie.rb
22 def domain=(domain)
23   domain = String(domain)
24 
25   if domain.start_with?(".")
26     @for_domain = true
27     domain = domain[1..-1]
28   end
29 
30   return if domain.empty?
31 
32   @domain_name = DomainName.new(domain)
33   # RFC 6265 5.3 5.
34   @for_domain = false if @domain_name.domain.nil? # a public suffix or IP address
35 
36   @domain = @domain_name.hostname
37 end
expired?(time = Time.now)
[show source]
    # File lib/httpx/plugins/cookies/cookie.rb
118 def expired?(time = Time.now)
119   return false unless expires
120 
121   expires <= time
122 end
expires()
[show source]
    # File lib/httpx/plugins/cookies/cookie.rb
114 def expires
115   @expires || (@created_at && @max_age ? @created_at + @max_age : nil)
116 end
path=(path)
[show source]
   # File lib/httpx/plugins/cookies/cookie.rb
16 def path=(path)
17   path = String(path)
18   @path = path.start_with?("/") ? path : "/"
19 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
133 def valid_for_uri?(uri)
134   uri = URI(uri)
135   # RFC 6265 5.4
136 
137   return false if @secure && uri.scheme != "https"
138 
139   acceptable_from_uri?(uri) && Cookie.path_match?(@path, uri.path)
140 end