Class: Ronin::Support::Network::HTTP::SetCookie
- Defined in:
- lib/ronin/support/network/http/set_cookie.rb
Overview
Parses and generates Set-Cookie header values.
Constant Summary collapse
- SAME_SITE =
Mapping of
SameSitevalues to Symbols. { 'None' => :none, 'Strict' => :strict, 'Lax' => :lax }
Instance Attribute Summary collapse
-
#domain ⇒ String?
readonly
The
Domaincookie attribute. -
#expires ⇒ Time?
readonly
The
Expirescookie attribute. -
#http_only ⇒ true?
readonly
The
HttpOnlyflag. -
#max_age ⇒ Integer?
readonly
The
Max-Agecookie attribute. -
#path ⇒ String?
readonly
The
Pathcookie attribute. -
#same_site ⇒ :strict, ...
readonly
The
SameSitecookie attribute. -
#secure ⇒ true?
readonly
The
Secureflag.
Attributes inherited from Cookie
Class Method Summary collapse
-
.parse(string) ⇒ Cookie
Parses a
Set-Cookiestring.
Instance Method Summary collapse
-
#http_only? ⇒ Boolean
Determines if the
HttpOnlyflag is set. -
#initialize(params, expires: nil, max_age: nil, path: nil, domain: nil, http_only: nil, secure: nil, same_site: nil) ⇒ SetCookie
constructor
Initializes the
Set-Cookieobject. -
#secure? ⇒ Boolean
Determines if the
Secureflag is set. -
#to_s ⇒ String
Converts the cookie back into a
Set-Cookievalue.
Methods inherited from Cookie
#[], #[]=, #each, #empty?, escape, #has_param?, #initialize_copy, #merge, #merge!, #to_h, unescape
Methods included from Enumerable
Constructor Details
#initialize(params, expires: nil, max_age: nil, path: nil, domain: nil, http_only: nil, secure: nil, same_site: nil) ⇒ SetCookie
Initializes the Set-Cookie object.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/ronin/support/network/http/set_cookie.rb', line 97 def initialize(params, expires: nil, max_age: nil, path: nil, domain: nil, http_only: nil, secure: nil, same_site: nil) super(params) @expires = expires @max_age = max_age @path = path @domain = domain @http_only = http_only @secure = secure @same_site = same_site end |
Instance Attribute Details
#domain ⇒ String? (readonly)
The Domain cookie attribute.
49 50 51 |
# File 'lib/ronin/support/network/http/set_cookie.rb', line 49 def domain @domain end |
#expires ⇒ Time? (readonly)
The Expires cookie attribute.
44 45 46 |
# File 'lib/ronin/support/network/http/set_cookie.rb', line 44 def expires @expires end |
#http_only ⇒ true? (readonly)
The HttpOnly flag.
64 65 66 |
# File 'lib/ronin/support/network/http/set_cookie.rb', line 64 def http_only @http_only end |
#max_age ⇒ Integer? (readonly)
The Max-Age cookie attribute.
39 40 41 |
# File 'lib/ronin/support/network/http/set_cookie.rb', line 39 def max_age @max_age end |
#path ⇒ String? (readonly)
The Path cookie attribute.
54 55 56 |
# File 'lib/ronin/support/network/http/set_cookie.rb', line 54 def path @path end |
#same_site ⇒ :strict, ... (readonly)
The SameSite cookie attribute.
59 60 61 |
# File 'lib/ronin/support/network/http/set_cookie.rb', line 59 def same_site @same_site end |
#secure ⇒ true? (readonly)
The Secure flag.
69 70 71 |
# File 'lib/ronin/support/network/http/set_cookie.rb', line 69 def secure @secure end |
Class Method Details
.parse(string) ⇒ Cookie
Parses a Set-Cookie string.
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/ronin/support/network/http/set_cookie.rb', line 134 def self.parse(string) kwargs = {} params = {} string.split(/;\s+/) do |field| if field.include?('=') key, value = field.split('=',2) case key when 'Max-Age' then kwargs[:max_age] = value.to_i when 'Expires' then kwargs[:expires] = Time.parse(value) when 'Path' then kwargs[:path] = value when 'Domain' then kwargs[:domain] = value when 'SameSite' kwargs[:same_site] = SAME_SITE.fetch(value) do raise(ArgumentError,"unrecognized SameSite value: #{value.inspect}") end else params[unescape(key)] = unescape(value) end else case field when 'HttpOnly' then kwargs[:http_only] = true when 'Secure' then kwargs[:secure] = true else raise(ArgumentError,"unrecognized Cookie flag: #{field.inspect}") end end end return new(params,**kwargs) end |
Instance Method Details
#http_only? ⇒ Boolean
Determines if the HttpOnly flag is set.
172 173 174 |
# File 'lib/ronin/support/network/http/set_cookie.rb', line 172 def http_only? @http_only == true end |
#secure? ⇒ Boolean
Determines if the Secure flag is set.
181 182 183 |
# File 'lib/ronin/support/network/http/set_cookie.rb', line 181 def secure? @secure == true end |
#to_s ⇒ String
Converts the cookie back into a Set-Cookie value.
191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/ronin/support/network/http/set_cookie.rb', line 191 def to_s string = super() string << "; Max-Age=#{@max_age}" if @max_age string << "; Expires=#{@expires.httpdate}" if @expires string << "; Path=#{@path}" if @path string << "; Domain=#{@domain}" if @domain string << "; SameSite=#{@same_site.to_s.capitalize}" if @same_site if @secure then string << '; Secure' elsif @http_only then string << '; HttpOnly' end string end |