The HTTP Cookie.
Contains the single cookie info: name, value and attributes.
Methods
Public Class
Public Instance
Included modules
- Comparable
Classes and Modules
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 51 def new(cookie, *args) 52 return cookie if cookie.is_a?(self) 53 54 super 55 end
new(arg, *attrs)
[show source]
# File lib/httpx/plugins/cookies/cookie.rb 87 def initialize(arg, *attrs) 88 @created_at = Time.now 89 90 if attrs.empty? 91 attr_hash = Hash.try_convert(arg) 92 else 93 @name = arg 94 @value, attr_hash = attrs 95 attr_hash = Hash.try_convert(attr_hash) 96 end 97 98 attr_hash.each do |key, val| 99 key = key.downcase.tr("-", "_").to_sym unless key.is_a?(Symbol) 100 101 case key 102 when :domain, :path 103 __send__(:"#{key}=", val) 104 else 105 instance_variable_set(:"@#{key}", val) 106 end 107 end if attr_hash 108 109 @path ||= "/" 110 raise ArgumentError, "name must be specified" if @name.nil? 111 112 @name = @name.to_s 113 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 74 def path_match?(base_path, target_path) 75 base_path.start_with?("/") || (return false) 76 # RFC 6265 5.1.4 77 bsize = base_path.size 78 tsize = target_path.size 79 return bsize == 1 if tsize.zero? # treat empty target_path as "/" 80 return false unless target_path.start_with?(base_path) 81 return true if bsize == tsize || base_path.end_with?("/") 82 83 target_path[bsize] == "/" 84 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 42 def <=>(other) 43 # RFC 6265 5.4 44 # Precedence: 1. longer path 2. older creation 45 (@name <=> other.name).nonzero? || 46 (other.path.length <=> @path.length).nonzero? || 47 (@created_at <=> other.created_at).nonzero? || 0 48 end
cookie_value()
Returns a string for use in the Cookie header, i.e. ‘name=value` or `name=“value”`.
[show source]
# File lib/httpx/plugins/cookies/cookie.rb 127 def cookie_value 128 "#{@name}=#{Scanner.quote(@value.to_s)}" 129 end
domain=(domain)
See domain.
[show source]
# File lib/httpx/plugins/cookies/cookie.rb 23 def domain=(domain) 24 domain = String(domain) 25 26 if domain.start_with?(".") 27 @for_domain = true 28 domain = domain[1..-1] 29 end 30 31 return if domain.empty? 32 33 @domain_name = DomainName.new(domain) 34 # RFC 6265 5.3 5. 35 @for_domain = false if @domain_name.domain.nil? # a public suffix or IP address 36 37 @domain = @domain_name.hostname 38 end
expired?(time = Time.now)
[show source]
# File lib/httpx/plugins/cookies/cookie.rb 119 def expired?(time = Time.now) 120 return false unless expires 121 122 expires <= time 123 end
expires()
[show source]
# File lib/httpx/plugins/cookies/cookie.rb 115 def expires 116 @expires || (@created_at && @max_age ? @created_at + @max_age : nil) 117 end
path=(path)
[show source]
# File lib/httpx/plugins/cookies/cookie.rb 17 def path=(path) 18 path = String(path) 19 @path = path.start_with?("/") ? path : "/" 20 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 134 def valid_for_uri?(uri) 135 uri = URI(uri) 136 # RFC 6265 5.4 137 138 return false if @secure && uri.scheme != "https" 139 140 acceptable_from_uri?(uri) && Cookie.path_match?(@path, uri.path) 141 end