From: Ben Darnell Date: Fri, 27 Apr 2018 14:14:18 +0000 (-0400) Subject: web: Add RequestHandler.detach X-Git-Tag: v5.1.0b1~21^2~8 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0200fc85b7b9eb0debd1a17c019a2164d32af6c6;p=thirdparty%2Ftornado.git web: Add RequestHandler.detach This method eliminates the need to use the asynchronous decorator on handlers that may call detach. --- diff --git a/tornado/test/simple_httpclient_test.py b/tornado/test/simple_httpclient_test.py index 818fdad83..09038710d 100644 --- a/tornado/test/simple_httpclient_test.py +++ b/tornado/test/simple_httpclient_test.py @@ -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() diff --git a/tornado/web.py b/tornado/web.py index 1c077fd21..888dc5431 100644 --- a/tornado/web.py +++ b/tornado/web.py @@ -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. diff --git a/tornado/websocket.py b/tornado/websocket.py index 0507a92c6..cc2d4cced 100644 --- a/tornado/websocket.py +++ b/tornado/websocket.py @@ -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",