Skip to content

Commit

Permalink
Fail the WebSocket connection on protocol error
Browse files Browse the repository at this point in the history
This is correct according to the spec for invalid UTF-8 errors. The spec isn't clear for other errors (e.g. receiving a message which is too large), but failing the connection seems appropriate.

Specifically this means that we send the close code and then _close the tcp connection_. Previously there was a bug where the input stream would not be processed and therefore a close handshake ack would not be processed, leaving the connection open.

Also refactored this logic into handler
  • Loading branch information
mloughran committed Jan 15, 2014
1 parent ff63781 commit d6758b5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
4 changes: 0 additions & 4 deletions lib/em-websocket/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,6 @@ def receive_data(data)
else
dispatch(data)
end
rescue WSProtocolError => e
debug [:error, e]
trigger_on_error(e)
close_websocket_private(e.code, e.message)
rescue => e
debug [:error, e]

Expand Down
10 changes: 10 additions & 0 deletions lib/em-websocket/handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,22 @@ def initialize(connection, debug = false)
def receive_data(data)
@data << data
process_data
rescue WSProtocolError => e
fail_websocket(e)
end

def close_websocket(code, body)
# Implemented in subclass
end

# This corresponds to "Fail the WebSocket Connection" in the spec.
def fail_websocket(e)
debug [:error, e]
close_websocket(e.code, e.message)
@connection.close_connection_after_writing
@connection.trigger_on_error(e)
end

def unbind
@state = :closed

Expand Down

0 comments on commit d6758b5

Please sign in to comment.