Class: Protocol::HTTP::Headers

Inherits:
Object
  • Object
show all
Defined in:
lib/protocol/http/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 = nil, indexed = nil) ⇒ Headers

Returns a new instance of Headers.



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/protocol/http/headers.rb', line 56

def initialize(fields = nil, indexed = nil)
	if fields
		@fields = fields.dup
	else
		@fields = []
	end
	
	if indexed
		@indexed = indexed.dup
	else
		@indexed = self.to_h
	end
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



74
75
76
# File 'lib/protocol/http/headers.rb', line 74

def fields
  @fields
end

Class Method Details

.[](hash) ⇒ Object



52
53
54
# File 'lib/protocol/http/headers.rb', line 52

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

Instance Method Details

#==(other) ⇒ Object



201
202
203
204
205
206
207
# File 'lib/protocol/http/headers.rb', line 201

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

#[](key) ⇒ Object



189
190
191
# File 'lib/protocol/http/headers.rb', line 189

def [] key
	@indexed[key]
end

#[]=(key, value) ⇒ Object

Append the value to the given key. Some values can be appended multiple times, others can only be set once.

Parameters:

  • key (String)

    The header key.

  • value

    The header value.



131
132
133
134
135
# File 'lib/protocol/http/headers.rb', line 131

def []= key, value
	merge_into(@indexed, key.downcase, value)
	
	@fields << [key, value]
end

#add(key, value) ⇒ Object



112
113
114
# File 'lib/protocol/http/headers.rb', line 112

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

#delete(key) ⇒ Object

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



166
167
168
169
170
171
172
# File 'lib/protocol/http/headers.rb', line 166

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

#dupObject



70
71
72
# File 'lib/protocol/http/headers.rb', line 70

def dup
	self.class.new(@fields, @indexed)
end

#each(&block) ⇒ Object



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

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

#empty?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/protocol/http/headers.rb', line 84

def empty?
	@fields.empty?
end

#freezeObject



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

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

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/protocol/http/headers.rb', line 92

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

#merge(headers) ⇒ Object



124
125
126
# File 'lib/protocol/http/headers.rb', line 124

def merge(headers)
	self.dup.merge!(headers)
end

#merge!(headers) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/protocol/http/headers.rb', line 116

def merge!(headers)
	headers.each do |key, value|
		self[key] = value
	end
	
	return self
end

#slice(keys) ⇒ Object



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

def slice(keys)
	self.dup.slice!(keys)
end

#slice!(keys) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/protocol/http/headers.rb', line 96

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

#to_hObject



193
194
195
196
197
198
199
# File 'lib/protocol/http/headers.rb', line 193

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