]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Echo the received websocket close code.
authorBen Darnell <ben@bendarnell.com>
Wed, 6 May 2015 05:14:05 +0000 (22:14 -0700)
committerBen Darnell <ben@bendarnell.com>
Wed, 6 May 2015 05:14:05 +0000 (22:14 -0700)
Fixes #1377.

tornado/test/websocket_test.py
tornado/websocket.py

index 6b182d0765ba7256d9a340143f057a6e97712322..23a4324ce66193ef8ee67c9c16afe2fdfd51033c 100644 (file)
@@ -224,7 +224,11 @@ class WebSocketTest(WebSocketBaseTestCase):
         self.assertEqual(ws.close_code, 1001)
         self.assertEqual(ws.close_reason, "goodbye")
         # The on_close callback is called no matter which side closed.
-        yield self.close_future
+        code, reason = yield self.close_future
+        # The client echoed the close code it received to the server,
+        # so the server's close code (returned via close_future) is
+        # the same.
+        self.assertEqual(code, 1001)
 
     @gen_test
     def test_client_close_reason(self):
index adf238bea160a3948409316da1c766f4caa8a1dd..2f57b99093ac76a9a545c676dd8550bbf66ae626 100644 (file)
@@ -835,7 +835,8 @@ class WebSocketProtocol13(WebSocketProtocol):
                 self.handler.close_code = struct.unpack('>H', data[:2])[0]
             if len(data) > 2:
                 self.handler.close_reason = to_unicode(data[2:])
-            self.close()
+            # Echo the received close code, if any (RFC 6455 section 5.5.1).
+            self.close(self.handler.close_code)
         elif opcode == 0x9:
             # Ping
             self._write_frame(True, 0xA, data)
@@ -886,6 +887,7 @@ class WebSocketClientConnection(simple_httpclient._HTTPConnection):
         self.read_queue = collections.deque()
         self.key = base64.b64encode(os.urandom(16))
         self._on_message_callback = on_message_callback
+        self.close_code = self.close_reason = None
 
         scheme, sep, rest = request.url.partition(':')
         scheme = {'ws': 'http', 'wss': 'https'}[scheme]