class Ronin::Support::Network::HTTP::Cookie

Parses and generates ‘Cookie` header values.

@api public

@since 1.0.0

Attributes

params[R]

Parameters of the cookie.

@return [Hash{String => String}]

Public Class Methods

escape(string) click to toggle source

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

@param [String] string

The string to escape.

@return [String]

The escaped String.
# File lib/ronin/support/network/http/cookie.rb, line 72
def self.escape(string)
  URI.encode_www_form_component(string)
end
new(params={}) click to toggle source

Initializes the ‘Cookie` header.

@param [Hash{String => String}] params

The params for the cookie.
# File lib/ronin/support/network/http/cookie.rb, line 48
def initialize(params={})
  @params = params
end
parse(string) click to toggle source

Parses a ‘Cookie` string.

@param [String] string

The raw `Cookie` string.

@return [Cookie]

The parsed cookie.
# 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) click to toggle source

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

@param [String] string

The String to unescape.

@param [Encoding] encoding

The optional String encoding to use.

@return [String]

The unescaped String.
# 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

Public Instance Methods

[](name) click to toggle source

Fetches a param from the cookie.

@param [String] name

The name of the param.

@return [String, nil]

The value of the param in the cookie.
# File lib/ronin/support/network/http/cookie.rb, line 134
def [](name)
  @params[name.to_s]
end
[]=(name,value) click to toggle source

Sets a param in the cookie.

@param [String] name

The param name to set.

@param [#to_s] value

The param value to set.

@return [#to_s]

The set param value.
# 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(&block) click to toggle source

Enumerates over the params in the cookie.

@yield [name,value]

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

@yieldparam [String] name

The name of the cookie param.

@yieldparam [String] value

The value of the cookie param.

@return [Enumerator]

If no block is given, an Enumerator will be returned.
# File lib/ronin/support/network/http/cookie.rb, line 177
def each(&block)
  @params.each(&block)
end
empty?() click to toggle source

Determines if the cookie is empty.

@return [Boolean]

# File lib/ronin/support/network/http/cookie.rb, line 215
def empty?
  @params.empty?
end
has_param?(name) click to toggle source

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

@param [String, Symbol] name

The param name to check for.

@return [Boolean]

# File lib/ronin/support/network/http/cookie.rb, line 121
def has_param?(name)
  @params.has_key?(name.to_s)
end
initialize_copy(other) click to toggle source

Initializes a copy of a cookie.

@param [Cookie] other

The original cookie that is being copied.
# File lib/ronin/support/network/http/cookie.rb, line 58
def initialize_copy(other)
  @params = other.params.dup
end
merge(params) click to toggle source

Merges the cookie with other cookie params.

@param [Cookie, Hash] params

The other cookie parmas to merge.

@return [Cookie]

The new combined cookie.
# File lib/ronin/support/network/http/cookie.rb, line 206
def merge(params)
  clone.merge!(params)
end
merge!(params) click to toggle source

Merges other cookie params into the cookie.

@param [Cookie, Hash] params

The other cookie params to merge into the cookie.

@return [self]

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

Converts the cookie into a Hash of names and values.

@return [Hash{String => String}]

The params of the cookie.
# File lib/ronin/support/network/http/cookie.rb, line 225
def to_h
  @params
end
to_s() click to toggle source

Converts the cookie back into a ‘Cookie` value.

@return [String]

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