Skip to content

Commit

Permalink
Fix draft-76 sockets when running behind HAProxy.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoglan committed Dec 4, 2011
1 parent 3bf33f5 commit 7f88e28
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 12 deletions.
21 changes: 21 additions & 0 deletions examples/haproxy.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
defaults
mode http
timeout client 5s
timeout connect 5s
timeout server 5s

frontend all 0.0.0.0:3000
mode http
timeout client 120s

option forwardfor
option http-server-close
option http-pretend-keepalive

default_backend sockets

backend sockets
balance uri depth 2
timeout server 120s
server socket1 127.0.0.1:7000

5 changes: 4 additions & 1 deletion lib/faye/websocket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ def initialize(env)
event.init_event('open', false, false)
dispatch_event(event)

@env[Thin::Request::WEBSOCKET_RECEIVE_CALLBACK] = @parser.method(:parse)
@env[Thin::Request::WEBSOCKET_RECEIVE_CALLBACK] = lambda do |data|
response = @parser.parse(data)
@stream.write(response) if response
end
end

private
Expand Down
1 change: 1 addition & 0 deletions lib/faye/websocket/draft75_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def handshake_response

def parse(data)
data.each_byte(&method(:handle_byte))
nil
end

def frame(data, type = nil, error_type = nil)
Expand Down
35 changes: 24 additions & 11 deletions lib/faye/websocket/draft76_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,40 @@ def version

def handshake_response
env = @socket.env

key1 = env['HTTP_SEC_WEBSOCKET_KEY1']
value1 = number_from_key(key1) / spaces_in_key(key1)

key2 = env['HTTP_SEC_WEBSOCKET_KEY2']
value2 = number_from_key(key2) / spaces_in_key(key2)

hash = Digest::MD5.digest(big_endian(value1) +
big_endian(value2) +
env['rack.input'].read)
signature = handshake_signature(env['rack.input'].read)

upgrade = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n"
upgrade << "Upgrade: WebSocket\r\n"
upgrade << "Connection: Upgrade\r\n"
upgrade << "Sec-WebSocket-Origin: #{env['HTTP_ORIGIN']}\r\n"
upgrade << "Sec-WebSocket-Location: #{@socket.url}\r\n"
upgrade << "\r\n"
upgrade << hash
upgrade << signature if signature
upgrade
end

def handshake_signature(head)
return nil if head.empty?
@handshake_complete = true

env = @socket.env

key1 = env['HTTP_SEC_WEBSOCKET_KEY1']
value1 = number_from_key(key1) / spaces_in_key(key1)

key2 = env['HTTP_SEC_WEBSOCKET_KEY2']
value2 = number_from_key(key2) / spaces_in_key(key2)

Digest::MD5.digest(big_endian(value1) +
big_endian(value2) +
head)
end

def parse(data)
return super if @handshake_complete
handshake_signature(data)
end

private

def number_from_key(key)
Expand Down
1 change: 1 addition & 0 deletions lib/faye/websocket/protocol8_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ def parse(data)
end
emit_frame if @stage == 4 and @length == 0
end
nil
end

def frame(data, type = nil, code = nil)
Expand Down

0 comments on commit 7f88e28

Please sign in to comment.