Class: Arachni::HTTP::Headers

Inherits:
Hash show all
Defined in:
lib/arachni/http/headers.rb

Overview

HTTP Headers.

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

Author:

Constant Summary collapse

FORMATTED_NAMES_CACHE =
Support::Cache::LeastRecentlyPushed.new( 1_000 )
CONTENT_TYPE =
'content-type'
'set-cookie'
LOCATION =
'location'

Instance Method Summary collapse

Methods inherited from Hash

#apply_recursively, #downcase, #find_symbol_keys_recursively, #my_stringify, #my_stringify_keys, #my_symbolize_keys, #recode, #recode!, #stringify_recursively_and_freeze

Constructor Details

#initialize(headers = {}) ⇒ Headers

Returns a new instance of Headers.

Parameters:



28
29
30
# File 'lib/arachni/http/headers.rb', line 28

def initialize( headers = {} )
    merge!( headers || {} )
end

Instance Method Details

#[](field) ⇒ String

Note:

field will be capitalized appropriately before storing.

Returns Field value.

Parameters:

  • field (String)

    Field name

Returns:



80
81
82
# File 'lib/arachni/http/headers.rb', line 80

def []( field )
    super format_field_name( field.to_s.downcase ).freeze
end

#[]=(field, value) ⇒ String

Note:

field will be capitalized appropriately before storing.

Returns Field value.

Parameters:

Returns:



93
94
95
96
# 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_typeString?

Returns Value of the Content-Type field.

Returns:

  • (String, nil)

    Value of the Content-Type field.



100
101
102
# File 'lib/arachni/http/headers.rb', line 100

def content_type
    (ct = self[CONTENT_TYPE]).is_a?( Array ) ? ct.first : ct
end

#cookiesArray<Hash>

Returns Cookies as hashes.

Returns:



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# 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) ⇒ String

Note:

field will be capitalized appropriately before storing.

Returns Field value.

Parameters:

  • field (String)

    Field name

Returns:



58
59
60
# File 'lib/arachni/http/headers.rb', line 58

def delete( field )
    super format_field_name( field.to_s.downcase )
end

#include?(field) ⇒ String

Note:

field will be capitalized appropriately before storing.

Returns Field value.

Parameters:

  • field (String)

    Field name

Returns:



69
70
71
# File 'lib/arachni/http/headers.rb', line 69

def include?( field )
    super format_field_name( field.to_s.downcase )
end

#locationString?

Returns Value of the Location field.

Returns:

  • (String, nil)

    Value of the Location field.



106
107
108
# File 'lib/arachni/http/headers.rb', line 106

def location
    self[LOCATION]
end

#merge(headers, convert_to_array = true) ⇒ Object



45
46
47
48
49
# 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) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# 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

Returns Set-cookie strings.

Returns:



112
113
114
115
# File 'lib/arachni/http/headers.rb', line 112

def set_cookie
    return [] if self[SET_COOKIE].to_s.empty?
    [self[SET_COOKIE]].flatten
end