Class: Ronin::Support::Network::HTTP::Cookie

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ronin/support/network/http/cookie.rb

Overview

Parses and generates Cookie header values.

Since:

  • 1.0.0

Direct Known Subclasses

SetCookie

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#map_hash

Constructor Details

#initialize(params = {}) ⇒ Cookie

Initializes the Cookie header.

Parameters:

  • params (Hash{String => String}) (defaults to: {})

    The params for the cookie.

Since:

  • 1.0.0



48
49
50
# File 'lib/ronin/support/network/http/cookie.rb', line 48

def initialize(params={})
  @params = params
end

Instance Attribute Details

#paramsHash{String => String} (readonly)

Parameters of the cookie.

Returns:

Since:

  • 1.0.0



40
41
42
# File 'lib/ronin/support/network/http/cookie.rb', line 40

def params
  @params
end

Class Method Details

.escape(string) ⇒ String

Escapes the string so that it can be embedded in a Cookie or Set-Cookie header.

Parameters:

  • string (String)

    The string to escape.

Returns:

  • (String)

    The escaped String.

Since:

  • 1.0.0



72
73
74
# File 'lib/ronin/support/network/http/cookie.rb', line 72

def self.escape(string)
  URI.encode_www_form_component(string)
end

.parse(string) ⇒ Cookie

Parses a Cookie string.

Parameters:

  • string (String)

    The raw Cookie string.

Returns:

  • (Cookie)

    The parsed cookie.

Since:

  • 1.0.0



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/ronin/support/network/http/cookie.rb', line 101

def self.parse(string)
  params = {}

  string.split(/;\s+/) do |field|
    key, value = field.split('=',2)

    params[unescape(key)] = unescape(value)
  end

  return new(params)
end

.unescape(string, encoding = Encoding::UTF_8) ⇒ String

Unescapes a string that came from a Cookie or Set-Cookie header.

Parameters:

  • string (String)

    The String to unescape.

  • encoding (Encoding) (defaults to: Encoding::UTF_8)

    The optional String encoding to use.

Returns:

  • (String)

    The unescaped String.

Since:

  • 1.0.0



88
89
90
# File 'lib/ronin/support/network/http/cookie.rb', line 88

def self.unescape(string,encoding=Encoding::UTF_8)
  URI.decode_www_form_component(string,encoding)
end

Instance Method Details

#[](name) ⇒ String?

Fetches a param from the cookie.

Parameters:

  • name (String)

    The name of the param.

Returns:

  • (String, nil)

    The value of the param in the cookie.

Since:

  • 1.0.0



134
135
136
# File 'lib/ronin/support/network/http/cookie.rb', line 134

def [](name)
  @params[name.to_s]
end

#[]=(name, value) ⇒ #to_s

Sets a param in the cookie.

Parameters:

  • name (String)

    The param name to set.

  • value (#to_s)

    The param value to set.

Returns:

  • (#to_s)

    The set param value.

Since:

  • 1.0.0



150
151
152
153
154
155
156
157
158
159
# File 'lib/ronin/support/network/http/cookie.rb', line 150

def []=(name,value)
  name = name.to_s

  case value
  when nil then @params.delete(name)
  else          @params[name] = value.to_s
  end

  return value
end

#each {|name, value| ... } ⇒ Enumerator

Enumerates over the params in the cookie.

Yields:

  • (name, value)

    If a block is given, then it will be passed each param name and corresponding value.

Yield Parameters:

  • name (String)

    The name of the cookie param.

  • value (String)

    The value of the cookie param.

Returns:

  • (Enumerator)

    If no block is given, an Enumerator will be returned.

Since:

  • 1.0.0



177
178
179
# File 'lib/ronin/support/network/http/cookie.rb', line 177

def each(&block)
  @params.each(&block)
end

#empty?Boolean

Determines if the cookie is empty.

Returns:

  • (Boolean)

Since:

  • 1.0.0



215
216
217
# File 'lib/ronin/support/network/http/cookie.rb', line 215

def empty?
  @params.empty?
end

#has_param?(name) ⇒ Boolean

Determines if the cookie has the param with the given name.

Parameters:

  • name (String, Symbol)

    The param name to check for.

Returns:

  • (Boolean)

Since:

  • 1.0.0



121
122
123
# File 'lib/ronin/support/network/http/cookie.rb', line 121

def has_param?(name)
  @params.has_key?(name.to_s)
end

#initialize_copy(other) ⇒ Object

Initializes a copy of a cookie.

Parameters:

  • other (Cookie)

    The original cookie that is being copied.

Since:

  • 1.0.0



58
59
60
# File 'lib/ronin/support/network/http/cookie.rb', line 58

def initialize_copy(other)
  @params = other.params.dup
end

#merge(params) ⇒ Cookie

Merges the cookie with other cookie params.

Parameters:

  • params (Cookie, Hash)

    The other cookie parmas to merge.

Returns:

  • (Cookie)

    The new combined cookie.

Since:

  • 1.0.0



206
207
208
# File 'lib/ronin/support/network/http/cookie.rb', line 206

def merge(params)
  clone.merge!(params)
end

#merge!(params) ⇒ self

Merges other cookie params into the cookie.

Parameters:

  • params (Cookie, Hash)

    The other cookie params to merge into the cookie.

Returns:

  • (self)

Since:

  • 1.0.0



189
190
191
192
193
194
195
# File 'lib/ronin/support/network/http/cookie.rb', line 189

def merge!(params)
  params.each do |name,value|
    self[name] = value
  end

  return self
end

#to_hHash{String => String}

Converts the cookie into a Hash of names and values.

Returns:

Since:

  • 1.0.0



225
226
227
# File 'lib/ronin/support/network/http/cookie.rb', line 225

def to_h
  @params
end

#to_sString

Converts the cookie back into a Cookie value.

Returns:

  • (String)

    The formatted cookie.

Since:

  • 1.0.0



235
236
237
238
239
# File 'lib/ronin/support/network/http/cookie.rb', line 235

def to_s
  @params.map { |name,value|
    "#{self.class.escape(name)}=#{self.class.escape(value)}"
  }.join('; ')
end