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:

Instance Method Summary collapse

Methods inherited from Hash

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

Constructor Details

#initialize(headers = {}) ⇒ Headers

Returns a new instance of Headers.

Parameters:



22
23
24
# File 'lib/arachni/http/headers.rb', line 22

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:



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

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`.



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

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.



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

def content_type
    self['content-type']
end

#cookiesArray<Hash>

Returns Cookies as hashes.

Returns:



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/arachni/http/headers.rb', line 98

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:



37
38
39
# File 'lib/arachni/http/headers.rb', line 37

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:



48
49
50
# File 'lib/arachni/http/headers.rb', line 48

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.



85
86
87
# File 'lib/arachni/http/headers.rb', line 85

def location
    self['location']
end

#merge!(headers) ⇒ Object



26
27
28
# File 'lib/arachni/http/headers.rb', line 26

def merge!( headers )
    headers.each { |k, v| self[k] = v }
end

Returns Set-cookie strings.

Returns:



91
92
93
94
# File 'lib/arachni/http/headers.rb', line 91

def set_cookie
    return [] if self['set-cookie'].to_s.empty?
    [self['set-cookie']].flatten
end