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 = {} )
    (headers || {}).each { |k, v| self[k] = v }
end

Instance Method Details

#[](field) ⇒ String

Note:

‘field` will be capitalized appropriately before storing.

Returns Field value.

Parameters:

  • field (String)

    Field name

Returns:



55
56
57
# File 'lib/arachni/http/headers.rb', line 55

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



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

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.



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

def content_type
    self['content-type']
end

#cookiesArray<Hash>

Returns Cookies as hashes.

Returns:



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/arachni/http/headers.rb', line 94

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:



33
34
35
# File 'lib/arachni/http/headers.rb', line 33

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:



44
45
46
# File 'lib/arachni/http/headers.rb', line 44

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.



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

def location
    self['location']
end

Returns Set-cookie strings.

Returns:



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

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