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
Expand Up @@ -76,10 +76,6 @@ def receive_data(data)
else else
dispatch(data) dispatch(data)
end end
rescue WSProtocolError => e
debug [:error, e]
trigger_on_error(e)
close_websocket_private(e.code, e.message)
rescue => e rescue => e
debug [:error, e] debug [:error, e]


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


def close_websocket(code, body) def close_websocket(code, body)
# Implemented in subclass # Implemented in subclass
end 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 def unbind
@state = :closed @state = :closed


Expand Down

0 comments on commit d6758b5

Please sign in to comment.