Skip to content

Commit

Permalink
fix websocket handshake failure causes an exception
Browse files Browse the repository at this point in the history
  • Loading branch information
pjknkda committed Sep 22, 2017
1 parent 34c43f4 commit 5d4a9ab
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
7 changes: 7 additions & 0 deletions tornado/test/websocket_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,13 @@ def test_http_request(self):
response = self.fetch('/echo')
self.assertEqual(response.code, 400)

def test_missing_websocket_key(self):
response = self.fetch('/echo',
headers={'Connection': 'Upgrade',
'Upgrade': 'WebSocket',
'Sec-WebSocket-Version': '13'})
self.assertEqual(response.code, 400)

def test_bad_websocket_version(self):
response = self.fetch('/echo',
headers={'Connection': 'Upgrade',
Expand Down
8 changes: 8 additions & 0 deletions tornado/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,14 @@ def __init__(self, handler, mask_outgoing=False,
def accept_connection(self):
try:
self._handle_websocket_headers()
except ValueError:
self.handler.set_status(400)
log_msg = "Missing/Invalid WebSocket headers"
self.handler.finish(log_msg)
gen_log.debug(log_msg)
return

try:
self._accept_connection()
except ValueError:
gen_log.debug("Malformed WebSocket request received",
Expand Down

0 comments on commit 5d4a9ab

Please sign in to comment.