lib/websocket/handshake/base.rb in websocket-1.2.3 vs lib/websocket/handshake/base.rb in websocket-1.2.4
- old
+ new
@@ -3,21 +3,23 @@
# @abstract Subclass and override to implement custom handshakes
class Base
include ExceptionHandler
attr_reader :host, :port, :path, :query,
- :state, :version, :secure, :headers
+ :state, :version, :secure,
+ :headers, :protocols
# Initialize new WebSocket Handshake and set it's state to :new
def initialize(args = {})
args.each { |k, v| instance_variable_set("@#{k}", v) }
@state = :new
@handler = nil
@data = ''
@headers ||= {}
+ @protocols ||= []
end
# @abstract Add data to handshake
def <<(data)
@data << data
@@ -50,11 +52,11 @@
end
rescue_method :valid?, return: false
# @abstract Should send data after parsing is finished?
def should_respond?
- fail NotImplementedError
+ raise NotImplementedError
end
# Data left from parsing. Sometimes data that doesn't belong to handshake are added - use this method to retrieve them.
# @return [String] String if some data are available. Nil otherwise
def leftovers
@@ -102,10 +104,18 @@
first_line = lines.shift
parse_first_line(first_line)
lines.each do |line|
h = HEADER.match(line)
- @headers[h[1].strip.downcase] = h[2].strip if h
+ next unless h # Skip any invalid headers
+ key = h[1].strip.downcase
+ val = h[2].strip
+ # If the header is already set and refers to the websocket protocol, append the new value
+ if @headers.key?(key) && key =~ /^(sec-)?websocket-protocol$/
+ @headers[key] << ", #{val}"
+ else
+ @headers[key] = val
+ end
end
@state = :finished
true
end