class RaptorIO::Protocol::HTTP::Headers

HTTP Headers, holds shared attributes of {Request} and {Response}.

For convenience, Hash-like getters and setters provide case-insensitive access.

@author Tasos Laskos <tasos_laskos@rapid7.com>

Public Class Methods

new( headers = {} ) click to toggle source

@param [Headers, Hash] headers

# File lib/raptor-io/protocol/http/headers.rb, line 17
def initialize( headers = {} )
  (headers || {}).each do |k, v|
    self[k] = v
  end
end
parse( headers_string ) click to toggle source

@param [String] headers_string @return [Headers]

# File lib/raptor-io/protocol/http/headers.rb, line 110
def self.parse( headers_string )
  return Headers.new if headers_string.to_s.empty?

  headers = Hash.new { |h, k| h[k] = [] }
  headers_string.split( CRLF_PATTERN ).each do |header|
    k, v = header.split( ':', 2 )
    headers[k.to_s.strip] << v.to_s.strip
  end

  headers.each { |k, v| headers[k] = v.first if v.size == 1 }
  new headers
end

Public Instance Methods

[]( field ) click to toggle source

@note ‘field` will be capitalized appropriately before storing. @param [String] field Field name @return [String] Field value.

Calls superclass method
# File lib/raptor-io/protocol/http/headers.rb, line 40
def []( field )
  super format_field_name( field.to_s.downcase )
end
[]=( field, value ) click to toggle source

@note ‘field` will be capitalized appropriately before storing. @param [String] field Field name @param [Array<String>, String] value Field value. @return [String] Field `value`.

Calls superclass method
# File lib/raptor-io/protocol/http/headers.rb, line 48
def []=( field, value )
  super format_field_name( field.to_s.downcase ),
        value.is_a?( Array ) ? value : value.to_s
end
cookies() click to toggle source

@return [Array<Hash>] Request cookies.

# File lib/raptor-io/protocol/http/headers.rb, line 79
def cookies
  return [] if !self['cookie']

  WEBrick::Cookie.parse( self['cookie'] ).flatten.uniq.map do |cookie|
    cookie_hash = {}
    cookie.instance_variables.each do |var|
      cookie_hash[var.to_s.gsub( /@/, '' ).to_sym] = cookie.instance_variable_get( var )
    end

    # Replace the string with a Time object.
    cookie_hash[:expires] = cookie.expires

    cookie_hash
  end
end
delete( field ) click to toggle source

@note ‘field` will be capitalized appropriately before storing. @param [String] field Field name @return [String] Field value.

Calls superclass method
# File lib/raptor-io/protocol/http/headers.rb, line 26
def delete( field )
  super format_field_name( field.to_s.downcase )
end
include?( field ) click to toggle source

@note ‘field` will be capitalized appropriately before storing. @param [String] field Field name @return [String] Field value.

Calls superclass method
# File lib/raptor-io/protocol/http/headers.rb, line 33
def include?( field )
  super format_field_name( field.to_s.downcase )
end
to_s() click to toggle source

@return [String] HTTP headers formatted for transmission.

# File lib/raptor-io/protocol/http/headers.rb, line 96
def to_s
  map { |k, v|
    if v.is_a? Array
      v.map do |cv|
        "#{k}: #{cv}"
      end
    else
      "#{k}: #{v}"
    end
  }.flatten.join( CRLF )
end

Private Instance Methods

format_field_name( field ) click to toggle source
# File lib/raptor-io/protocol/http/headers.rb, line 125
def format_field_name( field )
  field.to_s.split( '-' ).map( &:capitalize ).join( '-' )
end