Class: Ronin::Web::Browser::Cookie

Inherits:
Ferrum::Cookies::Cookie
  • Object
show all
Defined in:
lib/ronin/web/browser/cookie.rb

Overview

Represents a browser cookie.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse(string) ⇒ Cookie

Parses a browser cookie from a raw String.

Parameters:

  • string (String)

    The raw cookie String to parse.

Returns:

  • (Cookie)

    The parsed cookie.

Raises:

  • (ArgumentError)

    The string was empty or contains an unknown field.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ronin/web/browser/cookie.rb', line 43

def self.parse(string)
  fields = string.split(/;\s+/)

  if fields.empty?
    raise(ArgumentError,"cookie must not be empty: #{string.inspect}")
  end

  name, value = fields.shift.split('=',2)
  attributes  = {
    'name'  => name,
    'value' => value
  }

  fields.each do |field|
    if field.include?('=')
      key, value = field.split('=',2)

      case key
      when 'Expires', 'Max-Age'
        attributes['expires'] = Time.parse(value).to_i
      when 'Path'    then attributes['path']     = value
      when 'Domain'  then attributes['domain']   = value
      when 'SameSite'then attributes['sameSite'] = value
      else
        raise(ArgumentError,"unrecognized Cookie field: #{field.inspect}")
      end
    else
      case field
      when 'HttpOnly' then attributes['httpOnly'] = true
      when 'Secure'   then attributes['secure']   = true
      else
        raise(ArgumentError,"unrecognized Cookie flag: #{field.inspect}")
      end
    end
  end

  return new(attributes)
end

Instance Method Details

#priorityString

The priority of the cookie.

Returns:

  • (String)


87
88
89
# File 'lib/ronin/web/browser/cookie.rb', line 87

def priority
  @attributes['priority']
end

#sameparty?Boolean Also known as: same_party?

Returns:

  • (Boolean)


94
95
96
# File 'lib/ronin/web/browser/cookie.rb', line 94

def sameparty?
  @attributes['sameParty']
end

#source_portInteger

Returns:

  • (Integer)


110
111
112
# File 'lib/ronin/web/browser/cookie.rb', line 110

def source_port
  @attributes['sourcePort']
end

#source_schemeString

Returns:

  • (String)


103
104
105
# File 'lib/ronin/web/browser/cookie.rb', line 103

def source_scheme
  @attributes['sourceScheme']
end

#to_sString

Converts the cookie back into a raw cookie String.

Returns:

  • (String)

    The raw cookie string.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/ronin/web/browser/cookie.rb', line 120

def to_s
  string = "#{@attributes['name']}=#{@attributes['value']}"

  @attributes.each do |key,value|
    case key
    when 'name', 'value' # no-op
    when 'domain'   then string << "; Domain=#{value}"
    when 'path'     then string << "; Path=#{value}"
    when 'expires'  then string << "; Expires=#{Time.at(value).httpdate}"
    when 'httpOnly' then string << "; httpOnly" if value
    when 'secure'   then string << "; Secure"   if value
    end
  end

  return string
end