Always emit the empty string instead of nil as the closure reason in Hybi.

This commit is contained in:
James Coglan
2013-04-20 22:04:24 +01:00
parent 996afee493
commit 10da09afbd
3 changed files with 32 additions and 6 deletions
+2 -2
View File
@@ -85,7 +85,7 @@ module WebSocket
false
end
def close
def close(reason = nil, code = nil)
return false unless @ready_state == 1
@ready_state = 3
dispatch(:onclose, CloseEvent.new(nil, nil))
@@ -142,7 +142,7 @@ module WebSocket
def self.server(socket, options = {})
env = socket.env
if env['HTTP_SEC_WEBSOCKET_VERSION']
Hybi.new(socket, options)
Hybi.new(socket, options.merge(:require_masking => true))
elsif env['HTTP_SEC_WEBSOCKET_KEY1']
Draft76.new(socket, options)
else
+12 -3
View File
@@ -51,7 +51,8 @@ module WebSocket
@protocols = options[:protocols]
@protocols = @protocols.strip.split(/\s*,\s*/) if String === @protocols
@ping_callbacks = {}
@require_masking = options[:require_masking]
@ping_callbacks = {}
end
def version
@@ -161,13 +162,16 @@ module WebSocket
end
def close(reason = nil, code = nil)
reason ||= ''
code ||= ERRORS[:normal_closure]
case @ready_state
when 0 then
@ready_state = 3
dispatch(:onclose, CloseEvent.new(code || 1000, reason || ''))
dispatch(:onclose, CloseEvent.new(code, reason))
true
when 1 then
frame(reason || '', :close, code || ERRORS[:normal_closure])
frame(reason, :close, code)
@ready_state = 2
true
else
@@ -206,6 +210,9 @@ module WebSocket
end
def shutdown(code, reason)
code ||= ERRORS[:normal_closure]
reason ||= ''
frame(reason, :close, code)
@ready_state = 3
dispatch(:onclose, CloseEvent.new(code, reason))
@@ -238,6 +245,8 @@ module WebSocket
def parse_length(data)
@masked = (data & MASK) == MASK
return shutdown(ERRORS[:unacceptable], nil) if @require_masking and not @masked
@length = (data & LENGTH)
if @length <= 125
+18 -1
View File
@@ -223,7 +223,7 @@ describe WebSocket::Protocol::Hybi do
it "closes the socket if the frame has an unrecognized opcode" do
protocol.parse [0x83, 0x00]
@bytes.should == [0x88, 0x02, 0x03, 0xea]
@close.should == [1002, nil]
@close.should == [1002, ""]
protocol.state.should == :closed
end
@@ -357,6 +357,23 @@ describe WebSocket::Protocol::Hybi do
end
end
describe "when masking is required" do
before do
options[:require_masking] = true
protocol.start
end
it "does not emit a message" do
protocol.parse [0x81, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f]
@messages.should == nil
end
it "returns an error" do
protocol.parse [0x81, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f]
@close.should == [1003, ""]
end
end
describe "in the :closing state" do
before do
protocol.start