class Ronin::Support::Network::HTTP::SetCookie
Parses and generates ‘Set-Cookie` header values.
@api public
@since 1.0.0
Constants
- SAME_SITE
Mapping of ‘SameSite` values to Symbols.
Attributes
The ‘Domain` cookie attribute.
@return [String, nil]
The ‘Expires` cookie attribute.
@return [Time, nil]
The ‘HttpOnly` flag.
@return [true, nil]
The ‘Max-Age` cookie attribute.
@return [Integer, nil]
The ‘Path` cookie attribute.
@return [String, nil]
The ‘SameSite` cookie attribute.
@return [:strict, :lax, :none]
The ‘Secure` flag.
@return [true, nil]
Public Class Methods
Initializes the ‘Set-Cookie` object.
@param [Hash{String => String}] params
@param [Time, nil] expires
The parsed `Expires` value.
@param [Integer, nil] max_age
The parsed `Max-Age` value.
@param [String, nil] path
The parsed `Path` value.
@param [String, nil] domain
The parsed `Domain` value.
@param [true, nil] http_only
Indicates the `HttpOnly` flag is enabled.
@param [true, nil] secure
Indicates the `Secure` flag is enabled.
@param [:strict, :lax, :none, nil] same_site
The parsed `SameSite` value.
Ronin::Support::Network::HTTP::Cookie::new
# 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
Parses a ‘Set-Cookie` string.
@param [String] string
The raw `Set-Cookie` string.
@return [Cookie]
The parsed cookie.
@raise [ArgumentError]
The string contained an unknown `SameSite` value or flag.
# 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
Public Instance Methods
Determines if the ‘HttpOnly` flag is set.
@return [Boolean]
# File lib/ronin/support/network/http/set_cookie.rb, line 172 def http_only? @http_only == true end
Determines if the ‘Secure` flag is set.
@return [Boolean]
# File lib/ronin/support/network/http/set_cookie.rb, line 181 def secure? @secure == true end
Converts the cookie back into a ‘Set-Cookie` value.
@return [String]
The formatted cookie.
Ronin::Support::Network::HTTP::Cookie#to_s
# 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