Class: RaptorIO::Protocol::HTTP::Headers

Inherits:
Hash
  • Object
show all
Defined in:
lib/raptor-io/protocol/http/headers.rb

Overview

HTTP Headers, holds shared attributes of Request and Response.

For convenience, Hash-like getters and setters provide case-insensitive access.

Author:

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Hash

#stringify

Constructor Details

#initialize(headers = {}) ⇒ Headers

Returns a new instance of Headers.

Parameters:



17
18
19
20
21
# File 'lib/raptor-io/protocol/http/headers.rb', line 17

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

Class Method Details

.parse(headers_string) ⇒ Headers

Parameters:

Returns:



110
111
112
113
114
115
116
117
118
119
120
121
# 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

Instance Method Details

#[](field) ⇒ String

Note:

‘field` will be capitalized appropriately before storing.

Returns Field value.

Parameters:

  • field (String)

    Field name

Returns:



40
41
42
# File 'lib/raptor-io/protocol/http/headers.rb', line 40

def []( field )
  super format_field_name( field.to_s.downcase )
end

#[]=(field, value) ⇒ String

Note:

‘field` will be capitalized appropriately before storing.

Returns Field ‘value`.

Parameters:

Returns:

  • (String)

    Field ‘value`.



48
49
50
51
# 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

#cookiesArray<Hash>

Returns Request cookies.

Returns:

  • (Array<Hash>)

    Request cookies.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/raptor-io/protocol/http/headers.rb', line 79

def cookies
  return [] if !self['cookie']

  WEBrick::Cookie.parse( self['cookie'] ).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

#delete(field) ⇒ String

Note:

‘field` will be capitalized appropriately before storing.

Returns Field value.

Parameters:

  • field (String)

    Field name

Returns:



26
27
28
# File 'lib/raptor-io/protocol/http/headers.rb', line 26

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:



33
34
35
# File 'lib/raptor-io/protocol/http/headers.rb', line 33

def include?( field )
  super format_field_name( field.to_s.downcase )
end

Returns Cookies as hashes.

Returns:

  • (Array<Hash>)

    Cookies as hashes.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/raptor-io/protocol/http/headers.rb', line 60

def parsed_set_cookie
  return [] if set_cookie.empty?

  set_cookie.map { |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
  }.flatten.compact
end

Returns Set-cookie strings.

Returns:

  • (Array<String>)

    Set-cookie strings.



54
55
56
57
# File 'lib/raptor-io/protocol/http/headers.rb', line 54

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

#to_sString

Returns HTTP headers formatted for transmission.

Returns:

  • (String)

    HTTP headers formatted for transmission.



96
97
98
99
100
101
102
103
104
105
106
# 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