Class: HTTP::Protocol::Headers

Inherits:
Object
  • Object
show all
Defined in:
lib/http/protocol/headers.rb

Defined Under Namespace

Classes: Merged, Multiple, Split

Constant Summary collapse

MERGE_POLICY =
{
  # Headers which may only be specified once.
  'content-type' => false,
  'content-disposition' => false,
  'content-length' => false,
  'user-agent' => false,
  'referer' => false,
  'host' => false,
  'authorization' => false,
  'proxy-authorization' => false,
  'if-modified-since' => false,
  'if-unmodified-since' => false,
  'from' => false,
  'location' => false,
  'max-forwards' => false,
  
  'connection' => Split,
  
  # Headers specifically for proxies:
  'via' => Split,
  'x-forwarded-for' => Split,
  
  # Headers which may be specified multiple times, but which can't be concatenated.
  'set-cookie' => Multiple,
  'www-authenticate' => Multiple,
  'proxy-authenticate' => Multiple
}.tap{|hash| hash.default = Split}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fields = []) ⇒ Headers

Returns a new instance of Headers.



67
68
69
70
# File 'lib/http/protocol/headers.rb', line 67

def initialize(fields = [])
  @fields = fields
  @indexed = to_h
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



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

def fields
  @fields
end

Class Method Details

.[](hash) ⇒ Object



63
64
65
# File 'lib/http/protocol/headers.rb', line 63

def self.[] hash
  self.new(hash.to_a)
end

Instance Method Details

#==(other) ⇒ Object



179
180
181
182
183
184
185
# File 'lib/http/protocol/headers.rb', line 179

def == other
  if other.is_a? Hash
    to_h == other
  else
    @fields == other.fields
  end
end

#[](key) ⇒ Object



167
168
169
# File 'lib/http/protocol/headers.rb', line 167

def [] key
  @indexed[key]
end

#[]=(key, value) ⇒ Object



108
109
110
111
112
113
# File 'lib/http/protocol/headers.rb', line 108

def []= key, value
  @fields << [key, value]
  
  # It would be good to do some kind of validation here.
  merge(@indexed, key.downcase, value)
end

#add(key, value) ⇒ Object



104
105
106
# File 'lib/http/protocol/headers.rb', line 104

def add(key, value)
  self[key] = value
end

#delete(key) ⇒ Object

Delete all headers with the given key, and return the merged value.



144
145
146
147
148
149
150
# File 'lib/http/protocol/headers.rb', line 144

def delete(key)
  values, @fields = @fields.partition do |field|
    field.first.downcase == key
  end
  
  return @indexed.delete(key)
end

#each(&block) ⇒ Object



86
87
88
# File 'lib/http/protocol/headers.rb', line 86

def each(&block)
  @fields.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/http/protocol/headers.rb', line 82

def empty?
  @fields.empty?
end

#freezeObject



74
75
76
77
78
79
80
# File 'lib/http/protocol/headers.rb', line 74

def freeze
  return if frozen?
  
  @indexed = to_h
  
  super
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/http/protocol/headers.rb', line 90

def include? key
  self[key] != nil
end

#merge(hash, key, value) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/http/protocol/headers.rb', line 152

def merge(hash, key, value)
  if policy = MERGE_POLICY[key]
    if current_value = hash[key]
      current_value << value
    else
      hash[key] = policy.new(value)
    end
  else
    raise ArgumentError, "Header #{key} can only be set once!" if hash.include?(key)
    
    # We can't merge these, we only expose the last one set.
    hash[key] = value
  end
end

#slice!(keys) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/http/protocol/headers.rb', line 94

def slice!(keys)
  values, @fields = @fields.partition do |field|
    keys.include?(field.first.downcase)
  end
  
  keys.each do |key|
    @indexed.delete(key)
  end
end

#to_hObject



171
172
173
174
175
176
177
# File 'lib/http/protocol/headers.rb', line 171

def to_h
  @fields.inject({}) do |hash, (key, value)|
    merge(hash, key.downcase, value)
    
    hash
  end
end