Class: HTTPX::Plugins::Cookies::Cookie
- Inherits:
-
Object
- Object
- HTTPX::Plugins::Cookies::Cookie
- 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
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#domain ⇒ Object
Returns the value of attribute domain.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#path ⇒ Object
Returns the value of attribute path.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
- .new(cookie, *args) ⇒ Object
-
.path_match?(base_path, target_path) ⇒ Boolean
Tests if
target_path
is underbase_path
as described in RFC 6265 5.1.4.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Compares the cookie with another.
-
#cookie_value ⇒ Object
(also: #to_s)
Returns a string for use in the Cookie header, i.e.
- #expired?(time = Time.now) ⇒ Boolean
- #expires ⇒ Object
-
#initialize(arg, *attrs) ⇒ Cookie
constructor
A new instance of Cookie.
-
#valid_for_uri?(uri) ⇒ Boolean
Tests if it is OK to send this cookie to a given ‘uri`.
Constructor Details
#initialize(arg, *attrs) ⇒ Cookie
Returns a new instance of Cookie.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/httpx/plugins/cookies/cookie.rb', line 86 def initialize(arg, *attrs) @created_at = Time.now if attrs.empty? attr_hash = Hash.try_convert(arg) else @name = arg @value, attr_hash = attrs attr_hash = Hash.try_convert(attr_hash) end 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_at ⇒ Object (readonly)
Returns the value of attribute created_at.
14 15 16 |
# File 'lib/httpx/plugins/cookies/cookie.rb', line 14 def created_at @created_at end |
#domain ⇒ Object
Returns the value of attribute domain.
14 15 16 |
# File 'lib/httpx/plugins/cookies/cookie.rb', line 14 def domain @domain end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
14 15 16 |
# File 'lib/httpx/plugins/cookies/cookie.rb', line 14 def name @name end |
#path ⇒ Object
Returns the value of attribute path.
14 15 16 |
# File 'lib/httpx/plugins/cookies/cookie.rb', line 14 def path @path end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
14 15 16 |
# File 'lib/httpx/plugins/cookies/cookie.rb', line 14 def value @value end |
Class Method Details
.new(cookie, *args) ⇒ Object
50 51 52 53 54 |
# File 'lib/httpx/plugins/cookies/cookie.rb', line 50 def new(, *args) return if .is_a?(self) super 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
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/httpx/plugins/cookies/cookie.rb', line 73 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.
41 42 43 44 45 46 47 |
# File 'lib/httpx/plugins/cookies/cookie.rb', line 41 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 |
#cookie_value ⇒ Object Also known as: to_s
Returns a string for use in the Cookie header, i.e. ‘name=value` or `name=“value”`.
126 127 128 |
# File 'lib/httpx/plugins/cookies/cookie.rb', line 126 def "#{@name}=#{Scanner.quote(@value.to_s)}" end |
#expired?(time = Time.now) ⇒ Boolean
118 119 120 121 122 |
# File 'lib/httpx/plugins/cookies/cookie.rb', line 118 def expired?(time = Time.now) return false unless expires expires <= time end |
#expires ⇒ Object
114 115 116 |
# File 'lib/httpx/plugins/cookies/cookie.rb', line 114 def expires @expires || (@created_at && @max_age ? @created_at + @max_age : nil) 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.
133 134 135 136 137 138 139 140 |
# File 'lib/httpx/plugins/cookies/cookie.rb', line 133 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 |