class Arachni::HTTP::Headers

HTTP Headers.

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

@author Tasos Laskos <tasos.laskos@arachni-scanner.com>

Constants

CONTENT_TYPE
FORMATTED_NAMES_CACHE
LOCATION

Public Class Methods

new( headers = {} ) click to toggle source

@param [Headers, Hash] headers

# File lib/arachni/http/headers.rb, line 28
def initialize( headers = {} )
    merge!( headers || {} )
end

Private Class Methods

format_field_name( field ) click to toggle source
# File lib/arachni/http/headers.rb, line 142
def self.format_field_name( field )
    # If there's a '--' somewhere in there then skip it, it probably is an
    # audit payload.
    return field if field.include?( '--' )

    FORMATTED_NAMES_CACHE.fetch field do
        field.split( '-' ).map( &:capitalize ).join( '-' )
    end
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/arachni/http/headers.rb, line 80
def []( field )
    super format_field_name( field.to_s.downcase ).freeze
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/arachni/http/headers.rb, line 93
def []=( field, value )
    super format_field_name( field.to_s.downcase ).freeze,
          value.is_a?( Array ) ? value : value.to_s.freeze
end
content_type() click to toggle source

@return [String, nil]

Value of the `Content-Type` field.
# File lib/arachni/http/headers.rb, line 100
def content_type
    (ct = self[CONTENT_TYPE]).is_a?( Array ) ? ct.first : ct
end
cookies() click to toggle source

@return [Array<Hash>]

Cookies as hashes.
# File lib/arachni/http/headers.rb, line 119
def cookies
    return [] if set_cookie.empty?

    set_cookie.map do |set_cookie_string|
        WEBrick::Cookie.parse_set_cookies( set_cookie_string ).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.flatten.compact
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/arachni/http/headers.rb, line 58
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/arachni/http/headers.rb, line 69
def include?( field )
    super format_field_name( field.to_s.downcase )
end
location() click to toggle source

@return [String, nil]

Value of the `Location` field.
# File lib/arachni/http/headers.rb, line 106
def location
    self[LOCATION]
end
merge( headers, convert_to_array = true ) click to toggle source
# File lib/arachni/http/headers.rb, line 45
def merge( headers, convert_to_array = true )
    d = dup
    d.merge! headers, convert_to_array
    d
end
merge!( headers, convert_to_array = true ) click to toggle source
# File lib/arachni/http/headers.rb, line 32
def merge!( headers, convert_to_array = true )
    headers.each do |k, v|
        # Handle headers with identical normalized names, like a mixture of
        # Set-Cookie and SET-COOKIE.
        if convert_to_array && include?( k )
            self[k] = [self[k]].flatten
            self[k] << v
        else
            self[k] = v
        end
    end
end

Private Instance Methods

format_field_name( field ) click to toggle source
# File lib/arachni/http/headers.rb, line 138
def format_field_name( field )
    self.class.format_field_name( field )
end