Class: Arachni::HTTP::Headers
Overview
HTTP Headers.
For convenience, Hash-like getters and setters provide case-insensitive access.
Constant Summary collapse
- FORMATTED_NAMES_CACHE =
Support::Cache::LeastRecentlyPushed.new( 1_000 )
- CONTENT_TYPE =
'content-type'
- SET_COOKIE =
'set-cookie'
- LOCATION =
'location'
Instance Method Summary collapse
-
#[](field) ⇒ String
Field value.
-
#[]=(field, value) ⇒ String
Field `value`.
-
#content_type ⇒ String?
Value of the `Content-Type` field.
-
#cookies ⇒ Array<Hash>
Cookies as hashes.
-
#delete(field) ⇒ String
Field value.
-
#include?(field) ⇒ String
Field value.
-
#initialize(headers = {}) ⇒ Headers
constructor
A new instance of Headers.
-
#location ⇒ String?
Value of the `Location` field.
- #merge(headers, convert_to_array = true) ⇒ Object
- #merge!(headers, convert_to_array = true) ⇒ Object
-
#set_cookie ⇒ Array<String>
Set-cookie strings.
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.
28 29 30 |
# File 'lib/arachni/http/headers.rb', line 28 def initialize( headers = {} ) merge!( headers || {} ) end |
Instance Method Details
#[](field) ⇒ String
`field` will be capitalized appropriately before storing.
Returns Field value.
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
`field` will be capitalized appropriately before storing.
Returns Field `value`.
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_type ⇒ String?
Returns 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 |
#cookies ⇒ Array<Hash>
Returns Cookies as hashes.
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 return [] if .empty? .map do || WEBrick::Cookie.( ).flatten.uniq.map do || = {} .instance_variables.each do |var| [var.to_s.gsub( /@/, '' ).to_sym] = .instance_variable_get( var ) end # Replace the string with a Time object. [:expires] = .expires end end.flatten.compact end |
#delete(field) ⇒ String
`field` will be capitalized appropriately before storing.
Returns Field value.
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
`field` will be capitalized appropriately before storing.
Returns Field value.
69 70 71 |
# File 'lib/arachni/http/headers.rb', line 69 def include?( field ) super format_field_name( field.to_s.downcase ) end |
#location ⇒ String?
Returns 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 |
#set_cookie ⇒ Array<String>
Returns Set-cookie strings.
112 113 114 115 |
# File 'lib/arachni/http/headers.rb', line 112 def return [] if self[SET_COOKIE].to_s.empty? [self[SET_COOKIE]].flatten end |