Class: HTTPX::Plugins::Cookies::Cookie

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/httpx/plugins/cookies/cookie.rb

Overview

The HTTP Cookie.

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

Defined Under Namespace

Modules: Scanner

Constant Summary collapse

MAX_LENGTH =

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

4096

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arg, value, attrs = nil) ⇒ Cookie

Returns a new instance of Cookie.

Raises:



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/httpx/plugins/cookies/cookie.rb', line 114

def initialize(arg, value, attrs = nil)
  @created_at = Time.now

  @name = arg
  @value = value
  attr_hash = Hash.try_convert(attrs)

  attr_hash.each do |key, val|
    key = key.downcase.tr("-", "_").to_sym unless key.is_a?(Symbol)

    case key
    when :domain, :path
      __send__(:"#{key}=", val)
    else
      instance_variable_set(:"@#{key}", val)
    end
  end if attr_hash

  @path ||= "/"
  raise ArgumentError, "name must be specified" if @name.nil?

  @name = @name.to_s
end

Instance Attribute Details

#created_atObject (readonly)

Returns the value of attribute created_at.



15
16
17
# File 'lib/httpx/plugins/cookies/cookie.rb', line 15

def created_at
  @created_at
end

#domainObject

Returns the value of attribute domain.



15
16
17
# File 'lib/httpx/plugins/cookies/cookie.rb', line 15

def domain
  @domain
end

#nameObject (readonly)

Returns the value of attribute name.



15
16
17
# File 'lib/httpx/plugins/cookies/cookie.rb', line 15

def name
  @name
end

#pathObject

Returns the value of attribute path.



15
16
17
# File 'lib/httpx/plugins/cookies/cookie.rb', line 15

def path
  @path
end

#valueObject (readonly)

Returns the value of attribute value.



15
16
17
# File 'lib/httpx/plugins/cookies/cookie.rb', line 15

def value
  @value
end

Class Method Details

.new(cookie, *args) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/httpx/plugins/cookies/cookie.rb', line 71

def new(cookie, *args)
  case cookie
  when self
    cookie
  when Array, Hash
    options = Hash[cookie] #: cookie_attributes
    super(options[:name], options[:value], options)
  else

    super
  end
end

.path_match?(base_path, target_path) ⇒ Boolean

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

Returns:



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/httpx/plugins/cookies/cookie.rb', line 101

def path_match?(base_path, target_path)
  base_path.start_with?("/") || (return false)
  # RFC 6265 5.1.4
  bsize = base_path.size
  tsize = target_path.size
  return bsize == 1 if tsize.zero? # treat empty target_path as "/"
  return false unless target_path.start_with?(base_path)
  return true if bsize == tsize || base_path.end_with?("/")

  target_path[bsize] == "/"
end

Instance Method Details

#<=>(other) ⇒ Object

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.



51
52
53
54
55
56
57
# File 'lib/httpx/plugins/cookies/cookie.rb', line 51

def <=>(other)
  # RFC 6265 5.4
  # Precedence: 1. longer path  2. older creation
  (@name <=> other.name).nonzero? ||
    (other.path.length <=> @path.length).nonzero? ||
    (@created_at <=> other.created_at).nonzero? || 0
end

#==(other) ⇒ Object

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



44
45
46
47
# File 'lib/httpx/plugins/cookies/cookie.rb', line 44

def ==(other)
  @name == other.name && @value == other.value &&
    @path == other.path && @domain == other.domain
end

Returns a string for use in the Cookie header, i.e. ‘name=value` or `name=“value”`.



150
151
152
# File 'lib/httpx/plugins/cookies/cookie.rb', line 150

def cookie_value
  "#{@name}=#{Scanner.quote(@value.to_s)}"
end

#expired?(time = Time.now) ⇒ Boolean

Returns:



142
143
144
145
146
# File 'lib/httpx/plugins/cookies/cookie.rb', line 142

def expired?(time = Time.now)
  return false unless expires

  expires <= time
end

#expiresObject



138
139
140
# File 'lib/httpx/plugins/cookies/cookie.rb', line 138

def expires
  @expires || (@created_at && @max_age ? @created_at + @max_age : nil)
end

#match?(name_or_options) ⇒ Boolean

Returns:



59
60
61
62
63
64
65
66
67
68
# File 'lib/httpx/plugins/cookies/cookie.rb', line 59

def match?(name_or_options)
  case name_or_options
  when String
    @name == name_or_options
  when Hash, Array
    name_or_options.all? { |k, v| respond_to?(k) && send(k) == v }
  else
    false
  end
end

#valid_for_uri?(uri) ⇒ Boolean

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

Returns:



157
158
159
160
161
162
163
164
# File 'lib/httpx/plugins/cookies/cookie.rb', line 157

def valid_for_uri?(uri)
  uri = URI(uri)
  # RFC 6265 5.4

  return false if @secure && uri.scheme != "https"

  acceptable_from_uri?(uri) && Cookie.path_match?(@path, uri.path)
end