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
# File 'lib/websocket/driver/headers.rb', line 7

def initialize(received = {})
  @raw = received
  clear

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

Instance Method Details

#[](name) ⇒ Object



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

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

#[]=(name, value) ⇒ Object



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

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

#clearObject



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

def clear
  @sent  = Set.new
  @lines = []
end

#inspectObject



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

def inspect
  @raw.inspect
end

#to_hObject



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

def to_h
  @raw.dup
end

#to_sObject



39
40
41
# File 'lib/websocket/driver/headers.rb', line 39

def to_s
  @lines.join('')
end