]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Stop overriding _execute in WebSocketHandler; use get() instead.
authorBen Darnell <ben@bendarnell.com>
Mon, 17 Mar 2014 02:17:38 +0000 (22:17 -0400)
committerBen Darnell <ben@bendarnell.com>
Mon, 17 Mar 2014 03:29:50 +0000 (23:29 -0400)
Revise HTTP1Connection detach protocol.

As a side effect, this now decodes the arguments to open() in the same
way as the arguments to get().

maint/test/websocket/client.py
tornado/http1connection.py
tornado/websocket.py

index 71624552631a9547e0e8e19450134f957707fa81..91bcd28459db3ea8709fc01771e95be5c464011b 100644 (file)
@@ -5,7 +5,7 @@ import logging
 from tornado import gen
 from tornado.ioloop import IOLoop
 from tornado.options import define, options, parse_command_line
-from tornado.websocket import WebSocketConnect
+from tornado.websocket import websocket_connect
 
 define('url', default='ws://localhost:9001')
 define('name', default='Tornado')
@@ -13,7 +13,7 @@ define('name', default='Tornado')
 @gen.engine
 def run_tests():
     url = options.url + '/getCaseCount'
-    control_ws = yield WebSocketConnect(url, None)
+    control_ws = yield websocket_connect(url, None)
     num_tests = int((yield control_ws.read_message()))
     logging.info('running %d cases', num_tests)
     msg = yield control_ws.read_message()
@@ -22,7 +22,7 @@ def run_tests():
     for i in range(1, num_tests + 1):
         logging.info('running test case %d', i)
         url = options.url + '/runCase?case=%d&agent=%s' % (i, options.name)
-        test_ws = yield WebSocketConnect(url, None)
+        test_ws = yield websocket_connect(url, None)
         while True:
             message = yield test_ws.read_message()
             if message is None:
@@ -30,7 +30,7 @@ def run_tests():
             test_ws.write_message(message, binary=isinstance(message, bytes))
 
     url = options.url + '/updateReports?agent=%s' % options.name
-    update_ws = yield WebSocketConnect(url, None)
+    update_ws = yield websocket_connect(url, None)
     msg = yield update_ws.read_message()
     assert msg is None
     IOLoop.instance().stop()
index ac759a4b71e635afbb1b0b472f3e38fcffd9d906..a8c9498e0b34c9e4670ddd45272ae0074b87a4a5 100644 (file)
@@ -105,10 +105,11 @@ class HTTP1Connection(object):
 
             self._disconnect_on_finish = not self._can_keep_alive(
                 start_line, headers)
-            ret = delegate.headers_received(start_line, headers)
-            # TODO: finalize the 'detach' interface.
-            if ret == 'detach':
-                return
+            delegate.headers_received(start_line, headers)
+            if self.stream is None:
+                # We've been detached.
+                # TODO: where else do we need to check for detach?
+                raise gen.Return(False)
             skip_body = False
             if is_client:
                 if method == 'HEAD':
@@ -127,6 +128,8 @@ class HTTP1Connection(object):
                     yield body_future
             delegate.finish()
             yield self._finish_future
+            if self.stream is None:
+                raise gen.Return(False)
         except httputil.HTTPMessageException as e:
             gen_log.info("Malformed HTTP message from %r: %s",
                          self.address, e)
@@ -173,6 +176,11 @@ class HTTP1Connection(object):
         # cycle and delay garbage collection of this connection.
         self._clear_request_state()
 
+    def detach(self):
+        stream = self.stream
+        self.stream = None
+        return stream
+
     def write_headers(self, start_line, headers, chunk=None, callback=None):
         self._chunking = (
             # TODO: should this use self._version or start_line.version?
index 7fdd2f81c4f630a0bdac8af8deae41b88d4cb503..40d04e65a635ba7627e607eafddb694dcd3501ec 100644 (file)
@@ -108,22 +108,16 @@ class WebSocketHandler(tornado.web.RequestHandler):
     def __init__(self, application, request, **kwargs):
         tornado.web.RequestHandler.__init__(self, application, request,
                                             **kwargs)
-        self.stream = request.connection.stream
         self.ws_connection = None
         self.close_code = None
         self.close_reason = None
 
-    def _execute(self, transforms, *args, **kwargs):
+    @tornado.web.asynchronous
+    def get(self, *args, **kwargs):
         self.open_args = args
         self.open_kwargs = kwargs
 
-        # Websocket only supports GET method
-        if self.request.method != 'GET':
-            self.stream.write(tornado.escape.utf8(
-                "HTTP/1.1 405 Method Not Allowed\r\n\r\n"
-            ))
-            self.stream.close()
-            return
+        self.stream = self.request.connection.detach()
 
         # Upgrade header should be present and should be equal to WebSocket
         if self.request.headers.get("Upgrade", "").lower() != 'websocket':
@@ -878,10 +872,10 @@ class WebSocketClientConnection(simple_httpclient._HTTPConnection):
             self.io_loop.remove_timeout(self._timeout)
             self._timeout = None
 
+        self.stream = self.connection.detach()
         self.stream.set_close_callback(self._on_close)
 
         self.connect_future.set_result(self)
-        return 'detach'
 
     def write_message(self, message, binary=False):
         """Sends a message to the WebSocket server."""