]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
web: Add RequestHandler.detach
authorBen Darnell <ben@bendarnell.com>
Fri, 27 Apr 2018 14:14:18 +0000 (10:14 -0400)
committerBen Darnell <ben@bendarnell.com>
Fri, 27 Apr 2018 14:14:18 +0000 (10:14 -0400)
This method eliminates the need to use the asynchronous decorator on
handlers that may call detach.

tornado/test/simple_httpclient_test.py
tornado/web.py
tornado/websocket.py

index 818fdad83b2e96fb6d82426e755046743fa2613a..09038710dc09fd2e04dbed24bd94d22c081dd9c9 100644 (file)
@@ -57,9 +57,8 @@ class HangHandler(RequestHandler):
 
 
 class ContentLengthHandler(RequestHandler):
-    @asynchronous
     def get(self):
-        self.stream = self.request.connection.detach()
+        self.stream = self.detach()
         IOLoop.current().spawn_callback(self.write_response)
 
     @gen.coroutine
@@ -107,13 +106,12 @@ class HostEchoHandler(RequestHandler):
 
 
 class NoContentLengthHandler(RequestHandler):
-    @asynchronous
     def get(self):
         if self.request.version.startswith('HTTP/1'):
             # Emulate the old HTTP/1.0 behavior of returning a body with no
             # content-length.  Tornado handles content-length at the framework
             # level so we have to go around it.
-            stream = self.request.connection.detach()
+            stream = self.detach()
             stream.write(b"HTTP/1.0 200 OK\r\n\r\n"
                          b"hello")
             stream.close()
index 1c077fd21b5243190c2783159675caa0083c4fe7..888dc543138f2b4e65b48a33d5a1b4ddd9752233 100644 (file)
@@ -1022,6 +1022,20 @@ class RequestHandler(object):
         self.on_finish()
         self._break_cycles()
 
+    def detach(self):
+        """Take control of the underlying stream.
+
+        Returns the underlying `.IOStream` object and stops all
+        further HTTP processing. Intended for implementing protocols
+        like websockets that tunnel over an HTTP handshake.
+
+        This method is only supported when HTTP/1.1 is used.
+
+        .. versionadded:: 5.1
+        """
+        self._finished = True
+        return self.request.connection.detach()
+
     def _break_cycles(self):
         # Break up a reference cycle between this handler and the
         # _ui_module closures to allow for faster GC on CPython.
index 0507a92c67ea82ec5900d0378ca9a89b0abab8c8..cc2d4cced4be5bcc01c23bb589d87c355b049f01 100644 (file)
@@ -145,7 +145,6 @@ class WebSocketHandler(tornado.web.RequestHandler):
         self.stream = None
         self._on_close_called = False
 
-    @tornado.web.asynchronous
     def get(self, *args, **kwargs):
         self.open_args = args
         self.open_kwargs = kwargs
@@ -481,7 +480,7 @@ class WebSocketHandler(tornado.web.RequestHandler):
                 self, compression_options=self.get_compression_options())
 
     def _attach_stream(self):
-        self.stream = self.request.connection.detach()
+        self.stream = self.detach()
         self.stream.set_close_callback(self.on_connection_close)
         # disable non-WS methods
         for method in ["write", "redirect", "set_header", "set_cookie",