class RaptorIO::Protocol::HTTP::Headers
HTTP
Headers
, holds shared attributes of {Request} and {Response}.
For convenience, Hash-like getters and setters provide case-insensitive access.
@author Tasos Laskos <tasos_laskos@rapid7.com>
Public Class Methods
@param [Headers, Hash] headers
# File lib/raptor-io/protocol/http/headers.rb, line 17 def initialize( headers = {} ) (headers || {}).each do |k, v| self[k] = v end end
@param [String] headers_string @return [Headers]
# File lib/raptor-io/protocol/http/headers.rb, line 110 def self.parse( headers_string ) return Headers.new if headers_string.to_s.empty? headers = Hash.new { |h, k| h[k] = [] } headers_string.split( CRLF_PATTERN ).each do |header| k, v = header.split( ':', 2 ) headers[k.to_s.strip] << v.to_s.strip end headers.each { |k, v| headers[k] = v.first if v.size == 1 } new headers end
Public Instance Methods
@note ‘field` will be capitalized appropriately before storing. @param [String] field Field name @return [String] Field value.
# File lib/raptor-io/protocol/http/headers.rb, line 40 def []( field ) super format_field_name( field.to_s.downcase ) end
@note ‘field` will be capitalized appropriately before storing. @param [String] field Field name @param [Array<String>, String] value Field value. @return [String] Field `value`.
# File lib/raptor-io/protocol/http/headers.rb, line 48 def []=( field, value ) super format_field_name( field.to_s.downcase ), value.is_a?( Array ) ? value : value.to_s end
@note ‘field` will be capitalized appropriately before storing. @param [String] field Field name @return [String] Field value.
# File lib/raptor-io/protocol/http/headers.rb, line 26 def delete( field ) super format_field_name( field.to_s.downcase ) end
@note ‘field` will be capitalized appropriately before storing. @param [String] field Field name @return [String] Field value.
# File lib/raptor-io/protocol/http/headers.rb, line 33 def include?( field ) super format_field_name( field.to_s.downcase ) end
@return [String] HTTP
headers formatted for transmission.
# File lib/raptor-io/protocol/http/headers.rb, line 96 def to_s map { |k, v| if v.is_a? Array v.map do |cv| "#{k}: #{cv}" end else "#{k}: #{v}" end }.flatten.join( CRLF ) end
Private Instance Methods
# File lib/raptor-io/protocol/http/headers.rb, line 125 def format_field_name( field ) field.to_s.split( '-' ).map( &:capitalize ).join( '-' ) end