Class: WebSocket::Driver::Headers

Inherits:
Object
  • Object
show all
Defined in:
lib/websocket/driver/headers.rb

Constant Summary collapse

ALLOWED_DUPLICATES =
%w[set-cookie set-cookie2 warning www-authenticate]

Instance Method Summary collapse

Constructor Details

#initialize(received = {}) ⇒ Headers

Returns a new instance of Headers.



7
8
9
10
11
12
13
14
# File 'lib/websocket/driver/headers.rb', line 7

def initialize(received = {})
  @raw   = received
  @sent  = Set.new
  @lines = []

  @received = {}
  @raw.each { |k,v| @received[HTTP.normalize_header(k)] = v }
end

Instance Method Details

#[](name) ⇒ Object



16
17
18
# File 'lib/websocket/driver/headers.rb', line 16

def [](name)
  @received[HTTP.normalize_header(name)]
end

#[]=(name, value) ⇒ Object



20
21
22
23
24
25
# File 'lib/websocket/driver/headers.rb', line 20

def []=(name, value)
  return if value.nil?
  key = HTTP.normalize_header(name)
  return unless @sent.add?(key) or ALLOWED_DUPLICATES.include?(key)
  @lines << "#{name.strip}: #{value.to_s.strip}\r\n"
end

#inspectObject



27
28
29
# File 'lib/websocket/driver/headers.rb', line 27

def inspect
  @raw.inspect
end

#to_hObject



31
32
33
# File 'lib/websocket/driver/headers.rb', line 31

def to_h
  @raw.dup
end

#to_sObject



35
36
37
# File 'lib/websocket/driver/headers.rb', line 35

def to_s
  @lines.join('')
end