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

domain[R]

The ‘Domain` cookie attribute.

@return [String, nil]

expires[R]

The ‘Expires` cookie attribute.

@return [Time, nil]

http_only[R]

The ‘HttpOnly` flag.

@return [true, nil]

max_age[R]

The ‘Max-Age` cookie attribute.

@return [Integer, nil]

path[R]

The ‘Path` cookie attribute.

@return [String, nil]

same_site[R]

The ‘SameSite` cookie attribute.

@return [:strict, :lax, :none]

secure[R]

The ‘Secure` flag.

@return [true, nil]

Public Class Methods

new(params, expires: nil, max_age: nil, path: nil, domain: nil, http_only: nil, secure: nil, same_site: nil) click to toggle source

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.
# 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
parse(string) click to toggle source

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

http_only?() click to toggle source

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
secure?() click to toggle source

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
to_s() click to toggle source

Converts the cookie back into a ‘Set-Cookie` value.

@return [String]

The formatted cookie.
# 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