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_pathis underbase_pathas 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.
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 113 |
# File 'lib/httpx/plugins/cookies/cookie.rb', line 87 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.
15 16 17 |
# File 'lib/httpx/plugins/cookies/cookie.rb', line 15 def created_at @created_at end |
#domain ⇒ Object
Returns the value of attribute domain.
15 16 17 |
# File 'lib/httpx/plugins/cookies/cookie.rb', line 15 def domain @domain end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
15 16 17 |
# File 'lib/httpx/plugins/cookies/cookie.rb', line 15 def name @name end |
#path ⇒ Object
Returns the value of attribute path.
15 16 17 |
# File 'lib/httpx/plugins/cookies/cookie.rb', line 15 def path @path end |
#value ⇒ Object (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
51 52 53 54 55 |
# File 'lib/httpx/plugins/cookies/cookie.rb', line 51 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
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/httpx/plugins/cookies/cookie.rb', line 74 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.
42 43 44 45 46 47 48 |
# File 'lib/httpx/plugins/cookies/cookie.rb', line 42 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”`.
127 128 129 |
# File 'lib/httpx/plugins/cookies/cookie.rb', line 127 def "#{@name}=#{Scanner.quote(@value.to_s)}" end |
#expired?(time = Time.now) ⇒ Boolean
119 120 121 122 123 |
# File 'lib/httpx/plugins/cookies/cookie.rb', line 119 def expired?(time = Time.now) return false unless expires expires <= time end |
#expires ⇒ Object
115 116 117 |
# File 'lib/httpx/plugins/cookies/cookie.rb', line 115 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.
134 135 136 137 138 139 140 141 |
# File 'lib/httpx/plugins/cookies/cookie.rb', line 134 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 |