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( 100 )
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:



74
75
76
# File 'lib/arachni/http/headers.rb', line 74

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:

  • (String)

    Field ‘value`.



87
88
89
90
# File 'lib/arachni/http/headers.rb', line 87

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.



94
95
96
# File 'lib/arachni/http/headers.rb', line 94

def content_type
    self[CONTENT_TYPE]
end

#cookiesArray<Hash>

Returns Cookies as hashes.

Returns:



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/arachni/http/headers.rb', line 113

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:



52
53
54
# File 'lib/arachni/http/headers.rb', line 52

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:



63
64
65
# File 'lib/arachni/http/headers.rb', line 63

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.



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

def location
    self[LOCATION]
end

#merge!(headers) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/arachni/http/headers.rb', line 32

def merge!( headers )
    headers.each do |k, v|
        # Handle headers with identical normalized names, like a mixture of
        # Set-Cookie and SET-COOKIE.
        if include? k
            self[k] = [self[k]].flatten
            self[k] << v
        else
            self[k] = v
        end
    end
end

Returns Set-cookie strings.

Returns:



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

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