Skip to content

Commit

Permalink
Merge pull request igrigorik#63 from miksago/draft-13
Browse files Browse the repository at this point in the history
Support Draft 13 (minimal - lacking some close code nice to haves)
  • Loading branch information
Martyn Loughran committed Oct 19, 2011
2 parents 973bcec + 6fca28b commit f8d572c
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/em-websocket.rb
Expand Up @@ -9,7 +9,7 @@
close75 close03 close05 close06
masking04
message_processor_03 message_processor_06
handler_factory handler handler75 handler76 handler03 handler05 handler06 handler07 handler08
handler_factory handler handler75 handler76 handler03 handler05 handler06 handler07 handler08 handler13
].each do |file|
require "em-websocket/#{file}"
end
Expand Down
10 changes: 10 additions & 0 deletions lib/em-websocket/handler13.rb
@@ -0,0 +1,10 @@
module EventMachine
module WebSocket
class Handler13 < Handler
include Handshake04
include Framing07
include MessageProcessor06
include Close06
end
end
end
6 changes: 6 additions & 0 deletions lib/em-websocket/handler_factory.rb
Expand Up @@ -90,7 +90,13 @@ def self.build_with_request(connection, request, remains, secure = false, debug
when 7
Handler07.new(connection, request, debug)
when 8
# drafts 9, 10, 11 and 12 should never change the version
# number as they are all the same as version 08.
Handler08.new(connection, request, debug)
when 13
# drafts 13 to 17 all identify as version 13 as they are
# only minor changes or text changes.
Handler13.new(connection, request, debug)
else
# According to spec should abort the connection
raise WebSocketError, "Protocol version #{version} not supported"
Expand Down
26 changes: 26 additions & 0 deletions spec/helper.rb
Expand Up @@ -69,6 +69,32 @@ def send(application_data)
end
end

class Draft07FakeWebSocketClient < FakeWebSocketClient
def send(application_data)
frame = ''
opcode = 1 # fake only supports text frames
byte1 = opcode | 0b10000000 # since more, rsv1-3 are 0
frame << byte1

length = application_data.size
if length <= 125
byte2 = length # since rsv4 is 0
frame << byte2
elsif length < 65536 # write 2 byte length
frame << 126
frame << [length].pack('n')
else # write 8 byte length
frame << 127
frame << [length >> 32, length & 0xFFFFFFFF].pack("NN")
end

frame << application_data

send_data(frame)
end
end


# Wrap EM:HttpRequest in a websocket like interface so that it can be used in the specs with the same interface as FakeWebSocketClient
class Draft75WebSocketClient
def onopen(&blk); @onopen = blk; end
Expand Down
65 changes: 65 additions & 0 deletions spec/integration/draft13_spec.rb
@@ -0,0 +1,65 @@
require 'helper'
require 'integration/shared_examples'

describe "draft13" do
include EM::SpecHelper
default_timeout 1

before :each do
@request = {
:port => 80,
:method => "GET",
:path => "/demo",
:headers => {
'Host' => 'example.com',
'Upgrade' => 'websocket',
'Connection' => 'Upgrade',
'Sec-WebSocket-Key' => 'dGhlIHNhbXBsZSBub25jZQ==',
'Sec-WebSocket-Protocol' => 'sample',
'Sec-WebSocket-Origin' => 'http://example.com',
'Sec-WebSocket-Version' => '13'
}
}

@response = {
:protocol => "HTTP/1.1 101 Switching Protocols\r\n",
:headers => {
"Upgrade" => "websocket",
"Connection" => "Upgrade",
"Sec-WebSocket-Accept" => "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=",
}
}
end

it_behaves_like "a websocket server" do
def start_server
EM::WebSocket.start(:host => "0.0.0.0", :port => 12345) { |ws|
yield ws
}
end

def start_client
client = EM.connect('0.0.0.0', 12345, Draft07FakeWebSocketClient)
client.send_data(format_request(@request))
yield client if block_given?
end
end

it "should send back the correct handshake response" do
em {
EM.add_timer(0.1) do
EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 12345) { }

# Create a fake client which sends draft 07 handshake
connection = EM.connect('0.0.0.0', 12345, Draft07FakeWebSocketClient)
connection.send_data(format_request(@request))

connection.onopen {
connection.handshake_response.lines.sort.
should == format_response(@response).lines.sort
done
}
end
}
end
end

0 comments on commit f8d572c

Please sign in to comment.