{
    "file_diffs": [
        {
            "old_file_content": "#\n# Copyright 2014 Facebook\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\"); you may\n# not use this file except in compliance with the License. You may obtain\n# a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT\n# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the\n# License for the specific language governing permissions and limitations\n# under the License.\n\n\"\"\"Client and server implementations of HTTP/1.x.\n\n.. versionadded:: 4.0\n\"\"\"\n\nimport asyncio\nimport logging\nimport re\nimport types\n\nfrom tornado.concurrent import (\n    Future,\n    future_add_done_callback,\n    future_set_result_unless_cancelled,\n)\nfrom tornado.escape import native_str, utf8\nfrom tornado import gen\nfrom tornado import httputil\nfrom tornado import iostream\nfrom tornado.log import gen_log, app_log\nfrom tornado.util import GzipDecompressor\n\n\nfrom typing import cast, Optional, Type, Awaitable, Callable, Union, Tuple\n\n\nclass _QuietException(Exception):\n    def __init__(self) -> None:\n        pass\n\n\nclass _ExceptionLoggingContext(object):\n    \"\"\"Used with the ``with`` statement when calling delegate methods to\n    log any exceptions with the given logger.  Any exceptions caught are\n    converted to _QuietException\n    \"\"\"\n\n    def __init__(self, logger: logging.Logger) -> None:\n        self.logger = logger\n\n    def __enter__(self) -> None:\n        pass\n\n    def __exit__(\n        self,\n        typ: Optional[Type[BaseException]],\n        value: Optional[BaseException],\n        tb: types.TracebackType,\n    ) -> None:\n        if value is not None:\n            assert typ is not None\n            self.logger.error(\"Uncaught exception\", exc_info=(typ, value, tb))\n            raise _QuietException\n\n\nclass HTTP1ConnectionParameters(object):\n    \"\"\"Parameters for `.HTTP1Connection` and `.HTTP1ServerConnection`.\n    \"\"\"\n\n    def __init__(\n        self,\n        no_keep_alive: bool = False,\n        chunk_size: int = None,\n        max_header_size: int = None,\n        header_timeout: float = None,\n        max_body_size: int = None,\n        body_timeout: float = None,\n        decompress: bool = False,\n    ) -> None:\n        \"\"\"\n        :arg bool no_keep_alive: If true, always close the connection after\n            one request.\n        :arg int chunk_size: how much data to read into memory at once\n        :arg int max_header_size:  maximum amount of data for HTTP headers\n        :arg float header_timeout: how long to wait for all headers (seconds)\n        :arg int max_body_size: maximum amount of data for body\n        :arg float body_timeout: how long to wait while reading body (seconds)\n        :arg bool decompress: if true, decode incoming\n            ``Content-Encoding: gzip``\n        \"\"\"\n        self.no_keep_alive = no_keep_alive\n        self.chunk_size = chunk_size or 65536\n        self.max_header_size = max_header_size or 65536\n        self.header_timeout = header_timeout\n        self.max_body_size = max_body_size\n        self.body_timeout = body_timeout\n        self.decompress = decompress\n\n\nclass HTTP1Connection(httputil.HTTPConnection):\n    \"\"\"Implements the HTTP/1.x protocol.\n\n    This class can be on its own for clients, or via `HTTP1ServerConnection`\n    for servers.\n    \"\"\"\n\n    def __init__(\n        self,\n        stream: iostream.IOStream,\n        is_client: bool,\n        params: HTTP1ConnectionParameters = None,\n        context: object = None,\n    ) -> None:\n        \"\"\"\n        :arg stream: an `.IOStream`\n        :arg bool is_client: client or server\n        :arg params: a `.HTTP1ConnectionParameters` instance or ``None``\n        :arg context: an opaque application-defined object that can be accessed\n            as ``connection.context``.\n        \"\"\"\n        self.is_client = is_client\n        self.stream = stream\n        if params is None:\n            params = HTTP1ConnectionParameters()\n        self.params = params\n        self.context = context\n        self.no_keep_alive = params.no_keep_alive\n        # The body limits can be altered by the delegate, so save them\n        # here instead of just referencing self.params later.\n        self._max_body_size = self.params.max_body_size or self.stream.max_buffer_size\n        self._body_timeout = self.params.body_timeout\n        # _write_finished is set to True when finish() has been called,\n        # i.e. there will be no more data sent.  Data may still be in the\n        # stream's write buffer.\n        self._write_finished = False\n        # True when we have read the entire incoming body.\n        self._read_finished = False\n        # _finish_future resolves when all data has been written and flushed\n        # to the IOStream.\n        self._finish_future = Future()  # type: Future[None]\n        # If true, the connection should be closed after this request\n        # (after the response has been written in the server side,\n        # and after it has been read in the client)\n        self._disconnect_on_finish = False\n        self._clear_callbacks()\n        # Save the start lines after we read or write them; they\n        # affect later processing (e.g. 304 responses and HEAD methods\n        # have content-length but no bodies)\n        self._request_start_line = None  # type: Optional[httputil.RequestStartLine]\n        self._response_start_line = None  # type: Optional[httputil.ResponseStartLine]\n        self._request_headers = None  # type: Optional[httputil.HTTPHeaders]\n        # True if we are writing output with chunked encoding.\n        self._chunking_output = False\n        # While reading a body with a content-length, this is the\n        # amount left to read.\n        self._expected_content_remaining = None  # type: Optional[int]\n        # A Future for our outgoing writes, returned by IOStream.write.\n        self._pending_write = None  # type: Optional[Future[None]]\n\n    def read_response(self, delegate: httputil.HTTPMessageDelegate) -> Awaitable[bool]:\n        \"\"\"Read a single HTTP response.\n\n        Typical client-mode usage is to write a request using `write_headers`,\n        `write`, and `finish`, and then call ``read_response``.\n\n        :arg delegate: a `.HTTPMessageDelegate`\n\n        Returns a `.Future` that resolves to a bool after the full response has\n        been read. The result is true if the stream is still open.\n        \"\"\"\n        if self.params.decompress:\n            delegate = _GzipMessageDelegate(delegate, self.params.chunk_size)\n        return self._read_message(delegate)\n\n    async def _read_message(self, delegate: httputil.HTTPMessageDelegate) -> bool:\n        need_delegate_close = False\n        try:\n            header_future = self.stream.read_until_regex(\n                b\"\\r?\\n\\r?\\n\", max_bytes=self.params.max_header_size\n            )\n            if self.params.header_timeout is None:\n                header_data = await header_future\n            else:\n                try:\n                    header_data = await gen.with_timeout(\n                        self.stream.io_loop.time() + self.params.header_timeout,\n                        header_future,\n                        quiet_exceptions=iostream.StreamClosedError,\n                    )\n                except gen.TimeoutError:\n                    self.close()\n                    return False\n            start_line_str, headers = self._parse_headers(header_data)\n            if self.is_client:\n                resp_start_line = httputil.parse_response_start_line(start_line_str)\n                self._response_start_line = resp_start_line\n                start_line = (\n                    resp_start_line\n                )  # type: Union[httputil.RequestStartLine, httputil.ResponseStartLine]\n                # TODO: this will need to change to support client-side keepalive\n                self._disconnect_on_finish = False\n            else:\n                req_start_line = httputil.parse_request_start_line(start_line_str)\n                self._request_start_line = req_start_line\n                self._request_headers = headers\n                start_line = req_start_line\n                self._disconnect_on_finish = not self._can_keep_alive(\n                    req_start_line, headers\n                )\n            need_delegate_close = True\n            with _ExceptionLoggingContext(app_log):\n                header_recv_future = delegate.headers_received(start_line, headers)\n                if header_recv_future is not None:\n                    await header_recv_future\n            if self.stream is None:\n                # We've been detached.\n                need_delegate_close = False\n                return False\n            skip_body = False\n            if self.is_client:\n                assert isinstance(start_line, httputil.ResponseStartLine)\n                if (\n                    self._request_start_line is not None\n                    and self._request_start_line.method == \"HEAD\"\n                ):\n                    skip_body = True\n                code = start_line.code\n                if code == 304:\n                    # 304 responses may include the content-length header\n                    # but do not actually have a body.\n                    # http://tools.ietf.org/html/rfc7230#section-3.3\n                    skip_body = True\n                if code >= 100 and code < 200:\n                    # 1xx responses should never indicate the presence of\n                    # a body.\n                    if \"Content-Length\" in headers or \"Transfer-Encoding\" in headers:\n                        raise httputil.HTTPInputError(\n                            \"Response code %d cannot have body\" % code\n                        )\n                    # TODO: client delegates will get headers_received twice\n                    # in the case of a 100-continue.  Document or change?\n                    await self._read_message(delegate)\n            else:\n                if headers.get(\"Expect\") == \"100-continue\" and not self._write_finished:\n                    self.stream.write(b\"HTTP/1.1 100 (Continue)\\r\\n\\r\\n\")\n            if not skip_body:\n                body_future = self._read_body(\n                    resp_start_line.code if self.is_client else 0, headers, delegate\n                )\n                if body_future is not None:\n                    if self._body_timeout is None:\n                        await body_future\n                    else:\n                        try:\n                            await gen.with_timeout(\n                                self.stream.io_loop.time() + self._body_timeout,\n                                body_future,\n                                quiet_exceptions=iostream.StreamClosedError,\n                            )\n                        except gen.TimeoutError:\n                            gen_log.info(\"Timeout reading body from %s\", self.context)\n                            self.stream.close()\n                            return False\n            self._read_finished = True\n            if not self._write_finished or self.is_client:\n                need_delegate_close = False\n                with _ExceptionLoggingContext(app_log):\n                    delegate.finish()\n            # If we're waiting for the application to produce an asynchronous\n            # response, and we're not detached, register a close callback\n            # on the stream (we didn't need one while we were reading)\n            if (\n                not self._finish_future.done()\n                and self.stream is not None\n                and not self.stream.closed()\n            ):\n                self.stream.set_close_callback(self._on_connection_close)\n                await self._finish_future\n            if self.is_client and self._disconnect_on_finish:\n                self.close()\n            if self.stream is None:\n                return False\n        except httputil.HTTPInputError as e:\n            gen_log.info(\"Malformed HTTP message from %s: %s\", self.context, e)\n            if not self.is_client:\n                await self.stream.write(b\"HTTP/1.1 400 Bad Request\\r\\n\\r\\n\")\n            self.close()\n            return False\n        finally:\n            if need_delegate_close:\n                with _ExceptionLoggingContext(app_log):\n                    delegate.on_connection_close()\n            header_future = None  # type: ignore\n            self._clear_callbacks()\n        return True\n\n    def _clear_callbacks(self) -> None:\n        \"\"\"Clears the callback attributes.\n\n        This allows the request handler to be garbage collected more\n        quickly in CPython by breaking up reference cycles.\n        \"\"\"\n        self._write_callback = None\n        self._write_future = None  # type: Optional[Future[None]]\n        self._close_callback = None  # type: Optional[Callable[[], None]]\n        if self.stream is not None:\n            self.stream.set_close_callback(None)\n\n    def set_close_callback(self, callback: Optional[Callable[[], None]]) -> None:\n        \"\"\"Sets a callback that will be run when the connection is closed.\n\n        Note that this callback is slightly different from\n        `.HTTPMessageDelegate.on_connection_close`: The\n        `.HTTPMessageDelegate` method is called when the connection is\n        closed while recieving a message. This callback is used when\n        there is not an active delegate (for example, on the server\n        side this callback is used if the client closes the connection\n        after sending its request but before receiving all the\n        response.\n        \"\"\"\n        self._close_callback = callback\n\n    def _on_connection_close(self) -> None:\n        # Note that this callback is only registered on the IOStream\n        # when we have finished reading the request and are waiting for\n        # the application to produce its response.\n        if self._close_callback is not None:\n            callback = self._close_callback\n            self._close_callback = None\n            callback()\n        if not self._finish_future.done():\n            future_set_result_unless_cancelled(self._finish_future, None)\n        self._clear_callbacks()\n\n    def close(self) -> None:\n        if self.stream is not None:\n            self.stream.close()\n        self._clear_callbacks()\n        if not self._finish_future.done():\n            future_set_result_unless_cancelled(self._finish_future, None)\n\n    def detach(self) -> iostream.IOStream:\n        \"\"\"Take control of the underlying stream.\n\n        Returns the underlying `.IOStream` object and stops all further\n        HTTP processing.  May only be called during\n        `.HTTPMessageDelegate.headers_received`.  Intended for implementing\n        protocols like websockets that tunnel over an HTTP handshake.\n        \"\"\"\n        self._clear_callbacks()\n        stream = self.stream\n        self.stream = None  # type: ignore\n        if not self._finish_future.done():\n            future_set_result_unless_cancelled(self._finish_future, None)\n        return stream\n\n    def set_body_timeout(self, timeout: float) -> None:\n        \"\"\"Sets the body timeout for a single request.\n\n        Overrides the value from `.HTTP1ConnectionParameters`.\n        \"\"\"\n        self._body_timeout = timeout\n\n    def set_max_body_size(self, max_body_size: int) -> None:\n        \"\"\"Sets the body size limit for a single request.\n\n        Overrides the value from `.HTTP1ConnectionParameters`.\n        \"\"\"\n        self._max_body_size = max_body_size\n\n    def write_headers(\n        self,\n        start_line: Union[httputil.RequestStartLine, httputil.ResponseStartLine],\n        headers: httputil.HTTPHeaders,\n        chunk: bytes = None,\n    ) -> \"Future[None]\":\n        \"\"\"Implements `.HTTPConnection.write_headers`.\"\"\"\n        lines = []\n        if self.is_client:\n            assert isinstance(start_line, httputil.RequestStartLine)\n            self._request_start_line = start_line\n            lines.append(utf8(\"%s %s HTTP/1.1\" % (start_line[0], start_line[1])))\n            # Client requests with a non-empty body must have either a\n            # Content-Length or a Transfer-Encoding.\n            self._chunking_output = (\n                start_line.method in (\"POST\", \"PUT\", \"PATCH\")\n                and \"Content-Length\" not in headers\n                and \"Transfer-Encoding\" not in headers\n            )\n        else:\n            assert isinstance(start_line, httputil.ResponseStartLine)\n            assert self._request_start_line is not None\n            assert self._request_headers is not None\n            self._response_start_line = start_line\n            lines.append(utf8(\"HTTP/1.1 %d %s\" % (start_line[1], start_line[2])))\n            self._chunking_output = (\n                # TODO: should this use\n                # self._request_start_line.version or\n                # start_line.version?\n                self._request_start_line.version == \"HTTP/1.1\"\n                # 1xx, 204 and 304 responses have no body (not even a zero-length\n                # body), and so should not have either Content-Length or\n                # Transfer-Encoding headers.\n                and start_line.code not in (204, 304)\n                and (start_line.code < 100 or start_line.code >= 200)\n                # No need to chunk the output if a Content-Length is specified.\n                and \"Content-Length\" not in headers\n                # Applications are discouraged from touching Transfer-Encoding,\n                # but if they do, leave it alone.\n                and \"Transfer-Encoding\" not in headers\n            )\n            # If connection to a 1.1 client will be closed, inform client\n            if (\n                self._request_start_line.version == \"HTTP/1.1\"\n                and self._disconnect_on_finish\n            ):\n                headers[\"Connection\"] = \"close\"\n            # If a 1.0 client asked for keep-alive, add the header.\n            if (\n                self._request_start_line.version == \"HTTP/1.0\"\n                and self._request_headers.get(\"Connection\", \"\").lower() == \"keep-alive\"\n            ):\n                headers[\"Connection\"] = \"Keep-Alive\"\n        if self._chunking_output:\n            headers[\"Transfer-Encoding\"] = \"chunked\"\n        if not self.is_client and (\n            self._request_start_line.method == \"HEAD\"\n            or cast(httputil.ResponseStartLine, start_line).code == 304\n        ):\n            self._expected_content_remaining = 0\n        elif \"Content-Length\" in headers:\n            self._expected_content_remaining = int(headers[\"Content-Length\"])\n        else:\n            self._expected_content_remaining = None\n        # TODO: headers are supposed to be of type str, but we still have some\n        # cases that let bytes slip through. Remove these native_str calls when those\n        # are fixed.\n        header_lines = (\n            native_str(n) + \": \" + native_str(v) for n, v in headers.get_all()\n        )\n        lines.extend(l.encode(\"latin1\") for l in header_lines)\n        for line in lines:\n            if b\"\\n\" in line:\n                raise ValueError(\"Newline in header: \" + repr(line))\n        future = None\n        if self.stream.closed():\n            future = self._write_future = Future()\n            future.set_exception(iostream.StreamClosedError())\n            future.exception()\n        else:\n            future = self._write_future = Future()\n            data = b\"\\r\\n\".join(lines) + b\"\\r\\n\\r\\n\"\n            if chunk:\n                data += self._format_chunk(chunk)\n            self._pending_write = self.stream.write(data)\n            future_add_done_callback(self._pending_write, self._on_write_complete)\n        return future\n\n    def _format_chunk(self, chunk: bytes) -> bytes:\n        if self._expected_content_remaining is not None:\n            self._expected_content_remaining -= len(chunk)\n            if self._expected_content_remaining < 0:\n                # Close the stream now to stop further framing errors.\n                self.stream.close()\n                raise httputil.HTTPOutputError(\n                    \"Tried to write more data than Content-Length\"\n                )\n        if self._chunking_output and chunk:\n            # Don't write out empty chunks because that means END-OF-STREAM\n            # with chunked encoding\n            return utf8(\"%x\" % len(chunk)) + b\"\\r\\n\" + chunk + b\"\\r\\n\"\n        else:\n            return chunk\n\n    def write(self, chunk: bytes) -> \"Future[None]\":\n        \"\"\"Implements `.HTTPConnection.write`.\n\n        For backwards compatibility it is allowed but deprecated to\n        skip `write_headers` and instead call `write()` with a\n        pre-encoded header block.\n        \"\"\"\n        future = None\n        if self.stream.closed():\n            future = self._write_future = Future()\n            self._write_future.set_exception(iostream.StreamClosedError())\n            self._write_future.exception()\n        else:\n            future = self._write_future = Future()\n            self._pending_write = self.stream.write(self._format_chunk(chunk))\n            self._pending_write.add_done_callback(self._on_write_complete)\n        return future\n\n    def finish(self) -> None:\n        \"\"\"Implements `.HTTPConnection.finish`.\"\"\"\n        if (\n            self._expected_content_remaining is not None\n            and self._expected_content_remaining != 0\n            and not self.stream.closed()\n        ):\n            self.stream.close()\n            raise httputil.HTTPOutputError(\n                \"Tried to write %d bytes less than Content-Length\"\n                % self._expected_content_remaining\n            )\n        if self._chunking_output:\n            if not self.stream.closed():\n                self._pending_write = self.stream.write(b\"0\\r\\n\\r\\n\")\n                self._pending_write.add_done_callback(self._on_write_complete)\n        self._write_finished = True\n        # If the app finished the request while we're still reading,\n        # divert any remaining data away from the delegate and\n        # close the connection when we're done sending our response.\n        # Closing the connection is the only way to avoid reading the\n        # whole input body.\n        if not self._read_finished:\n            self._disconnect_on_finish = True\n        # No more data is coming, so instruct TCP to send any remaining\n        # data immediately instead of waiting for a full packet or ack.\n        self.stream.set_nodelay(True)\n        if self._pending_write is None:\n            self._finish_request(None)\n        else:\n            future_add_done_callback(self._pending_write, self._finish_request)\n\n    def _on_write_complete(self, future: \"Future[None]\") -> None:\n        exc = future.exception()\n        if exc is not None and not isinstance(exc, iostream.StreamClosedError):\n            future.result()\n        if self._write_callback is not None:\n            callback = self._write_callback\n            self._write_callback = None\n            self.stream.io_loop.add_callback(callback)\n        if self._write_future is not None:\n            future = self._write_future\n            self._write_future = None\n            future_set_result_unless_cancelled(future, None)\n\n    def _can_keep_alive(\n        self, start_line: httputil.RequestStartLine, headers: httputil.HTTPHeaders\n    ) -> bool:\n        if self.params.no_keep_alive:\n            return False\n        connection_header = headers.get(\"Connection\")\n        if connection_header is not None:\n            connection_header = connection_header.lower()\n        if start_line.version == \"HTTP/1.1\":\n            return connection_header != \"close\"\n        elif (\n            \"Content-Length\" in headers\n            or headers.get(\"Transfer-Encoding\", \"\").lower() == \"chunked\"\n            or getattr(start_line, \"method\", None) in (\"HEAD\", \"GET\")\n        ):\n            # start_line may be a request or response start line; only\n            # the former has a method attribute.\n            return connection_header == \"keep-alive\"\n        return False\n\n    def _finish_request(self, future: Optional[\"Future[None]\"]) -> None:\n        self._clear_callbacks()\n        if not self.is_client and self._disconnect_on_finish:\n            self.close()\n            return\n        # Turn Nagle's algorithm back on, leaving the stream in its\n        # default state for the next request.\n        self.stream.set_nodelay(False)\n        if not self._finish_future.done():\n            future_set_result_unless_cancelled(self._finish_future, None)\n\n    def _parse_headers(self, data: bytes) -> Tuple[str, httputil.HTTPHeaders]:\n        # The lstrip removes newlines that some implementations sometimes\n        # insert between messages of a reused connection.  Per RFC 7230,\n        # we SHOULD ignore at least one empty line before the request.\n        # http://tools.ietf.org/html/rfc7230#section-3.5\n        data_str = native_str(data.decode(\"latin1\")).lstrip(\"\\r\\n\")\n        # RFC 7230 section allows for both CRLF and bare LF.\n        eol = data_str.find(\"\\n\")\n        start_line = data_str[:eol].rstrip(\"\\r\")\n        headers = httputil.HTTPHeaders.parse(data_str[eol:])\n        return start_line, headers\n\n    def _read_body(\n        self,\n        code: int,\n        headers: httputil.HTTPHeaders,\n        delegate: httputil.HTTPMessageDelegate,\n    ) -> Optional[Awaitable[None]]:\n        if \"Content-Length\" in headers:\n            if \"Transfer-Encoding\" in headers:\n                # Response cannot contain both Content-Length and\n                # Transfer-Encoding headers.\n                # http://tools.ietf.org/html/rfc7230#section-3.3.3\n                raise httputil.HTTPInputError(\n                    \"Response with both Transfer-Encoding and Content-Length\"\n                )\n            if \",\" in headers[\"Content-Length\"]:\n                # Proxies sometimes cause Content-Length headers to get\n                # duplicated.  If all the values are identical then we can\n                # use them but if they differ it's an error.\n                pieces = re.split(r\",\\s*\", headers[\"Content-Length\"])\n                if any(i != pieces[0] for i in pieces):\n                    raise httputil.HTTPInputError(\n                        \"Multiple unequal Content-Lengths: %r\"\n                        % headers[\"Content-Length\"]\n                    )\n                headers[\"Content-Length\"] = pieces[0]\n\n            try:\n                content_length = int(headers[\"Content-Length\"])  # type: Optional[int]\n            except ValueError:\n                # Handles non-integer Content-Length value.\n                raise httputil.HTTPInputError(\n                    \"Only integer Content-Length is allowed: %s\"\n                    % headers[\"Content-Length\"]\n                )\n\n            if cast(int, content_length) > self._max_body_size:\n                raise httputil.HTTPInputError(\"Content-Length too long\")\n        else:\n            content_length = None\n\n        if code == 204:\n            # This response code is not allowed to have a non-empty body,\n            # and has an implicit length of zero instead of read-until-close.\n            # http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.3\n            if \"Transfer-Encoding\" in headers or content_length not in (None, 0):\n                raise httputil.HTTPInputError(\n                    \"Response with code %d should not have body\" % code\n                )\n            content_length = 0\n\n        if content_length is not None:\n            return self._read_fixed_body(content_length, delegate)\n        if headers.get(\"Transfer-Encoding\", \"\").lower() == \"chunked\":\n            return self._read_chunked_body(delegate)\n        if self.is_client:\n            return self._read_body_until_close(delegate)\n        return None\n\n    async def _read_fixed_body(\n        self, content_length: int, delegate: httputil.HTTPMessageDelegate\n    ) -> None:\n        while content_length > 0:\n            body = await self.stream.read_bytes(\n                min(self.params.chunk_size, content_length), partial=True\n            )\n            content_length -= len(body)\n            if not self._write_finished or self.is_client:\n                with _ExceptionLoggingContext(app_log):\n                    ret = delegate.data_received(body)\n                    if ret is not None:\n                        await ret\n\n    async def _read_chunked_body(self, delegate: httputil.HTTPMessageDelegate) -> None:\n        # TODO: \"chunk extensions\" http://tools.ietf.org/html/rfc2616#section-3.6.1\n        total_size = 0\n        while True:\n            chunk_len_str = await self.stream.read_until(b\"\\r\\n\", max_bytes=64)\n            chunk_len = int(chunk_len_str.strip(), 16)\n            if chunk_len == 0:\n                crlf = await self.stream.read_bytes(2)\n                if crlf != b\"\\r\\n\":\n                    raise httputil.HTTPInputError(\n                        \"improperly terminated chunked request\"\n                    )\n                return\n            total_size += chunk_len\n            if total_size > self._max_body_size:\n                raise httputil.HTTPInputError(\"chunked body too large\")\n            bytes_to_read = chunk_len\n            while bytes_to_read:\n                chunk = await self.stream.read_bytes(\n                    min(bytes_to_read, self.params.chunk_size), partial=True\n                )\n                bytes_to_read -= len(chunk)\n                if not self._write_finished or self.is_client:\n                    with _ExceptionLoggingContext(app_log):\n                        ret = delegate.data_received(chunk)\n                        if ret is not None:\n                            await ret\n            # chunk ends with \\r\\n\n            crlf = await self.stream.read_bytes(2)\n            assert crlf == b\"\\r\\n\"\n\n    async def _read_body_until_close(\n        self, delegate: httputil.HTTPMessageDelegate\n    ) -> None:\n        body = await self.stream.read_until_close()\n        if not self._write_finished or self.is_client:\n            with _ExceptionLoggingContext(app_log):\n                ret = delegate.data_received(body)\n                if ret is not None:\n                    await ret\n\n\nclass _GzipMessageDelegate(httputil.HTTPMessageDelegate):\n    \"\"\"Wraps an `HTTPMessageDelegate` to decode ``Content-Encoding: gzip``.\n    \"\"\"\n\n    def __init__(self, delegate: httputil.HTTPMessageDelegate, chunk_size: int) -> None:\n        self._delegate = delegate\n        self._chunk_size = chunk_size\n        self._decompressor = None  # type: Optional[GzipDecompressor]\n\n    def headers_received(\n        self,\n        start_line: Union[httputil.RequestStartLine, httputil.ResponseStartLine],\n        headers: httputil.HTTPHeaders,\n    ) -> Optional[Awaitable[None]]:\n        if headers.get(\"Content-Encoding\") == \"gzip\":\n            self._decompressor = GzipDecompressor()\n            # Downstream delegates will only see uncompressed data,\n            # so rename the content-encoding header.\n            # (but note that curl_httpclient doesn't do this).\n            headers.add(\"X-Consumed-Content-Encoding\", headers[\"Content-Encoding\"])\n            del headers[\"Content-Encoding\"]\n        return self._delegate.headers_received(start_line, headers)\n\n    async def data_received(self, chunk: bytes) -> None:\n        if self._decompressor:\n            compressed_data = chunk\n            while compressed_data:\n                decompressed = self._decompressor.decompress(\n                    compressed_data, self._chunk_size\n                )\n                if decompressed:\n                    ret = self._delegate.data_received(decompressed)\n                    if ret is not None:\n                        await ret\n                compressed_data = self._decompressor.unconsumed_tail\n        else:\n            ret = self._delegate.data_received(chunk)\n            if ret is not None:\n                await ret\n\n    def finish(self) -> None:\n        if self._decompressor is not None:\n            tail = self._decompressor.flush()\n            if tail:\n                # The tail should always be empty: decompress returned\n                # all that it can in data_received and the only\n                # purpose of the flush call is to detect errors such\n                # as truncated input. If we did legitimately get a new\n                # chunk at this point we'd need to change the\n                # interface to make finish() a coroutine.\n                raise ValueError(\n                    \"decompressor.flush returned data; possile truncated input\"\n                )\n        return self._delegate.finish()\n\n    def on_connection_close(self) -> None:\n        return self._delegate.on_connection_close()\n\n\nclass HTTP1ServerConnection(object):\n    \"\"\"An HTTP/1.x server.\"\"\"\n\n    def __init__(\n        self,\n        stream: iostream.IOStream,\n        params: HTTP1ConnectionParameters = None,\n        context: object = None,\n    ) -> None:\n        \"\"\"\n        :arg stream: an `.IOStream`\n        :arg params: a `.HTTP1ConnectionParameters` or None\n        :arg context: an opaque application-defined object that is accessible\n            as ``connection.context``\n        \"\"\"\n        self.stream = stream\n        if params is None:\n            params = HTTP1ConnectionParameters()\n        self.params = params\n        self.context = context\n        self._serving_future = None  # type: Optional[Future[None]]\n\n    async def close(self) -> None:\n        \"\"\"Closes the connection.\n\n        Returns a `.Future` that resolves after the serving loop has exited.\n        \"\"\"\n        self.stream.close()\n        # Block until the serving loop is done, but ignore any exceptions\n        # (start_serving is already responsible for logging them).\n        assert self._serving_future is not None\n        try:\n            await self._serving_future\n        except Exception:\n            pass\n\n    def start_serving(self, delegate: httputil.HTTPServerConnectionDelegate) -> None:\n        \"\"\"Starts serving requests on this connection.\n\n        :arg delegate: a `.HTTPServerConnectionDelegate`\n        \"\"\"\n        assert isinstance(delegate, httputil.HTTPServerConnectionDelegate)\n        fut = gen.convert_yielded(self._server_request_loop(delegate))\n        self._serving_future = fut\n        # Register the future on the IOLoop so its errors get logged.\n        self.stream.io_loop.add_future(fut, lambda f: f.result())\n\n    async def _server_request_loop(\n        self, delegate: httputil.HTTPServerConnectionDelegate\n    ) -> None:\n        try:\n            while True:\n                conn = HTTP1Connection(self.stream, False, self.params, self.context)\n                request_delegate = delegate.start_request(self, conn)\n                try:\n                    ret = await conn.read_response(request_delegate)\n                except (iostream.StreamClosedError, iostream.UnsatisfiableReadError):\n                    return\n                except _QuietException:\n                    # This exception was already logged.\n                    conn.close()\n                    return\n                except Exception:\n                    gen_log.error(\"Uncaught exception\", exc_info=True)\n                    conn.close()\n                    return\n                if not ret:\n                    return\n                await asyncio.sleep(0)\n        finally:\n            delegate.on_close(self)\n",
            "new_file_content": "#\n# Copyright 2014 Facebook\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\"); you may\n# not use this file except in compliance with the License. You may obtain\n# a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT\n# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the\n# License for the specific language governing permissions and limitations\n# under the License.\n\n\"\"\"Client and server implementations of HTTP/1.x.\n\n.. versionadded:: 4.0\n\"\"\"\n\nimport asyncio\nimport logging\nimport re\nimport types\n\nfrom tornado.concurrent import (\n    Future,\n    future_add_done_callback,\n    future_set_result_unless_cancelled,\n)\nfrom tornado.escape import native_str, utf8\nfrom tornado import gen\nfrom tornado import httputil\nfrom tornado import iostream\nfrom tornado.log import gen_log, app_log\nfrom tornado.util import GzipDecompressor\n\n\nfrom typing import cast, Optional, Type, Awaitable, Callable, Union, Tuple\n\n\nclass _QuietException(Exception):\n    def __init__(self) -> None:\n        pass\n\n\nclass _ExceptionLoggingContext(object):\n    \"\"\"Used with the ``with`` statement when calling delegate methods to\n    log any exceptions with the given logger.  Any exceptions caught are\n    converted to _QuietException\n    \"\"\"\n\n    def __init__(self, logger: logging.Logger) -> None:\n        self.logger = logger\n\n    def __enter__(self) -> None:\n        pass\n\n    def __exit__(\n        self,\n        typ: Optional[Type[BaseException]],\n        value: Optional[BaseException],\n        tb: types.TracebackType,\n    ) -> None:\n        if value is not None:\n            assert typ is not None\n            self.logger.error(\"Uncaught exception\", exc_info=(typ, value, tb))\n            raise _QuietException\n\n\nclass HTTP1ConnectionParameters(object):\n    \"\"\"Parameters for `.HTTP1Connection` and `.HTTP1ServerConnection`.\n    \"\"\"\n\n    def __init__(\n        self,\n        no_keep_alive: bool = False,\n        chunk_size: int = None,\n        max_header_size: int = None,\n        header_timeout: float = None,\n        max_body_size: int = None,\n        body_timeout: float = None,\n        decompress: bool = False,\n    ) -> None:\n        \"\"\"\n        :arg bool no_keep_alive: If true, always close the connection after\n            one request.\n        :arg int chunk_size: how much data to read into memory at once\n        :arg int max_header_size:  maximum amount of data for HTTP headers\n        :arg float header_timeout: how long to wait for all headers (seconds)\n        :arg int max_body_size: maximum amount of data for body\n        :arg float body_timeout: how long to wait while reading body (seconds)\n        :arg bool decompress: if true, decode incoming\n            ``Content-Encoding: gzip``\n        \"\"\"\n        self.no_keep_alive = no_keep_alive\n        self.chunk_size = chunk_size or 65536\n        self.max_header_size = max_header_size or 65536\n        self.header_timeout = header_timeout\n        self.max_body_size = max_body_size\n        self.body_timeout = body_timeout\n        self.decompress = decompress\n\n\nclass HTTP1Connection(httputil.HTTPConnection):\n    \"\"\"Implements the HTTP/1.x protocol.\n\n    This class can be on its own for clients, or via `HTTP1ServerConnection`\n    for servers.\n    \"\"\"\n\n    def __init__(\n        self,\n        stream: iostream.IOStream,\n        is_client: bool,\n        params: HTTP1ConnectionParameters = None,\n        context: object = None,\n    ) -> None:\n        \"\"\"\n        :arg stream: an `.IOStream`\n        :arg bool is_client: client or server\n        :arg params: a `.HTTP1ConnectionParameters` instance or ``None``\n        :arg context: an opaque application-defined object that can be accessed\n            as ``connection.context``.\n        \"\"\"\n        self.is_client = is_client\n        self.stream = stream\n        if params is None:\n            params = HTTP1ConnectionParameters()\n        self.params = params\n        self.context = context\n        self.no_keep_alive = params.no_keep_alive\n        # The body limits can be altered by the delegate, so save them\n        # here instead of just referencing self.params later.\n        self._max_body_size = self.params.max_body_size or self.stream.max_buffer_size\n        self._body_timeout = self.params.body_timeout\n        # _write_finished is set to True when finish() has been called,\n        # i.e. there will be no more data sent.  Data may still be in the\n        # stream's write buffer.\n        self._write_finished = False\n        # True when we have read the entire incoming body.\n        self._read_finished = False\n        # _finish_future resolves when all data has been written and flushed\n        # to the IOStream.\n        self._finish_future = Future()  # type: Future[None]\n        # If true, the connection should be closed after this request\n        # (after the response has been written in the server side,\n        # and after it has been read in the client)\n        self._disconnect_on_finish = False\n        self._clear_callbacks()\n        # Save the start lines after we read or write them; they\n        # affect later processing (e.g. 304 responses and HEAD methods\n        # have content-length but no bodies)\n        self._request_start_line = None  # type: Optional[httputil.RequestStartLine]\n        self._response_start_line = None  # type: Optional[httputil.ResponseStartLine]\n        self._request_headers = None  # type: Optional[httputil.HTTPHeaders]\n        # True if we are writing output with chunked encoding.\n        self._chunking_output = False\n        # While reading a body with a content-length, this is the\n        # amount left to read.\n        self._expected_content_remaining = None  # type: Optional[int]\n        # A Future for our outgoing writes, returned by IOStream.write.\n        self._pending_write = None  # type: Optional[Future[None]]\n\n    def read_response(self, delegate: httputil.HTTPMessageDelegate) -> Awaitable[bool]:\n        \"\"\"Read a single HTTP response.\n\n        Typical client-mode usage is to write a request using `write_headers`,\n        `write`, and `finish`, and then call ``read_response``.\n\n        :arg delegate: a `.HTTPMessageDelegate`\n\n        Returns a `.Future` that resolves to a bool after the full response has\n        been read. The result is true if the stream is still open.\n        \"\"\"\n        if self.params.decompress:\n            delegate = _GzipMessageDelegate(delegate, self.params.chunk_size)\n        return self._read_message(delegate)\n\n    async def _read_message(self, delegate: httputil.HTTPMessageDelegate) -> bool:\n        need_delegate_close = False\n        try:\n            header_future = self.stream.read_until_regex(\n                b\"\\r?\\n\\r?\\n\", max_bytes=self.params.max_header_size\n            )\n            if self.params.header_timeout is None:\n                header_data = await header_future\n            else:\n                try:\n                    header_data = await gen.with_timeout(\n                        self.stream.io_loop.time() + self.params.header_timeout,\n                        header_future,\n                        quiet_exceptions=iostream.StreamClosedError,\n                    )\n                except gen.TimeoutError:\n                    self.close()\n                    return False\n            start_line_str, headers = self._parse_headers(header_data)\n            if self.is_client:\n                resp_start_line = httputil.parse_response_start_line(start_line_str)\n                self._response_start_line = resp_start_line\n                start_line = (\n                    resp_start_line\n                )  # type: Union[httputil.RequestStartLine, httputil.ResponseStartLine]\n                # TODO: this will need to change to support client-side keepalive\n                self._disconnect_on_finish = False\n            else:\n                req_start_line = httputil.parse_request_start_line(start_line_str)\n                self._request_start_line = req_start_line\n                self._request_headers = headers\n                start_line = req_start_line\n                self._disconnect_on_finish = not self._can_keep_alive(\n                    req_start_line, headers\n                )\n            need_delegate_close = True\n            with _ExceptionLoggingContext(app_log):\n                header_recv_future = delegate.headers_received(start_line, headers)\n                if header_recv_future is not None:\n                    await header_recv_future\n            if self.stream is None:\n                # We've been detached.\n                need_delegate_close = False\n                return False\n            skip_body = False\n            if self.is_client:\n                assert isinstance(start_line, httputil.ResponseStartLine)\n                if (\n                    self._request_start_line is not None\n                    and self._request_start_line.method == \"HEAD\"\n                ):\n                    skip_body = True\n                code = start_line.code\n                if code == 304:\n                    # 304 responses may include the content-length header\n                    # but do not actually have a body.\n                    # http://tools.ietf.org/html/rfc7230#section-3.3\n                    skip_body = True\n                if code >= 100 and code < 200:\n                    # 1xx responses should never indicate the presence of\n                    # a body.\n                    if \"Content-Length\" in headers or \"Transfer-Encoding\" in headers:\n                        raise httputil.HTTPInputError(\n                            \"Response code %d cannot have body\" % code\n                        )\n                    # TODO: client delegates will get headers_received twice\n                    # in the case of a 100-continue.  Document or change?\n                    await self._read_message(delegate)\n            else:\n                if headers.get(\"Expect\") == \"100-continue\" and not self._write_finished:\n                    self.stream.write(b\"HTTP/1.1 100 (Continue)\\r\\n\\r\\n\")\n            if not skip_body:\n                body_future = self._read_body(\n                    resp_start_line.code if self.is_client else 0, headers, delegate\n                )\n                if body_future is not None:\n                    if self._body_timeout is None:\n                        await body_future\n                    else:\n                        try:\n                            await gen.with_timeout(\n                                self.stream.io_loop.time() + self._body_timeout,\n                                body_future,\n                                quiet_exceptions=iostream.StreamClosedError,\n                            )\n                        except gen.TimeoutError:\n                            gen_log.info(\"Timeout reading body from %s\", self.context)\n                            self.stream.close()\n                            return False\n            self._read_finished = True\n            if not self._write_finished or self.is_client:\n                need_delegate_close = False\n                with _ExceptionLoggingContext(app_log):\n                    delegate.finish()\n            # If we're waiting for the application to produce an asynchronous\n            # response, and we're not detached, register a close callback\n            # on the stream (we didn't need one while we were reading)\n            if (\n                not self._finish_future.done()\n                and self.stream is not None\n                and not self.stream.closed()\n            ):\n                self.stream.set_close_callback(self._on_connection_close)\n                await self._finish_future\n            if self.is_client and self._disconnect_on_finish:\n                self.close()\n            if self.stream is None:\n                return False\n        except httputil.HTTPInputError as e:\n            gen_log.info(\"Malformed HTTP message from %s: %s\", self.context, e)\n            if not self.is_client:\n                await self.stream.write(b\"HTTP/1.1 400 Bad Request\\r\\n\\r\\n\")\n            self.close()\n            return False\n        finally:\n            if need_delegate_close:\n                with _ExceptionLoggingContext(app_log):\n                    delegate.on_connection_close()\n            header_future = None  # type: ignore\n            self._clear_callbacks()\n        return True\n\n    def _clear_callbacks(self) -> None:\n        \"\"\"Clears the callback attributes.\n\n        This allows the request handler to be garbage collected more\n        quickly in CPython by breaking up reference cycles.\n        \"\"\"\n        self._write_callback = None\n        self._write_future = None  # type: Optional[Future[None]]\n        self._close_callback = None  # type: Optional[Callable[[], None]]\n        if self.stream is not None:\n            self.stream.set_close_callback(None)\n\n    def set_close_callback(self, callback: Optional[Callable[[], None]]) -> None:\n        \"\"\"Sets a callback that will be run when the connection is closed.\n\n        Note that this callback is slightly different from\n        `.HTTPMessageDelegate.on_connection_close`: The\n        `.HTTPMessageDelegate` method is called when the connection is\n        closed while recieving a message. This callback is used when\n        there is not an active delegate (for example, on the server\n        side this callback is used if the client closes the connection\n        after sending its request but before receiving all the\n        response.\n        \"\"\"\n        self._close_callback = callback\n\n    def _on_connection_close(self) -> None:\n        # Note that this callback is only registered on the IOStream\n        # when we have finished reading the request and are waiting for\n        # the application to produce its response.\n        if self._close_callback is not None:\n            callback = self._close_callback\n            self._close_callback = None\n            callback()\n        if not self._finish_future.done():\n            future_set_result_unless_cancelled(self._finish_future, None)\n        self._clear_callbacks()\n\n    def close(self) -> None:\n        if self.stream is not None:\n            self.stream.close()\n        self._clear_callbacks()\n        if not self._finish_future.done():\n            future_set_result_unless_cancelled(self._finish_future, None)\n\n    def detach(self) -> iostream.IOStream:\n        \"\"\"Take control of the underlying stream.\n\n        Returns the underlying `.IOStream` object and stops all further\n        HTTP processing.  May only be called during\n        `.HTTPMessageDelegate.headers_received`.  Intended for implementing\n        protocols like websockets that tunnel over an HTTP handshake.\n        \"\"\"\n        self._clear_callbacks()\n        stream = self.stream\n        self.stream = None  # type: ignore\n        if not self._finish_future.done():\n            future_set_result_unless_cancelled(self._finish_future, None)\n        return stream\n\n    def set_body_timeout(self, timeout: float) -> None:\n        \"\"\"Sets the body timeout for a single request.\n\n        Overrides the value from `.HTTP1ConnectionParameters`.\n        \"\"\"\n        self._body_timeout = timeout\n\n    def set_max_body_size(self, max_body_size: int) -> None:\n        \"\"\"Sets the body size limit for a single request.\n\n        Overrides the value from `.HTTP1ConnectionParameters`.\n        \"\"\"\n        self._max_body_size = max_body_size\n\n    def write_headers(\n        self,\n        start_line: Union[httputil.RequestStartLine, httputil.ResponseStartLine],\n        headers: httputil.HTTPHeaders,\n        chunk: bytes = None,\n    ) -> \"Future[None]\":\n        \"\"\"Implements `.HTTPConnection.write_headers`.\"\"\"\n        lines = []\n        if self.is_client:\n            assert isinstance(start_line, httputil.RequestStartLine)\n            self._request_start_line = start_line\n            lines.append(utf8(\"%s %s HTTP/1.1\" % (start_line[0], start_line[1])))\n            # Client requests with a non-empty body must have either a\n            # Content-Length or a Transfer-Encoding.\n            self._chunking_output = (\n                start_line.method in (\"POST\", \"PUT\", \"PATCH\")\n                and \"Content-Length\" not in headers\n                and \"Transfer-Encoding\" not in headers\n            )\n        else:\n            assert isinstance(start_line, httputil.ResponseStartLine)\n            assert self._request_start_line is not None\n            assert self._request_headers is not None\n            self._response_start_line = start_line\n            lines.append(utf8(\"HTTP/1.1 %d %s\" % (start_line[1], start_line[2])))\n            self._chunking_output = (\n                # TODO: should this use\n                # self._request_start_line.version or\n                # start_line.version?\n                self._request_start_line.version == \"HTTP/1.1\"\n                # 1xx, 204 and 304 responses have no body (not even a zero-length\n                # body), and so should not have either Content-Length or\n                # Transfer-Encoding headers.\n                and start_line.code not in (204, 304)\n                and (start_line.code < 100 or start_line.code >= 200)\n                # No need to chunk the output if a Content-Length is specified.\n                and \"Content-Length\" not in headers\n                # Applications are discouraged from touching Transfer-Encoding,\n                # but if they do, leave it alone.\n                and \"Transfer-Encoding\" not in headers\n            )\n            # If connection to a 1.1 client will be closed, inform client\n            if (\n                self._request_start_line.version == \"HTTP/1.1\"\n                and self._disconnect_on_finish\n            ):\n                headers[\"Connection\"] = \"close\"\n            # If a 1.0 client asked for keep-alive, add the header.\n            if (\n                self._request_start_line.version == \"HTTP/1.0\"\n                and self._request_headers.get(\"Connection\", \"\").lower() == \"keep-alive\"\n            ):\n                headers[\"Connection\"] = \"Keep-Alive\"\n        if self._chunking_output:\n            headers[\"Transfer-Encoding\"] = \"chunked\"\n        if not self.is_client and (\n            self._request_start_line.method == \"HEAD\"\n            or cast(httputil.ResponseStartLine, start_line).code == 304\n        ):\n            self._expected_content_remaining = 0\n        elif \"Content-Length\" in headers:\n            self._expected_content_remaining = int(headers[\"Content-Length\"])\n        else:\n            self._expected_content_remaining = None\n        # TODO: headers are supposed to be of type str, but we still have some\n        # cases that let bytes slip through. Remove these native_str calls when those\n        # are fixed.\n        header_lines = (\n            native_str(n) + \": \" + native_str(v) for n, v in headers.get_all()\n        )\n        lines.extend(l.encode(\"latin1\") for l in header_lines)\n        for line in lines:\n            if b\"\\n\" in line:\n                raise ValueError(\"Newline in header: \" + repr(line))\n        future = None\n        if self.stream.closed():\n            future = self._write_future = Future()\n            future.set_exception(iostream.StreamClosedError())\n            future.exception()\n        else:\n            future = self._write_future = Future()\n            data = b\"\\r\\n\".join(lines) + b\"\\r\\n\\r\\n\"\n            if chunk:\n                data += self._format_chunk(chunk)\n            self._pending_write = self.stream.write(data)\n            future_add_done_callback(self._pending_write, self._on_write_complete)\n        return future\n\n    def _format_chunk(self, chunk: bytes) -> bytes:\n        if self._expected_content_remaining is not None:\n            self._expected_content_remaining -= len(chunk)\n            if self._expected_content_remaining < 0:\n                # Close the stream now to stop further framing errors.\n                self.stream.close()\n                raise httputil.HTTPOutputError(\n                    \"Tried to write more data than Content-Length\"\n                )\n        if self._chunking_output and chunk:\n            # Don't write out empty chunks because that means END-OF-STREAM\n            # with chunked encoding\n            return utf8(\"%x\" % len(chunk)) + b\"\\r\\n\" + chunk + b\"\\r\\n\"\n        else:\n            return chunk\n\n    def write(self, chunk: bytes) -> \"Future[None]\":\n        \"\"\"Implements `.HTTPConnection.write`.\n\n        For backwards compatibility it is allowed but deprecated to\n        skip `write_headers` and instead call `write()` with a\n        pre-encoded header block.\n        \"\"\"\n        future = None\n        if self.stream.closed():\n            future = self._write_future = Future()\n            self._write_future.set_exception(iostream.StreamClosedError())\n            self._write_future.exception()\n        else:\n            future = self._write_future = Future()\n            self._pending_write = self.stream.write(self._format_chunk(chunk))\n            future_add_done_callback(self._pending_write, self._on_write_complete)\n        return future\n\n    def finish(self) -> None:\n        \"\"\"Implements `.HTTPConnection.finish`.\"\"\"\n        if (\n            self._expected_content_remaining is not None\n            and self._expected_content_remaining != 0\n            and not self.stream.closed()\n        ):\n            self.stream.close()\n            raise httputil.HTTPOutputError(\n                \"Tried to write %d bytes less than Content-Length\"\n                % self._expected_content_remaining\n            )\n        if self._chunking_output:\n            if not self.stream.closed():\n                self._pending_write = self.stream.write(b\"0\\r\\n\\r\\n\")\n                self._pending_write.add_done_callback(self._on_write_complete)\n        self._write_finished = True\n        # If the app finished the request while we're still reading,\n        # divert any remaining data away from the delegate and\n        # close the connection when we're done sending our response.\n        # Closing the connection is the only way to avoid reading the\n        # whole input body.\n        if not self._read_finished:\n            self._disconnect_on_finish = True\n        # No more data is coming, so instruct TCP to send any remaining\n        # data immediately instead of waiting for a full packet or ack.\n        self.stream.set_nodelay(True)\n        if self._pending_write is None:\n            self._finish_request(None)\n        else:\n            future_add_done_callback(self._pending_write, self._finish_request)\n\n    def _on_write_complete(self, future: \"Future[None]\") -> None:\n        exc = future.exception()\n        if exc is not None and not isinstance(exc, iostream.StreamClosedError):\n            future.result()\n        if self._write_callback is not None:\n            callback = self._write_callback\n            self._write_callback = None\n            self.stream.io_loop.add_callback(callback)\n        if self._write_future is not None:\n            future = self._write_future\n            self._write_future = None\n            future_set_result_unless_cancelled(future, None)\n\n    def _can_keep_alive(\n        self, start_line: httputil.RequestStartLine, headers: httputil.HTTPHeaders\n    ) -> bool:\n        if self.params.no_keep_alive:\n            return False\n        connection_header = headers.get(\"Connection\")\n        if connection_header is not None:\n            connection_header = connection_header.lower()\n        if start_line.version == \"HTTP/1.1\":\n            return connection_header != \"close\"\n        elif (\n            \"Content-Length\" in headers\n            or headers.get(\"Transfer-Encoding\", \"\").lower() == \"chunked\"\n            or getattr(start_line, \"method\", None) in (\"HEAD\", \"GET\")\n        ):\n            # start_line may be a request or response start line; only\n            # the former has a method attribute.\n            return connection_header == \"keep-alive\"\n        return False\n\n    def _finish_request(self, future: Optional[\"Future[None]\"]) -> None:\n        self._clear_callbacks()\n        if not self.is_client and self._disconnect_on_finish:\n            self.close()\n            return\n        # Turn Nagle's algorithm back on, leaving the stream in its\n        # default state for the next request.\n        self.stream.set_nodelay(False)\n        if not self._finish_future.done():\n            future_set_result_unless_cancelled(self._finish_future, None)\n\n    def _parse_headers(self, data: bytes) -> Tuple[str, httputil.HTTPHeaders]:\n        # The lstrip removes newlines that some implementations sometimes\n        # insert between messages of a reused connection.  Per RFC 7230,\n        # we SHOULD ignore at least one empty line before the request.\n        # http://tools.ietf.org/html/rfc7230#section-3.5\n        data_str = native_str(data.decode(\"latin1\")).lstrip(\"\\r\\n\")\n        # RFC 7230 section allows for both CRLF and bare LF.\n        eol = data_str.find(\"\\n\")\n        start_line = data_str[:eol].rstrip(\"\\r\")\n        headers = httputil.HTTPHeaders.parse(data_str[eol:])\n        return start_line, headers\n\n    def _read_body(\n        self,\n        code: int,\n        headers: httputil.HTTPHeaders,\n        delegate: httputil.HTTPMessageDelegate,\n    ) -> Optional[Awaitable[None]]:\n        if \"Content-Length\" in headers:\n            if \"Transfer-Encoding\" in headers:\n                # Response cannot contain both Content-Length and\n                # Transfer-Encoding headers.\n                # http://tools.ietf.org/html/rfc7230#section-3.3.3\n                raise httputil.HTTPInputError(\n                    \"Response with both Transfer-Encoding and Content-Length\"\n                )\n            if \",\" in headers[\"Content-Length\"]:\n                # Proxies sometimes cause Content-Length headers to get\n                # duplicated.  If all the values are identical then we can\n                # use them but if they differ it's an error.\n                pieces = re.split(r\",\\s*\", headers[\"Content-Length\"])\n                if any(i != pieces[0] for i in pieces):\n                    raise httputil.HTTPInputError(\n                        \"Multiple unequal Content-Lengths: %r\"\n                        % headers[\"Content-Length\"]\n                    )\n                headers[\"Content-Length\"] = pieces[0]\n\n            try:\n                content_length = int(headers[\"Content-Length\"])  # type: Optional[int]\n            except ValueError:\n                # Handles non-integer Content-Length value.\n                raise httputil.HTTPInputError(\n                    \"Only integer Content-Length is allowed: %s\"\n                    % headers[\"Content-Length\"]\n                )\n\n            if cast(int, content_length) > self._max_body_size:\n                raise httputil.HTTPInputError(\"Content-Length too long\")\n        else:\n            content_length = None\n\n        if code == 204:\n            # This response code is not allowed to have a non-empty body,\n            # and has an implicit length of zero instead of read-until-close.\n            # http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.3\n            if \"Transfer-Encoding\" in headers or content_length not in (None, 0):\n                raise httputil.HTTPInputError(\n                    \"Response with code %d should not have body\" % code\n                )\n            content_length = 0\n\n        if content_length is not None:\n            return self._read_fixed_body(content_length, delegate)\n        if headers.get(\"Transfer-Encoding\", \"\").lower() == \"chunked\":\n            return self._read_chunked_body(delegate)\n        if self.is_client:\n            return self._read_body_until_close(delegate)\n        return None\n\n    async def _read_fixed_body(\n        self, content_length: int, delegate: httputil.HTTPMessageDelegate\n    ) -> None:\n        while content_length > 0:\n            body = await self.stream.read_bytes(\n                min(self.params.chunk_size, content_length), partial=True\n            )\n            content_length -= len(body)\n            if not self._write_finished or self.is_client:\n                with _ExceptionLoggingContext(app_log):\n                    ret = delegate.data_received(body)\n                    if ret is not None:\n                        await ret\n\n    async def _read_chunked_body(self, delegate: httputil.HTTPMessageDelegate) -> None:\n        # TODO: \"chunk extensions\" http://tools.ietf.org/html/rfc2616#section-3.6.1\n        total_size = 0\n        while True:\n            chunk_len_str = await self.stream.read_until(b\"\\r\\n\", max_bytes=64)\n            chunk_len = int(chunk_len_str.strip(), 16)\n            if chunk_len == 0:\n                crlf = await self.stream.read_bytes(2)\n                if crlf != b\"\\r\\n\":\n                    raise httputil.HTTPInputError(\n                        \"improperly terminated chunked request\"\n                    )\n                return\n            total_size += chunk_len\n            if total_size > self._max_body_size:\n                raise httputil.HTTPInputError(\"chunked body too large\")\n            bytes_to_read = chunk_len\n            while bytes_to_read:\n                chunk = await self.stream.read_bytes(\n                    min(bytes_to_read, self.params.chunk_size), partial=True\n                )\n                bytes_to_read -= len(chunk)\n                if not self._write_finished or self.is_client:\n                    with _ExceptionLoggingContext(app_log):\n                        ret = delegate.data_received(chunk)\n                        if ret is not None:\n                            await ret\n            # chunk ends with \\r\\n\n            crlf = await self.stream.read_bytes(2)\n            assert crlf == b\"\\r\\n\"\n\n    async def _read_body_until_close(\n        self, delegate: httputil.HTTPMessageDelegate\n    ) -> None:\n        body = await self.stream.read_until_close()\n        if not self._write_finished or self.is_client:\n            with _ExceptionLoggingContext(app_log):\n                ret = delegate.data_received(body)\n                if ret is not None:\n                    await ret\n\n\nclass _GzipMessageDelegate(httputil.HTTPMessageDelegate):\n    \"\"\"Wraps an `HTTPMessageDelegate` to decode ``Content-Encoding: gzip``.\n    \"\"\"\n\n    def __init__(self, delegate: httputil.HTTPMessageDelegate, chunk_size: int) -> None:\n        self._delegate = delegate\n        self._chunk_size = chunk_size\n        self._decompressor = None  # type: Optional[GzipDecompressor]\n\n    def headers_received(\n        self,\n        start_line: Union[httputil.RequestStartLine, httputil.ResponseStartLine],\n        headers: httputil.HTTPHeaders,\n    ) -> Optional[Awaitable[None]]:\n        if headers.get(\"Content-Encoding\") == \"gzip\":\n            self._decompressor = GzipDecompressor()\n            # Downstream delegates will only see uncompressed data,\n            # so rename the content-encoding header.\n            # (but note that curl_httpclient doesn't do this).\n            headers.add(\"X-Consumed-Content-Encoding\", headers[\"Content-Encoding\"])\n            del headers[\"Content-Encoding\"]\n        return self._delegate.headers_received(start_line, headers)\n\n    async def data_received(self, chunk: bytes) -> None:\n        if self._decompressor:\n            compressed_data = chunk\n            while compressed_data:\n                decompressed = self._decompressor.decompress(\n                    compressed_data, self._chunk_size\n                )\n                if decompressed:\n                    ret = self._delegate.data_received(decompressed)\n                    if ret is not None:\n                        await ret\n                compressed_data = self._decompressor.unconsumed_tail\n        else:\n            ret = self._delegate.data_received(chunk)\n            if ret is not None:\n                await ret\n\n    def finish(self) -> None:\n        if self._decompressor is not None:\n            tail = self._decompressor.flush()\n            if tail:\n                # The tail should always be empty: decompress returned\n                # all that it can in data_received and the only\n                # purpose of the flush call is to detect errors such\n                # as truncated input. If we did legitimately get a new\n                # chunk at this point we'd need to change the\n                # interface to make finish() a coroutine.\n                raise ValueError(\n                    \"decompressor.flush returned data; possile truncated input\"\n                )\n        return self._delegate.finish()\n\n    def on_connection_close(self) -> None:\n        return self._delegate.on_connection_close()\n\n\nclass HTTP1ServerConnection(object):\n    \"\"\"An HTTP/1.x server.\"\"\"\n\n    def __init__(\n        self,\n        stream: iostream.IOStream,\n        params: HTTP1ConnectionParameters = None,\n        context: object = None,\n    ) -> None:\n        \"\"\"\n        :arg stream: an `.IOStream`\n        :arg params: a `.HTTP1ConnectionParameters` or None\n        :arg context: an opaque application-defined object that is accessible\n            as ``connection.context``\n        \"\"\"\n        self.stream = stream\n        if params is None:\n            params = HTTP1ConnectionParameters()\n        self.params = params\n        self.context = context\n        self._serving_future = None  # type: Optional[Future[None]]\n\n    async def close(self) -> None:\n        \"\"\"Closes the connection.\n\n        Returns a `.Future` that resolves after the serving loop has exited.\n        \"\"\"\n        self.stream.close()\n        # Block until the serving loop is done, but ignore any exceptions\n        # (start_serving is already responsible for logging them).\n        assert self._serving_future is not None\n        try:\n            await self._serving_future\n        except Exception:\n            pass\n\n    def start_serving(self, delegate: httputil.HTTPServerConnectionDelegate) -> None:\n        \"\"\"Starts serving requests on this connection.\n\n        :arg delegate: a `.HTTPServerConnectionDelegate`\n        \"\"\"\n        assert isinstance(delegate, httputil.HTTPServerConnectionDelegate)\n        fut = gen.convert_yielded(self._server_request_loop(delegate))\n        self._serving_future = fut\n        # Register the future on the IOLoop so its errors get logged.\n        self.stream.io_loop.add_future(fut, lambda f: f.result())\n\n    async def _server_request_loop(\n        self, delegate: httputil.HTTPServerConnectionDelegate\n    ) -> None:\n        try:\n            while True:\n                conn = HTTP1Connection(self.stream, False, self.params, self.context)\n                request_delegate = delegate.start_request(self, conn)\n                try:\n                    ret = await conn.read_response(request_delegate)\n                except (iostream.StreamClosedError, iostream.UnsatisfiableReadError):\n                    return\n                except _QuietException:\n                    # This exception was already logged.\n                    conn.close()\n                    return\n                except Exception:\n                    gen_log.error(\"Uncaught exception\", exc_info=True)\n                    conn.close()\n                    return\n                if not ret:\n                    return\n                await asyncio.sleep(0)\n        finally:\n            delegate.on_close(self)\n",
            "header": {
                "file": {
                    "path": "tornado/http1connection.py"
                },
                "misc_line": null
            },
            "index_line": {
                "old_commit_hash": "10402b4b",
                "new_commit_hash": "0413fd9a",
                "mode": "100644"
            },
            "is_binary_file": false,
            "binary_line": null,
            "minus_file": {
                "path": "a/tornado/http1connection.py"
            },
            "plus_file": {
                "path": "b/tornado/http1connection.py"
            },
            "hunks": [
                {
                    "descriptor": {
                        "old_range": {
                            "start": 492,
                            "length": 7
                        },
                        "new_range": {
                            "start": 492,
                            "length": 7
                        },
                        "section": "class HTTP1Connection(httputil.HTTPConnection):"
                    },
                    "line_group": {
                        "all_lines": [
                            {
                                "content": "        else:",
                                "type": "context"
                            },
                            {
                                "content": "            future = self._write_future = Future()",
                                "type": "context"
                            },
                            {
                                "content": "            self._pending_write = self.stream.write(self._format_chunk(chunk))",
                                "type": "context"
                            },
                            {
                                "content": "            self._pending_write.add_done_callback(self._on_write_complete)",
                                "type": "deleted"
                            },
                            {
                                "content": "            future_add_done_callback(self._pending_write, self._on_write_complete)",
                                "type": "added"
                            },
                            {
                                "content": "        return future",
                                "type": "context"
                            },
                            {
                                "content": "",
                                "type": "context"
                            },
                            {
                                "content": "    def finish(self) -> None:",
                                "type": "context"
                            }
                        ]
                    },
                    "modified_entities": [
                        {
                            "file_name": "tornado/http1connection.py",
                            "type": "method",
                            "name": "HTTP1Connection.write",
                            "content": "class HTTP1Connection(httputil.HTTPConnection):\n    \"\"\"Implements the HTTP/1.x protocol.\n\n    This class can be on its own for clients, or via `HTTP1ServerConnection`\n    for servers.\n    \"\"\"\n\n    def __init__(\n        self,\n        stream: iostream.IOStream,\n        is_client: bool,\n        params: HTTP1ConnectionParameters = None,\n        context: object = None,\n    ) -> None:\n        \"\"\"\n        :arg stream: an `.IOStream`\n        :arg bool is_client: client or server\n        :arg params: a `.HTTP1ConnectionParameters` instance or ``None``\n        :arg context: an opaque application-defined object that can be accessed\n            as ``connection.context``.\n        \"\"\"\n        self.is_client = is_client\n        self.stream = stream\n        if params is None:\n            params = HTTP1ConnectionParameters()\n        self.params = params\n        self.context = context\n        self.no_keep_alive = params.no_keep_alive\n        # The body limits can be altered by the delegate, so save them\n        # here instead of just referencing self.params later.\n        self._max_body_size = self.params.max_body_size or self.stream.max_buffer_size\n        self._body_timeout = self.params.body_timeout\n        # _write_finished is set to True when finish() has been called,\n        # i.e. there will be no more data sent.  Data may still be in the\n        # stream's write buffer.\n        self._write_finished = False\n        # True when we have read the entire incoming body.\n        self._read_finished = False\n        # _finish_future resolves when all data has been written and flushed\n        # to the IOStream.\n        self._finish_future = Future()  # type: Future[None]\n        # If true, the connection should be closed after this request\n        # (after the response has been written in the server side,\n        # and after it has been read in the client)\n        self._disconnect_on_finish = False\n        self._clear_callbacks()\n        # Save the start lines after we read or write them; they\n        # affect later processing (e.g. 304 responses and HEAD methods\n        # have content-length but no bodies)\n        self._request_start_line = None  # type: Optional[httputil.RequestStartLine]\n        self._response_start_line = None  # type: Optional[httputil.ResponseStartLine]\n        self._request_headers = None  # type: Optional[httputil.HTTPHeaders]\n        # True if we are writing output with chunked encoding.\n        self._chunking_output = False\n        # While reading a body with a content-length, this is the\n        # amount left to read.\n        self._expected_content_remaining = None  # type: Optional[int]\n        # A Future for our outgoing writes, returned by IOStream.write.\n        self._pending_write = None  # type: Optional[Future[None]]\n\n    def read_response(self, delegate: httputil.HTTPMessageDelegate) -> Awaitable[bool]:\n        \"\"\"Read a single HTTP response.\n\n        Typical client-mode usage is to write a request using `write_headers`,\n        `write`, and `finish`, and then call ``read_response``.\n\n        :arg delegate: a `.HTTPMessageDelegate`\n\n        Returns a `.Future` that resolves to a bool after the full response has\n        been read. The result is true if the stream is still open.\n        \"\"\"\n        if self.params.decompress:\n            delegate = _GzipMessageDelegate(delegate, self.params.chunk_size)\n        return self._read_message(delegate)\n\n    async def _read_message(self, delegate: httputil.HTTPMessageDelegate) -> bool:\n        need_delegate_close = False\n        try:\n            header_future = self.stream.read_until_regex(\n                b\"\\r?\\n\\r?\\n\", max_bytes=self.params.max_header_size\n            )\n            if self.params.header_timeout is None:\n                header_data = await header_future\n            else:\n                try:\n                    header_data = await gen.with_timeout(\n                        self.stream.io_loop.time() + self.params.header_timeout,\n                        header_future,\n                        quiet_exceptions=iostream.StreamClosedError,\n                    )\n                except gen.TimeoutError:\n                    self.close()\n                    return False\n            start_line_str, headers = self._parse_headers(header_data)\n            if self.is_client:\n                resp_start_line = httputil.parse_response_start_line(start_line_str)\n                self._response_start_line = resp_start_line\n                start_line = (\n                    resp_start_line\n                )  # type: Union[httputil.RequestStartLine, httputil.ResponseStartLine]\n                # TODO: this will need to change to support client-side keepalive\n                self._disconnect_on_finish = False\n            else:\n                req_start_line = httputil.parse_request_start_line(start_line_str)\n                self._request_start_line = req_start_line\n                self._request_headers = headers\n                start_line = req_start_line\n                self._disconnect_on_finish = not self._can_keep_alive(\n                    req_start_line, headers\n                )\n            need_delegate_close = True\n            with _ExceptionLoggingContext(app_log):\n                header_recv_future = delegate.headers_received(start_line, headers)\n                if header_recv_future is not None:\n                    await header_recv_future\n            if self.stream is None:\n                # We've been detached.\n                need_delegate_close = False\n                return False\n            skip_body = False\n            if self.is_client:\n                assert isinstance(start_line, httputil.ResponseStartLine)\n                if (\n                    self._request_start_line is not None\n                    and self._request_start_line.method == \"HEAD\"\n                ):\n                    skip_body = True\n                code = start_line.code\n                if code == 304:\n                    # 304 responses may include the content-length header\n                    # but do not actually have a body.\n                    # http://tools.ietf.org/html/rfc7230#section-3.3\n                    skip_body = True\n                if code >= 100 and code < 200:\n                    # 1xx responses should never indicate the presence of\n                    # a body.\n                    if \"Content-Length\" in headers or \"Transfer-Encoding\" in headers:\n                        raise httputil.HTTPInputError(\n                            \"Response code %d cannot have body\" % code\n                        )\n                    # TODO: client delegates will get headers_received twice\n                    # in the case of a 100-continue.  Document or change?\n                    await self._read_message(delegate)\n            else:\n                if headers.get(\"Expect\") == \"100-continue\" and not self._write_finished:\n                    self.stream.write(b\"HTTP/1.1 100 (Continue)\\r\\n\\r\\n\")\n            if not skip_body:\n                body_future = self._read_body(\n                    resp_start_line.code if self.is_client else 0, headers, delegate\n                )\n                if body_future is not None:\n                    if self._body_timeout is None:\n                        await body_future\n                    else:\n                        try:\n                            await gen.with_timeout(\n                                self.stream.io_loop.time() + self._body_timeout,\n                                body_future,\n                                quiet_exceptions=iostream.StreamClosedError,\n                            )\n                        except gen.TimeoutError:\n                            gen_log.info(\"Timeout reading body from %s\", self.context)\n                            self.stream.close()\n                            return False\n            self._read_finished = True\n            if not self._write_finished or self.is_client:\n                need_delegate_close = False\n                with _ExceptionLoggingContext(app_log):\n                    delegate.finish()\n            # If we're waiting for the application to produce an asynchronous\n            # response, and we're not detached, register a close callback\n            # on the stream (we didn't need one while we were reading)\n            if (\n                not self._finish_future.done()\n                and self.stream is not None\n                and not self.stream.closed()\n            ):\n                self.stream.set_close_callback(self._on_connection_close)\n                await self._finish_future\n            if self.is_client and self._disconnect_on_finish:\n                self.close()\n            if self.stream is None:\n                return False\n        except httputil.HTTPInputError as e:\n            gen_log.info(\"Malformed HTTP message from %s: %s\", self.context, e)\n            if not self.is_client:\n                await self.stream.write(b\"HTTP/1.1 400 Bad Request\\r\\n\\r\\n\")\n            self.close()\n            return False\n        finally:\n            if need_delegate_close:\n                with _ExceptionLoggingContext(app_log):\n                    delegate.on_connection_close()\n            header_future = None  # type: ignore\n            self._clear_callbacks()\n        return True\n\n    def _clear_callbacks(self) -> None:\n        \"\"\"Clears the callback attributes.\n\n        This allows the request handler to be garbage collected more\n        quickly in CPython by breaking up reference cycles.\n        \"\"\"\n        self._write_callback = None\n        self._write_future = None  # type: Optional[Future[None]]\n        self._close_callback = None  # type: Optional[Callable[[], None]]\n        if self.stream is not None:\n            self.stream.set_close_callback(None)\n\n    def set_close_callback(self, callback: Optional[Callable[[], None]]) -> None:\n        \"\"\"Sets a callback that will be run when the connection is closed.\n\n        Note that this callback is slightly different from\n        `.HTTPMessageDelegate.on_connection_close`: The\n        `.HTTPMessageDelegate` method is called when the connection is\n        closed while recieving a message. This callback is used when\n        there is not an active delegate (for example, on the server\n        side this callback is used if the client closes the connection\n        after sending its request but before receiving all the\n        response.\n        \"\"\"\n        self._close_callback = callback\n\n    def _on_connection_close(self) -> None:\n        # Note that this callback is only registered on the IOStream\n        # when we have finished reading the request and are waiting for\n        # the application to produce its response.\n        if self._close_callback is not None:\n            callback = self._close_callback\n            self._close_callback = None\n            callback()\n        if not self._finish_future.done():\n            future_set_result_unless_cancelled(self._finish_future, None)\n        self._clear_callbacks()\n\n    def close(self) -> None:\n        if self.stream is not None:\n            self.stream.close()\n        self._clear_callbacks()\n        if not self._finish_future.done():\n            future_set_result_unless_cancelled(self._finish_future, None)\n\n    def detach(self) -> iostream.IOStream:\n        \"\"\"Take control of the underlying stream.\n\n        Returns the underlying `.IOStream` object and stops all further\n        HTTP processing.  May only be called during\n        `.HTTPMessageDelegate.headers_received`.  Intended for implementing\n        protocols like websockets that tunnel over an HTTP handshake.\n        \"\"\"\n        self._clear_callbacks()\n        stream = self.stream\n        self.stream = None  # type: ignore\n        if not self._finish_future.done():\n            future_set_result_unless_cancelled(self._finish_future, None)\n        return stream\n\n    def set_body_timeout(self, timeout: float) -> None:\n        \"\"\"Sets the body timeout for a single request.\n\n        Overrides the value from `.HTTP1ConnectionParameters`.\n        \"\"\"\n        self._body_timeout = timeout\n\n    def set_max_body_size(self, max_body_size: int) -> None:\n        \"\"\"Sets the body size limit for a single request.\n\n        Overrides the value from `.HTTP1ConnectionParameters`.\n        \"\"\"\n        self._max_body_size = max_body_size\n\n    def write_headers(\n        self,\n        start_line: Union[httputil.RequestStartLine, httputil.ResponseStartLine],\n        headers: httputil.HTTPHeaders,\n        chunk: bytes = None,\n    ) -> \"Future[None]\":\n        \"\"\"Implements `.HTTPConnection.write_headers`.\"\"\"\n        lines = []\n        if self.is_client:\n            assert isinstance(start_line, httputil.RequestStartLine)\n            self._request_start_line = start_line\n            lines.append(utf8(\"%s %s HTTP/1.1\" % (start_line[0], start_line[1])))\n            # Client requests with a non-empty body must have either a\n            # Content-Length or a Transfer-Encoding.\n            self._chunking_output = (\n                start_line.method in (\"POST\", \"PUT\", \"PATCH\")\n                and \"Content-Length\" not in headers\n                and \"Transfer-Encoding\" not in headers\n            )\n        else:\n            assert isinstance(start_line, httputil.ResponseStartLine)\n            assert self._request_start_line is not None\n            assert self._request_headers is not None\n            self._response_start_line = start_line\n            lines.append(utf8(\"HTTP/1.1 %d %s\" % (start_line[1], start_line[2])))\n            self._chunking_output = (\n                # TODO: should this use\n                # self._request_start_line.version or\n                # start_line.version?\n                self._request_start_line.version == \"HTTP/1.1\"\n                # 1xx, 204 and 304 responses have no body (not even a zero-length\n                # body), and so should not have either Content-Length or\n                # Transfer-Encoding headers.\n                and start_line.code not in (204, 304)\n                and (start_line.code < 100 or start_line.code >= 200)\n                # No need to chunk the output if a Content-Length is specified.\n                and \"Content-Length\" not in headers\n                # Applications are discouraged from touching Transfer-Encoding,\n                # but if they do, leave it alone.\n                and \"Transfer-Encoding\" not in headers\n            )\n            # If connection to a 1.1 client will be closed, inform client\n            if (\n                self._request_start_line.version == \"HTTP/1.1\"\n                and self._disconnect_on_finish\n            ):\n                headers[\"Connection\"] = \"close\"\n            # If a 1.0 client asked for keep-alive, add the header.\n            if (\n                self._request_start_line.version == \"HTTP/1.0\"\n                and self._request_headers.get(\"Connection\", \"\").lower() == \"keep-alive\"\n            ):\n                headers[\"Connection\"] = \"Keep-Alive\"\n        if self._chunking_output:\n            headers[\"Transfer-Encoding\"] = \"chunked\"\n        if not self.is_client and (\n            self._request_start_line.method == \"HEAD\"\n            or cast(httputil.ResponseStartLine, start_line).code == 304\n        ):\n            self._expected_content_remaining = 0\n        elif \"Content-Length\" in headers:\n            self._expected_content_remaining = int(headers[\"Content-Length\"])\n        else:\n            self._expected_content_remaining = None\n        # TODO: headers are supposed to be of type str, but we still have some\n        # cases that let bytes slip through. Remove these native_str calls when those\n        # are fixed.\n        header_lines = (\n            native_str(n) + \": \" + native_str(v) for n, v in headers.get_all()\n        )\n        lines.extend(l.encode(\"latin1\") for l in header_lines)\n        for line in lines:\n            if b\"\\n\" in line:\n                raise ValueError(\"Newline in header: \" + repr(line))\n        future = None\n        if self.stream.closed():\n            future = self._write_future = Future()\n            future.set_exception(iostream.StreamClosedError())\n            future.exception()\n        else:\n            future = self._write_future = Future()\n            data = b\"\\r\\n\".join(lines) + b\"\\r\\n\\r\\n\"\n            if chunk:\n                data += self._format_chunk(chunk)\n            self._pending_write = self.stream.write(data)\n            future_add_done_callback(self._pending_write, self._on_write_complete)\n        return future\n\n    def _format_chunk(self, chunk: bytes) -> bytes:\n        if self._expected_content_remaining is not None:\n            self._expected_content_remaining -= len(chunk)\n            if self._expected_content_remaining < 0:\n                # Close the stream now to stop further framing errors.\n                self.stream.close()\n                raise httputil.HTTPOutputError(\n                    \"Tried to write more data than Content-Length\"\n                )\n        if self._chunking_output and chunk:\n            # Don't write out empty chunks because that means END-OF-STREAM\n            # with chunked encoding\n            return utf8(\"%x\" % len(chunk)) + b\"\\r\\n\" + chunk + b\"\\r\\n\"\n        else:\n            return chunk\n\n    def write(self, chunk: bytes) -> \"Future[None]\":\n        \"\"\"Implements `.HTTPConnection.write`.\n\n        For backwards compatibility it is allowed but deprecated to\n        skip `write_headers` and instead call `write()` with a\n        pre-encoded header block.\n        \"\"\"\n        future = None\n        if self.stream.closed():\n            future = self._write_future = Future()\n            self._write_future.set_exception(iostream.StreamClosedError())\n            self._write_future.exception()\n        else:\n            future = self._write_future = Future()\n            self._pending_write = self.stream.write(self._format_chunk(chunk))\n            self._pending_write.add_done_callback(self._on_write_complete)\n        return future\n\n    def finish(self) -> None:\n        \"\"\"Implements `.HTTPConnection.finish`.\"\"\"\n        if (\n            self._expected_content_remaining is not None\n            and self._expected_content_remaining != 0\n            and not self.stream.closed()\n        ):\n            self.stream.close()\n            raise httputil.HTTPOutputError(\n                \"Tried to write %d bytes less than Content-Length\"\n                % self._expected_content_remaining\n            )\n        if self._chunking_output:\n            if not self.stream.closed():\n                self._pending_write = self.stream.write(b\"0\\r\\n\\r\\n\")\n                self._pending_write.add_done_callback(self._on_write_complete)\n        self._write_finished = True\n        # If the app finished the request while we're still reading,\n        # divert any remaining data away from the delegate and\n        # close the connection when we're done sending our response.\n        # Closing the connection is the only way to avoid reading the\n        # whole input body.\n        if not self._read_finished:\n            self._disconnect_on_finish = True\n        # No more data is coming, so instruct TCP to send any remaining\n        # data immediately instead of waiting for a full packet or ack.\n        self.stream.set_nodelay(True)\n        if self._pending_write is None:\n            self._finish_request(None)\n        else:\n            future_add_done_callback(self._pending_write, self._finish_request)\n\n    def _on_write_complete(self, future: \"Future[None]\") -> None:\n        exc = future.exception()\n        if exc is not None and not isinstance(exc, iostream.StreamClosedError):\n            future.result()\n        if self._write_callback is not None:\n            callback = self._write_callback\n            self._write_callback = None\n            self.stream.io_loop.add_callback(callback)\n        if self._write_future is not None:\n            future = self._write_future\n            self._write_future = None\n            future_set_result_unless_cancelled(future, None)\n\n    def _can_keep_alive(\n        self, start_line: httputil.RequestStartLine, headers: httputil.HTTPHeaders\n    ) -> bool:\n        if self.params.no_keep_alive:\n            return False\n        connection_header = headers.get(\"Connection\")\n        if connection_header is not None:\n            connection_header = connection_header.lower()\n        if start_line.version == \"HTTP/1.1\":\n            return connection_header != \"close\"\n        elif (\n            \"Content-Length\" in headers\n            or headers.get(\"Transfer-Encoding\", \"\").lower() == \"chunked\"\n            or getattr(start_line, \"method\", None) in (\"HEAD\", \"GET\")\n        ):\n            # start_line may be a request or response start line; only\n            # the former has a method attribute.\n            return connection_header == \"keep-alive\"\n        return False\n\n    def _finish_request(self, future: Optional[\"Future[None]\"]) -> None:\n        self._clear_callbacks()\n        if not self.is_client and self._disconnect_on_finish:\n            self.close()\n            return\n        # Turn Nagle's algorithm back on, leaving the stream in its\n        # default state for the next request.\n        self.stream.set_nodelay(False)\n        if not self._finish_future.done():\n            future_set_result_unless_cancelled(self._finish_future, None)\n\n    def _parse_headers(self, data: bytes) -> Tuple[str, httputil.HTTPHeaders]:\n        # The lstrip removes newlines that some implementations sometimes\n        # insert between messages of a reused connection.  Per RFC 7230,\n        # we SHOULD ignore at least one empty line before the request.\n        # http://tools.ietf.org/html/rfc7230#section-3.5\n        data_str = native_str(data.decode(\"latin1\")).lstrip(\"\\r\\n\")\n        # RFC 7230 section allows for both CRLF and bare LF.\n        eol = data_str.find(\"\\n\")\n        start_line = data_str[:eol].rstrip(\"\\r\")\n        headers = httputil.HTTPHeaders.parse(data_str[eol:])\n        return start_line, headers\n\n    def _read_body(\n        self,\n        code: int,\n        headers: httputil.HTTPHeaders,\n        delegate: httputil.HTTPMessageDelegate,\n    ) -> Optional[Awaitable[None]]:\n        if \"Content-Length\" in headers:\n            if \"Transfer-Encoding\" in headers:\n                # Response cannot contain both Content-Length and\n                # Transfer-Encoding headers.\n                # http://tools.ietf.org/html/rfc7230#section-3.3.3\n                raise httputil.HTTPInputError(\n                    \"Response with both Transfer-Encoding and Content-Length\"\n                )\n            if \",\" in headers[\"Content-Length\"]:\n                # Proxies sometimes cause Content-Length headers to get\n                # duplicated.  If all the values are identical then we can\n                # use them but if they differ it's an error.\n                pieces = re.split(r\",\\s*\", headers[\"Content-Length\"])\n                if any(i != pieces[0] for i in pieces):\n                    raise httputil.HTTPInputError(\n                        \"Multiple unequal Content-Lengths: %r\"\n                        % headers[\"Content-Length\"]\n                    )\n                headers[\"Content-Length\"] = pieces[0]\n\n            try:\n                content_length = int(headers[\"Content-Length\"])  # type: Optional[int]\n            except ValueError:\n                # Handles non-integer Content-Length value.\n                raise httputil.HTTPInputError(\n                    \"Only integer Content-Length is allowed: %s\"\n                    % headers[\"Content-Length\"]\n                )\n\n            if cast(int, content_length) > self._max_body_size:\n                raise httputil.HTTPInputError(\"Content-Length too long\")\n        else:\n            content_length = None\n\n        if code == 204:\n            # This response code is not allowed to have a non-empty body,\n            # and has an implicit length of zero instead of read-until-close.\n            # http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.3\n            if \"Transfer-Encoding\" in headers or content_length not in (None, 0):\n                raise httputil.HTTPInputError(\n                    \"Response with code %d should not have body\" % code\n                )\n            content_length = 0\n\n        if content_length is not None:\n            return self._read_fixed_body(content_length, delegate)\n        if headers.get(\"Transfer-Encoding\", \"\").lower() == \"chunked\":\n            return self._read_chunked_body(delegate)\n        if self.is_client:\n            return self._read_body_until_close(delegate)\n        return None\n\n    async def _read_fixed_body(\n        self, content_length: int, delegate: httputil.HTTPMessageDelegate\n    ) -> None:\n        while content_length > 0:\n            body = await self.stream.read_bytes(\n                min(self.params.chunk_size, content_length), partial=True\n            )\n            content_length -= len(body)\n            if not self._write_finished or self.is_client:\n                with _ExceptionLoggingContext(app_log):\n                    ret = delegate.data_received(body)\n                    if ret is not None:\n                        await ret\n\n    async def _read_chunked_body(self, delegate: httputil.HTTPMessageDelegate) -> None:\n        # TODO: \"chunk extensions\" http://tools.ietf.org/html/rfc2616#section-3.6.1\n        total_size = 0\n        while True:\n            chunk_len_str = await self.stream.read_until(b\"\\r\\n\", max_bytes=64)\n            chunk_len = int(chunk_len_str.strip(), 16)\n            if chunk_len == 0:\n                crlf = await self.stream.read_bytes(2)\n                if crlf != b\"\\r\\n\":\n                    raise httputil.HTTPInputError(\n                        \"improperly terminated chunked request\"\n                    )\n                return\n            total_size += chunk_len\n            if total_size > self._max_body_size:\n                raise httputil.HTTPInputError(\"chunked body too large\")\n            bytes_to_read = chunk_len\n            while bytes_to_read:\n                chunk = await self.stream.read_bytes(\n                    min(bytes_to_read, self.params.chunk_size), partial=True\n                )\n                bytes_to_read -= len(chunk)\n                if not self._write_finished or self.is_client:\n                    with _ExceptionLoggingContext(app_log):\n                        ret = delegate.data_received(chunk)\n                        if ret is not None:\n                            await ret\n            # chunk ends with \\r\\n\n            crlf = await self.stream.read_bytes(2)\n            assert crlf == b\"\\r\\n\"\n\n    async def _read_body_until_close(\n        self, delegate: httputil.HTTPMessageDelegate\n    ) -> None:\n        body = await self.stream.read_until_close()\n        if not self._write_finished or self.is_client:\n            with _ExceptionLoggingContext(app_log):\n                ret = delegate.data_received(body)\n                if ret is not None:\n                    await ret",
                            "ast_type_str": "ClassDef",
                            "start_lineno": 480,
                            "end_lineno": 496,
                            "parent": {
                                "file_name": "tornado/http1connection.py",
                                "type": "class",
                                "name": "HTTP1Connection",
                                "content": "class HTTP1Connection(httputil.HTTPConnection):\n    \"\"\"Implements the HTTP/1.x protocol.\n\n    This class can be on its own for clients, or via `HTTP1ServerConnection`\n    for servers.\n    \"\"\"\n\n    def __init__(\n        self,\n        stream: iostream.IOStream,\n        is_client: bool,\n        params: HTTP1ConnectionParameters = None,\n        context: object = None,\n    ) -> None:\n        \"\"\"\n        :arg stream: an `.IOStream`\n        :arg bool is_client: client or server\n        :arg params: a `.HTTP1ConnectionParameters` instance or ``None``\n        :arg context: an opaque application-defined object that can be accessed\n            as ``connection.context``.\n        \"\"\"\n        self.is_client = is_client\n        self.stream = stream\n        if params is None:\n            params = HTTP1ConnectionParameters()\n        self.params = params\n        self.context = context\n        self.no_keep_alive = params.no_keep_alive\n        # The body limits can be altered by the delegate, so save them\n        # here instead of just referencing self.params later.\n        self._max_body_size = self.params.max_body_size or self.stream.max_buffer_size\n        self._body_timeout = self.params.body_timeout\n        # _write_finished is set to True when finish() has been called,\n        # i.e. there will be no more data sent.  Data may still be in the\n        # stream's write buffer.\n        self._write_finished = False\n        # True when we have read the entire incoming body.\n        self._read_finished = False\n        # _finish_future resolves when all data has been written and flushed\n        # to the IOStream.\n        self._finish_future = Future()  # type: Future[None]\n        # If true, the connection should be closed after this request\n        # (after the response has been written in the server side,\n        # and after it has been read in the client)\n        self._disconnect_on_finish = False\n        self._clear_callbacks()\n        # Save the start lines after we read or write them; they\n        # affect later processing (e.g. 304 responses and HEAD methods\n        # have content-length but no bodies)\n        self._request_start_line = None  # type: Optional[httputil.RequestStartLine]\n        self._response_start_line = None  # type: Optional[httputil.ResponseStartLine]\n        self._request_headers = None  # type: Optional[httputil.HTTPHeaders]\n        # True if we are writing output with chunked encoding.\n        self._chunking_output = False\n        # While reading a body with a content-length, this is the\n        # amount left to read.\n        self._expected_content_remaining = None  # type: Optional[int]\n        # A Future for our outgoing writes, returned by IOStream.write.\n        self._pending_write = None  # type: Optional[Future[None]]\n\n    def read_response(self, delegate: httputil.HTTPMessageDelegate) -> Awaitable[bool]:\n        \"\"\"Read a single HTTP response.\n\n        Typical client-mode usage is to write a request using `write_headers`,\n        `write`, and `finish`, and then call ``read_response``.\n\n        :arg delegate: a `.HTTPMessageDelegate`\n\n        Returns a `.Future` that resolves to a bool after the full response has\n        been read. The result is true if the stream is still open.\n        \"\"\"\n        if self.params.decompress:\n            delegate = _GzipMessageDelegate(delegate, self.params.chunk_size)\n        return self._read_message(delegate)\n\n    async def _read_message(self, delegate: httputil.HTTPMessageDelegate) -> bool:\n        need_delegate_close = False\n        try:\n            header_future = self.stream.read_until_regex(\n                b\"\\r?\\n\\r?\\n\", max_bytes=self.params.max_header_size\n            )\n            if self.params.header_timeout is None:\n                header_data = await header_future\n            else:\n                try:\n                    header_data = await gen.with_timeout(\n                        self.stream.io_loop.time() + self.params.header_timeout,\n                        header_future,\n                        quiet_exceptions=iostream.StreamClosedError,\n                    )\n                except gen.TimeoutError:\n                    self.close()\n                    return False\n            start_line_str, headers = self._parse_headers(header_data)\n            if self.is_client:\n                resp_start_line = httputil.parse_response_start_line(start_line_str)\n                self._response_start_line = resp_start_line\n                start_line = (\n                    resp_start_line\n                )  # type: Union[httputil.RequestStartLine, httputil.ResponseStartLine]\n                # TODO: this will need to change to support client-side keepalive\n                self._disconnect_on_finish = False\n            else:\n                req_start_line = httputil.parse_request_start_line(start_line_str)\n                self._request_start_line = req_start_line\n                self._request_headers = headers\n                start_line = req_start_line\n                self._disconnect_on_finish = not self._can_keep_alive(\n                    req_start_line, headers\n                )\n            need_delegate_close = True\n            with _ExceptionLoggingContext(app_log):\n                header_recv_future = delegate.headers_received(start_line, headers)\n                if header_recv_future is not None:\n                    await header_recv_future\n            if self.stream is None:\n                # We've been detached.\n                need_delegate_close = False\n                return False\n            skip_body = False\n            if self.is_client:\n                assert isinstance(start_line, httputil.ResponseStartLine)\n                if (\n                    self._request_start_line is not None\n                    and self._request_start_line.method == \"HEAD\"\n                ):\n                    skip_body = True\n                code = start_line.code\n                if code == 304:\n                    # 304 responses may include the content-length header\n                    # but do not actually have a body.\n                    # http://tools.ietf.org/html/rfc7230#section-3.3\n                    skip_body = True\n                if code >= 100 and code < 200:\n                    # 1xx responses should never indicate the presence of\n                    # a body.\n                    if \"Content-Length\" in headers or \"Transfer-Encoding\" in headers:\n                        raise httputil.HTTPInputError(\n                            \"Response code %d cannot have body\" % code\n                        )\n                    # TODO: client delegates will get headers_received twice\n                    # in the case of a 100-continue.  Document or change?\n                    await self._read_message(delegate)\n            else:\n                if headers.get(\"Expect\") == \"100-continue\" and not self._write_finished:\n                    self.stream.write(b\"HTTP/1.1 100 (Continue)\\r\\n\\r\\n\")\n            if not skip_body:\n                body_future = self._read_body(\n                    resp_start_line.code if self.is_client else 0, headers, delegate\n                )\n                if body_future is not None:\n                    if self._body_timeout is None:\n                        await body_future\n                    else:\n                        try:\n                            await gen.with_timeout(\n                                self.stream.io_loop.time() + self._body_timeout,\n                                body_future,\n                                quiet_exceptions=iostream.StreamClosedError,\n                            )\n                        except gen.TimeoutError:\n                            gen_log.info(\"Timeout reading body from %s\", self.context)\n                            self.stream.close()\n                            return False\n            self._read_finished = True\n            if not self._write_finished or self.is_client:\n                need_delegate_close = False\n                with _ExceptionLoggingContext(app_log):\n                    delegate.finish()\n            # If we're waiting for the application to produce an asynchronous\n            # response, and we're not detached, register a close callback\n            # on the stream (we didn't need one while we were reading)\n            if (\n                not self._finish_future.done()\n                and self.stream is not None\n                and not self.stream.closed()\n            ):\n                self.stream.set_close_callback(self._on_connection_close)\n                await self._finish_future\n            if self.is_client and self._disconnect_on_finish:\n                self.close()\n            if self.stream is None:\n                return False\n        except httputil.HTTPInputError as e:\n            gen_log.info(\"Malformed HTTP message from %s: %s\", self.context, e)\n            if not self.is_client:\n                await self.stream.write(b\"HTTP/1.1 400 Bad Request\\r\\n\\r\\n\")\n            self.close()\n            return False\n        finally:\n            if need_delegate_close:\n                with _ExceptionLoggingContext(app_log):\n                    delegate.on_connection_close()\n            header_future = None  # type: ignore\n            self._clear_callbacks()\n        return True\n\n    def _clear_callbacks(self) -> None:\n        \"\"\"Clears the callback attributes.\n\n        This allows the request handler to be garbage collected more\n        quickly in CPython by breaking up reference cycles.\n        \"\"\"\n        self._write_callback = None\n        self._write_future = None  # type: Optional[Future[None]]\n        self._close_callback = None  # type: Optional[Callable[[], None]]\n        if self.stream is not None:\n            self.stream.set_close_callback(None)\n\n    def set_close_callback(self, callback: Optional[Callable[[], None]]) -> None:\n        \"\"\"Sets a callback that will be run when the connection is closed.\n\n        Note that this callback is slightly different from\n        `.HTTPMessageDelegate.on_connection_close`: The\n        `.HTTPMessageDelegate` method is called when the connection is\n        closed while recieving a message. This callback is used when\n        there is not an active delegate (for example, on the server\n        side this callback is used if the client closes the connection\n        after sending its request but before receiving all the\n        response.\n        \"\"\"\n        self._close_callback = callback\n\n    def _on_connection_close(self) -> None:\n        # Note that this callback is only registered on the IOStream\n        # when we have finished reading the request and are waiting for\n        # the application to produce its response.\n        if self._close_callback is not None:\n            callback = self._close_callback\n            self._close_callback = None\n            callback()\n        if not self._finish_future.done():\n            future_set_result_unless_cancelled(self._finish_future, None)\n        self._clear_callbacks()\n\n    def close(self) -> None:\n        if self.stream is not None:\n            self.stream.close()\n        self._clear_callbacks()\n        if not self._finish_future.done():\n            future_set_result_unless_cancelled(self._finish_future, None)\n\n    def detach(self) -> iostream.IOStream:\n        \"\"\"Take control of the underlying stream.\n\n        Returns the underlying `.IOStream` object and stops all further\n        HTTP processing.  May only be called during\n        `.HTTPMessageDelegate.headers_received`.  Intended for implementing\n        protocols like websockets that tunnel over an HTTP handshake.\n        \"\"\"\n        self._clear_callbacks()\n        stream = self.stream\n        self.stream = None  # type: ignore\n        if not self._finish_future.done():\n            future_set_result_unless_cancelled(self._finish_future, None)\n        return stream\n\n    def set_body_timeout(self, timeout: float) -> None:\n        \"\"\"Sets the body timeout for a single request.\n\n        Overrides the value from `.HTTP1ConnectionParameters`.\n        \"\"\"\n        self._body_timeout = timeout\n\n    def set_max_body_size(self, max_body_size: int) -> None:\n        \"\"\"Sets the body size limit for a single request.\n\n        Overrides the value from `.HTTP1ConnectionParameters`.\n        \"\"\"\n        self._max_body_size = max_body_size\n\n    def write_headers(\n        self,\n        start_line: Union[httputil.RequestStartLine, httputil.ResponseStartLine],\n        headers: httputil.HTTPHeaders,\n        chunk: bytes = None,\n    ) -> \"Future[None]\":\n        \"\"\"Implements `.HTTPConnection.write_headers`.\"\"\"\n        lines = []\n        if self.is_client:\n            assert isinstance(start_line, httputil.RequestStartLine)\n            self._request_start_line = start_line\n            lines.append(utf8(\"%s %s HTTP/1.1\" % (start_line[0], start_line[1])))\n            # Client requests with a non-empty body must have either a\n            # Content-Length or a Transfer-Encoding.\n            self._chunking_output = (\n                start_line.method in (\"POST\", \"PUT\", \"PATCH\")\n                and \"Content-Length\" not in headers\n                and \"Transfer-Encoding\" not in headers\n            )\n        else:\n            assert isinstance(start_line, httputil.ResponseStartLine)\n            assert self._request_start_line is not None\n            assert self._request_headers is not None\n            self._response_start_line = start_line\n            lines.append(utf8(\"HTTP/1.1 %d %s\" % (start_line[1], start_line[2])))\n            self._chunking_output = (\n                # TODO: should this use\n                # self._request_start_line.version or\n                # start_line.version?\n                self._request_start_line.version == \"HTTP/1.1\"\n                # 1xx, 204 and 304 responses have no body (not even a zero-length\n                # body), and so should not have either Content-Length or\n                # Transfer-Encoding headers.\n                and start_line.code not in (204, 304)\n                and (start_line.code < 100 or start_line.code >= 200)\n                # No need to chunk the output if a Content-Length is specified.\n                and \"Content-Length\" not in headers\n                # Applications are discouraged from touching Transfer-Encoding,\n                # but if they do, leave it alone.\n                and \"Transfer-Encoding\" not in headers\n            )\n            # If connection to a 1.1 client will be closed, inform client\n            if (\n                self._request_start_line.version == \"HTTP/1.1\"\n                and self._disconnect_on_finish\n            ):\n                headers[\"Connection\"] = \"close\"\n            # If a 1.0 client asked for keep-alive, add the header.\n            if (\n                self._request_start_line.version == \"HTTP/1.0\"\n                and self._request_headers.get(\"Connection\", \"\").lower() == \"keep-alive\"\n            ):\n                headers[\"Connection\"] = \"Keep-Alive\"\n        if self._chunking_output:\n            headers[\"Transfer-Encoding\"] = \"chunked\"\n        if not self.is_client and (\n            self._request_start_line.method == \"HEAD\"\n            or cast(httputil.ResponseStartLine, start_line).code == 304\n        ):\n            self._expected_content_remaining = 0\n        elif \"Content-Length\" in headers:\n            self._expected_content_remaining = int(headers[\"Content-Length\"])\n        else:\n            self._expected_content_remaining = None\n        # TODO: headers are supposed to be of type str, but we still have some\n        # cases that let bytes slip through. Remove these native_str calls when those\n        # are fixed.\n        header_lines = (\n            native_str(n) + \": \" + native_str(v) for n, v in headers.get_all()\n        )\n        lines.extend(l.encode(\"latin1\") for l in header_lines)\n        for line in lines:\n            if b\"\\n\" in line:\n                raise ValueError(\"Newline in header: \" + repr(line))\n        future = None\n        if self.stream.closed():\n            future = self._write_future = Future()\n            future.set_exception(iostream.StreamClosedError())\n            future.exception()\n        else:\n            future = self._write_future = Future()\n            data = b\"\\r\\n\".join(lines) + b\"\\r\\n\\r\\n\"\n            if chunk:\n                data += self._format_chunk(chunk)\n            self._pending_write = self.stream.write(data)\n            future_add_done_callback(self._pending_write, self._on_write_complete)\n        return future\n\n    def _format_chunk(self, chunk: bytes) -> bytes:\n        if self._expected_content_remaining is not None:\n            self._expected_content_remaining -= len(chunk)\n            if self._expected_content_remaining < 0:\n                # Close the stream now to stop further framing errors.\n                self.stream.close()\n                raise httputil.HTTPOutputError(\n                    \"Tried to write more data than Content-Length\"\n                )\n        if self._chunking_output and chunk:\n            # Don't write out empty chunks because that means END-OF-STREAM\n            # with chunked encoding\n            return utf8(\"%x\" % len(chunk)) + b\"\\r\\n\" + chunk + b\"\\r\\n\"\n        else:\n            return chunk\n\n    def write(self, chunk: bytes) -> \"Future[None]\":\n        \"\"\"Implements `.HTTPConnection.write`.\n\n        For backwards compatibility it is allowed but deprecated to\n        skip `write_headers` and instead call `write()` with a\n        pre-encoded header block.\n        \"\"\"\n        future = None\n        if self.stream.closed():\n            future = self._write_future = Future()\n            self._write_future.set_exception(iostream.StreamClosedError())\n            self._write_future.exception()\n        else:\n            future = self._write_future = Future()\n            self._pending_write = self.stream.write(self._format_chunk(chunk))\n            self._pending_write.add_done_callback(self._on_write_complete)\n        return future\n\n    def finish(self) -> None:\n        \"\"\"Implements `.HTTPConnection.finish`.\"\"\"\n        if (\n            self._expected_content_remaining is not None\n            and self._expected_content_remaining != 0\n            and not self.stream.closed()\n        ):\n            self.stream.close()\n            raise httputil.HTTPOutputError(\n                \"Tried to write %d bytes less than Content-Length\"\n                % self._expected_content_remaining\n            )\n        if self._chunking_output:\n            if not self.stream.closed():\n                self._pending_write = self.stream.write(b\"0\\r\\n\\r\\n\")\n                self._pending_write.add_done_callback(self._on_write_complete)\n        self._write_finished = True\n        # If the app finished the request while we're still reading,\n        # divert any remaining data away from the delegate and\n        # close the connection when we're done sending our response.\n        # Closing the connection is the only way to avoid reading the\n        # whole input body.\n        if not self._read_finished:\n            self._disconnect_on_finish = True\n        # No more data is coming, so instruct TCP to send any remaining\n        # data immediately instead of waiting for a full packet or ack.\n        self.stream.set_nodelay(True)\n        if self._pending_write is None:\n            self._finish_request(None)\n        else:\n            future_add_done_callback(self._pending_write, self._finish_request)\n\n    def _on_write_complete(self, future: \"Future[None]\") -> None:\n        exc = future.exception()\n        if exc is not None and not isinstance(exc, iostream.StreamClosedError):\n            future.result()\n        if self._write_callback is not None:\n            callback = self._write_callback\n            self._write_callback = None\n            self.stream.io_loop.add_callback(callback)\n        if self._write_future is not None:\n            future = self._write_future\n            self._write_future = None\n            future_set_result_unless_cancelled(future, None)\n\n    def _can_keep_alive(\n        self, start_line: httputil.RequestStartLine, headers: httputil.HTTPHeaders\n    ) -> bool:\n        if self.params.no_keep_alive:\n            return False\n        connection_header = headers.get(\"Connection\")\n        if connection_header is not None:\n            connection_header = connection_header.lower()\n        if start_line.version == \"HTTP/1.1\":\n            return connection_header != \"close\"\n        elif (\n            \"Content-Length\" in headers\n            or headers.get(\"Transfer-Encoding\", \"\").lower() == \"chunked\"\n            or getattr(start_line, \"method\", None) in (\"HEAD\", \"GET\")\n        ):\n            # start_line may be a request or response start line; only\n            # the former has a method attribute.\n            return connection_header == \"keep-alive\"\n        return False\n\n    def _finish_request(self, future: Optional[\"Future[None]\"]) -> None:\n        self._clear_callbacks()\n        if not self.is_client and self._disconnect_on_finish:\n            self.close()\n            return\n        # Turn Nagle's algorithm back on, leaving the stream in its\n        # default state for the next request.\n        self.stream.set_nodelay(False)\n        if not self._finish_future.done():\n            future_set_result_unless_cancelled(self._finish_future, None)\n\n    def _parse_headers(self, data: bytes) -> Tuple[str, httputil.HTTPHeaders]:\n        # The lstrip removes newlines that some implementations sometimes\n        # insert between messages of a reused connection.  Per RFC 7230,\n        # we SHOULD ignore at least one empty line before the request.\n        # http://tools.ietf.org/html/rfc7230#section-3.5\n        data_str = native_str(data.decode(\"latin1\")).lstrip(\"\\r\\n\")\n        # RFC 7230 section allows for both CRLF and bare LF.\n        eol = data_str.find(\"\\n\")\n        start_line = data_str[:eol].rstrip(\"\\r\")\n        headers = httputil.HTTPHeaders.parse(data_str[eol:])\n        return start_line, headers\n\n    def _read_body(\n        self,\n        code: int,\n        headers: httputil.HTTPHeaders,\n        delegate: httputil.HTTPMessageDelegate,\n    ) -> Optional[Awaitable[None]]:\n        if \"Content-Length\" in headers:\n            if \"Transfer-Encoding\" in headers:\n                # Response cannot contain both Content-Length and\n                # Transfer-Encoding headers.\n                # http://tools.ietf.org/html/rfc7230#section-3.3.3\n                raise httputil.HTTPInputError(\n                    \"Response with both Transfer-Encoding and Content-Length\"\n                )\n            if \",\" in headers[\"Content-Length\"]:\n                # Proxies sometimes cause Content-Length headers to get\n                # duplicated.  If all the values are identical then we can\n                # use them but if they differ it's an error.\n                pieces = re.split(r\",\\s*\", headers[\"Content-Length\"])\n                if any(i != pieces[0] for i in pieces):\n                    raise httputil.HTTPInputError(\n                        \"Multiple unequal Content-Lengths: %r\"\n                        % headers[\"Content-Length\"]\n                    )\n                headers[\"Content-Length\"] = pieces[0]\n\n            try:\n                content_length = int(headers[\"Content-Length\"])  # type: Optional[int]\n            except ValueError:\n                # Handles non-integer Content-Length value.\n                raise httputil.HTTPInputError(\n                    \"Only integer Content-Length is allowed: %s\"\n                    % headers[\"Content-Length\"]\n                )\n\n            if cast(int, content_length) > self._max_body_size:\n                raise httputil.HTTPInputError(\"Content-Length too long\")\n        else:\n            content_length = None\n\n        if code == 204:\n            # This response code is not allowed to have a non-empty body,\n            # and has an implicit length of zero instead of read-until-close.\n            # http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.3\n            if \"Transfer-Encoding\" in headers or content_length not in (None, 0):\n                raise httputil.HTTPInputError(\n                    \"Response with code %d should not have body\" % code\n                )\n            content_length = 0\n\n        if content_length is not None:\n            return self._read_fixed_body(content_length, delegate)\n        if headers.get(\"Transfer-Encoding\", \"\").lower() == \"chunked\":\n            return self._read_chunked_body(delegate)\n        if self.is_client:\n            return self._read_body_until_close(delegate)\n        return None\n\n    async def _read_fixed_body(\n        self, content_length: int, delegate: httputil.HTTPMessageDelegate\n    ) -> None:\n        while content_length > 0:\n            body = await self.stream.read_bytes(\n                min(self.params.chunk_size, content_length), partial=True\n            )\n            content_length -= len(body)\n            if not self._write_finished or self.is_client:\n                with _ExceptionLoggingContext(app_log):\n                    ret = delegate.data_received(body)\n                    if ret is not None:\n                        await ret\n\n    async def _read_chunked_body(self, delegate: httputil.HTTPMessageDelegate) -> None:\n        # TODO: \"chunk extensions\" http://tools.ietf.org/html/rfc2616#section-3.6.1\n        total_size = 0\n        while True:\n            chunk_len_str = await self.stream.read_until(b\"\\r\\n\", max_bytes=64)\n            chunk_len = int(chunk_len_str.strip(), 16)\n            if chunk_len == 0:\n                crlf = await self.stream.read_bytes(2)\n                if crlf != b\"\\r\\n\":\n                    raise httputil.HTTPInputError(\n                        \"improperly terminated chunked request\"\n                    )\n                return\n            total_size += chunk_len\n            if total_size > self._max_body_size:\n                raise httputil.HTTPInputError(\"chunked body too large\")\n            bytes_to_read = chunk_len\n            while bytes_to_read:\n                chunk = await self.stream.read_bytes(\n                    min(bytes_to_read, self.params.chunk_size), partial=True\n                )\n                bytes_to_read -= len(chunk)\n                if not self._write_finished or self.is_client:\n                    with _ExceptionLoggingContext(app_log):\n                        ret = delegate.data_received(chunk)\n                        if ret is not None:\n                            await ret\n            # chunk ends with \\r\\n\n            crlf = await self.stream.read_bytes(2)\n            assert crlf == b\"\\r\\n\"\n\n    async def _read_body_until_close(\n        self, delegate: httputil.HTTPMessageDelegate\n    ) -> None:\n        body = await self.stream.read_until_close()\n        if not self._write_finished or self.is_client:\n            with _ExceptionLoggingContext(app_log):\n                ret = delegate.data_received(body)\n                if ret is not None:\n                    await ret",
                                "ast_type_str": "ClassDef",
                                "start_lineno": 105,
                                "end_lineno": 697,
                                "parent": null
                            }
                        },
                        {
                            "file_name": "tornado/http1connection.py",
                            "type": "class",
                            "name": "HTTP1Connection",
                            "content": "class HTTP1Connection(httputil.HTTPConnection):\n    \"\"\"Implements the HTTP/1.x protocol.\n\n    This class can be on its own for clients, or via `HTTP1ServerConnection`\n    for servers.\n    \"\"\"\n\n    def __init__(\n        self,\n        stream: iostream.IOStream,\n        is_client: bool,\n        params: HTTP1ConnectionParameters = None,\n        context: object = None,\n    ) -> None:\n        \"\"\"\n        :arg stream: an `.IOStream`\n        :arg bool is_client: client or server\n        :arg params: a `.HTTP1ConnectionParameters` instance or ``None``\n        :arg context: an opaque application-defined object that can be accessed\n            as ``connection.context``.\n        \"\"\"\n        self.is_client = is_client\n        self.stream = stream\n        if params is None:\n            params = HTTP1ConnectionParameters()\n        self.params = params\n        self.context = context\n        self.no_keep_alive = params.no_keep_alive\n        # The body limits can be altered by the delegate, so save them\n        # here instead of just referencing self.params later.\n        self._max_body_size = self.params.max_body_size or self.stream.max_buffer_size\n        self._body_timeout = self.params.body_timeout\n        # _write_finished is set to True when finish() has been called,\n        # i.e. there will be no more data sent.  Data may still be in the\n        # stream's write buffer.\n        self._write_finished = False\n        # True when we have read the entire incoming body.\n        self._read_finished = False\n        # _finish_future resolves when all data has been written and flushed\n        # to the IOStream.\n        self._finish_future = Future()  # type: Future[None]\n        # If true, the connection should be closed after this request\n        # (after the response has been written in the server side,\n        # and after it has been read in the client)\n        self._disconnect_on_finish = False\n        self._clear_callbacks()\n        # Save the start lines after we read or write them; they\n        # affect later processing (e.g. 304 responses and HEAD methods\n        # have content-length but no bodies)\n        self._request_start_line = None  # type: Optional[httputil.RequestStartLine]\n        self._response_start_line = None  # type: Optional[httputil.ResponseStartLine]\n        self._request_headers = None  # type: Optional[httputil.HTTPHeaders]\n        # True if we are writing output with chunked encoding.\n        self._chunking_output = False\n        # While reading a body with a content-length, this is the\n        # amount left to read.\n        self._expected_content_remaining = None  # type: Optional[int]\n        # A Future for our outgoing writes, returned by IOStream.write.\n        self._pending_write = None  # type: Optional[Future[None]]\n\n    def read_response(self, delegate: httputil.HTTPMessageDelegate) -> Awaitable[bool]:\n        \"\"\"Read a single HTTP response.\n\n        Typical client-mode usage is to write a request using `write_headers`,\n        `write`, and `finish`, and then call ``read_response``.\n\n        :arg delegate: a `.HTTPMessageDelegate`\n\n        Returns a `.Future` that resolves to a bool after the full response has\n        been read. The result is true if the stream is still open.\n        \"\"\"\n        if self.params.decompress:\n            delegate = _GzipMessageDelegate(delegate, self.params.chunk_size)\n        return self._read_message(delegate)\n\n    async def _read_message(self, delegate: httputil.HTTPMessageDelegate) -> bool:\n        need_delegate_close = False\n        try:\n            header_future = self.stream.read_until_regex(\n                b\"\\r?\\n\\r?\\n\", max_bytes=self.params.max_header_size\n            )\n            if self.params.header_timeout is None:\n                header_data = await header_future\n            else:\n                try:\n                    header_data = await gen.with_timeout(\n                        self.stream.io_loop.time() + self.params.header_timeout,\n                        header_future,\n                        quiet_exceptions=iostream.StreamClosedError,\n                    )\n                except gen.TimeoutError:\n                    self.close()\n                    return False\n            start_line_str, headers = self._parse_headers(header_data)\n            if self.is_client:\n                resp_start_line = httputil.parse_response_start_line(start_line_str)\n                self._response_start_line = resp_start_line\n                start_line = (\n                    resp_start_line\n                )  # type: Union[httputil.RequestStartLine, httputil.ResponseStartLine]\n                # TODO: this will need to change to support client-side keepalive\n                self._disconnect_on_finish = False\n            else:\n                req_start_line = httputil.parse_request_start_line(start_line_str)\n                self._request_start_line = req_start_line\n                self._request_headers = headers\n                start_line = req_start_line\n                self._disconnect_on_finish = not self._can_keep_alive(\n                    req_start_line, headers\n                )\n            need_delegate_close = True\n            with _ExceptionLoggingContext(app_log):\n                header_recv_future = delegate.headers_received(start_line, headers)\n                if header_recv_future is not None:\n                    await header_recv_future\n            if self.stream is None:\n                # We've been detached.\n                need_delegate_close = False\n                return False\n            skip_body = False\n            if self.is_client:\n                assert isinstance(start_line, httputil.ResponseStartLine)\n                if (\n                    self._request_start_line is not None\n                    and self._request_start_line.method == \"HEAD\"\n                ):\n                    skip_body = True\n                code = start_line.code\n                if code == 304:\n                    # 304 responses may include the content-length header\n                    # but do not actually have a body.\n                    # http://tools.ietf.org/html/rfc7230#section-3.3\n                    skip_body = True\n                if code >= 100 and code < 200:\n                    # 1xx responses should never indicate the presence of\n                    # a body.\n                    if \"Content-Length\" in headers or \"Transfer-Encoding\" in headers:\n                        raise httputil.HTTPInputError(\n                            \"Response code %d cannot have body\" % code\n                        )\n                    # TODO: client delegates will get headers_received twice\n                    # in the case of a 100-continue.  Document or change?\n                    await self._read_message(delegate)\n            else:\n                if headers.get(\"Expect\") == \"100-continue\" and not self._write_finished:\n                    self.stream.write(b\"HTTP/1.1 100 (Continue)\\r\\n\\r\\n\")\n            if not skip_body:\n                body_future = self._read_body(\n                    resp_start_line.code if self.is_client else 0, headers, delegate\n                )\n                if body_future is not None:\n                    if self._body_timeout is None:\n                        await body_future\n                    else:\n                        try:\n                            await gen.with_timeout(\n                                self.stream.io_loop.time() + self._body_timeout,\n                                body_future,\n                                quiet_exceptions=iostream.StreamClosedError,\n                            )\n                        except gen.TimeoutError:\n                            gen_log.info(\"Timeout reading body from %s\", self.context)\n                            self.stream.close()\n                            return False\n            self._read_finished = True\n            if not self._write_finished or self.is_client:\n                need_delegate_close = False\n                with _ExceptionLoggingContext(app_log):\n                    delegate.finish()\n            # If we're waiting for the application to produce an asynchronous\n            # response, and we're not detached, register a close callback\n            # on the stream (we didn't need one while we were reading)\n            if (\n                not self._finish_future.done()\n                and self.stream is not None\n                and not self.stream.closed()\n            ):\n                self.stream.set_close_callback(self._on_connection_close)\n                await self._finish_future\n            if self.is_client and self._disconnect_on_finish:\n                self.close()\n            if self.stream is None:\n                return False\n        except httputil.HTTPInputError as e:\n            gen_log.info(\"Malformed HTTP message from %s: %s\", self.context, e)\n            if not self.is_client:\n                await self.stream.write(b\"HTTP/1.1 400 Bad Request\\r\\n\\r\\n\")\n            self.close()\n            return False\n        finally:\n            if need_delegate_close:\n                with _ExceptionLoggingContext(app_log):\n                    delegate.on_connection_close()\n            header_future = None  # type: ignore\n            self._clear_callbacks()\n        return True\n\n    def _clear_callbacks(self) -> None:\n        \"\"\"Clears the callback attributes.\n\n        This allows the request handler to be garbage collected more\n        quickly in CPython by breaking up reference cycles.\n        \"\"\"\n        self._write_callback = None\n        self._write_future = None  # type: Optional[Future[None]]\n        self._close_callback = None  # type: Optional[Callable[[], None]]\n        if self.stream is not None:\n            self.stream.set_close_callback(None)\n\n    def set_close_callback(self, callback: Optional[Callable[[], None]]) -> None:\n        \"\"\"Sets a callback that will be run when the connection is closed.\n\n        Note that this callback is slightly different from\n        `.HTTPMessageDelegate.on_connection_close`: The\n        `.HTTPMessageDelegate` method is called when the connection is\n        closed while recieving a message. This callback is used when\n        there is not an active delegate (for example, on the server\n        side this callback is used if the client closes the connection\n        after sending its request but before receiving all the\n        response.\n        \"\"\"\n        self._close_callback = callback\n\n    def _on_connection_close(self) -> None:\n        # Note that this callback is only registered on the IOStream\n        # when we have finished reading the request and are waiting for\n        # the application to produce its response.\n        if self._close_callback is not None:\n            callback = self._close_callback\n            self._close_callback = None\n            callback()\n        if not self._finish_future.done():\n            future_set_result_unless_cancelled(self._finish_future, None)\n        self._clear_callbacks()\n\n    def close(self) -> None:\n        if self.stream is not None:\n            self.stream.close()\n        self._clear_callbacks()\n        if not self._finish_future.done():\n            future_set_result_unless_cancelled(self._finish_future, None)\n\n    def detach(self) -> iostream.IOStream:\n        \"\"\"Take control of the underlying stream.\n\n        Returns the underlying `.IOStream` object and stops all further\n        HTTP processing.  May only be called during\n        `.HTTPMessageDelegate.headers_received`.  Intended for implementing\n        protocols like websockets that tunnel over an HTTP handshake.\n        \"\"\"\n        self._clear_callbacks()\n        stream = self.stream\n        self.stream = None  # type: ignore\n        if not self._finish_future.done():\n            future_set_result_unless_cancelled(self._finish_future, None)\n        return stream\n\n    def set_body_timeout(self, timeout: float) -> None:\n        \"\"\"Sets the body timeout for a single request.\n\n        Overrides the value from `.HTTP1ConnectionParameters`.\n        \"\"\"\n        self._body_timeout = timeout\n\n    def set_max_body_size(self, max_body_size: int) -> None:\n        \"\"\"Sets the body size limit for a single request.\n\n        Overrides the value from `.HTTP1ConnectionParameters`.\n        \"\"\"\n        self._max_body_size = max_body_size\n\n    def write_headers(\n        self,\n        start_line: Union[httputil.RequestStartLine, httputil.ResponseStartLine],\n        headers: httputil.HTTPHeaders,\n        chunk: bytes = None,\n    ) -> \"Future[None]\":\n        \"\"\"Implements `.HTTPConnection.write_headers`.\"\"\"\n        lines = []\n        if self.is_client:\n            assert isinstance(start_line, httputil.RequestStartLine)\n            self._request_start_line = start_line\n            lines.append(utf8(\"%s %s HTTP/1.1\" % (start_line[0], start_line[1])))\n            # Client requests with a non-empty body must have either a\n            # Content-Length or a Transfer-Encoding.\n            self._chunking_output = (\n                start_line.method in (\"POST\", \"PUT\", \"PATCH\")\n                and \"Content-Length\" not in headers\n                and \"Transfer-Encoding\" not in headers\n            )\n        else:\n            assert isinstance(start_line, httputil.ResponseStartLine)\n            assert self._request_start_line is not None\n            assert self._request_headers is not None\n            self._response_start_line = start_line\n            lines.append(utf8(\"HTTP/1.1 %d %s\" % (start_line[1], start_line[2])))\n            self._chunking_output = (\n                # TODO: should this use\n                # self._request_start_line.version or\n                # start_line.version?\n                self._request_start_line.version == \"HTTP/1.1\"\n                # 1xx, 204 and 304 responses have no body (not even a zero-length\n                # body), and so should not have either Content-Length or\n                # Transfer-Encoding headers.\n                and start_line.code not in (204, 304)\n                and (start_line.code < 100 or start_line.code >= 200)\n                # No need to chunk the output if a Content-Length is specified.\n                and \"Content-Length\" not in headers\n                # Applications are discouraged from touching Transfer-Encoding,\n                # but if they do, leave it alone.\n                and \"Transfer-Encoding\" not in headers\n            )\n            # If connection to a 1.1 client will be closed, inform client\n            if (\n                self._request_start_line.version == \"HTTP/1.1\"\n                and self._disconnect_on_finish\n            ):\n                headers[\"Connection\"] = \"close\"\n            # If a 1.0 client asked for keep-alive, add the header.\n            if (\n                self._request_start_line.version == \"HTTP/1.0\"\n                and self._request_headers.get(\"Connection\", \"\").lower() == \"keep-alive\"\n            ):\n                headers[\"Connection\"] = \"Keep-Alive\"\n        if self._chunking_output:\n            headers[\"Transfer-Encoding\"] = \"chunked\"\n        if not self.is_client and (\n            self._request_start_line.method == \"HEAD\"\n            or cast(httputil.ResponseStartLine, start_line).code == 304\n        ):\n            self._expected_content_remaining = 0\n        elif \"Content-Length\" in headers:\n            self._expected_content_remaining = int(headers[\"Content-Length\"])\n        else:\n            self._expected_content_remaining = None\n        # TODO: headers are supposed to be of type str, but we still have some\n        # cases that let bytes slip through. Remove these native_str calls when those\n        # are fixed.\n        header_lines = (\n            native_str(n) + \": \" + native_str(v) for n, v in headers.get_all()\n        )\n        lines.extend(l.encode(\"latin1\") for l in header_lines)\n        for line in lines:\n            if b\"\\n\" in line:\n                raise ValueError(\"Newline in header: \" + repr(line))\n        future = None\n        if self.stream.closed():\n            future = self._write_future = Future()\n            future.set_exception(iostream.StreamClosedError())\n            future.exception()\n        else:\n            future = self._write_future = Future()\n            data = b\"\\r\\n\".join(lines) + b\"\\r\\n\\r\\n\"\n            if chunk:\n                data += self._format_chunk(chunk)\n            self._pending_write = self.stream.write(data)\n            future_add_done_callback(self._pending_write, self._on_write_complete)\n        return future\n\n    def _format_chunk(self, chunk: bytes) -> bytes:\n        if self._expected_content_remaining is not None:\n            self._expected_content_remaining -= len(chunk)\n            if self._expected_content_remaining < 0:\n                # Close the stream now to stop further framing errors.\n                self.stream.close()\n                raise httputil.HTTPOutputError(\n                    \"Tried to write more data than Content-Length\"\n                )\n        if self._chunking_output and chunk:\n            # Don't write out empty chunks because that means END-OF-STREAM\n            # with chunked encoding\n            return utf8(\"%x\" % len(chunk)) + b\"\\r\\n\" + chunk + b\"\\r\\n\"\n        else:\n            return chunk\n\n    def write(self, chunk: bytes) -> \"Future[None]\":\n        \"\"\"Implements `.HTTPConnection.write`.\n\n        For backwards compatibility it is allowed but deprecated to\n        skip `write_headers` and instead call `write()` with a\n        pre-encoded header block.\n        \"\"\"\n        future = None\n        if self.stream.closed():\n            future = self._write_future = Future()\n            self._write_future.set_exception(iostream.StreamClosedError())\n            self._write_future.exception()\n        else:\n            future = self._write_future = Future()\n            self._pending_write = self.stream.write(self._format_chunk(chunk))\n            self._pending_write.add_done_callback(self._on_write_complete)\n        return future\n\n    def finish(self) -> None:\n        \"\"\"Implements `.HTTPConnection.finish`.\"\"\"\n        if (\n            self._expected_content_remaining is not None\n            and self._expected_content_remaining != 0\n            and not self.stream.closed()\n        ):\n            self.stream.close()\n            raise httputil.HTTPOutputError(\n                \"Tried to write %d bytes less than Content-Length\"\n                % self._expected_content_remaining\n            )\n        if self._chunking_output:\n            if not self.stream.closed():\n                self._pending_write = self.stream.write(b\"0\\r\\n\\r\\n\")\n                self._pending_write.add_done_callback(self._on_write_complete)\n        self._write_finished = True\n        # If the app finished the request while we're still reading,\n        # divert any remaining data away from the delegate and\n        # close the connection when we're done sending our response.\n        # Closing the connection is the only way to avoid reading the\n        # whole input body.\n        if not self._read_finished:\n            self._disconnect_on_finish = True\n        # No more data is coming, so instruct TCP to send any remaining\n        # data immediately instead of waiting for a full packet or ack.\n        self.stream.set_nodelay(True)\n        if self._pending_write is None:\n            self._finish_request(None)\n        else:\n            future_add_done_callback(self._pending_write, self._finish_request)\n\n    def _on_write_complete(self, future: \"Future[None]\") -> None:\n        exc = future.exception()\n        if exc is not None and not isinstance(exc, iostream.StreamClosedError):\n            future.result()\n        if self._write_callback is not None:\n            callback = self._write_callback\n            self._write_callback = None\n            self.stream.io_loop.add_callback(callback)\n        if self._write_future is not None:\n            future = self._write_future\n            self._write_future = None\n            future_set_result_unless_cancelled(future, None)\n\n    def _can_keep_alive(\n        self, start_line: httputil.RequestStartLine, headers: httputil.HTTPHeaders\n    ) -> bool:\n        if self.params.no_keep_alive:\n            return False\n        connection_header = headers.get(\"Connection\")\n        if connection_header is not None:\n            connection_header = connection_header.lower()\n        if start_line.version == \"HTTP/1.1\":\n            return connection_header != \"close\"\n        elif (\n            \"Content-Length\" in headers\n            or headers.get(\"Transfer-Encoding\", \"\").lower() == \"chunked\"\n            or getattr(start_line, \"method\", None) in (\"HEAD\", \"GET\")\n        ):\n            # start_line may be a request or response start line; only\n            # the former has a method attribute.\n            return connection_header == \"keep-alive\"\n        return False\n\n    def _finish_request(self, future: Optional[\"Future[None]\"]) -> None:\n        self._clear_callbacks()\n        if not self.is_client and self._disconnect_on_finish:\n            self.close()\n            return\n        # Turn Nagle's algorithm back on, leaving the stream in its\n        # default state for the next request.\n        self.stream.set_nodelay(False)\n        if not self._finish_future.done():\n            future_set_result_unless_cancelled(self._finish_future, None)\n\n    def _parse_headers(self, data: bytes) -> Tuple[str, httputil.HTTPHeaders]:\n        # The lstrip removes newlines that some implementations sometimes\n        # insert between messages of a reused connection.  Per RFC 7230,\n        # we SHOULD ignore at least one empty line before the request.\n        # http://tools.ietf.org/html/rfc7230#section-3.5\n        data_str = native_str(data.decode(\"latin1\")).lstrip(\"\\r\\n\")\n        # RFC 7230 section allows for both CRLF and bare LF.\n        eol = data_str.find(\"\\n\")\n        start_line = data_str[:eol].rstrip(\"\\r\")\n        headers = httputil.HTTPHeaders.parse(data_str[eol:])\n        return start_line, headers\n\n    def _read_body(\n        self,\n        code: int,\n        headers: httputil.HTTPHeaders,\n        delegate: httputil.HTTPMessageDelegate,\n    ) -> Optional[Awaitable[None]]:\n        if \"Content-Length\" in headers:\n            if \"Transfer-Encoding\" in headers:\n                # Response cannot contain both Content-Length and\n                # Transfer-Encoding headers.\n                # http://tools.ietf.org/html/rfc7230#section-3.3.3\n                raise httputil.HTTPInputError(\n                    \"Response with both Transfer-Encoding and Content-Length\"\n                )\n            if \",\" in headers[\"Content-Length\"]:\n                # Proxies sometimes cause Content-Length headers to get\n                # duplicated.  If all the values are identical then we can\n                # use them but if they differ it's an error.\n                pieces = re.split(r\",\\s*\", headers[\"Content-Length\"])\n                if any(i != pieces[0] for i in pieces):\n                    raise httputil.HTTPInputError(\n                        \"Multiple unequal Content-Lengths: %r\"\n                        % headers[\"Content-Length\"]\n                    )\n                headers[\"Content-Length\"] = pieces[0]\n\n            try:\n                content_length = int(headers[\"Content-Length\"])  # type: Optional[int]\n            except ValueError:\n                # Handles non-integer Content-Length value.\n                raise httputil.HTTPInputError(\n                    \"Only integer Content-Length is allowed: %s\"\n                    % headers[\"Content-Length\"]\n                )\n\n            if cast(int, content_length) > self._max_body_size:\n                raise httputil.HTTPInputError(\"Content-Length too long\")\n        else:\n            content_length = None\n\n        if code == 204:\n            # This response code is not allowed to have a non-empty body,\n            # and has an implicit length of zero instead of read-until-close.\n            # http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.3\n            if \"Transfer-Encoding\" in headers or content_length not in (None, 0):\n                raise httputil.HTTPInputError(\n                    \"Response with code %d should not have body\" % code\n                )\n            content_length = 0\n\n        if content_length is not None:\n            return self._read_fixed_body(content_length, delegate)\n        if headers.get(\"Transfer-Encoding\", \"\").lower() == \"chunked\":\n            return self._read_chunked_body(delegate)\n        if self.is_client:\n            return self._read_body_until_close(delegate)\n        return None\n\n    async def _read_fixed_body(\n        self, content_length: int, delegate: httputil.HTTPMessageDelegate\n    ) -> None:\n        while content_length > 0:\n            body = await self.stream.read_bytes(\n                min(self.params.chunk_size, content_length), partial=True\n            )\n            content_length -= len(body)\n            if not self._write_finished or self.is_client:\n                with _ExceptionLoggingContext(app_log):\n                    ret = delegate.data_received(body)\n                    if ret is not None:\n                        await ret\n\n    async def _read_chunked_body(self, delegate: httputil.HTTPMessageDelegate) -> None:\n        # TODO: \"chunk extensions\" http://tools.ietf.org/html/rfc2616#section-3.6.1\n        total_size = 0\n        while True:\n            chunk_len_str = await self.stream.read_until(b\"\\r\\n\", max_bytes=64)\n            chunk_len = int(chunk_len_str.strip(), 16)\n            if chunk_len == 0:\n                crlf = await self.stream.read_bytes(2)\n                if crlf != b\"\\r\\n\":\n                    raise httputil.HTTPInputError(\n                        \"improperly terminated chunked request\"\n                    )\n                return\n            total_size += chunk_len\n            if total_size > self._max_body_size:\n                raise httputil.HTTPInputError(\"chunked body too large\")\n            bytes_to_read = chunk_len\n            while bytes_to_read:\n                chunk = await self.stream.read_bytes(\n                    min(bytes_to_read, self.params.chunk_size), partial=True\n                )\n                bytes_to_read -= len(chunk)\n                if not self._write_finished or self.is_client:\n                    with _ExceptionLoggingContext(app_log):\n                        ret = delegate.data_received(chunk)\n                        if ret is not None:\n                            await ret\n            # chunk ends with \\r\\n\n            crlf = await self.stream.read_bytes(2)\n            assert crlf == b\"\\r\\n\"\n\n    async def _read_body_until_close(\n        self, delegate: httputil.HTTPMessageDelegate\n    ) -> None:\n        body = await self.stream.read_until_close()\n        if not self._write_finished or self.is_client:\n            with _ExceptionLoggingContext(app_log):\n                ret = delegate.data_received(body)\n                if ret is not None:\n                    await ret",
                            "ast_type_str": "ClassDef",
                            "start_lineno": 105,
                            "end_lineno": 697,
                            "parent": null
                        }
                    ],
                    "added_entities": [],
                    "deleted_entities": []
                }
            ]
        },
        {
            "old_file_content": "from tornado.concurrent import Future\nfrom tornado import gen\nfrom tornado.escape import (\n    json_decode,\n    utf8,\n    to_unicode,\n    recursive_unicode,\n    native_str,\n    to_basestring,\n)\nfrom tornado.httpclient import HTTPClientError\nfrom tornado.httputil import format_timestamp\nfrom tornado.iostream import IOStream\nfrom tornado import locale\nfrom tornado.locks import Event\nfrom tornado.log import app_log, gen_log\nfrom tornado.simple_httpclient import SimpleAsyncHTTPClient\nfrom tornado.template import DictLoader\nfrom tornado.testing import AsyncHTTPTestCase, AsyncTestCase, ExpectLog, gen_test\nfrom tornado.util import ObjectDict, unicode_type\nfrom tornado.web import (\n    Application,\n    RequestHandler,\n    StaticFileHandler,\n    RedirectHandler as WebRedirectHandler,\n    HTTPError,\n    MissingArgumentError,\n    ErrorHandler,\n    authenticated,\n    url,\n    _create_signature_v1,\n    create_signed_value,\n    decode_signed_value,\n    get_signature_key_version,\n    UIModule,\n    Finish,\n    stream_request_body,\n    removeslash,\n    addslash,\n    GZipContentEncoding,\n)\n\nimport binascii\nimport contextlib\nimport copy\nimport datetime\nimport email.utils\nimport gzip\nfrom io import BytesIO\nimport itertools\nimport logging\nimport os\nimport re\nimport socket\nimport typing  # noqa: F401\nimport unittest\nimport urllib.parse\n\n\ndef relpath(*a):\n    return os.path.join(os.path.dirname(__file__), *a)\n\n\nclass WebTestCase(AsyncHTTPTestCase):\n    \"\"\"Base class for web tests that also supports WSGI mode.\n\n    Override get_handlers and get_app_kwargs instead of get_app.\n    This class is deprecated since WSGI mode is no longer supported.\n    \"\"\"\n\n    def get_app(self):\n        self.app = Application(self.get_handlers(), **self.get_app_kwargs())\n        return self.app\n\n    def get_handlers(self):\n        raise NotImplementedError()\n\n    def get_app_kwargs(self):\n        return {}\n\n\nclass SimpleHandlerTestCase(WebTestCase):\n    \"\"\"Simplified base class for tests that work with a single handler class.\n\n    To use, define a nested class named ``Handler``.\n    \"\"\"\n\n    def get_handlers(self):\n        return [(\"/\", self.Handler)]\n\n\nclass HelloHandler(RequestHandler):\n    def get(self):\n        self.write(\"hello\")\n\n\nclass CookieTestRequestHandler(RequestHandler):\n    # stub out enough methods to make the secure_cookie functions work\n    def __init__(self, cookie_secret=\"0123456789\", key_version=None):\n        # don't call super.__init__\n        self._cookies = {}  # type: typing.Dict[str, bytes]\n        if key_version is None:\n            self.application = ObjectDict(settings=dict(cookie_secret=cookie_secret))\n        else:\n            self.application = ObjectDict(\n                settings=dict(cookie_secret=cookie_secret, key_version=key_version)\n            )\n\n    def get_cookie(self, name):\n        return self._cookies.get(name)\n\n    def set_cookie(self, name, value, expires_days=None):\n        self._cookies[name] = value\n\n\n# See SignedValueTest below for more.\nclass SecureCookieV1Test(unittest.TestCase):\n    def test_round_trip(self):\n        handler = CookieTestRequestHandler()\n        handler.set_secure_cookie(\"foo\", b\"bar\", version=1)\n        self.assertEqual(handler.get_secure_cookie(\"foo\", min_version=1), b\"bar\")\n\n    def test_cookie_tampering_future_timestamp(self):\n        handler = CookieTestRequestHandler()\n        # this string base64-encodes to '12345678'\n        handler.set_secure_cookie(\"foo\", binascii.a2b_hex(b\"d76df8e7aefc\"), version=1)\n        cookie = handler._cookies[\"foo\"]\n        match = re.match(br\"12345678\\|([0-9]+)\\|([0-9a-f]+)\", cookie)\n        assert match is not None\n        timestamp = match.group(1)\n        sig = match.group(2)\n        self.assertEqual(\n            _create_signature_v1(\n                handler.application.settings[\"cookie_secret\"],\n                \"foo\",\n                \"12345678\",\n                timestamp,\n            ),\n            sig,\n        )\n        # shifting digits from payload to timestamp doesn't alter signature\n        # (this is not desirable behavior, just confirming that that's how it\n        # works)\n        self.assertEqual(\n            _create_signature_v1(\n                handler.application.settings[\"cookie_secret\"],\n                \"foo\",\n                \"1234\",\n                b\"5678\" + timestamp,\n            ),\n            sig,\n        )\n        # tamper with the cookie\n        handler._cookies[\"foo\"] = utf8(\n            \"1234|5678%s|%s\" % (to_basestring(timestamp), to_basestring(sig))\n        )\n        # it gets rejected\n        with ExpectLog(gen_log, \"Cookie timestamp in future\"):\n            self.assertTrue(handler.get_secure_cookie(\"foo\", min_version=1) is None)\n\n    def test_arbitrary_bytes(self):\n        # Secure cookies accept arbitrary data (which is base64 encoded).\n        # Note that normal cookies accept only a subset of ascii.\n        handler = CookieTestRequestHandler()\n        handler.set_secure_cookie(\"foo\", b\"\\xe9\", version=1)\n        self.assertEqual(handler.get_secure_cookie(\"foo\", min_version=1), b\"\\xe9\")\n\n\n# See SignedValueTest below for more.\nclass SecureCookieV2Test(unittest.TestCase):\n    KEY_VERSIONS = {0: \"ajklasdf0ojaisdf\", 1: \"aslkjasaolwkjsdf\"}\n\n    def test_round_trip(self):\n        handler = CookieTestRequestHandler()\n        handler.set_secure_cookie(\"foo\", b\"bar\", version=2)\n        self.assertEqual(handler.get_secure_cookie(\"foo\", min_version=2), b\"bar\")\n\n    def test_key_version_roundtrip(self):\n        handler = CookieTestRequestHandler(\n            cookie_secret=self.KEY_VERSIONS, key_version=0\n        )\n        handler.set_secure_cookie(\"foo\", b\"bar\")\n        self.assertEqual(handler.get_secure_cookie(\"foo\"), b\"bar\")\n\n    def test_key_version_roundtrip_differing_version(self):\n        handler = CookieTestRequestHandler(\n            cookie_secret=self.KEY_VERSIONS, key_version=1\n        )\n        handler.set_secure_cookie(\"foo\", b\"bar\")\n        self.assertEqual(handler.get_secure_cookie(\"foo\"), b\"bar\")\n\n    def test_key_version_increment_version(self):\n        handler = CookieTestRequestHandler(\n            cookie_secret=self.KEY_VERSIONS, key_version=0\n        )\n        handler.set_secure_cookie(\"foo\", b\"bar\")\n        new_handler = CookieTestRequestHandler(\n            cookie_secret=self.KEY_VERSIONS, key_version=1\n        )\n        new_handler._cookies = handler._cookies\n        self.assertEqual(new_handler.get_secure_cookie(\"foo\"), b\"bar\")\n\n    def test_key_version_invalidate_version(self):\n        handler = CookieTestRequestHandler(\n            cookie_secret=self.KEY_VERSIONS, key_version=0\n        )\n        handler.set_secure_cookie(\"foo\", b\"bar\")\n        new_key_versions = self.KEY_VERSIONS.copy()\n        new_key_versions.pop(0)\n        new_handler = CookieTestRequestHandler(\n            cookie_secret=new_key_versions, key_version=1\n        )\n        new_handler._cookies = handler._cookies\n        self.assertEqual(new_handler.get_secure_cookie(\"foo\"), None)\n\n\nclass FinalReturnTest(WebTestCase):\n    def get_handlers(self):\n        test = self\n\n        class FinishHandler(RequestHandler):\n            @gen.coroutine\n            def get(self):\n                test.final_return = self.finish()\n                yield test.final_return\n\n        class RenderHandler(RequestHandler):\n            def create_template_loader(self, path):\n                return DictLoader({\"foo.html\": \"hi\"})\n\n            @gen.coroutine\n            def get(self):\n                test.final_return = self.render(\"foo.html\")\n\n        return [(\"/finish\", FinishHandler), (\"/render\", RenderHandler)]\n\n    def get_app_kwargs(self):\n        return dict(template_path=\"FinalReturnTest\")\n\n    def test_finish_method_return_future(self):\n        response = self.fetch(self.get_url(\"/finish\"))\n        self.assertEqual(response.code, 200)\n        self.assertIsInstance(self.final_return, Future)\n        self.assertTrue(self.final_return.done())\n\n    def test_render_method_return_future(self):\n        response = self.fetch(self.get_url(\"/render\"))\n        self.assertEqual(response.code, 200)\n        self.assertIsInstance(self.final_return, Future)\n\n\nclass CookieTest(WebTestCase):\n    def get_handlers(self):\n        class SetCookieHandler(RequestHandler):\n            def get(self):\n                # Try setting cookies with different argument types\n                # to ensure that everything gets encoded correctly\n                self.set_cookie(\"str\", \"asdf\")\n                self.set_cookie(\"unicode\", u\"qwer\")\n                self.set_cookie(\"bytes\", b\"zxcv\")\n\n        class GetCookieHandler(RequestHandler):\n            def get(self):\n                self.write(self.get_cookie(\"foo\", \"default\"))\n\n        class SetCookieDomainHandler(RequestHandler):\n            def get(self):\n                # unicode domain and path arguments shouldn't break things\n                # either (see bug #285)\n                self.set_cookie(\"unicode_args\", \"blah\", domain=u\"foo.com\", path=u\"/foo\")\n\n        class SetCookieSpecialCharHandler(RequestHandler):\n            def get(self):\n                self.set_cookie(\"equals\", \"a=b\")\n                self.set_cookie(\"semicolon\", \"a;b\")\n                self.set_cookie(\"quote\", 'a\"b')\n\n        class SetCookieOverwriteHandler(RequestHandler):\n            def get(self):\n                self.set_cookie(\"a\", \"b\", domain=\"example.com\")\n                self.set_cookie(\"c\", \"d\", domain=\"example.com\")\n                # A second call with the same name clobbers the first.\n                # Attributes from the first call are not carried over.\n                self.set_cookie(\"a\", \"e\")\n\n        class SetCookieMaxAgeHandler(RequestHandler):\n            def get(self):\n                self.set_cookie(\"foo\", \"bar\", max_age=10)\n\n        class SetCookieExpiresDaysHandler(RequestHandler):\n            def get(self):\n                self.set_cookie(\"foo\", \"bar\", expires_days=10)\n\n        class SetCookieFalsyFlags(RequestHandler):\n            def get(self):\n                self.set_cookie(\"a\", \"1\", secure=True)\n                self.set_cookie(\"b\", \"1\", secure=False)\n                self.set_cookie(\"c\", \"1\", httponly=True)\n                self.set_cookie(\"d\", \"1\", httponly=False)\n\n        return [\n            (\"/set\", SetCookieHandler),\n            (\"/get\", GetCookieHandler),\n            (\"/set_domain\", SetCookieDomainHandler),\n            (\"/special_char\", SetCookieSpecialCharHandler),\n            (\"/set_overwrite\", SetCookieOverwriteHandler),\n            (\"/set_max_age\", SetCookieMaxAgeHandler),\n            (\"/set_expires_days\", SetCookieExpiresDaysHandler),\n            (\"/set_falsy_flags\", SetCookieFalsyFlags),\n        ]\n\n    def test_set_cookie(self):\n        response = self.fetch(\"/set\")\n        self.assertEqual(\n            sorted(response.headers.get_list(\"Set-Cookie\")),\n            [\"bytes=zxcv; Path=/\", \"str=asdf; Path=/\", \"unicode=qwer; Path=/\"],\n        )\n\n    def test_get_cookie(self):\n        response = self.fetch(\"/get\", headers={\"Cookie\": \"foo=bar\"})\n        self.assertEqual(response.body, b\"bar\")\n\n        response = self.fetch(\"/get\", headers={\"Cookie\": 'foo=\"bar\"'})\n        self.assertEqual(response.body, b\"bar\")\n\n        response = self.fetch(\"/get\", headers={\"Cookie\": \"/=exception;\"})\n        self.assertEqual(response.body, b\"default\")\n\n    def test_set_cookie_domain(self):\n        response = self.fetch(\"/set_domain\")\n        self.assertEqual(\n            response.headers.get_list(\"Set-Cookie\"),\n            [\"unicode_args=blah; Domain=foo.com; Path=/foo\"],\n        )\n\n    def test_cookie_special_char(self):\n        response = self.fetch(\"/special_char\")\n        headers = sorted(response.headers.get_list(\"Set-Cookie\"))\n        self.assertEqual(len(headers), 3)\n        self.assertEqual(headers[0], 'equals=\"a=b\"; Path=/')\n        self.assertEqual(headers[1], 'quote=\"a\\\\\"b\"; Path=/')\n        # python 2.7 octal-escapes the semicolon; older versions leave it alone\n        self.assertTrue(\n            headers[2] in ('semicolon=\"a;b\"; Path=/', 'semicolon=\"a\\\\073b\"; Path=/'),\n            headers[2],\n        )\n\n        data = [\n            (\"foo=a=b\", \"a=b\"),\n            ('foo=\"a=b\"', \"a=b\"),\n            ('foo=\"a;b\"', '\"a'),  # even quoted, \";\" is a delimiter\n            (\"foo=a\\\\073b\", \"a\\\\073b\"),  # escapes only decoded in quotes\n            ('foo=\"a\\\\073b\"', \"a;b\"),\n            ('foo=\"a\\\\\"b\"', 'a\"b'),\n        ]\n        for header, expected in data:\n            logging.debug(\"trying %r\", header)\n            response = self.fetch(\"/get\", headers={\"Cookie\": header})\n            self.assertEqual(response.body, utf8(expected))\n\n    def test_set_cookie_overwrite(self):\n        response = self.fetch(\"/set_overwrite\")\n        headers = response.headers.get_list(\"Set-Cookie\")\n        self.assertEqual(\n            sorted(headers), [\"a=e; Path=/\", \"c=d; Domain=example.com; Path=/\"]\n        )\n\n    def test_set_cookie_max_age(self):\n        response = self.fetch(\"/set_max_age\")\n        headers = response.headers.get_list(\"Set-Cookie\")\n        self.assertEqual(sorted(headers), [\"foo=bar; Max-Age=10; Path=/\"])\n\n    def test_set_cookie_expires_days(self):\n        response = self.fetch(\"/set_expires_days\")\n        header = response.headers.get(\"Set-Cookie\")\n        match = re.match(\"foo=bar; expires=(?P<expires>.+); Path=/\", header)\n        assert match is not None\n\n        expires = datetime.datetime.utcnow() + datetime.timedelta(days=10)\n        parsed = email.utils.parsedate(match.groupdict()[\"expires\"])\n        assert parsed is not None\n        header_expires = datetime.datetime(*parsed[:6])\n        self.assertTrue(abs((expires - header_expires).total_seconds()) < 10)\n\n    def test_set_cookie_false_flags(self):\n        response = self.fetch(\"/set_falsy_flags\")\n        headers = sorted(response.headers.get_list(\"Set-Cookie\"))\n        # The secure and httponly headers are capitalized in py35 and\n        # lowercase in older versions.\n        self.assertEqual(headers[0].lower(), \"a=1; path=/; secure\")\n        self.assertEqual(headers[1].lower(), \"b=1; path=/\")\n        self.assertEqual(headers[2].lower(), \"c=1; httponly; path=/\")\n        self.assertEqual(headers[3].lower(), \"d=1; path=/\")\n\n\nclass AuthRedirectRequestHandler(RequestHandler):\n    def initialize(self, login_url):\n        self.login_url = login_url\n\n    def get_login_url(self):\n        return self.login_url\n\n    @authenticated\n    def get(self):\n        # we'll never actually get here because the test doesn't follow redirects\n        self.send_error(500)\n\n\nclass AuthRedirectTest(WebTestCase):\n    def get_handlers(self):\n        return [\n            (\"/relative\", AuthRedirectRequestHandler, dict(login_url=\"/login\")),\n            (\n                \"/absolute\",\n                AuthRedirectRequestHandler,\n                dict(login_url=\"http://example.com/login\"),\n            ),\n        ]\n\n    def test_relative_auth_redirect(self):\n        response = self.fetch(self.get_url(\"/relative\"), follow_redirects=False)\n        self.assertEqual(response.code, 302)\n        self.assertEqual(response.headers[\"Location\"], \"/login?next=%2Frelative\")\n\n    def test_absolute_auth_redirect(self):\n        response = self.fetch(self.get_url(\"/absolute\"), follow_redirects=False)\n        self.assertEqual(response.code, 302)\n        self.assertTrue(\n            re.match(\n                r\"http://example.com/login\\?next=http%3A%2F%2F127.0.0.1%3A[0-9]+%2Fabsolute\",\n                response.headers[\"Location\"],\n            ),\n            response.headers[\"Location\"],\n        )\n\n\nclass ConnectionCloseHandler(RequestHandler):\n    def initialize(self, test):\n        self.test = test\n\n    @gen.coroutine\n    def get(self):\n        self.test.on_handler_waiting()\n        yield self.test.cleanup_event.wait()\n\n    def on_connection_close(self):\n        self.test.on_connection_close()\n\n\nclass ConnectionCloseTest(WebTestCase):\n    def get_handlers(self):\n        self.cleanup_event = Event()\n        return [(\"/\", ConnectionCloseHandler, dict(test=self))]\n\n    def test_connection_close(self):\n        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)\n        s.connect((\"127.0.0.1\", self.get_http_port()))\n        self.stream = IOStream(s)\n        self.stream.write(b\"GET / HTTP/1.0\\r\\n\\r\\n\")\n        self.wait()\n        # Let the hanging coroutine clean up after itself\n        self.cleanup_event.set()\n        self.io_loop.run_sync(lambda: gen.sleep(0))\n\n    def on_handler_waiting(self):\n        logging.debug(\"handler waiting\")\n        self.stream.close()\n\n    def on_connection_close(self):\n        logging.debug(\"connection closed\")\n        self.stop()\n\n\nclass EchoHandler(RequestHandler):\n    def get(self, *path_args):\n        # Type checks: web.py interfaces convert argument values to\n        # unicode strings (by default, but see also decode_argument).\n        # In httpserver.py (i.e. self.request.arguments), they're left\n        # as bytes.  Keys are always native strings.\n        for key in self.request.arguments:\n            if type(key) != str:\n                raise Exception(\"incorrect type for key: %r\" % type(key))\n            for value in self.request.arguments[key]:\n                if type(value) != bytes:\n                    raise Exception(\"incorrect type for value: %r\" % type(value))\n            for value in self.get_arguments(key):\n                if type(value) != unicode_type:\n                    raise Exception(\"incorrect type for value: %r\" % type(value))\n        for arg in path_args:\n            if type(arg) != unicode_type:\n                raise Exception(\"incorrect type for path arg: %r\" % type(arg))\n        self.write(\n            dict(\n                path=self.request.path,\n                path_args=path_args,\n                args=recursive_unicode(self.request.arguments),\n            )\n        )\n\n\nclass RequestEncodingTest(WebTestCase):\n    def get_handlers(self):\n        return [(\"/group/(.*)\", EchoHandler), (\"/slashes/([^/]*)/([^/]*)\", EchoHandler)]\n\n    def fetch_json(self, path):\n        return json_decode(self.fetch(path).body)\n\n    def test_group_question_mark(self):\n        # Ensure that url-encoded question marks are handled properly\n        self.assertEqual(\n            self.fetch_json(\"/group/%3F\"),\n            dict(path=\"/group/%3F\", path_args=[\"?\"], args={}),\n        )\n        self.assertEqual(\n            self.fetch_json(\"/group/%3F?%3F=%3F\"),\n            dict(path=\"/group/%3F\", path_args=[\"?\"], args={\"?\": [\"?\"]}),\n        )\n\n    def test_group_encoding(self):\n        # Path components and query arguments should be decoded the same way\n        self.assertEqual(\n            self.fetch_json(\"/group/%C3%A9?arg=%C3%A9\"),\n            {\n                u\"path\": u\"/group/%C3%A9\",\n                u\"path_args\": [u\"\\u00e9\"],\n                u\"args\": {u\"arg\": [u\"\\u00e9\"]},\n            },\n        )\n\n    def test_slashes(self):\n        # Slashes may be escaped to appear as a single \"directory\" in the path,\n        # but they are then unescaped when passed to the get() method.\n        self.assertEqual(\n            self.fetch_json(\"/slashes/foo/bar\"),\n            dict(path=\"/slashes/foo/bar\", path_args=[\"foo\", \"bar\"], args={}),\n        )\n        self.assertEqual(\n            self.fetch_json(\"/slashes/a%2Fb/c%2Fd\"),\n            dict(path=\"/slashes/a%2Fb/c%2Fd\", path_args=[\"a/b\", \"c/d\"], args={}),\n        )\n\n    def test_error(self):\n        # Percent signs (encoded as %25) should not mess up printf-style\n        # messages in logs\n        with ExpectLog(gen_log, \".*Invalid unicode\"):\n            self.fetch(\"/group/?arg=%25%e9\")\n\n\nclass TypeCheckHandler(RequestHandler):\n    def prepare(self):\n        self.errors = {}  # type: typing.Dict[str, str]\n\n        self.check_type(\"status\", self.get_status(), int)\n\n        # get_argument is an exception from the general rule of using\n        # type str for non-body data mainly for historical reasons.\n        self.check_type(\"argument\", self.get_argument(\"foo\"), unicode_type)\n        self.check_type(\"cookie_key\", list(self.cookies.keys())[0], str)\n        self.check_type(\"cookie_value\", list(self.cookies.values())[0].value, str)\n\n        # Secure cookies return bytes because they can contain arbitrary\n        # data, but regular cookies are native strings.\n        if list(self.cookies.keys()) != [\"asdf\"]:\n            raise Exception(\n                \"unexpected values for cookie keys: %r\" % self.cookies.keys()\n            )\n        self.check_type(\"get_secure_cookie\", self.get_secure_cookie(\"asdf\"), bytes)\n        self.check_type(\"get_cookie\", self.get_cookie(\"asdf\"), str)\n\n        self.check_type(\"xsrf_token\", self.xsrf_token, bytes)\n        self.check_type(\"xsrf_form_html\", self.xsrf_form_html(), str)\n\n        self.check_type(\"reverse_url\", self.reverse_url(\"typecheck\", \"foo\"), str)\n\n        self.check_type(\"request_summary\", self._request_summary(), str)\n\n    def get(self, path_component):\n        # path_component uses type unicode instead of str for consistency\n        # with get_argument()\n        self.check_type(\"path_component\", path_component, unicode_type)\n        self.write(self.errors)\n\n    def post(self, path_component):\n        self.check_type(\"path_component\", path_component, unicode_type)\n        self.write(self.errors)\n\n    def check_type(self, name, obj, expected_type):\n        actual_type = type(obj)\n        if expected_type != actual_type:\n            self.errors[name] = \"expected %s, got %s\" % (expected_type, actual_type)\n\n\nclass DecodeArgHandler(RequestHandler):\n    def decode_argument(self, value, name=None):\n        if type(value) != bytes:\n            raise Exception(\"unexpected type for value: %r\" % type(value))\n        # use self.request.arguments directly to avoid recursion\n        if \"encoding\" in self.request.arguments:\n            return value.decode(to_unicode(self.request.arguments[\"encoding\"][0]))\n        else:\n            return value\n\n    def get(self, arg):\n        def describe(s):\n            if type(s) == bytes:\n                return [\"bytes\", native_str(binascii.b2a_hex(s))]\n            elif type(s) == unicode_type:\n                return [\"unicode\", s]\n            raise Exception(\"unknown type\")\n\n        self.write({\"path\": describe(arg), \"query\": describe(self.get_argument(\"foo\"))})\n\n\nclass LinkifyHandler(RequestHandler):\n    def get(self):\n        self.render(\"linkify.html\", message=\"http://example.com\")\n\n\nclass UIModuleResourceHandler(RequestHandler):\n    def get(self):\n        self.render(\"page.html\", entries=[1, 2])\n\n\nclass OptionalPathHandler(RequestHandler):\n    def get(self, path):\n        self.write({\"path\": path})\n\n\nclass MultiHeaderHandler(RequestHandler):\n    def get(self):\n        self.set_header(\"x-overwrite\", \"1\")\n        self.set_header(\"X-Overwrite\", 2)\n        self.add_header(\"x-multi\", 3)\n        self.add_header(\"X-Multi\", \"4\")\n\n\nclass RedirectHandler(RequestHandler):\n    def get(self):\n        if self.get_argument(\"permanent\", None) is not None:\n            self.redirect(\"/\", permanent=int(self.get_argument(\"permanent\")))\n        elif self.get_argument(\"status\", None) is not None:\n            self.redirect(\"/\", status=int(self.get_argument(\"status\")))\n        else:\n            raise Exception(\"didn't get permanent or status arguments\")\n\n\nclass EmptyFlushCallbackHandler(RequestHandler):\n    @gen.coroutine\n    def get(self):\n        # Ensure that the flush callback is run whether or not there\n        # was any output.  The gen.Task and direct yield forms are\n        # equivalent.\n        yield self.flush()  # \"empty\" flush, but writes headers\n        yield self.flush()  # empty flush\n        self.write(\"o\")\n        yield self.flush()  # flushes the \"o\"\n        yield self.flush()  # empty flush\n        self.finish(\"k\")\n\n\nclass HeaderInjectionHandler(RequestHandler):\n    def get(self):\n        try:\n            self.set_header(\"X-Foo\", \"foo\\r\\nX-Bar: baz\")\n            raise Exception(\"Didn't get expected exception\")\n        except ValueError as e:\n            if \"Unsafe header value\" in str(e):\n                self.finish(b\"ok\")\n            else:\n                raise\n\n\nclass GetArgumentHandler(RequestHandler):\n    def prepare(self):\n        if self.get_argument(\"source\", None) == \"query\":\n            method = self.get_query_argument\n        elif self.get_argument(\"source\", None) == \"body\":\n            method = self.get_body_argument\n        else:\n            method = self.get_argument\n        self.finish(method(\"foo\", \"default\"))\n\n\nclass GetArgumentsHandler(RequestHandler):\n    def prepare(self):\n        self.finish(\n            dict(\n                default=self.get_arguments(\"foo\"),\n                query=self.get_query_arguments(\"foo\"),\n                body=self.get_body_arguments(\"foo\"),\n            )\n        )\n\n\n# This test was shared with wsgi_test.py; now the name is meaningless.\nclass WSGISafeWebTest(WebTestCase):\n    COOKIE_SECRET = \"WebTest.COOKIE_SECRET\"\n\n    def get_app_kwargs(self):\n        loader = DictLoader(\n            {\n                \"linkify.html\": \"{% module linkify(message) %}\",\n                \"page.html\": \"\"\"\\\n<html><head></head><body>\n{% for e in entries %}\n{% module Template(\"entry.html\", entry=e) %}\n{% end %}\n</body></html>\"\"\",\n                \"entry.html\": \"\"\"\\\n{{ set_resources(embedded_css=\".entry { margin-bottom: 1em; }\",\n                 embedded_javascript=\"js_embed()\",\n                 css_files=[\"/base.css\", \"/foo.css\"],\n                 javascript_files=\"/common.js\",\n                 html_head=\"<meta>\",\n                 html_body='<script src=\"/analytics.js\"/>') }}\n<div class=\"entry\">...</div>\"\"\",\n            }\n        )\n        return dict(\n            template_loader=loader,\n            autoescape=\"xhtml_escape\",\n            cookie_secret=self.COOKIE_SECRET,\n        )\n\n    def tearDown(self):\n        super(WSGISafeWebTest, self).tearDown()\n        RequestHandler._template_loaders.clear()\n\n    def get_handlers(self):\n        urls = [\n            url(\"/typecheck/(.*)\", TypeCheckHandler, name=\"typecheck\"),\n            url(\"/decode_arg/(.*)\", DecodeArgHandler, name=\"decode_arg\"),\n            url(\"/decode_arg_kw/(?P<arg>.*)\", DecodeArgHandler),\n            url(\"/linkify\", LinkifyHandler),\n            url(\"/uimodule_resources\", UIModuleResourceHandler),\n            url(\"/optional_path/(.+)?\", OptionalPathHandler),\n            url(\"/multi_header\", MultiHeaderHandler),\n            url(\"/redirect\", RedirectHandler),\n            url(\n                \"/web_redirect_permanent\",\n                WebRedirectHandler,\n                {\"url\": \"/web_redirect_newpath\"},\n            ),\n            url(\n                \"/web_redirect\",\n                WebRedirectHandler,\n                {\"url\": \"/web_redirect_newpath\", \"permanent\": False},\n            ),\n            url(\n                \"//web_redirect_double_slash\",\n                WebRedirectHandler,\n                {\"url\": \"/web_redirect_newpath\"},\n            ),\n            url(\"/header_injection\", HeaderInjectionHandler),\n            url(\"/get_argument\", GetArgumentHandler),\n            url(\"/get_arguments\", GetArgumentsHandler),\n        ]\n        return urls\n\n    def fetch_json(self, *args, **kwargs):\n        response = self.fetch(*args, **kwargs)\n        response.rethrow()\n        return json_decode(response.body)\n\n    def test_types(self):\n        cookie_value = to_unicode(\n            create_signed_value(self.COOKIE_SECRET, \"asdf\", \"qwer\")\n        )\n        response = self.fetch(\n            \"/typecheck/asdf?foo=bar\", headers={\"Cookie\": \"asdf=\" + cookie_value}\n        )\n        data = json_decode(response.body)\n        self.assertEqual(data, {})\n\n        response = self.fetch(\n            \"/typecheck/asdf?foo=bar\",\n            method=\"POST\",\n            headers={\"Cookie\": \"asdf=\" + cookie_value},\n            body=\"foo=bar\",\n        )\n\n    def test_decode_argument(self):\n        # These urls all decode to the same thing\n        urls = [\n            \"/decode_arg/%C3%A9?foo=%C3%A9&encoding=utf-8\",\n            \"/decode_arg/%E9?foo=%E9&encoding=latin1\",\n            \"/decode_arg_kw/%E9?foo=%E9&encoding=latin1\",\n        ]\n        for req_url in urls:\n            response = self.fetch(req_url)\n            response.rethrow()\n            data = json_decode(response.body)\n            self.assertEqual(\n                data,\n                {u\"path\": [u\"unicode\", u\"\\u00e9\"], u\"query\": [u\"unicode\", u\"\\u00e9\"]},\n            )\n\n        response = self.fetch(\"/decode_arg/%C3%A9?foo=%C3%A9\")\n        response.rethrow()\n        data = json_decode(response.body)\n        self.assertEqual(\n            data, {u\"path\": [u\"bytes\", u\"c3a9\"], u\"query\": [u\"bytes\", u\"c3a9\"]}\n        )\n\n    def test_decode_argument_invalid_unicode(self):\n        # test that invalid unicode in URLs causes 400, not 500\n        with ExpectLog(gen_log, \".*Invalid unicode.*\"):\n            response = self.fetch(\"/typecheck/invalid%FF\")\n            self.assertEqual(response.code, 400)\n            response = self.fetch(\"/typecheck/invalid?foo=%FF\")\n            self.assertEqual(response.code, 400)\n\n    def test_decode_argument_plus(self):\n        # These urls are all equivalent.\n        urls = [\n            \"/decode_arg/1%20%2B%201?foo=1%20%2B%201&encoding=utf-8\",\n            \"/decode_arg/1%20+%201?foo=1+%2B+1&encoding=utf-8\",\n        ]\n        for req_url in urls:\n            response = self.fetch(req_url)\n            response.rethrow()\n            data = json_decode(response.body)\n            self.assertEqual(\n                data,\n                {u\"path\": [u\"unicode\", u\"1 + 1\"], u\"query\": [u\"unicode\", u\"1 + 1\"]},\n            )\n\n    def test_reverse_url(self):\n        self.assertEqual(self.app.reverse_url(\"decode_arg\", \"foo\"), \"/decode_arg/foo\")\n        self.assertEqual(self.app.reverse_url(\"decode_arg\", 42), \"/decode_arg/42\")\n        self.assertEqual(self.app.reverse_url(\"decode_arg\", b\"\\xe9\"), \"/decode_arg/%E9\")\n        self.assertEqual(\n            self.app.reverse_url(\"decode_arg\", u\"\\u00e9\"), \"/decode_arg/%C3%A9\"\n        )\n        self.assertEqual(\n            self.app.reverse_url(\"decode_arg\", \"1 + 1\"), \"/decode_arg/1%20%2B%201\"\n        )\n\n    def test_uimodule_unescaped(self):\n        response = self.fetch(\"/linkify\")\n        self.assertEqual(\n            response.body, b'<a href=\"http://example.com\">http://example.com</a>'\n        )\n\n    def test_uimodule_resources(self):\n        response = self.fetch(\"/uimodule_resources\")\n        self.assertEqual(\n            response.body,\n            b\"\"\"\\\n<html><head><link href=\"/base.css\" type=\"text/css\" rel=\"stylesheet\"/><link href=\"/foo.css\" type=\"text/css\" rel=\"stylesheet\"/>\n<style type=\"text/css\">\n.entry { margin-bottom: 1em; }\n</style>\n<meta>\n</head><body>\n\n\n<div class=\"entry\">...</div>\n\n\n<div class=\"entry\">...</div>\n\n<script src=\"/common.js\" type=\"text/javascript\"></script>\n<script type=\"text/javascript\">\n//<![CDATA[\njs_embed()\n//]]>\n</script>\n<script src=\"/analytics.js\"/>\n</body></html>\"\"\",  # noqa: E501\n        )\n\n    def test_optional_path(self):\n        self.assertEqual(self.fetch_json(\"/optional_path/foo\"), {u\"path\": u\"foo\"})\n        self.assertEqual(self.fetch_json(\"/optional_path/\"), {u\"path\": None})\n\n    def test_multi_header(self):\n        response = self.fetch(\"/multi_header\")\n        self.assertEqual(response.headers[\"x-overwrite\"], \"2\")\n        self.assertEqual(response.headers.get_list(\"x-multi\"), [\"3\", \"4\"])\n\n    def test_redirect(self):\n        response = self.fetch(\"/redirect?permanent=1\", follow_redirects=False)\n        self.assertEqual(response.code, 301)\n        response = self.fetch(\"/redirect?permanent=0\", follow_redirects=False)\n        self.assertEqual(response.code, 302)\n        response = self.fetch(\"/redirect?status=307\", follow_redirects=False)\n        self.assertEqual(response.code, 307)\n\n    def test_web_redirect(self):\n        response = self.fetch(\"/web_redirect_permanent\", follow_redirects=False)\n        self.assertEqual(response.code, 301)\n        self.assertEqual(response.headers[\"Location\"], \"/web_redirect_newpath\")\n        response = self.fetch(\"/web_redirect\", follow_redirects=False)\n        self.assertEqual(response.code, 302)\n        self.assertEqual(response.headers[\"Location\"], \"/web_redirect_newpath\")\n\n    def test_web_redirect_double_slash(self):\n        response = self.fetch(\"//web_redirect_double_slash\", follow_redirects=False)\n        self.assertEqual(response.code, 301)\n        self.assertEqual(response.headers[\"Location\"], \"/web_redirect_newpath\")\n\n    def test_header_injection(self):\n        response = self.fetch(\"/header_injection\")\n        self.assertEqual(response.body, b\"ok\")\n\n    def test_get_argument(self):\n        response = self.fetch(\"/get_argument?foo=bar\")\n        self.assertEqual(response.body, b\"bar\")\n        response = self.fetch(\"/get_argument?foo=\")\n        self.assertEqual(response.body, b\"\")\n        response = self.fetch(\"/get_argument\")\n        self.assertEqual(response.body, b\"default\")\n\n        # Test merging of query and body arguments.\n        # In singular form, body arguments take precedence over query arguments.\n        body = urllib.parse.urlencode(dict(foo=\"hello\"))\n        response = self.fetch(\"/get_argument?foo=bar\", method=\"POST\", body=body)\n        self.assertEqual(response.body, b\"hello\")\n        # In plural methods they are merged.\n        response = self.fetch(\"/get_arguments?foo=bar\", method=\"POST\", body=body)\n        self.assertEqual(\n            json_decode(response.body),\n            dict(default=[\"bar\", \"hello\"], query=[\"bar\"], body=[\"hello\"]),\n        )\n\n    def test_get_query_arguments(self):\n        # send as a post so we can ensure the separation between query\n        # string and body arguments.\n        body = urllib.parse.urlencode(dict(foo=\"hello\"))\n        response = self.fetch(\n            \"/get_argument?source=query&foo=bar\", method=\"POST\", body=body\n        )\n        self.assertEqual(response.body, b\"bar\")\n        response = self.fetch(\n            \"/get_argument?source=query&foo=\", method=\"POST\", body=body\n        )\n        self.assertEqual(response.body, b\"\")\n        response = self.fetch(\"/get_argument?source=query\", method=\"POST\", body=body)\n        self.assertEqual(response.body, b\"default\")\n\n    def test_get_body_arguments(self):\n        body = urllib.parse.urlencode(dict(foo=\"bar\"))\n        response = self.fetch(\n            \"/get_argument?source=body&foo=hello\", method=\"POST\", body=body\n        )\n        self.assertEqual(response.body, b\"bar\")\n\n        body = urllib.parse.urlencode(dict(foo=\"\"))\n        response = self.fetch(\n            \"/get_argument?source=body&foo=hello\", method=\"POST\", body=body\n        )\n        self.assertEqual(response.body, b\"\")\n\n        body = urllib.parse.urlencode(dict())\n        response = self.fetch(\n            \"/get_argument?source=body&foo=hello\", method=\"POST\", body=body\n        )\n        self.assertEqual(response.body, b\"default\")\n\n    def test_no_gzip(self):\n        response = self.fetch(\"/get_argument\")\n        self.assertNotIn(\"Accept-Encoding\", response.headers.get(\"Vary\", \"\"))\n        self.assertNotIn(\"gzip\", response.headers.get(\"Content-Encoding\", \"\"))\n\n\nclass NonWSGIWebTests(WebTestCase):\n    def get_handlers(self):\n        return [(\"/empty_flush\", EmptyFlushCallbackHandler)]\n\n    def test_empty_flush(self):\n        response = self.fetch(\"/empty_flush\")\n        self.assertEqual(response.body, b\"ok\")\n\n\nclass ErrorResponseTest(WebTestCase):\n    def get_handlers(self):\n        class DefaultHandler(RequestHandler):\n            def get(self):\n                if self.get_argument(\"status\", None):\n                    raise HTTPError(int(self.get_argument(\"status\")))\n                1 / 0\n\n        class WriteErrorHandler(RequestHandler):\n            def get(self):\n                if self.get_argument(\"status\", None):\n                    self.send_error(int(self.get_argument(\"status\")))\n                else:\n                    1 / 0\n\n            def write_error(self, status_code, **kwargs):\n                self.set_header(\"Content-Type\", \"text/plain\")\n                if \"exc_info\" in kwargs:\n                    self.write(\"Exception: %s\" % kwargs[\"exc_info\"][0].__name__)\n                else:\n                    self.write(\"Status: %d\" % status_code)\n\n        class FailedWriteErrorHandler(RequestHandler):\n            def get(self):\n                1 / 0\n\n            def write_error(self, status_code, **kwargs):\n                raise Exception(\"exception in write_error\")\n\n        return [\n            url(\"/default\", DefaultHandler),\n            url(\"/write_error\", WriteErrorHandler),\n            url(\"/failed_write_error\", FailedWriteErrorHandler),\n        ]\n\n    def test_default(self):\n        with ExpectLog(app_log, \"Uncaught exception\"):\n            response = self.fetch(\"/default\")\n            self.assertEqual(response.code, 500)\n            self.assertTrue(b\"500: Internal Server Error\" in response.body)\n\n            response = self.fetch(\"/default?status=503\")\n            self.assertEqual(response.code, 503)\n            self.assertTrue(b\"503: Service Unavailable\" in response.body)\n\n            response = self.fetch(\"/default?status=435\")\n            self.assertEqual(response.code, 435)\n            self.assertTrue(b\"435: Unknown\" in response.body)\n\n    def test_write_error(self):\n        with ExpectLog(app_log, \"Uncaught exception\"):\n            response = self.fetch(\"/write_error\")\n            self.assertEqual(response.code, 500)\n            self.assertEqual(b\"Exception: ZeroDivisionError\", response.body)\n\n            response = self.fetch(\"/write_error?status=503\")\n            self.assertEqual(response.code, 503)\n            self.assertEqual(b\"Status: 503\", response.body)\n\n    def test_failed_write_error(self):\n        with ExpectLog(app_log, \"Uncaught exception\"):\n            response = self.fetch(\"/failed_write_error\")\n            self.assertEqual(response.code, 500)\n            self.assertEqual(b\"\", response.body)\n\n\nclass StaticFileTest(WebTestCase):\n    # The expected MD5 hash of robots.txt, used in tests that call\n    # StaticFileHandler.get_version\n    robots_txt_hash = b\"f71d20196d4caf35b6a670db8c70b03d\"\n    static_dir = os.path.join(os.path.dirname(__file__), \"static\")\n\n    def get_handlers(self):\n        class StaticUrlHandler(RequestHandler):\n            def get(self, path):\n                with_v = int(self.get_argument(\"include_version\", 1))\n                self.write(self.static_url(path, include_version=with_v))\n\n        class AbsoluteStaticUrlHandler(StaticUrlHandler):\n            include_host = True\n\n        class OverrideStaticUrlHandler(RequestHandler):\n            def get(self, path):\n                do_include = bool(self.get_argument(\"include_host\"))\n                self.include_host = not do_include\n\n                regular_url = self.static_url(path)\n                override_url = self.static_url(path, include_host=do_include)\n                if override_url == regular_url:\n                    return self.write(str(False))\n\n                protocol = self.request.protocol + \"://\"\n                protocol_length = len(protocol)\n                check_regular = regular_url.find(protocol, 0, protocol_length)\n                check_override = override_url.find(protocol, 0, protocol_length)\n\n                if do_include:\n                    result = check_override == 0 and check_regular == -1\n                else:\n                    result = check_override == -1 and check_regular == 0\n                self.write(str(result))\n\n        return [\n            (\"/static_url/(.*)\", StaticUrlHandler),\n            (\"/abs_static_url/(.*)\", AbsoluteStaticUrlHandler),\n            (\"/override_static_url/(.*)\", OverrideStaticUrlHandler),\n            (\"/root_static/(.*)\", StaticFileHandler, dict(path=\"/\")),\n        ]\n\n    def get_app_kwargs(self):\n        return dict(static_path=relpath(\"static\"))\n\n    def test_static_files(self):\n        response = self.fetch(\"/robots.txt\")\n        self.assertTrue(b\"Disallow: /\" in response.body)\n\n        response = self.fetch(\"/static/robots.txt\")\n        self.assertTrue(b\"Disallow: /\" in response.body)\n        self.assertEqual(response.headers.get(\"Content-Type\"), \"text/plain\")\n\n    def test_static_compressed_files(self):\n        response = self.fetch(\"/static/sample.xml.gz\")\n        self.assertEqual(response.headers.get(\"Content-Type\"), \"application/gzip\")\n        response = self.fetch(\"/static/sample.xml.bz2\")\n        self.assertEqual(\n            response.headers.get(\"Content-Type\"), \"application/octet-stream\"\n        )\n        # make sure the uncompressed file still has the correct type\n        response = self.fetch(\"/static/sample.xml\")\n        self.assertTrue(\n            response.headers.get(\"Content-Type\") in set((\"text/xml\", \"application/xml\"))\n        )\n\n    def test_static_url(self):\n        response = self.fetch(\"/static_url/robots.txt\")\n        self.assertEqual(response.body, b\"/static/robots.txt?v=\" + self.robots_txt_hash)\n\n    def test_absolute_static_url(self):\n        response = self.fetch(\"/abs_static_url/robots.txt\")\n        self.assertEqual(\n            response.body,\n            (utf8(self.get_url(\"/\")) + b\"static/robots.txt?v=\" + self.robots_txt_hash),\n        )\n\n    def test_relative_version_exclusion(self):\n        response = self.fetch(\"/static_url/robots.txt?include_version=0\")\n        self.assertEqual(response.body, b\"/static/robots.txt\")\n\n    def test_absolute_version_exclusion(self):\n        response = self.fetch(\"/abs_static_url/robots.txt?include_version=0\")\n        self.assertEqual(response.body, utf8(self.get_url(\"/\") + \"static/robots.txt\"))\n\n    def test_include_host_override(self):\n        self._trigger_include_host_check(False)\n        self._trigger_include_host_check(True)\n\n    def _trigger_include_host_check(self, include_host):\n        path = \"/override_static_url/robots.txt?include_host=%s\"\n        response = self.fetch(path % int(include_host))\n        self.assertEqual(response.body, utf8(str(True)))\n\n    def get_and_head(self, *args, **kwargs):\n        \"\"\"Performs a GET and HEAD request and returns the GET response.\n\n        Fails if any ``Content-*`` headers returned by the two requests\n        differ.\n        \"\"\"\n        head_response = self.fetch(*args, method=\"HEAD\", **kwargs)\n        get_response = self.fetch(*args, method=\"GET\", **kwargs)\n        content_headers = set()\n        for h in itertools.chain(head_response.headers, get_response.headers):\n            if h.startswith(\"Content-\"):\n                content_headers.add(h)\n        for h in content_headers:\n            self.assertEqual(\n                head_response.headers.get(h),\n                get_response.headers.get(h),\n                \"%s differs between GET (%s) and HEAD (%s)\"\n                % (h, head_response.headers.get(h), get_response.headers.get(h)),\n            )\n        return get_response\n\n    def test_static_304_if_modified_since(self):\n        response1 = self.get_and_head(\"/static/robots.txt\")\n        response2 = self.get_and_head(\n            \"/static/robots.txt\",\n            headers={\"If-Modified-Since\": response1.headers[\"Last-Modified\"]},\n        )\n        self.assertEqual(response2.code, 304)\n        self.assertTrue(\"Content-Length\" not in response2.headers)\n        self.assertTrue(\"Last-Modified\" not in response2.headers)\n\n    def test_static_304_if_none_match(self):\n        response1 = self.get_and_head(\"/static/robots.txt\")\n        response2 = self.get_and_head(\n            \"/static/robots.txt\", headers={\"If-None-Match\": response1.headers[\"Etag\"]}\n        )\n        self.assertEqual(response2.code, 304)\n\n    def test_static_304_etag_modified_bug(self):\n        response1 = self.get_and_head(\"/static/robots.txt\")\n        response2 = self.get_and_head(\n            \"/static/robots.txt\",\n            headers={\n                \"If-None-Match\": '\"MISMATCH\"',\n                \"If-Modified-Since\": response1.headers[\"Last-Modified\"],\n            },\n        )\n        self.assertEqual(response2.code, 200)\n\n    def test_static_if_modified_since_pre_epoch(self):\n        # On windows, the functions that work with time_t do not accept\n        # negative values, and at least one client (processing.js) seems\n        # to use if-modified-since 1/1/1960 as a cache-busting technique.\n        response = self.get_and_head(\n            \"/static/robots.txt\",\n            headers={\"If-Modified-Since\": \"Fri, 01 Jan 1960 00:00:00 GMT\"},\n        )\n        self.assertEqual(response.code, 200)\n\n    def test_static_if_modified_since_time_zone(self):\n        # Instead of the value from Last-Modified, make requests with times\n        # chosen just before and after the known modification time\n        # of the file to ensure that the right time zone is being used\n        # when parsing If-Modified-Since.\n        stat = os.stat(relpath(\"static/robots.txt\"))\n\n        response = self.get_and_head(\n            \"/static/robots.txt\",\n            headers={\"If-Modified-Since\": format_timestamp(stat.st_mtime - 1)},\n        )\n        self.assertEqual(response.code, 200)\n        response = self.get_and_head(\n            \"/static/robots.txt\",\n            headers={\"If-Modified-Since\": format_timestamp(stat.st_mtime + 1)},\n        )\n        self.assertEqual(response.code, 304)\n\n    def test_static_etag(self):\n        response = self.get_and_head(\"/static/robots.txt\")\n        self.assertEqual(\n            utf8(response.headers.get(\"Etag\")), b'\"' + self.robots_txt_hash + b'\"'\n        )\n\n    def test_static_with_range(self):\n        response = self.get_and_head(\n            \"/static/robots.txt\", headers={\"Range\": \"bytes=0-9\"}\n        )\n        self.assertEqual(response.code, 206)\n        self.assertEqual(response.body, b\"User-agent\")\n        self.assertEqual(\n            utf8(response.headers.get(\"Etag\")), b'\"' + self.robots_txt_hash + b'\"'\n        )\n        self.assertEqual(response.headers.get(\"Content-Length\"), \"10\")\n        self.assertEqual(response.headers.get(\"Content-Range\"), \"bytes 0-9/26\")\n\n    def test_static_with_range_full_file(self):\n        response = self.get_and_head(\n            \"/static/robots.txt\", headers={\"Range\": \"bytes=0-\"}\n        )\n        # Note: Chrome refuses to play audio if it gets an HTTP 206 in response\n        # to ``Range: bytes=0-`` :(\n        self.assertEqual(response.code, 200)\n        robots_file_path = os.path.join(self.static_dir, \"robots.txt\")\n        with open(robots_file_path) as f:\n            self.assertEqual(response.body, utf8(f.read()))\n        self.assertEqual(response.headers.get(\"Content-Length\"), \"26\")\n        self.assertEqual(response.headers.get(\"Content-Range\"), None)\n\n    def test_static_with_range_full_past_end(self):\n        response = self.get_and_head(\n            \"/static/robots.txt\", headers={\"Range\": \"bytes=0-10000000\"}\n        )\n        self.assertEqual(response.code, 200)\n        robots_file_path = os.path.join(self.static_dir, \"robots.txt\")\n        with open(robots_file_path) as f:\n            self.assertEqual(response.body, utf8(f.read()))\n        self.assertEqual(response.headers.get(\"Content-Length\"), \"26\")\n        self.assertEqual(response.headers.get(\"Content-Range\"), None)\n\n    def test_static_with_range_partial_past_end(self):\n        response = self.get_and_head(\n            \"/static/robots.txt\", headers={\"Range\": \"bytes=1-10000000\"}\n        )\n        self.assertEqual(response.code, 206)\n        robots_file_path = os.path.join(self.static_dir, \"robots.txt\")\n        with open(robots_file_path) as f:\n            self.assertEqual(response.body, utf8(f.read()[1:]))\n        self.assertEqual(response.headers.get(\"Content-Length\"), \"25\")\n        self.assertEqual(response.headers.get(\"Content-Range\"), \"bytes 1-25/26\")\n\n    def test_static_with_range_end_edge(self):\n        response = self.get_and_head(\n            \"/static/robots.txt\", headers={\"Range\": \"bytes=22-\"}\n        )\n        self.assertEqual(response.body, b\": /\\n\")\n        self.assertEqual(response.headers.get(\"Content-Length\"), \"4\")\n        self.assertEqual(response.headers.get(\"Content-Range\"), \"bytes 22-25/26\")\n\n    def test_static_with_range_neg_end(self):\n        response = self.get_and_head(\n            \"/static/robots.txt\", headers={\"Range\": \"bytes=-4\"}\n        )\n        self.assertEqual(response.body, b\": /\\n\")\n        self.assertEqual(response.headers.get(\"Content-Length\"), \"4\")\n        self.assertEqual(response.headers.get(\"Content-Range\"), \"bytes 22-25/26\")\n\n    def test_static_with_range_neg_past_start(self):\n        response = self.get_and_head(\n            \"/static/robots.txt\", headers={\"Range\": \"bytes=-1000000\"}\n        )\n        self.assertEqual(response.code, 200)\n        robots_file_path = os.path.join(self.static_dir, \"robots.txt\")\n        with open(robots_file_path) as f:\n            self.assertEqual(response.body, utf8(f.read()))\n        self.assertEqual(response.headers.get(\"Content-Length\"), \"26\")\n        self.assertEqual(response.headers.get(\"Content-Range\"), None)\n\n    def test_static_invalid_range(self):\n        response = self.get_and_head(\"/static/robots.txt\", headers={\"Range\": \"asdf\"})\n        self.assertEqual(response.code, 200)\n\n    def test_static_unsatisfiable_range_zero_suffix(self):\n        response = self.get_and_head(\n            \"/static/robots.txt\", headers={\"Range\": \"bytes=-0\"}\n        )\n        self.assertEqual(response.headers.get(\"Content-Range\"), \"bytes */26\")\n        self.assertEqual(response.code, 416)\n\n    def test_static_unsatisfiable_range_invalid_start(self):\n        response = self.get_and_head(\n            \"/static/robots.txt\", headers={\"Range\": \"bytes=26\"}\n        )\n        self.assertEqual(response.code, 416)\n        self.assertEqual(response.headers.get(\"Content-Range\"), \"bytes */26\")\n\n    def test_static_unsatisfiable_range_end_less_than_start(self):\n        response = self.get_and_head(\n            \"/static/robots.txt\", headers={\"Range\": \"bytes=10-3\"}\n        )\n        self.assertEqual(response.code, 416)\n        self.assertEqual(response.headers.get(\"Content-Range\"), \"bytes */26\")\n\n    def test_static_head(self):\n        response = self.fetch(\"/static/robots.txt\", method=\"HEAD\")\n        self.assertEqual(response.code, 200)\n        # No body was returned, but we did get the right content length.\n        self.assertEqual(response.body, b\"\")\n        self.assertEqual(response.headers[\"Content-Length\"], \"26\")\n        self.assertEqual(\n            utf8(response.headers[\"Etag\"]), b'\"' + self.robots_txt_hash + b'\"'\n        )\n\n    def test_static_head_range(self):\n        response = self.fetch(\n            \"/static/robots.txt\", method=\"HEAD\", headers={\"Range\": \"bytes=1-4\"}\n        )\n        self.assertEqual(response.code, 206)\n        self.assertEqual(response.body, b\"\")\n        self.assertEqual(response.headers[\"Content-Length\"], \"4\")\n        self.assertEqual(\n            utf8(response.headers[\"Etag\"]), b'\"' + self.robots_txt_hash + b'\"'\n        )\n\n    def test_static_range_if_none_match(self):\n        response = self.get_and_head(\n            \"/static/robots.txt\",\n            headers={\n                \"Range\": \"bytes=1-4\",\n                \"If-None-Match\": b'\"' + self.robots_txt_hash + b'\"',\n            },\n        )\n        self.assertEqual(response.code, 304)\n        self.assertEqual(response.body, b\"\")\n        self.assertTrue(\"Content-Length\" not in response.headers)\n        self.assertEqual(\n            utf8(response.headers[\"Etag\"]), b'\"' + self.robots_txt_hash + b'\"'\n        )\n\n    def test_static_404(self):\n        response = self.get_and_head(\"/static/blarg\")\n        self.assertEqual(response.code, 404)\n\n    def test_path_traversal_protection(self):\n        # curl_httpclient processes \"..\" on the client side, so we\n        # must test this with simple_httpclient.\n        self.http_client.close()\n        self.http_client = SimpleAsyncHTTPClient()\n        with ExpectLog(gen_log, \".*not in root static directory\"):\n            response = self.get_and_head(\"/static/../static_foo.txt\")\n        # Attempted path traversal should result in 403, not 200\n        # (which means the check failed and the file was served)\n        # or 404 (which means that the file didn't exist and\n        # is probably a packaging error).\n        self.assertEqual(response.code, 403)\n\n    @unittest.skipIf(os.name != \"posix\", \"non-posix OS\")\n    def test_root_static_path(self):\n        # Sometimes people set the StaticFileHandler's path to '/'\n        # to disable Tornado's path validation (in conjunction with\n        # their own validation in get_absolute_path). Make sure\n        # that the stricter validation in 4.2.1 doesn't break them.\n        path = os.path.join(\n            os.path.dirname(os.path.abspath(__file__)), \"static/robots.txt\"\n        )\n        response = self.get_and_head(\"/root_static\" + urllib.parse.quote(path))\n        self.assertEqual(response.code, 200)\n\n\nclass StaticDefaultFilenameTest(WebTestCase):\n    def get_app_kwargs(self):\n        return dict(\n            static_path=relpath(\"static\"),\n            static_handler_args=dict(default_filename=\"index.html\"),\n        )\n\n    def get_handlers(self):\n        return []\n\n    def test_static_default_filename(self):\n        response = self.fetch(\"/static/dir/\", follow_redirects=False)\n        self.assertEqual(response.code, 200)\n        self.assertEqual(b\"this is the index\\n\", response.body)\n\n    def test_static_default_redirect(self):\n        response = self.fetch(\"/static/dir\", follow_redirects=False)\n        self.assertEqual(response.code, 301)\n        self.assertTrue(response.headers[\"Location\"].endswith(\"/static/dir/\"))\n\n\nclass StaticFileWithPathTest(WebTestCase):\n    def get_app_kwargs(self):\n        return dict(\n            static_path=relpath(\"static\"),\n            static_handler_args=dict(default_filename=\"index.html\"),\n        )\n\n    def get_handlers(self):\n        return [(\"/foo/(.*)\", StaticFileHandler, {\"path\": relpath(\"templates/\")})]\n\n    def test_serve(self):\n        response = self.fetch(\"/foo/utf8.html\")\n        self.assertEqual(response.body, b\"H\\xc3\\xa9llo\\n\")\n\n\nclass CustomStaticFileTest(WebTestCase):\n    def get_handlers(self):\n        class MyStaticFileHandler(StaticFileHandler):\n            @classmethod\n            def make_static_url(cls, settings, path):\n                version_hash = cls.get_version(settings, path)\n                extension_index = path.rindex(\".\")\n                before_version = path[:extension_index]\n                after_version = path[(extension_index + 1) :]\n                return \"/static/%s.%s.%s\" % (\n                    before_version,\n                    version_hash,\n                    after_version,\n                )\n\n            def parse_url_path(self, url_path):\n                extension_index = url_path.rindex(\".\")\n                version_index = url_path.rindex(\".\", 0, extension_index)\n                return \"%s%s\" % (url_path[:version_index], url_path[extension_index:])\n\n            @classmethod\n            def get_absolute_path(cls, settings, path):\n                return \"CustomStaticFileTest:\" + path\n\n            def validate_absolute_path(self, root, absolute_path):\n                return absolute_path\n\n            @classmethod\n            def get_content(self, path, start=None, end=None):\n                assert start is None and end is None\n                if path == \"CustomStaticFileTest:foo.txt\":\n                    return b\"bar\"\n                raise Exception(\"unexpected path %r\" % path)\n\n            def get_content_size(self):\n                if self.absolute_path == \"CustomStaticFileTest:foo.txt\":\n                    return 3\n                raise Exception(\"unexpected path %r\" % self.absolute_path)\n\n            def get_modified_time(self):\n                return None\n\n            @classmethod\n            def get_version(cls, settings, path):\n                return \"42\"\n\n        class StaticUrlHandler(RequestHandler):\n            def get(self, path):\n                self.write(self.static_url(path))\n\n        self.static_handler_class = MyStaticFileHandler\n\n        return [(\"/static_url/(.*)\", StaticUrlHandler)]\n\n    def get_app_kwargs(self):\n        return dict(static_path=\"dummy\", static_handler_class=self.static_handler_class)\n\n    def test_serve(self):\n        response = self.fetch(\"/static/foo.42.txt\")\n        self.assertEqual(response.body, b\"bar\")\n\n    def test_static_url(self):\n        with ExpectLog(gen_log, \"Could not open static file\", required=False):\n            response = self.fetch(\"/static_url/foo.txt\")\n            self.assertEqual(response.body, b\"/static/foo.42.txt\")\n\n\nclass HostMatchingTest(WebTestCase):\n    class Handler(RequestHandler):\n        def initialize(self, reply):\n            self.reply = reply\n\n        def get(self):\n            self.write(self.reply)\n\n    def get_handlers(self):\n        return [(\"/foo\", HostMatchingTest.Handler, {\"reply\": \"wildcard\"})]\n\n    def test_host_matching(self):\n        self.app.add_handlers(\n            \"www.example.com\", [(\"/foo\", HostMatchingTest.Handler, {\"reply\": \"[0]\"})]\n        )\n        self.app.add_handlers(\n            r\"www\\.example\\.com\", [(\"/bar\", HostMatchingTest.Handler, {\"reply\": \"[1]\"})]\n        )\n        self.app.add_handlers(\n            \"www.example.com\", [(\"/baz\", HostMatchingTest.Handler, {\"reply\": \"[2]\"})]\n        )\n        self.app.add_handlers(\n            \"www.e.*e.com\", [(\"/baz\", HostMatchingTest.Handler, {\"reply\": \"[3]\"})]\n        )\n\n        response = self.fetch(\"/foo\")\n        self.assertEqual(response.body, b\"wildcard\")\n        response = self.fetch(\"/bar\")\n        self.assertEqual(response.code, 404)\n        response = self.fetch(\"/baz\")\n        self.assertEqual(response.code, 404)\n\n        response = self.fetch(\"/foo\", headers={\"Host\": \"www.example.com\"})\n        self.assertEqual(response.body, b\"[0]\")\n        response = self.fetch(\"/bar\", headers={\"Host\": \"www.example.com\"})\n        self.assertEqual(response.body, b\"[1]\")\n        response = self.fetch(\"/baz\", headers={\"Host\": \"www.example.com\"})\n        self.assertEqual(response.body, b\"[2]\")\n        response = self.fetch(\"/baz\", headers={\"Host\": \"www.exe.com\"})\n        self.assertEqual(response.body, b\"[3]\")\n\n\nclass DefaultHostMatchingTest(WebTestCase):\n    def get_handlers(self):\n        return []\n\n    def get_app_kwargs(self):\n        return {\"default_host\": \"www.example.com\"}\n\n    def test_default_host_matching(self):\n        self.app.add_handlers(\n            \"www.example.com\", [(\"/foo\", HostMatchingTest.Handler, {\"reply\": \"[0]\"})]\n        )\n        self.app.add_handlers(\n            r\"www\\.example\\.com\", [(\"/bar\", HostMatchingTest.Handler, {\"reply\": \"[1]\"})]\n        )\n        self.app.add_handlers(\n            \"www.test.com\", [(\"/baz\", HostMatchingTest.Handler, {\"reply\": \"[2]\"})]\n        )\n\n        response = self.fetch(\"/foo\")\n        self.assertEqual(response.body, b\"[0]\")\n        response = self.fetch(\"/bar\")\n        self.assertEqual(response.body, b\"[1]\")\n        response = self.fetch(\"/baz\")\n        self.assertEqual(response.code, 404)\n\n        response = self.fetch(\"/foo\", headers={\"X-Real-Ip\": \"127.0.0.1\"})\n        self.assertEqual(response.code, 404)\n\n        self.app.default_host = \"www.test.com\"\n\n        response = self.fetch(\"/baz\")\n        self.assertEqual(response.body, b\"[2]\")\n\n\nclass NamedURLSpecGroupsTest(WebTestCase):\n    def get_handlers(self):\n        class EchoHandler(RequestHandler):\n            def get(self, path):\n                self.write(path)\n\n        return [\n            (\"/str/(?P<path>.*)\", EchoHandler),\n            (u\"/unicode/(?P<path>.*)\", EchoHandler),\n        ]\n\n    def test_named_urlspec_groups(self):\n        response = self.fetch(\"/str/foo\")\n        self.assertEqual(response.body, b\"foo\")\n\n        response = self.fetch(\"/unicode/bar\")\n        self.assertEqual(response.body, b\"bar\")\n\n\nclass ClearHeaderTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            self.set_header(\"h1\", \"foo\")\n            self.set_header(\"h2\", \"bar\")\n            self.clear_header(\"h1\")\n            self.clear_header(\"nonexistent\")\n\n    def test_clear_header(self):\n        response = self.fetch(\"/\")\n        self.assertTrue(\"h1\" not in response.headers)\n        self.assertEqual(response.headers[\"h2\"], \"bar\")\n\n\nclass Header204Test(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            self.set_status(204)\n            self.finish()\n\n    def test_204_headers(self):\n        response = self.fetch(\"/\")\n        self.assertEqual(response.code, 204)\n        self.assertNotIn(\"Content-Length\", response.headers)\n        self.assertNotIn(\"Transfer-Encoding\", response.headers)\n\n\nclass Header304Test(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            self.set_header(\"Content-Language\", \"en_US\")\n            self.write(\"hello\")\n\n    def test_304_headers(self):\n        response1 = self.fetch(\"/\")\n        self.assertEqual(response1.headers[\"Content-Length\"], \"5\")\n        self.assertEqual(response1.headers[\"Content-Language\"], \"en_US\")\n\n        response2 = self.fetch(\n            \"/\", headers={\"If-None-Match\": response1.headers[\"Etag\"]}\n        )\n        self.assertEqual(response2.code, 304)\n        self.assertTrue(\"Content-Length\" not in response2.headers)\n        self.assertTrue(\"Content-Language\" not in response2.headers)\n        # Not an entity header, but should not be added to 304s by chunking\n        self.assertTrue(\"Transfer-Encoding\" not in response2.headers)\n\n\nclass StatusReasonTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            reason = self.request.arguments.get(\"reason\", [])\n            self.set_status(\n                int(self.get_argument(\"code\")), reason=reason[0] if reason else None\n            )\n\n    def get_http_client(self):\n        # simple_httpclient only: curl doesn't expose the reason string\n        return SimpleAsyncHTTPClient()\n\n    def test_status(self):\n        response = self.fetch(\"/?code=304\")\n        self.assertEqual(response.code, 304)\n        self.assertEqual(response.reason, \"Not Modified\")\n        response = self.fetch(\"/?code=304&reason=Foo\")\n        self.assertEqual(response.code, 304)\n        self.assertEqual(response.reason, \"Foo\")\n        response = self.fetch(\"/?code=682&reason=Bar\")\n        self.assertEqual(response.code, 682)\n        self.assertEqual(response.reason, \"Bar\")\n        response = self.fetch(\"/?code=682\")\n        self.assertEqual(response.code, 682)\n        self.assertEqual(response.reason, \"Unknown\")\n\n\nclass DateHeaderTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            self.write(\"hello\")\n\n    def test_date_header(self):\n        response = self.fetch(\"/\")\n        parsed = email.utils.parsedate(response.headers[\"Date\"])\n        assert parsed is not None\n        header_date = datetime.datetime(*parsed[:6])\n        self.assertTrue(\n            header_date - datetime.datetime.utcnow() < datetime.timedelta(seconds=2)\n        )\n\n\nclass RaiseWithReasonTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            raise HTTPError(682, reason=\"Foo\")\n\n    def get_http_client(self):\n        # simple_httpclient only: curl doesn't expose the reason string\n        return SimpleAsyncHTTPClient()\n\n    def test_raise_with_reason(self):\n        response = self.fetch(\"/\")\n        self.assertEqual(response.code, 682)\n        self.assertEqual(response.reason, \"Foo\")\n        self.assertIn(b\"682: Foo\", response.body)\n\n    def test_httperror_str(self):\n        self.assertEqual(str(HTTPError(682, reason=\"Foo\")), \"HTTP 682: Foo\")\n\n    def test_httperror_str_from_httputil(self):\n        self.assertEqual(str(HTTPError(682)), \"HTTP 682: Unknown\")\n\n\nclass ErrorHandlerXSRFTest(WebTestCase):\n    def get_handlers(self):\n        # note that if the handlers list is empty we get the default_host\n        # redirect fallback instead of a 404, so test with both an\n        # explicitly defined error handler and an implicit 404.\n        return [(\"/error\", ErrorHandler, dict(status_code=417))]\n\n    def get_app_kwargs(self):\n        return dict(xsrf_cookies=True)\n\n    def test_error_xsrf(self):\n        response = self.fetch(\"/error\", method=\"POST\", body=\"\")\n        self.assertEqual(response.code, 417)\n\n    def test_404_xsrf(self):\n        response = self.fetch(\"/404\", method=\"POST\", body=\"\")\n        self.assertEqual(response.code, 404)\n\n\nclass GzipTestCase(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            for v in self.get_arguments(\"vary\"):\n                self.add_header(\"Vary\", v)\n            # Must write at least MIN_LENGTH bytes to activate compression.\n            self.write(\"hello world\" + (\"!\" * GZipContentEncoding.MIN_LENGTH))\n\n    def get_app_kwargs(self):\n        return dict(\n            gzip=True, static_path=os.path.join(os.path.dirname(__file__), \"static\")\n        )\n\n    def assert_compressed(self, response):\n        # simple_httpclient renames the content-encoding header;\n        # curl_httpclient doesn't.\n        self.assertEqual(\n            response.headers.get(\n                \"Content-Encoding\", response.headers.get(\"X-Consumed-Content-Encoding\")\n            ),\n            \"gzip\",\n        )\n\n    def test_gzip(self):\n        response = self.fetch(\"/\")\n        self.assert_compressed(response)\n        self.assertEqual(response.headers[\"Vary\"], \"Accept-Encoding\")\n\n    def test_gzip_static(self):\n        # The streaming responses in StaticFileHandler have subtle\n        # interactions with the gzip output so test this case separately.\n        response = self.fetch(\"/robots.txt\")\n        self.assert_compressed(response)\n        self.assertEqual(response.headers[\"Vary\"], \"Accept-Encoding\")\n\n    def test_gzip_not_requested(self):\n        response = self.fetch(\"/\", use_gzip=False)\n        self.assertNotIn(\"Content-Encoding\", response.headers)\n        self.assertEqual(response.headers[\"Vary\"], \"Accept-Encoding\")\n\n    def test_vary_already_present(self):\n        response = self.fetch(\"/?vary=Accept-Language\")\n        self.assert_compressed(response)\n        self.assertEqual(\n            [s.strip() for s in response.headers[\"Vary\"].split(\",\")],\n            [\"Accept-Language\", \"Accept-Encoding\"],\n        )\n\n    def test_vary_already_present_multiple(self):\n        # Regression test for https://github.com/tornadoweb/tornado/issues/1670\n        response = self.fetch(\"/?vary=Accept-Language&vary=Cookie\")\n        self.assert_compressed(response)\n        self.assertEqual(\n            [s.strip() for s in response.headers[\"Vary\"].split(\",\")],\n            [\"Accept-Language\", \"Cookie\", \"Accept-Encoding\"],\n        )\n\n\nclass PathArgsInPrepareTest(WebTestCase):\n    class Handler(RequestHandler):\n        def prepare(self):\n            self.write(dict(args=self.path_args, kwargs=self.path_kwargs))\n\n        def get(self, path):\n            assert path == \"foo\"\n            self.finish()\n\n    def get_handlers(self):\n        return [(\"/pos/(.*)\", self.Handler), (\"/kw/(?P<path>.*)\", self.Handler)]\n\n    def test_pos(self):\n        response = self.fetch(\"/pos/foo\")\n        response.rethrow()\n        data = json_decode(response.body)\n        self.assertEqual(data, {\"args\": [\"foo\"], \"kwargs\": {}})\n\n    def test_kw(self):\n        response = self.fetch(\"/kw/foo\")\n        response.rethrow()\n        data = json_decode(response.body)\n        self.assertEqual(data, {\"args\": [], \"kwargs\": {\"path\": \"foo\"}})\n\n\nclass ClearAllCookiesTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            self.clear_all_cookies()\n            self.write(\"ok\")\n\n    def test_clear_all_cookies(self):\n        response = self.fetch(\"/\", headers={\"Cookie\": \"foo=bar; baz=xyzzy\"})\n        set_cookies = sorted(response.headers.get_list(\"Set-Cookie\"))\n        # Python 3.5 sends 'baz=\"\";'; older versions use 'baz=;'\n        self.assertTrue(\n            set_cookies[0].startswith(\"baz=;\") or set_cookies[0].startswith('baz=\"\";')\n        )\n        self.assertTrue(\n            set_cookies[1].startswith(\"foo=;\") or set_cookies[1].startswith('foo=\"\";')\n        )\n\n\nclass PermissionError(Exception):\n    pass\n\n\nclass ExceptionHandlerTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            exc = self.get_argument(\"exc\")\n            if exc == \"http\":\n                raise HTTPError(410, \"no longer here\")\n            elif exc == \"zero\":\n                1 / 0\n            elif exc == \"permission\":\n                raise PermissionError(\"not allowed\")\n\n        def write_error(self, status_code, **kwargs):\n            if \"exc_info\" in kwargs:\n                typ, value, tb = kwargs[\"exc_info\"]\n                if isinstance(value, PermissionError):\n                    self.set_status(403)\n                    self.write(\"PermissionError\")\n                    return\n            RequestHandler.write_error(self, status_code, **kwargs)\n\n        def log_exception(self, typ, value, tb):\n            if isinstance(value, PermissionError):\n                app_log.warning(\"custom logging for PermissionError: %s\", value.args[0])\n            else:\n                RequestHandler.log_exception(self, typ, value, tb)\n\n    def test_http_error(self):\n        # HTTPErrors are logged as warnings with no stack trace.\n        # TODO: extend ExpectLog to test this more precisely\n        with ExpectLog(gen_log, \".*no longer here\"):\n            response = self.fetch(\"/?exc=http\")\n            self.assertEqual(response.code, 410)\n\n    def test_unknown_error(self):\n        # Unknown errors are logged as errors with a stack trace.\n        with ExpectLog(app_log, \"Uncaught exception\"):\n            response = self.fetch(\"/?exc=zero\")\n            self.assertEqual(response.code, 500)\n\n    def test_known_error(self):\n        # log_exception can override logging behavior, and write_error\n        # can override the response.\n        with ExpectLog(app_log, \"custom logging for PermissionError: not allowed\"):\n            response = self.fetch(\"/?exc=permission\")\n            self.assertEqual(response.code, 403)\n\n\nclass BuggyLoggingTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            1 / 0\n\n        def log_exception(self, typ, value, tb):\n            1 / 0\n\n    def test_buggy_log_exception(self):\n        # Something gets logged even though the application's\n        # logger is broken.\n        with ExpectLog(app_log, \".*\"):\n            self.fetch(\"/\")\n\n\nclass UIMethodUIModuleTest(SimpleHandlerTestCase):\n    \"\"\"Test that UI methods and modules are created correctly and\n    associated with the handler.\n    \"\"\"\n\n    class Handler(RequestHandler):\n        def get(self):\n            self.render(\"foo.html\")\n\n        def value(self):\n            return self.get_argument(\"value\")\n\n    def get_app_kwargs(self):\n        def my_ui_method(handler, x):\n            return \"In my_ui_method(%s) with handler value %s.\" % (x, handler.value())\n\n        class MyModule(UIModule):\n            def render(self, x):\n                return \"In MyModule(%s) with handler value %s.\" % (\n                    x,\n                    self.handler.value(),\n                )\n\n        loader = DictLoader(\n            {\"foo.html\": \"{{ my_ui_method(42) }} {% module MyModule(123) %}\"}\n        )\n        return dict(\n            template_loader=loader,\n            ui_methods={\"my_ui_method\": my_ui_method},\n            ui_modules={\"MyModule\": MyModule},\n        )\n\n    def tearDown(self):\n        super(UIMethodUIModuleTest, self).tearDown()\n        # TODO: fix template loader caching so this isn't necessary.\n        RequestHandler._template_loaders.clear()\n\n    def test_ui_method(self):\n        response = self.fetch(\"/?value=asdf\")\n        self.assertEqual(\n            response.body,\n            b\"In my_ui_method(42) with handler value asdf. \"\n            b\"In MyModule(123) with handler value asdf.\",\n        )\n\n\nclass GetArgumentErrorTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            try:\n                self.get_argument(\"foo\")\n                self.write({})\n            except MissingArgumentError as e:\n                self.write({\"arg_name\": e.arg_name, \"log_message\": e.log_message})\n\n    def test_catch_error(self):\n        response = self.fetch(\"/\")\n        self.assertEqual(\n            json_decode(response.body),\n            {\"arg_name\": \"foo\", \"log_message\": \"Missing argument foo\"},\n        )\n\n\nclass SetLazyPropertiesTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def prepare(self):\n            self.current_user = \"Ben\"\n            self.locale = locale.get(\"en_US\")\n\n        def get_user_locale(self):\n            raise NotImplementedError()\n\n        def get_current_user(self):\n            raise NotImplementedError()\n\n        def get(self):\n            self.write(\"Hello %s (%s)\" % (self.current_user, self.locale.code))\n\n    def test_set_properties(self):\n        # Ensure that current_user can be assigned to normally for apps\n        # that want to forgo the lazy get_current_user property\n        response = self.fetch(\"/\")\n        self.assertEqual(response.body, b\"Hello Ben (en_US)\")\n\n\nclass GetCurrentUserTest(WebTestCase):\n    def get_app_kwargs(self):\n        class WithoutUserModule(UIModule):\n            def render(self):\n                return \"\"\n\n        class WithUserModule(UIModule):\n            def render(self):\n                return str(self.current_user)\n\n        loader = DictLoader(\n            {\n                \"without_user.html\": \"\",\n                \"with_user.html\": \"{{ current_user }}\",\n                \"without_user_module.html\": \"{% module WithoutUserModule() %}\",\n                \"with_user_module.html\": \"{% module WithUserModule() %}\",\n            }\n        )\n        return dict(\n            template_loader=loader,\n            ui_modules={\n                \"WithUserModule\": WithUserModule,\n                \"WithoutUserModule\": WithoutUserModule,\n            },\n        )\n\n    def tearDown(self):\n        super(GetCurrentUserTest, self).tearDown()\n        RequestHandler._template_loaders.clear()\n\n    def get_handlers(self):\n        class CurrentUserHandler(RequestHandler):\n            def prepare(self):\n                self.has_loaded_current_user = False\n\n            def get_current_user(self):\n                self.has_loaded_current_user = True\n                return \"\"\n\n        class WithoutUserHandler(CurrentUserHandler):\n            def get(self):\n                self.render_string(\"without_user.html\")\n                self.finish(str(self.has_loaded_current_user))\n\n        class WithUserHandler(CurrentUserHandler):\n            def get(self):\n                self.render_string(\"with_user.html\")\n                self.finish(str(self.has_loaded_current_user))\n\n        class CurrentUserModuleHandler(CurrentUserHandler):\n            def get_template_namespace(self):\n                # If RequestHandler.get_template_namespace is called, then\n                # get_current_user is evaluated. Until #820 is fixed, this\n                # is a small hack to circumvent the issue.\n                return self.ui\n\n        class WithoutUserModuleHandler(CurrentUserModuleHandler):\n            def get(self):\n                self.render_string(\"without_user_module.html\")\n                self.finish(str(self.has_loaded_current_user))\n\n        class WithUserModuleHandler(CurrentUserModuleHandler):\n            def get(self):\n                self.render_string(\"with_user_module.html\")\n                self.finish(str(self.has_loaded_current_user))\n\n        return [\n            (\"/without_user\", WithoutUserHandler),\n            (\"/with_user\", WithUserHandler),\n            (\"/without_user_module\", WithoutUserModuleHandler),\n            (\"/with_user_module\", WithUserModuleHandler),\n        ]\n\n    @unittest.skip(\"needs fix\")\n    def test_get_current_user_is_lazy(self):\n        # TODO: Make this test pass. See #820.\n        response = self.fetch(\"/without_user\")\n        self.assertEqual(response.body, b\"False\")\n\n    def test_get_current_user_works(self):\n        response = self.fetch(\"/with_user\")\n        self.assertEqual(response.body, b\"True\")\n\n    def test_get_current_user_from_ui_module_is_lazy(self):\n        response = self.fetch(\"/without_user_module\")\n        self.assertEqual(response.body, b\"False\")\n\n    def test_get_current_user_from_ui_module_works(self):\n        response = self.fetch(\"/with_user_module\")\n        self.assertEqual(response.body, b\"True\")\n\n\nclass UnimplementedHTTPMethodsTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        pass\n\n    def test_unimplemented_standard_methods(self):\n        for method in [\"HEAD\", \"GET\", \"DELETE\", \"OPTIONS\"]:\n            response = self.fetch(\"/\", method=method)\n            self.assertEqual(response.code, 405)\n        for method in [\"POST\", \"PUT\"]:\n            response = self.fetch(\"/\", method=method, body=b\"\")\n            self.assertEqual(response.code, 405)\n\n\nclass UnimplementedNonStandardMethodsTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def other(self):\n            # Even though this method exists, it won't get called automatically\n            # because it is not in SUPPORTED_METHODS.\n            self.write(\"other\")\n\n    def test_unimplemented_patch(self):\n        # PATCH is recently standardized; Tornado supports it by default\n        # but wsgiref.validate doesn't like it.\n        response = self.fetch(\"/\", method=\"PATCH\", body=b\"\")\n        self.assertEqual(response.code, 405)\n\n    def test_unimplemented_other(self):\n        response = self.fetch(\"/\", method=\"OTHER\", allow_nonstandard_methods=True)\n        self.assertEqual(response.code, 405)\n\n\nclass AllHTTPMethodsTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def method(self):\n            self.write(self.request.method)\n\n        get = delete = options = post = put = method  # type: ignore\n\n    def test_standard_methods(self):\n        response = self.fetch(\"/\", method=\"HEAD\")\n        self.assertEqual(response.body, b\"\")\n        for method in [\"GET\", \"DELETE\", \"OPTIONS\"]:\n            response = self.fetch(\"/\", method=method)\n            self.assertEqual(response.body, utf8(method))\n        for method in [\"POST\", \"PUT\"]:\n            response = self.fetch(\"/\", method=method, body=b\"\")\n            self.assertEqual(response.body, utf8(method))\n\n\nclass PatchMethodTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        SUPPORTED_METHODS = RequestHandler.SUPPORTED_METHODS + (  # type: ignore\n            \"OTHER\",\n        )\n\n        def patch(self):\n            self.write(\"patch\")\n\n        def other(self):\n            self.write(\"other\")\n\n    def test_patch(self):\n        response = self.fetch(\"/\", method=\"PATCH\", body=b\"\")\n        self.assertEqual(response.body, b\"patch\")\n\n    def test_other(self):\n        response = self.fetch(\"/\", method=\"OTHER\", allow_nonstandard_methods=True)\n        self.assertEqual(response.body, b\"other\")\n\n\nclass FinishInPrepareTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def prepare(self):\n            self.finish(\"done\")\n\n        def get(self):\n            # It's difficult to assert for certain that a method did not\n            # or will not be called in an asynchronous context, but this\n            # will be logged noisily if it is reached.\n            raise Exception(\"should not reach this method\")\n\n    def test_finish_in_prepare(self):\n        response = self.fetch(\"/\")\n        self.assertEqual(response.body, b\"done\")\n\n\nclass Default404Test(WebTestCase):\n    def get_handlers(self):\n        # If there are no handlers at all a default redirect handler gets added.\n        return [(\"/foo\", RequestHandler)]\n\n    def test_404(self):\n        response = self.fetch(\"/\")\n        self.assertEqual(response.code, 404)\n        self.assertEqual(\n            response.body,\n            b\"<html><title>404: Not Found</title>\"\n            b\"<body>404: Not Found</body></html>\",\n        )\n\n\nclass Custom404Test(WebTestCase):\n    def get_handlers(self):\n        return [(\"/foo\", RequestHandler)]\n\n    def get_app_kwargs(self):\n        class Custom404Handler(RequestHandler):\n            def get(self):\n                self.set_status(404)\n                self.write(\"custom 404 response\")\n\n        return dict(default_handler_class=Custom404Handler)\n\n    def test_404(self):\n        response = self.fetch(\"/\")\n        self.assertEqual(response.code, 404)\n        self.assertEqual(response.body, b\"custom 404 response\")\n\n\nclass DefaultHandlerArgumentsTest(WebTestCase):\n    def get_handlers(self):\n        return [(\"/foo\", RequestHandler)]\n\n    def get_app_kwargs(self):\n        return dict(\n            default_handler_class=ErrorHandler,\n            default_handler_args=dict(status_code=403),\n        )\n\n    def test_403(self):\n        response = self.fetch(\"/\")\n        self.assertEqual(response.code, 403)\n\n\nclass HandlerByNameTest(WebTestCase):\n    def get_handlers(self):\n        # All three are equivalent.\n        return [\n            (\"/hello1\", HelloHandler),\n            (\"/hello2\", \"tornado.test.web_test.HelloHandler\"),\n            url(\"/hello3\", \"tornado.test.web_test.HelloHandler\"),\n        ]\n\n    def test_handler_by_name(self):\n        resp = self.fetch(\"/hello1\")\n        self.assertEqual(resp.body, b\"hello\")\n        resp = self.fetch(\"/hello2\")\n        self.assertEqual(resp.body, b\"hello\")\n        resp = self.fetch(\"/hello3\")\n        self.assertEqual(resp.body, b\"hello\")\n\n\nclass StreamingRequestBodyTest(WebTestCase):\n    def get_handlers(self):\n        @stream_request_body\n        class StreamingBodyHandler(RequestHandler):\n            def initialize(self, test):\n                self.test = test\n\n            def prepare(self):\n                self.test.prepared.set_result(None)\n\n            def data_received(self, data):\n                self.test.data.set_result(data)\n\n            def get(self):\n                self.test.finished.set_result(None)\n                self.write({})\n\n        @stream_request_body\n        class EarlyReturnHandler(RequestHandler):\n            def prepare(self):\n                # If we finish the response in prepare, it won't continue to\n                # the (non-existent) data_received.\n                raise HTTPError(401)\n\n        @stream_request_body\n        class CloseDetectionHandler(RequestHandler):\n            def initialize(self, test):\n                self.test = test\n\n            def on_connection_close(self):\n                super(CloseDetectionHandler, self).on_connection_close()\n                self.test.close_future.set_result(None)\n\n        return [\n            (\"/stream_body\", StreamingBodyHandler, dict(test=self)),\n            (\"/early_return\", EarlyReturnHandler),\n            (\"/close_detection\", CloseDetectionHandler, dict(test=self)),\n        ]\n\n    def connect(self, url, connection_close):\n        # Use a raw connection so we can control the sending of data.\n        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)\n        s.connect((\"127.0.0.1\", self.get_http_port()))\n        stream = IOStream(s)\n        stream.write(b\"GET \" + url + b\" HTTP/1.1\\r\\n\")\n        if connection_close:\n            stream.write(b\"Connection: close\\r\\n\")\n        stream.write(b\"Transfer-Encoding: chunked\\r\\n\\r\\n\")\n        return stream\n\n    @gen_test\n    def test_streaming_body(self):\n        self.prepared = Future()  # type: Future[None]\n        self.data = Future()  # type: Future[bytes]\n        self.finished = Future()  # type: Future[None]\n\n        stream = self.connect(b\"/stream_body\", connection_close=True)\n        yield self.prepared\n        stream.write(b\"4\\r\\nasdf\\r\\n\")\n        # Ensure the first chunk is received before we send the second.\n        data = yield self.data\n        self.assertEqual(data, b\"asdf\")\n        self.data = Future()\n        stream.write(b\"4\\r\\nqwer\\r\\n\")\n        data = yield self.data\n        self.assertEquals(data, b\"qwer\")\n        stream.write(b\"0\\r\\n\\r\\n\")\n        yield self.finished\n        data = yield stream.read_until_close()\n        # This would ideally use an HTTP1Connection to read the response.\n        self.assertTrue(data.endswith(b\"{}\"))\n        stream.close()\n\n    @gen_test\n    def test_early_return(self):\n        stream = self.connect(b\"/early_return\", connection_close=False)\n        data = yield stream.read_until_close()\n        self.assertTrue(data.startswith(b\"HTTP/1.1 401\"))\n\n    @gen_test\n    def test_early_return_with_data(self):\n        stream = self.connect(b\"/early_return\", connection_close=False)\n        stream.write(b\"4\\r\\nasdf\\r\\n\")\n        data = yield stream.read_until_close()\n        self.assertTrue(data.startswith(b\"HTTP/1.1 401\"))\n\n    @gen_test\n    def test_close_during_upload(self):\n        self.close_future = Future()  # type: Future[None]\n        stream = self.connect(b\"/close_detection\", connection_close=False)\n        stream.close()\n        yield self.close_future\n\n\n# Each method in this handler returns a yieldable object and yields to the\n# IOLoop so the future is not immediately ready.  Ensure that the\n# yieldables are respected and no method is called before the previous\n# one has completed.\n@stream_request_body\nclass BaseFlowControlHandler(RequestHandler):\n    def initialize(self, test):\n        self.test = test\n        self.method = None\n        self.methods = []  # type: typing.List[str]\n\n    @contextlib.contextmanager\n    def in_method(self, method):\n        if self.method is not None:\n            self.test.fail(\"entered method %s while in %s\" % (method, self.method))\n        self.method = method\n        self.methods.append(method)\n        try:\n            yield\n        finally:\n            self.method = None\n\n    @gen.coroutine\n    def prepare(self):\n        # Note that asynchronous prepare() does not block data_received,\n        # so we don't use in_method here.\n        self.methods.append(\"prepare\")\n        yield gen.moment\n\n    @gen.coroutine\n    def post(self):\n        with self.in_method(\"post\"):\n            yield gen.moment\n        self.write(dict(methods=self.methods))\n\n\nclass BaseStreamingRequestFlowControlTest(object):\n    def get_httpserver_options(self):\n        # Use a small chunk size so flow control is relevant even though\n        # all the data arrives at once.\n        return dict(chunk_size=10, decompress_request=True)\n\n    def get_http_client(self):\n        # simple_httpclient only: curl doesn't support body_producer.\n        return SimpleAsyncHTTPClient()\n\n    # Test all the slightly different code paths for fixed, chunked, etc bodies.\n    def test_flow_control_fixed_body(self):\n        response = self.fetch(\"/\", body=\"abcdefghijklmnopqrstuvwxyz\", method=\"POST\")\n        response.rethrow()\n        self.assertEqual(\n            json_decode(response.body),\n            dict(\n                methods=[\n                    \"prepare\",\n                    \"data_received\",\n                    \"data_received\",\n                    \"data_received\",\n                    \"post\",\n                ]\n            ),\n        )\n\n    def test_flow_control_chunked_body(self):\n        chunks = [b\"abcd\", b\"efgh\", b\"ijkl\"]\n\n        @gen.coroutine\n        def body_producer(write):\n            for i in chunks:\n                yield write(i)\n\n        response = self.fetch(\"/\", body_producer=body_producer, method=\"POST\")\n        response.rethrow()\n        self.assertEqual(\n            json_decode(response.body),\n            dict(\n                methods=[\n                    \"prepare\",\n                    \"data_received\",\n                    \"data_received\",\n                    \"data_received\",\n                    \"post\",\n                ]\n            ),\n        )\n\n    def test_flow_control_compressed_body(self):\n        bytesio = BytesIO()\n        gzip_file = gzip.GzipFile(mode=\"w\", fileobj=bytesio)\n        gzip_file.write(b\"abcdefghijklmnopqrstuvwxyz\")\n        gzip_file.close()\n        compressed_body = bytesio.getvalue()\n        response = self.fetch(\n            \"/\",\n            body=compressed_body,\n            method=\"POST\",\n            headers={\"Content-Encoding\": \"gzip\"},\n        )\n        response.rethrow()\n        self.assertEqual(\n            json_decode(response.body),\n            dict(\n                methods=[\n                    \"prepare\",\n                    \"data_received\",\n                    \"data_received\",\n                    \"data_received\",\n                    \"post\",\n                ]\n            ),\n        )\n\n\nclass DecoratedStreamingRequestFlowControlTest(\n    BaseStreamingRequestFlowControlTest, WebTestCase\n):\n    def get_handlers(self):\n        class DecoratedFlowControlHandler(BaseFlowControlHandler):\n            @gen.coroutine\n            def data_received(self, data):\n                with self.in_method(\"data_received\"):\n                    yield gen.moment\n\n        return [(\"/\", DecoratedFlowControlHandler, dict(test=self))]\n\n\nclass NativeStreamingRequestFlowControlTest(\n    BaseStreamingRequestFlowControlTest, WebTestCase\n):\n    def get_handlers(self):\n        class NativeFlowControlHandler(BaseFlowControlHandler):\n            async def data_received(self, data):\n                with self.in_method(\"data_received\"):\n                    import asyncio\n\n                    await asyncio.sleep(0)\n\n        return [(\"/\", NativeFlowControlHandler, dict(test=self))]\n\n\nclass IncorrectContentLengthTest(SimpleHandlerTestCase):\n    def get_handlers(self):\n        test = self\n        self.server_error = None\n\n        # Manually set a content-length that doesn't match the actual content.\n        class TooHigh(RequestHandler):\n            def get(self):\n                self.set_header(\"Content-Length\", \"42\")\n                try:\n                    self.finish(\"ok\")\n                except Exception as e:\n                    test.server_error = e\n                    raise\n\n        class TooLow(RequestHandler):\n            def get(self):\n                self.set_header(\"Content-Length\", \"2\")\n                try:\n                    self.finish(\"hello\")\n                except Exception as e:\n                    test.server_error = e\n                    raise\n\n        return [(\"/high\", TooHigh), (\"/low\", TooLow)]\n\n    def test_content_length_too_high(self):\n        # When the content-length is too high, the connection is simply\n        # closed without completing the response.  An error is logged on\n        # the server.\n        with ExpectLog(app_log, \"(Uncaught exception|Exception in callback)\"):\n            with ExpectLog(\n                gen_log,\n                \"(Cannot send error response after headers written\"\n                \"|Failed to flush partial response)\",\n            ):\n                with self.assertRaises(HTTPClientError):\n                    self.fetch(\"/high\", raise_error=True)\n        self.assertEqual(\n            str(self.server_error), \"Tried to write 40 bytes less than Content-Length\"\n        )\n\n    def test_content_length_too_low(self):\n        # When the content-length is too low, the connection is closed\n        # without writing the last chunk, so the client never sees the request\n        # complete (which would be a framing error).\n        with ExpectLog(app_log, \"(Uncaught exception|Exception in callback)\"):\n            with ExpectLog(\n                gen_log,\n                \"(Cannot send error response after headers written\"\n                \"|Failed to flush partial response)\",\n            ):\n                with self.assertRaises(HTTPClientError):\n                    self.fetch(\"/low\", raise_error=True)\n        self.assertEqual(\n            str(self.server_error), \"Tried to write more data than Content-Length\"\n        )\n\n\nclass ClientCloseTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            if self.request.version.startswith(\"HTTP/1\"):\n                # Simulate a connection closed by the client during\n                # request processing.  The client will see an error, but the\n                # server should respond gracefully (without logging errors\n                # because we were unable to write out as many bytes as\n                # Content-Length said we would)\n                self.request.connection.stream.close()\n                self.write(\"hello\")\n            else:\n                # TODO: add a HTTP2-compatible version of this test.\n                self.write(\"requires HTTP/1.x\")\n\n    def test_client_close(self):\n        with self.assertRaises((HTTPClientError, unittest.SkipTest)):\n            response = self.fetch(\"/\", raise_error=True)\n            if response.body == b\"requires HTTP/1.x\":\n                self.skipTest(\"requires HTTP/1.x\")\n            self.assertEqual(response.code, 599)\n\n\nclass SignedValueTest(unittest.TestCase):\n    SECRET = \"It's a secret to everybody\"\n    SECRET_DICT = {0: \"asdfbasdf\", 1: \"12312312\", 2: \"2342342\"}\n\n    def past(self):\n        return self.present() - 86400 * 32\n\n    def present(self):\n        return 1300000000\n\n    def test_known_values(self):\n        signed_v1 = create_signed_value(\n            SignedValueTest.SECRET, \"key\", \"value\", version=1, clock=self.present\n        )\n        self.assertEqual(\n            signed_v1, b\"dmFsdWU=|1300000000|31c934969f53e48164c50768b40cbd7e2daaaa4f\"\n        )\n\n        signed_v2 = create_signed_value(\n            SignedValueTest.SECRET, \"key\", \"value\", version=2, clock=self.present\n        )\n        self.assertEqual(\n            signed_v2,\n            b\"2|1:0|10:1300000000|3:key|8:dmFsdWU=|\"\n            b\"3d4e60b996ff9c5d5788e333a0cba6f238a22c6c0f94788870e1a9ecd482e152\",\n        )\n\n        signed_default = create_signed_value(\n            SignedValueTest.SECRET, \"key\", \"value\", clock=self.present\n        )\n        self.assertEqual(signed_default, signed_v2)\n\n        decoded_v1 = decode_signed_value(\n            SignedValueTest.SECRET, \"key\", signed_v1, min_version=1, clock=self.present\n        )\n        self.assertEqual(decoded_v1, b\"value\")\n\n        decoded_v2 = decode_signed_value(\n            SignedValueTest.SECRET, \"key\", signed_v2, min_version=2, clock=self.present\n        )\n        self.assertEqual(decoded_v2, b\"value\")\n\n    def test_name_swap(self):\n        signed1 = create_signed_value(\n            SignedValueTest.SECRET, \"key1\", \"value\", clock=self.present\n        )\n        signed2 = create_signed_value(\n            SignedValueTest.SECRET, \"key2\", \"value\", clock=self.present\n        )\n        # Try decoding each string with the other's \"name\"\n        decoded1 = decode_signed_value(\n            SignedValueTest.SECRET, \"key2\", signed1, clock=self.present\n        )\n        self.assertIs(decoded1, None)\n        decoded2 = decode_signed_value(\n            SignedValueTest.SECRET, \"key1\", signed2, clock=self.present\n        )\n        self.assertIs(decoded2, None)\n\n    def test_expired(self):\n        signed = create_signed_value(\n            SignedValueTest.SECRET, \"key1\", \"value\", clock=self.past\n        )\n        decoded_past = decode_signed_value(\n            SignedValueTest.SECRET, \"key1\", signed, clock=self.past\n        )\n        self.assertEqual(decoded_past, b\"value\")\n        decoded_present = decode_signed_value(\n            SignedValueTest.SECRET, \"key1\", signed, clock=self.present\n        )\n        self.assertIs(decoded_present, None)\n\n    def test_payload_tampering(self):\n        # These cookies are variants of the one in test_known_values.\n        sig = \"3d4e60b996ff9c5d5788e333a0cba6f238a22c6c0f94788870e1a9ecd482e152\"\n\n        def validate(prefix):\n            return b\"value\" == decode_signed_value(\n                SignedValueTest.SECRET, \"key\", prefix + sig, clock=self.present\n            )\n\n        self.assertTrue(validate(\"2|1:0|10:1300000000|3:key|8:dmFsdWU=|\"))\n        # Change key version\n        self.assertFalse(validate(\"2|1:1|10:1300000000|3:key|8:dmFsdWU=|\"))\n        # length mismatch (field too short)\n        self.assertFalse(validate(\"2|1:0|10:130000000|3:key|8:dmFsdWU=|\"))\n        # length mismatch (field too long)\n        self.assertFalse(validate(\"2|1:0|10:1300000000|3:keey|8:dmFsdWU=|\"))\n\n    def test_signature_tampering(self):\n        prefix = \"2|1:0|10:1300000000|3:key|8:dmFsdWU=|\"\n\n        def validate(sig):\n            return b\"value\" == decode_signed_value(\n                SignedValueTest.SECRET, \"key\", prefix + sig, clock=self.present\n            )\n\n        self.assertTrue(\n            validate(\"3d4e60b996ff9c5d5788e333a0cba6f238a22c6c0f94788870e1a9ecd482e152\")\n        )\n        # All zeros\n        self.assertFalse(validate(\"0\" * 32))\n        # Change one character\n        self.assertFalse(\n            validate(\"4d4e60b996ff9c5d5788e333a0cba6f238a22c6c0f94788870e1a9ecd482e152\")\n        )\n        # Change another character\n        self.assertFalse(\n            validate(\"3d4e60b996ff9c5d5788e333a0cba6f238a22c6c0f94788870e1a9ecd482e153\")\n        )\n        # Truncate\n        self.assertFalse(\n            validate(\"3d4e60b996ff9c5d5788e333a0cba6f238a22c6c0f94788870e1a9ecd482e15\")\n        )\n        # Lengthen\n        self.assertFalse(\n            validate(\n                \"3d4e60b996ff9c5d5788e333a0cba6f238a22c6c0f94788870e1a9ecd482e1538\"\n            )\n        )\n\n    def test_non_ascii(self):\n        value = b\"\\xe9\"\n        signed = create_signed_value(\n            SignedValueTest.SECRET, \"key\", value, clock=self.present\n        )\n        decoded = decode_signed_value(\n            SignedValueTest.SECRET, \"key\", signed, clock=self.present\n        )\n        self.assertEqual(value, decoded)\n\n    def test_key_versioning_read_write_default_key(self):\n        value = b\"\\xe9\"\n        signed = create_signed_value(\n            SignedValueTest.SECRET_DICT, \"key\", value, clock=self.present, key_version=0\n        )\n        decoded = decode_signed_value(\n            SignedValueTest.SECRET_DICT, \"key\", signed, clock=self.present\n        )\n        self.assertEqual(value, decoded)\n\n    def test_key_versioning_read_write_non_default_key(self):\n        value = b\"\\xe9\"\n        signed = create_signed_value(\n            SignedValueTest.SECRET_DICT, \"key\", value, clock=self.present, key_version=1\n        )\n        decoded = decode_signed_value(\n            SignedValueTest.SECRET_DICT, \"key\", signed, clock=self.present\n        )\n        self.assertEqual(value, decoded)\n\n    def test_key_versioning_invalid_key(self):\n        value = b\"\\xe9\"\n        signed = create_signed_value(\n            SignedValueTest.SECRET_DICT, \"key\", value, clock=self.present, key_version=0\n        )\n        newkeys = SignedValueTest.SECRET_DICT.copy()\n        newkeys.pop(0)\n        decoded = decode_signed_value(newkeys, \"key\", signed, clock=self.present)\n        self.assertEqual(None, decoded)\n\n    def test_key_version_retrieval(self):\n        value = b\"\\xe9\"\n        signed = create_signed_value(\n            SignedValueTest.SECRET_DICT, \"key\", value, clock=self.present, key_version=1\n        )\n        key_version = get_signature_key_version(signed)\n        self.assertEqual(1, key_version)\n\n\nclass XSRFTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            version = int(self.get_argument(\"version\", \"2\"))\n            # This would be a bad idea in a real app, but in this test\n            # it's fine.\n            self.settings[\"xsrf_cookie_version\"] = version\n            self.write(self.xsrf_token)\n\n        def post(self):\n            self.write(\"ok\")\n\n    def get_app_kwargs(self):\n        return dict(xsrf_cookies=True)\n\n    def setUp(self):\n        super(XSRFTest, self).setUp()\n        self.xsrf_token = self.get_token()\n\n    def get_token(self, old_token=None, version=None):\n        if old_token is not None:\n            headers = self.cookie_headers(old_token)\n        else:\n            headers = None\n        response = self.fetch(\n            \"/\" if version is None else (\"/?version=%d\" % version), headers=headers\n        )\n        response.rethrow()\n        return native_str(response.body)\n\n    def cookie_headers(self, token=None):\n        if token is None:\n            token = self.xsrf_token\n        return {\"Cookie\": \"_xsrf=\" + token}\n\n    def test_xsrf_fail_no_token(self):\n        with ExpectLog(gen_log, \".*'_xsrf' argument missing\"):\n            response = self.fetch(\"/\", method=\"POST\", body=b\"\")\n        self.assertEqual(response.code, 403)\n\n    def test_xsrf_fail_body_no_cookie(self):\n        with ExpectLog(gen_log, \".*XSRF cookie does not match POST\"):\n            response = self.fetch(\n                \"/\",\n                method=\"POST\",\n                body=urllib.parse.urlencode(dict(_xsrf=self.xsrf_token)),\n            )\n        self.assertEqual(response.code, 403)\n\n    def test_xsrf_fail_argument_invalid_format(self):\n        with ExpectLog(gen_log, \".*'_xsrf' argument has invalid format\"):\n            response = self.fetch(\n                \"/\",\n                method=\"POST\",\n                headers=self.cookie_headers(),\n                body=urllib.parse.urlencode(dict(_xsrf=\"3|\")),\n            )\n        self.assertEqual(response.code, 403)\n\n    def test_xsrf_fail_cookie_invalid_format(self):\n        with ExpectLog(gen_log, \".*XSRF cookie does not match POST\"):\n            response = self.fetch(\n                \"/\",\n                method=\"POST\",\n                headers=self.cookie_headers(token=\"3|\"),\n                body=urllib.parse.urlencode(dict(_xsrf=self.xsrf_token)),\n            )\n        self.assertEqual(response.code, 403)\n\n    def test_xsrf_fail_cookie_no_body(self):\n        with ExpectLog(gen_log, \".*'_xsrf' argument missing\"):\n            response = self.fetch(\n                \"/\", method=\"POST\", body=b\"\", headers=self.cookie_headers()\n            )\n        self.assertEqual(response.code, 403)\n\n    def test_xsrf_success_short_token(self):\n        response = self.fetch(\n            \"/\",\n            method=\"POST\",\n            body=urllib.parse.urlencode(dict(_xsrf=\"deadbeef\")),\n            headers=self.cookie_headers(token=\"deadbeef\"),\n        )\n        self.assertEqual(response.code, 200)\n\n    def test_xsrf_success_non_hex_token(self):\n        response = self.fetch(\n            \"/\",\n            method=\"POST\",\n            body=urllib.parse.urlencode(dict(_xsrf=\"xoxo\")),\n            headers=self.cookie_headers(token=\"xoxo\"),\n        )\n        self.assertEqual(response.code, 200)\n\n    def test_xsrf_success_post_body(self):\n        response = self.fetch(\n            \"/\",\n            method=\"POST\",\n            body=urllib.parse.urlencode(dict(_xsrf=self.xsrf_token)),\n            headers=self.cookie_headers(),\n        )\n        self.assertEqual(response.code, 200)\n\n    def test_xsrf_success_query_string(self):\n        response = self.fetch(\n            \"/?\" + urllib.parse.urlencode(dict(_xsrf=self.xsrf_token)),\n            method=\"POST\",\n            body=b\"\",\n            headers=self.cookie_headers(),\n        )\n        self.assertEqual(response.code, 200)\n\n    def test_xsrf_success_header(self):\n        response = self.fetch(\n            \"/\",\n            method=\"POST\",\n            body=b\"\",\n            headers=dict(\n                {\"X-Xsrftoken\": self.xsrf_token},  # type: ignore\n                **self.cookie_headers()\n            ),\n        )\n        self.assertEqual(response.code, 200)\n\n    def test_distinct_tokens(self):\n        # Every request gets a distinct token.\n        NUM_TOKENS = 10\n        tokens = set()\n        for i in range(NUM_TOKENS):\n            tokens.add(self.get_token())\n        self.assertEqual(len(tokens), NUM_TOKENS)\n\n    def test_cross_user(self):\n        token2 = self.get_token()\n        # Each token can be used to authenticate its own request.\n        for token in (self.xsrf_token, token2):\n            response = self.fetch(\n                \"/\",\n                method=\"POST\",\n                body=urllib.parse.urlencode(dict(_xsrf=token)),\n                headers=self.cookie_headers(token),\n            )\n            self.assertEqual(response.code, 200)\n        # Sending one in the cookie and the other in the body is not allowed.\n        for cookie_token, body_token in (\n            (self.xsrf_token, token2),\n            (token2, self.xsrf_token),\n        ):\n            with ExpectLog(gen_log, \".*XSRF cookie does not match POST\"):\n                response = self.fetch(\n                    \"/\",\n                    method=\"POST\",\n                    body=urllib.parse.urlencode(dict(_xsrf=body_token)),\n                    headers=self.cookie_headers(cookie_token),\n                )\n            self.assertEqual(response.code, 403)\n\n    def test_refresh_token(self):\n        token = self.xsrf_token\n        tokens_seen = set([token])\n        # A user's token is stable over time.  Refreshing the page in one tab\n        # might update the cookie while an older tab still has the old cookie\n        # in its DOM.  Simulate this scenario by passing a constant token\n        # in the body and re-querying for the token.\n        for i in range(5):\n            token = self.get_token(token)\n            # Tokens are encoded uniquely each time\n            tokens_seen.add(token)\n            response = self.fetch(\n                \"/\",\n                method=\"POST\",\n                body=urllib.parse.urlencode(dict(_xsrf=self.xsrf_token)),\n                headers=self.cookie_headers(token),\n            )\n            self.assertEqual(response.code, 200)\n        self.assertEqual(len(tokens_seen), 6)\n\n    def test_versioning(self):\n        # Version 1 still produces distinct tokens per request.\n        self.assertNotEqual(self.get_token(version=1), self.get_token(version=1))\n\n        # Refreshed v1 tokens are all identical.\n        v1_token = self.get_token(version=1)\n        for i in range(5):\n            self.assertEqual(self.get_token(v1_token, version=1), v1_token)\n\n        # Upgrade to a v2 version of the same token\n        v2_token = self.get_token(v1_token)\n        self.assertNotEqual(v1_token, v2_token)\n        # Each v1 token can map to many v2 tokens.\n        self.assertNotEqual(v2_token, self.get_token(v1_token))\n\n        # The tokens are cross-compatible.\n        for cookie_token, body_token in ((v1_token, v2_token), (v2_token, v1_token)):\n            response = self.fetch(\n                \"/\",\n                method=\"POST\",\n                body=urllib.parse.urlencode(dict(_xsrf=body_token)),\n                headers=self.cookie_headers(cookie_token),\n            )\n            self.assertEqual(response.code, 200)\n\n\nclass XSRFCookieKwargsTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            self.write(self.xsrf_token)\n\n    def get_app_kwargs(self):\n        return dict(\n            xsrf_cookies=True, xsrf_cookie_kwargs=dict(httponly=True, expires_days=2)\n        )\n\n    def test_xsrf_httponly(self):\n        response = self.fetch(\"/\")\n        self.assertIn(\"httponly;\", response.headers[\"Set-Cookie\"].lower())\n        self.assertIn(\"expires=\", response.headers[\"Set-Cookie\"].lower())\n        header = response.headers.get(\"Set-Cookie\")\n        match = re.match(\".*; expires=(?P<expires>.+);.*\", header)\n        assert match is not None\n\n        expires = datetime.datetime.utcnow() + datetime.timedelta(days=2)\n        parsed = email.utils.parsedate(match.groupdict()[\"expires\"])\n        assert parsed is not None\n        header_expires = datetime.datetime(*parsed[:6])\n        self.assertTrue(abs((expires - header_expires).total_seconds()) < 10)\n\n\nclass FinishExceptionTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            self.set_status(401)\n            self.set_header(\"WWW-Authenticate\", 'Basic realm=\"something\"')\n            if self.get_argument(\"finish_value\", \"\"):\n                raise Finish(\"authentication required\")\n            else:\n                self.write(\"authentication required\")\n                raise Finish()\n\n    def test_finish_exception(self):\n        for u in [\"/\", \"/?finish_value=1\"]:\n            response = self.fetch(u)\n            self.assertEqual(response.code, 401)\n            self.assertEqual(\n                'Basic realm=\"something\"', response.headers.get(\"WWW-Authenticate\")\n            )\n            self.assertEqual(b\"authentication required\", response.body)\n\n\nclass DecoratorTest(WebTestCase):\n    def get_handlers(self):\n        class RemoveSlashHandler(RequestHandler):\n            @removeslash\n            def get(self):\n                pass\n\n        class AddSlashHandler(RequestHandler):\n            @addslash\n            def get(self):\n                pass\n\n        return [(\"/removeslash/\", RemoveSlashHandler), (\"/addslash\", AddSlashHandler)]\n\n    def test_removeslash(self):\n        response = self.fetch(\"/removeslash/\", follow_redirects=False)\n        self.assertEqual(response.code, 301)\n        self.assertEqual(response.headers[\"Location\"], \"/removeslash\")\n\n        response = self.fetch(\"/removeslash/?foo=bar\", follow_redirects=False)\n        self.assertEqual(response.code, 301)\n        self.assertEqual(response.headers[\"Location\"], \"/removeslash?foo=bar\")\n\n    def test_addslash(self):\n        response = self.fetch(\"/addslash\", follow_redirects=False)\n        self.assertEqual(response.code, 301)\n        self.assertEqual(response.headers[\"Location\"], \"/addslash/\")\n\n        response = self.fetch(\"/addslash?foo=bar\", follow_redirects=False)\n        self.assertEqual(response.code, 301)\n        self.assertEqual(response.headers[\"Location\"], \"/addslash/?foo=bar\")\n\n\nclass CacheTest(WebTestCase):\n    def get_handlers(self):\n        class EtagHandler(RequestHandler):\n            def get(self, computed_etag):\n                self.write(computed_etag)\n\n            def compute_etag(self):\n                return self._write_buffer[0]\n\n        return [(\"/etag/(.*)\", EtagHandler)]\n\n    def test_wildcard_etag(self):\n        computed_etag = '\"xyzzy\"'\n        etags = \"*\"\n        self._test_etag(computed_etag, etags, 304)\n\n    def test_strong_etag_match(self):\n        computed_etag = '\"xyzzy\"'\n        etags = '\"xyzzy\"'\n        self._test_etag(computed_etag, etags, 304)\n\n    def test_multiple_strong_etag_match(self):\n        computed_etag = '\"xyzzy1\"'\n        etags = '\"xyzzy1\", \"xyzzy2\"'\n        self._test_etag(computed_etag, etags, 304)\n\n    def test_strong_etag_not_match(self):\n        computed_etag = '\"xyzzy\"'\n        etags = '\"xyzzy1\"'\n        self._test_etag(computed_etag, etags, 200)\n\n    def test_multiple_strong_etag_not_match(self):\n        computed_etag = '\"xyzzy\"'\n        etags = '\"xyzzy1\", \"xyzzy2\"'\n        self._test_etag(computed_etag, etags, 200)\n\n    def test_weak_etag_match(self):\n        computed_etag = '\"xyzzy1\"'\n        etags = 'W/\"xyzzy1\"'\n        self._test_etag(computed_etag, etags, 304)\n\n    def test_multiple_weak_etag_match(self):\n        computed_etag = '\"xyzzy2\"'\n        etags = 'W/\"xyzzy1\", W/\"xyzzy2\"'\n        self._test_etag(computed_etag, etags, 304)\n\n    def test_weak_etag_not_match(self):\n        computed_etag = '\"xyzzy2\"'\n        etags = 'W/\"xyzzy1\"'\n        self._test_etag(computed_etag, etags, 200)\n\n    def test_multiple_weak_etag_not_match(self):\n        computed_etag = '\"xyzzy3\"'\n        etags = 'W/\"xyzzy1\", W/\"xyzzy2\"'\n        self._test_etag(computed_etag, etags, 200)\n\n    def _test_etag(self, computed_etag, etags, status_code):\n        response = self.fetch(\n            \"/etag/\" + computed_etag, headers={\"If-None-Match\": etags}\n        )\n        self.assertEqual(response.code, status_code)\n\n\nclass RequestSummaryTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            # remote_ip is optional, although it's set by\n            # both HTTPServer and WSGIAdapter.\n            # Clobber it to make sure it doesn't break logging.\n            self.request.remote_ip = None\n            self.finish(self._request_summary())\n\n    def test_missing_remote_ip(self):\n        resp = self.fetch(\"/\")\n        self.assertEqual(resp.body, b\"GET / (None)\")\n\n\nclass HTTPErrorTest(unittest.TestCase):\n    def test_copy(self):\n        e = HTTPError(403, reason=\"Go away\")\n        e2 = copy.copy(e)\n        self.assertIsNot(e, e2)\n        self.assertEqual(e.status_code, e2.status_code)\n        self.assertEqual(e.reason, e2.reason)\n\n\nclass ApplicationTest(AsyncTestCase):\n    def test_listen(self):\n        app = Application([])\n        server = app.listen(0, address=\"127.0.0.1\")\n        server.stop()\n\n\nclass URLSpecReverseTest(unittest.TestCase):\n    def test_reverse(self):\n        self.assertEqual(\"/favicon.ico\", url(r\"/favicon\\.ico\", None).reverse())\n        self.assertEqual(\"/favicon.ico\", url(r\"^/favicon\\.ico$\", None).reverse())\n\n    def test_non_reversible(self):\n        # URLSpecs are non-reversible if they include non-constant\n        # regex features outside capturing groups. Currently, this is\n        # only strictly enforced for backslash-escaped character\n        # classes.\n        paths = [r\"^/api/v\\d+/foo/(\\w+)$\"]\n        for path in paths:\n            # A URLSpec can still be created even if it cannot be reversed.\n            url_spec = url(path, None)\n            try:\n                result = url_spec.reverse()\n                self.fail(\n                    \"did not get expected exception when reversing %s. \"\n                    \"result: %s\" % (path, result)\n                )\n            except ValueError:\n                pass\n\n    def test_reverse_arguments(self):\n        self.assertEqual(\n            \"/api/v1/foo/bar\", url(r\"^/api/v1/foo/(\\w+)$\", None).reverse(\"bar\")\n        )\n\n\nclass RedirectHandlerTest(WebTestCase):\n    def get_handlers(self):\n        return [\n            (\"/src\", WebRedirectHandler, {\"url\": \"/dst\"}),\n            (\"/src2\", WebRedirectHandler, {\"url\": \"/dst2?foo=bar\"}),\n            (r\"/(.*?)/(.*?)/(.*)\", WebRedirectHandler, {\"url\": \"/{1}/{0}/{2}\"}),\n        ]\n\n    def test_basic_redirect(self):\n        response = self.fetch(\"/src\", follow_redirects=False)\n        self.assertEqual(response.code, 301)\n        self.assertEqual(response.headers[\"Location\"], \"/dst\")\n\n    def test_redirect_with_argument(self):\n        response = self.fetch(\"/src?foo=bar\", follow_redirects=False)\n        self.assertEqual(response.code, 301)\n        self.assertEqual(response.headers[\"Location\"], \"/dst?foo=bar\")\n\n    def test_redirect_with_appending_argument(self):\n        response = self.fetch(\"/src2?foo2=bar2\", follow_redirects=False)\n        self.assertEqual(response.code, 301)\n        self.assertEqual(response.headers[\"Location\"], \"/dst2?foo=bar&foo2=bar2\")\n\n    def test_redirect_pattern(self):\n        response = self.fetch(\"/a/b/c\", follow_redirects=False)\n        self.assertEqual(response.code, 301)\n        self.assertEqual(response.headers[\"Location\"], \"/b/a/c\")\n",
            "new_file_content": "from tornado.concurrent import Future\nfrom tornado import gen\nfrom tornado.escape import (\n    json_decode,\n    utf8,\n    to_unicode,\n    recursive_unicode,\n    native_str,\n    to_basestring,\n)\nfrom tornado.httpclient import HTTPClientError\nfrom tornado.httputil import format_timestamp\nfrom tornado.iostream import IOStream\nfrom tornado import locale\nfrom tornado.locks import Event\nfrom tornado.log import app_log, gen_log\nfrom tornado.simple_httpclient import SimpleAsyncHTTPClient\nfrom tornado.template import DictLoader\nfrom tornado.testing import AsyncHTTPTestCase, AsyncTestCase, ExpectLog, gen_test\nfrom tornado.util import ObjectDict, unicode_type\nfrom tornado.web import (\n    Application,\n    RequestHandler,\n    StaticFileHandler,\n    RedirectHandler as WebRedirectHandler,\n    HTTPError,\n    MissingArgumentError,\n    ErrorHandler,\n    authenticated,\n    url,\n    _create_signature_v1,\n    create_signed_value,\n    decode_signed_value,\n    get_signature_key_version,\n    UIModule,\n    Finish,\n    stream_request_body,\n    removeslash,\n    addslash,\n    GZipContentEncoding,\n)\n\nimport binascii\nimport contextlib\nimport copy\nimport datetime\nimport email.utils\nimport gzip\nfrom io import BytesIO\nimport itertools\nimport logging\nimport os\nimport re\nimport socket\nimport typing  # noqa: F401\nimport unittest\nimport urllib.parse\n\n\ndef relpath(*a):\n    return os.path.join(os.path.dirname(__file__), *a)\n\n\nclass WebTestCase(AsyncHTTPTestCase):\n    \"\"\"Base class for web tests that also supports WSGI mode.\n\n    Override get_handlers and get_app_kwargs instead of get_app.\n    This class is deprecated since WSGI mode is no longer supported.\n    \"\"\"\n\n    def get_app(self):\n        self.app = Application(self.get_handlers(), **self.get_app_kwargs())\n        return self.app\n\n    def get_handlers(self):\n        raise NotImplementedError()\n\n    def get_app_kwargs(self):\n        return {}\n\n\nclass SimpleHandlerTestCase(WebTestCase):\n    \"\"\"Simplified base class for tests that work with a single handler class.\n\n    To use, define a nested class named ``Handler``.\n    \"\"\"\n\n    def get_handlers(self):\n        return [(\"/\", self.Handler)]\n\n\nclass HelloHandler(RequestHandler):\n    def get(self):\n        self.write(\"hello\")\n\n\nclass CookieTestRequestHandler(RequestHandler):\n    # stub out enough methods to make the secure_cookie functions work\n    def __init__(self, cookie_secret=\"0123456789\", key_version=None):\n        # don't call super.__init__\n        self._cookies = {}  # type: typing.Dict[str, bytes]\n        if key_version is None:\n            self.application = ObjectDict(settings=dict(cookie_secret=cookie_secret))\n        else:\n            self.application = ObjectDict(\n                settings=dict(cookie_secret=cookie_secret, key_version=key_version)\n            )\n\n    def get_cookie(self, name):\n        return self._cookies.get(name)\n\n    def set_cookie(self, name, value, expires_days=None):\n        self._cookies[name] = value\n\n\n# See SignedValueTest below for more.\nclass SecureCookieV1Test(unittest.TestCase):\n    def test_round_trip(self):\n        handler = CookieTestRequestHandler()\n        handler.set_secure_cookie(\"foo\", b\"bar\", version=1)\n        self.assertEqual(handler.get_secure_cookie(\"foo\", min_version=1), b\"bar\")\n\n    def test_cookie_tampering_future_timestamp(self):\n        handler = CookieTestRequestHandler()\n        # this string base64-encodes to '12345678'\n        handler.set_secure_cookie(\"foo\", binascii.a2b_hex(b\"d76df8e7aefc\"), version=1)\n        cookie = handler._cookies[\"foo\"]\n        match = re.match(br\"12345678\\|([0-9]+)\\|([0-9a-f]+)\", cookie)\n        assert match is not None\n        timestamp = match.group(1)\n        sig = match.group(2)\n        self.assertEqual(\n            _create_signature_v1(\n                handler.application.settings[\"cookie_secret\"],\n                \"foo\",\n                \"12345678\",\n                timestamp,\n            ),\n            sig,\n        )\n        # shifting digits from payload to timestamp doesn't alter signature\n        # (this is not desirable behavior, just confirming that that's how it\n        # works)\n        self.assertEqual(\n            _create_signature_v1(\n                handler.application.settings[\"cookie_secret\"],\n                \"foo\",\n                \"1234\",\n                b\"5678\" + timestamp,\n            ),\n            sig,\n        )\n        # tamper with the cookie\n        handler._cookies[\"foo\"] = utf8(\n            \"1234|5678%s|%s\" % (to_basestring(timestamp), to_basestring(sig))\n        )\n        # it gets rejected\n        with ExpectLog(gen_log, \"Cookie timestamp in future\"):\n            self.assertTrue(handler.get_secure_cookie(\"foo\", min_version=1) is None)\n\n    def test_arbitrary_bytes(self):\n        # Secure cookies accept arbitrary data (which is base64 encoded).\n        # Note that normal cookies accept only a subset of ascii.\n        handler = CookieTestRequestHandler()\n        handler.set_secure_cookie(\"foo\", b\"\\xe9\", version=1)\n        self.assertEqual(handler.get_secure_cookie(\"foo\", min_version=1), b\"\\xe9\")\n\n\n# See SignedValueTest below for more.\nclass SecureCookieV2Test(unittest.TestCase):\n    KEY_VERSIONS = {0: \"ajklasdf0ojaisdf\", 1: \"aslkjasaolwkjsdf\"}\n\n    def test_round_trip(self):\n        handler = CookieTestRequestHandler()\n        handler.set_secure_cookie(\"foo\", b\"bar\", version=2)\n        self.assertEqual(handler.get_secure_cookie(\"foo\", min_version=2), b\"bar\")\n\n    def test_key_version_roundtrip(self):\n        handler = CookieTestRequestHandler(\n            cookie_secret=self.KEY_VERSIONS, key_version=0\n        )\n        handler.set_secure_cookie(\"foo\", b\"bar\")\n        self.assertEqual(handler.get_secure_cookie(\"foo\"), b\"bar\")\n\n    def test_key_version_roundtrip_differing_version(self):\n        handler = CookieTestRequestHandler(\n            cookie_secret=self.KEY_VERSIONS, key_version=1\n        )\n        handler.set_secure_cookie(\"foo\", b\"bar\")\n        self.assertEqual(handler.get_secure_cookie(\"foo\"), b\"bar\")\n\n    def test_key_version_increment_version(self):\n        handler = CookieTestRequestHandler(\n            cookie_secret=self.KEY_VERSIONS, key_version=0\n        )\n        handler.set_secure_cookie(\"foo\", b\"bar\")\n        new_handler = CookieTestRequestHandler(\n            cookie_secret=self.KEY_VERSIONS, key_version=1\n        )\n        new_handler._cookies = handler._cookies\n        self.assertEqual(new_handler.get_secure_cookie(\"foo\"), b\"bar\")\n\n    def test_key_version_invalidate_version(self):\n        handler = CookieTestRequestHandler(\n            cookie_secret=self.KEY_VERSIONS, key_version=0\n        )\n        handler.set_secure_cookie(\"foo\", b\"bar\")\n        new_key_versions = self.KEY_VERSIONS.copy()\n        new_key_versions.pop(0)\n        new_handler = CookieTestRequestHandler(\n            cookie_secret=new_key_versions, key_version=1\n        )\n        new_handler._cookies = handler._cookies\n        self.assertEqual(new_handler.get_secure_cookie(\"foo\"), None)\n\n\nclass FinalReturnTest(WebTestCase):\n    def get_handlers(self):\n        test = self\n\n        class FinishHandler(RequestHandler):\n            @gen.coroutine\n            def get(self):\n                test.final_return = self.finish()\n                yield test.final_return\n\n            @gen.coroutine\n            def post(self):\n                self.write(\"hello,\")\n                yield self.flush()\n                test.final_return = self.finish(\"world\")\n                yield test.final_return\n\n        class RenderHandler(RequestHandler):\n            def create_template_loader(self, path):\n                return DictLoader({\"foo.html\": \"hi\"})\n\n            @gen.coroutine\n            def get(self):\n                test.final_return = self.render(\"foo.html\")\n\n        return [(\"/finish\", FinishHandler), (\"/render\", RenderHandler)]\n\n    def get_app_kwargs(self):\n        return dict(template_path=\"FinalReturnTest\")\n\n    def test_finish_method_return_future(self):\n        response = self.fetch(self.get_url(\"/finish\"))\n        self.assertEqual(response.code, 200)\n        self.assertIsInstance(self.final_return, Future)\n        self.assertTrue(self.final_return.done())\n\n        response = self.fetch(self.get_url(\"/finish\"), method=\"POST\", body=b\"\")\n        self.assertEqual(response.code, 200)\n        self.assertIsInstance(self.final_return, Future)\n        self.assertTrue(self.final_return.done())\n\n    def test_render_method_return_future(self):\n        response = self.fetch(self.get_url(\"/render\"))\n        self.assertEqual(response.code, 200)\n        self.assertIsInstance(self.final_return, Future)\n\n\nclass CookieTest(WebTestCase):\n    def get_handlers(self):\n        class SetCookieHandler(RequestHandler):\n            def get(self):\n                # Try setting cookies with different argument types\n                # to ensure that everything gets encoded correctly\n                self.set_cookie(\"str\", \"asdf\")\n                self.set_cookie(\"unicode\", u\"qwer\")\n                self.set_cookie(\"bytes\", b\"zxcv\")\n\n        class GetCookieHandler(RequestHandler):\n            def get(self):\n                self.write(self.get_cookie(\"foo\", \"default\"))\n\n        class SetCookieDomainHandler(RequestHandler):\n            def get(self):\n                # unicode domain and path arguments shouldn't break things\n                # either (see bug #285)\n                self.set_cookie(\"unicode_args\", \"blah\", domain=u\"foo.com\", path=u\"/foo\")\n\n        class SetCookieSpecialCharHandler(RequestHandler):\n            def get(self):\n                self.set_cookie(\"equals\", \"a=b\")\n                self.set_cookie(\"semicolon\", \"a;b\")\n                self.set_cookie(\"quote\", 'a\"b')\n\n        class SetCookieOverwriteHandler(RequestHandler):\n            def get(self):\n                self.set_cookie(\"a\", \"b\", domain=\"example.com\")\n                self.set_cookie(\"c\", \"d\", domain=\"example.com\")\n                # A second call with the same name clobbers the first.\n                # Attributes from the first call are not carried over.\n                self.set_cookie(\"a\", \"e\")\n\n        class SetCookieMaxAgeHandler(RequestHandler):\n            def get(self):\n                self.set_cookie(\"foo\", \"bar\", max_age=10)\n\n        class SetCookieExpiresDaysHandler(RequestHandler):\n            def get(self):\n                self.set_cookie(\"foo\", \"bar\", expires_days=10)\n\n        class SetCookieFalsyFlags(RequestHandler):\n            def get(self):\n                self.set_cookie(\"a\", \"1\", secure=True)\n                self.set_cookie(\"b\", \"1\", secure=False)\n                self.set_cookie(\"c\", \"1\", httponly=True)\n                self.set_cookie(\"d\", \"1\", httponly=False)\n\n        return [\n            (\"/set\", SetCookieHandler),\n            (\"/get\", GetCookieHandler),\n            (\"/set_domain\", SetCookieDomainHandler),\n            (\"/special_char\", SetCookieSpecialCharHandler),\n            (\"/set_overwrite\", SetCookieOverwriteHandler),\n            (\"/set_max_age\", SetCookieMaxAgeHandler),\n            (\"/set_expires_days\", SetCookieExpiresDaysHandler),\n            (\"/set_falsy_flags\", SetCookieFalsyFlags),\n        ]\n\n    def test_set_cookie(self):\n        response = self.fetch(\"/set\")\n        self.assertEqual(\n            sorted(response.headers.get_list(\"Set-Cookie\")),\n            [\"bytes=zxcv; Path=/\", \"str=asdf; Path=/\", \"unicode=qwer; Path=/\"],\n        )\n\n    def test_get_cookie(self):\n        response = self.fetch(\"/get\", headers={\"Cookie\": \"foo=bar\"})\n        self.assertEqual(response.body, b\"bar\")\n\n        response = self.fetch(\"/get\", headers={\"Cookie\": 'foo=\"bar\"'})\n        self.assertEqual(response.body, b\"bar\")\n\n        response = self.fetch(\"/get\", headers={\"Cookie\": \"/=exception;\"})\n        self.assertEqual(response.body, b\"default\")\n\n    def test_set_cookie_domain(self):\n        response = self.fetch(\"/set_domain\")\n        self.assertEqual(\n            response.headers.get_list(\"Set-Cookie\"),\n            [\"unicode_args=blah; Domain=foo.com; Path=/foo\"],\n        )\n\n    def test_cookie_special_char(self):\n        response = self.fetch(\"/special_char\")\n        headers = sorted(response.headers.get_list(\"Set-Cookie\"))\n        self.assertEqual(len(headers), 3)\n        self.assertEqual(headers[0], 'equals=\"a=b\"; Path=/')\n        self.assertEqual(headers[1], 'quote=\"a\\\\\"b\"; Path=/')\n        # python 2.7 octal-escapes the semicolon; older versions leave it alone\n        self.assertTrue(\n            headers[2] in ('semicolon=\"a;b\"; Path=/', 'semicolon=\"a\\\\073b\"; Path=/'),\n            headers[2],\n        )\n\n        data = [\n            (\"foo=a=b\", \"a=b\"),\n            ('foo=\"a=b\"', \"a=b\"),\n            ('foo=\"a;b\"', '\"a'),  # even quoted, \";\" is a delimiter\n            (\"foo=a\\\\073b\", \"a\\\\073b\"),  # escapes only decoded in quotes\n            ('foo=\"a\\\\073b\"', \"a;b\"),\n            ('foo=\"a\\\\\"b\"', 'a\"b'),\n        ]\n        for header, expected in data:\n            logging.debug(\"trying %r\", header)\n            response = self.fetch(\"/get\", headers={\"Cookie\": header})\n            self.assertEqual(response.body, utf8(expected))\n\n    def test_set_cookie_overwrite(self):\n        response = self.fetch(\"/set_overwrite\")\n        headers = response.headers.get_list(\"Set-Cookie\")\n        self.assertEqual(\n            sorted(headers), [\"a=e; Path=/\", \"c=d; Domain=example.com; Path=/\"]\n        )\n\n    def test_set_cookie_max_age(self):\n        response = self.fetch(\"/set_max_age\")\n        headers = response.headers.get_list(\"Set-Cookie\")\n        self.assertEqual(sorted(headers), [\"foo=bar; Max-Age=10; Path=/\"])\n\n    def test_set_cookie_expires_days(self):\n        response = self.fetch(\"/set_expires_days\")\n        header = response.headers.get(\"Set-Cookie\")\n        match = re.match(\"foo=bar; expires=(?P<expires>.+); Path=/\", header)\n        assert match is not None\n\n        expires = datetime.datetime.utcnow() + datetime.timedelta(days=10)\n        parsed = email.utils.parsedate(match.groupdict()[\"expires\"])\n        assert parsed is not None\n        header_expires = datetime.datetime(*parsed[:6])\n        self.assertTrue(abs((expires - header_expires).total_seconds()) < 10)\n\n    def test_set_cookie_false_flags(self):\n        response = self.fetch(\"/set_falsy_flags\")\n        headers = sorted(response.headers.get_list(\"Set-Cookie\"))\n        # The secure and httponly headers are capitalized in py35 and\n        # lowercase in older versions.\n        self.assertEqual(headers[0].lower(), \"a=1; path=/; secure\")\n        self.assertEqual(headers[1].lower(), \"b=1; path=/\")\n        self.assertEqual(headers[2].lower(), \"c=1; httponly; path=/\")\n        self.assertEqual(headers[3].lower(), \"d=1; path=/\")\n\n\nclass AuthRedirectRequestHandler(RequestHandler):\n    def initialize(self, login_url):\n        self.login_url = login_url\n\n    def get_login_url(self):\n        return self.login_url\n\n    @authenticated\n    def get(self):\n        # we'll never actually get here because the test doesn't follow redirects\n        self.send_error(500)\n\n\nclass AuthRedirectTest(WebTestCase):\n    def get_handlers(self):\n        return [\n            (\"/relative\", AuthRedirectRequestHandler, dict(login_url=\"/login\")),\n            (\n                \"/absolute\",\n                AuthRedirectRequestHandler,\n                dict(login_url=\"http://example.com/login\"),\n            ),\n        ]\n\n    def test_relative_auth_redirect(self):\n        response = self.fetch(self.get_url(\"/relative\"), follow_redirects=False)\n        self.assertEqual(response.code, 302)\n        self.assertEqual(response.headers[\"Location\"], \"/login?next=%2Frelative\")\n\n    def test_absolute_auth_redirect(self):\n        response = self.fetch(self.get_url(\"/absolute\"), follow_redirects=False)\n        self.assertEqual(response.code, 302)\n        self.assertTrue(\n            re.match(\n                r\"http://example.com/login\\?next=http%3A%2F%2F127.0.0.1%3A[0-9]+%2Fabsolute\",\n                response.headers[\"Location\"],\n            ),\n            response.headers[\"Location\"],\n        )\n\n\nclass ConnectionCloseHandler(RequestHandler):\n    def initialize(self, test):\n        self.test = test\n\n    @gen.coroutine\n    def get(self):\n        self.test.on_handler_waiting()\n        yield self.test.cleanup_event.wait()\n\n    def on_connection_close(self):\n        self.test.on_connection_close()\n\n\nclass ConnectionCloseTest(WebTestCase):\n    def get_handlers(self):\n        self.cleanup_event = Event()\n        return [(\"/\", ConnectionCloseHandler, dict(test=self))]\n\n    def test_connection_close(self):\n        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)\n        s.connect((\"127.0.0.1\", self.get_http_port()))\n        self.stream = IOStream(s)\n        self.stream.write(b\"GET / HTTP/1.0\\r\\n\\r\\n\")\n        self.wait()\n        # Let the hanging coroutine clean up after itself\n        self.cleanup_event.set()\n        self.io_loop.run_sync(lambda: gen.sleep(0))\n\n    def on_handler_waiting(self):\n        logging.debug(\"handler waiting\")\n        self.stream.close()\n\n    def on_connection_close(self):\n        logging.debug(\"connection closed\")\n        self.stop()\n\n\nclass EchoHandler(RequestHandler):\n    def get(self, *path_args):\n        # Type checks: web.py interfaces convert argument values to\n        # unicode strings (by default, but see also decode_argument).\n        # In httpserver.py (i.e. self.request.arguments), they're left\n        # as bytes.  Keys are always native strings.\n        for key in self.request.arguments:\n            if type(key) != str:\n                raise Exception(\"incorrect type for key: %r\" % type(key))\n            for value in self.request.arguments[key]:\n                if type(value) != bytes:\n                    raise Exception(\"incorrect type for value: %r\" % type(value))\n            for value in self.get_arguments(key):\n                if type(value) != unicode_type:\n                    raise Exception(\"incorrect type for value: %r\" % type(value))\n        for arg in path_args:\n            if type(arg) != unicode_type:\n                raise Exception(\"incorrect type for path arg: %r\" % type(arg))\n        self.write(\n            dict(\n                path=self.request.path,\n                path_args=path_args,\n                args=recursive_unicode(self.request.arguments),\n            )\n        )\n\n\nclass RequestEncodingTest(WebTestCase):\n    def get_handlers(self):\n        return [(\"/group/(.*)\", EchoHandler), (\"/slashes/([^/]*)/([^/]*)\", EchoHandler)]\n\n    def fetch_json(self, path):\n        return json_decode(self.fetch(path).body)\n\n    def test_group_question_mark(self):\n        # Ensure that url-encoded question marks are handled properly\n        self.assertEqual(\n            self.fetch_json(\"/group/%3F\"),\n            dict(path=\"/group/%3F\", path_args=[\"?\"], args={}),\n        )\n        self.assertEqual(\n            self.fetch_json(\"/group/%3F?%3F=%3F\"),\n            dict(path=\"/group/%3F\", path_args=[\"?\"], args={\"?\": [\"?\"]}),\n        )\n\n    def test_group_encoding(self):\n        # Path components and query arguments should be decoded the same way\n        self.assertEqual(\n            self.fetch_json(\"/group/%C3%A9?arg=%C3%A9\"),\n            {\n                u\"path\": u\"/group/%C3%A9\",\n                u\"path_args\": [u\"\\u00e9\"],\n                u\"args\": {u\"arg\": [u\"\\u00e9\"]},\n            },\n        )\n\n    def test_slashes(self):\n        # Slashes may be escaped to appear as a single \"directory\" in the path,\n        # but they are then unescaped when passed to the get() method.\n        self.assertEqual(\n            self.fetch_json(\"/slashes/foo/bar\"),\n            dict(path=\"/slashes/foo/bar\", path_args=[\"foo\", \"bar\"], args={}),\n        )\n        self.assertEqual(\n            self.fetch_json(\"/slashes/a%2Fb/c%2Fd\"),\n            dict(path=\"/slashes/a%2Fb/c%2Fd\", path_args=[\"a/b\", \"c/d\"], args={}),\n        )\n\n    def test_error(self):\n        # Percent signs (encoded as %25) should not mess up printf-style\n        # messages in logs\n        with ExpectLog(gen_log, \".*Invalid unicode\"):\n            self.fetch(\"/group/?arg=%25%e9\")\n\n\nclass TypeCheckHandler(RequestHandler):\n    def prepare(self):\n        self.errors = {}  # type: typing.Dict[str, str]\n\n        self.check_type(\"status\", self.get_status(), int)\n\n        # get_argument is an exception from the general rule of using\n        # type str for non-body data mainly for historical reasons.\n        self.check_type(\"argument\", self.get_argument(\"foo\"), unicode_type)\n        self.check_type(\"cookie_key\", list(self.cookies.keys())[0], str)\n        self.check_type(\"cookie_value\", list(self.cookies.values())[0].value, str)\n\n        # Secure cookies return bytes because they can contain arbitrary\n        # data, but regular cookies are native strings.\n        if list(self.cookies.keys()) != [\"asdf\"]:\n            raise Exception(\n                \"unexpected values for cookie keys: %r\" % self.cookies.keys()\n            )\n        self.check_type(\"get_secure_cookie\", self.get_secure_cookie(\"asdf\"), bytes)\n        self.check_type(\"get_cookie\", self.get_cookie(\"asdf\"), str)\n\n        self.check_type(\"xsrf_token\", self.xsrf_token, bytes)\n        self.check_type(\"xsrf_form_html\", self.xsrf_form_html(), str)\n\n        self.check_type(\"reverse_url\", self.reverse_url(\"typecheck\", \"foo\"), str)\n\n        self.check_type(\"request_summary\", self._request_summary(), str)\n\n    def get(self, path_component):\n        # path_component uses type unicode instead of str for consistency\n        # with get_argument()\n        self.check_type(\"path_component\", path_component, unicode_type)\n        self.write(self.errors)\n\n    def post(self, path_component):\n        self.check_type(\"path_component\", path_component, unicode_type)\n        self.write(self.errors)\n\n    def check_type(self, name, obj, expected_type):\n        actual_type = type(obj)\n        if expected_type != actual_type:\n            self.errors[name] = \"expected %s, got %s\" % (expected_type, actual_type)\n\n\nclass DecodeArgHandler(RequestHandler):\n    def decode_argument(self, value, name=None):\n        if type(value) != bytes:\n            raise Exception(\"unexpected type for value: %r\" % type(value))\n        # use self.request.arguments directly to avoid recursion\n        if \"encoding\" in self.request.arguments:\n            return value.decode(to_unicode(self.request.arguments[\"encoding\"][0]))\n        else:\n            return value\n\n    def get(self, arg):\n        def describe(s):\n            if type(s) == bytes:\n                return [\"bytes\", native_str(binascii.b2a_hex(s))]\n            elif type(s) == unicode_type:\n                return [\"unicode\", s]\n            raise Exception(\"unknown type\")\n\n        self.write({\"path\": describe(arg), \"query\": describe(self.get_argument(\"foo\"))})\n\n\nclass LinkifyHandler(RequestHandler):\n    def get(self):\n        self.render(\"linkify.html\", message=\"http://example.com\")\n\n\nclass UIModuleResourceHandler(RequestHandler):\n    def get(self):\n        self.render(\"page.html\", entries=[1, 2])\n\n\nclass OptionalPathHandler(RequestHandler):\n    def get(self, path):\n        self.write({\"path\": path})\n\n\nclass MultiHeaderHandler(RequestHandler):\n    def get(self):\n        self.set_header(\"x-overwrite\", \"1\")\n        self.set_header(\"X-Overwrite\", 2)\n        self.add_header(\"x-multi\", 3)\n        self.add_header(\"X-Multi\", \"4\")\n\n\nclass RedirectHandler(RequestHandler):\n    def get(self):\n        if self.get_argument(\"permanent\", None) is not None:\n            self.redirect(\"/\", permanent=int(self.get_argument(\"permanent\")))\n        elif self.get_argument(\"status\", None) is not None:\n            self.redirect(\"/\", status=int(self.get_argument(\"status\")))\n        else:\n            raise Exception(\"didn't get permanent or status arguments\")\n\n\nclass EmptyFlushCallbackHandler(RequestHandler):\n    @gen.coroutine\n    def get(self):\n        # Ensure that the flush callback is run whether or not there\n        # was any output.  The gen.Task and direct yield forms are\n        # equivalent.\n        yield self.flush()  # \"empty\" flush, but writes headers\n        yield self.flush()  # empty flush\n        self.write(\"o\")\n        yield self.flush()  # flushes the \"o\"\n        yield self.flush()  # empty flush\n        self.finish(\"k\")\n\n\nclass HeaderInjectionHandler(RequestHandler):\n    def get(self):\n        try:\n            self.set_header(\"X-Foo\", \"foo\\r\\nX-Bar: baz\")\n            raise Exception(\"Didn't get expected exception\")\n        except ValueError as e:\n            if \"Unsafe header value\" in str(e):\n                self.finish(b\"ok\")\n            else:\n                raise\n\n\nclass GetArgumentHandler(RequestHandler):\n    def prepare(self):\n        if self.get_argument(\"source\", None) == \"query\":\n            method = self.get_query_argument\n        elif self.get_argument(\"source\", None) == \"body\":\n            method = self.get_body_argument\n        else:\n            method = self.get_argument\n        self.finish(method(\"foo\", \"default\"))\n\n\nclass GetArgumentsHandler(RequestHandler):\n    def prepare(self):\n        self.finish(\n            dict(\n                default=self.get_arguments(\"foo\"),\n                query=self.get_query_arguments(\"foo\"),\n                body=self.get_body_arguments(\"foo\"),\n            )\n        )\n\n\n# This test was shared with wsgi_test.py; now the name is meaningless.\nclass WSGISafeWebTest(WebTestCase):\n    COOKIE_SECRET = \"WebTest.COOKIE_SECRET\"\n\n    def get_app_kwargs(self):\n        loader = DictLoader(\n            {\n                \"linkify.html\": \"{% module linkify(message) %}\",\n                \"page.html\": \"\"\"\\\n<html><head></head><body>\n{% for e in entries %}\n{% module Template(\"entry.html\", entry=e) %}\n{% end %}\n</body></html>\"\"\",\n                \"entry.html\": \"\"\"\\\n{{ set_resources(embedded_css=\".entry { margin-bottom: 1em; }\",\n                 embedded_javascript=\"js_embed()\",\n                 css_files=[\"/base.css\", \"/foo.css\"],\n                 javascript_files=\"/common.js\",\n                 html_head=\"<meta>\",\n                 html_body='<script src=\"/analytics.js\"/>') }}\n<div class=\"entry\">...</div>\"\"\",\n            }\n        )\n        return dict(\n            template_loader=loader,\n            autoescape=\"xhtml_escape\",\n            cookie_secret=self.COOKIE_SECRET,\n        )\n\n    def tearDown(self):\n        super(WSGISafeWebTest, self).tearDown()\n        RequestHandler._template_loaders.clear()\n\n    def get_handlers(self):\n        urls = [\n            url(\"/typecheck/(.*)\", TypeCheckHandler, name=\"typecheck\"),\n            url(\"/decode_arg/(.*)\", DecodeArgHandler, name=\"decode_arg\"),\n            url(\"/decode_arg_kw/(?P<arg>.*)\", DecodeArgHandler),\n            url(\"/linkify\", LinkifyHandler),\n            url(\"/uimodule_resources\", UIModuleResourceHandler),\n            url(\"/optional_path/(.+)?\", OptionalPathHandler),\n            url(\"/multi_header\", MultiHeaderHandler),\n            url(\"/redirect\", RedirectHandler),\n            url(\n                \"/web_redirect_permanent\",\n                WebRedirectHandler,\n                {\"url\": \"/web_redirect_newpath\"},\n            ),\n            url(\n                \"/web_redirect\",\n                WebRedirectHandler,\n                {\"url\": \"/web_redirect_newpath\", \"permanent\": False},\n            ),\n            url(\n                \"//web_redirect_double_slash\",\n                WebRedirectHandler,\n                {\"url\": \"/web_redirect_newpath\"},\n            ),\n            url(\"/header_injection\", HeaderInjectionHandler),\n            url(\"/get_argument\", GetArgumentHandler),\n            url(\"/get_arguments\", GetArgumentsHandler),\n        ]\n        return urls\n\n    def fetch_json(self, *args, **kwargs):\n        response = self.fetch(*args, **kwargs)\n        response.rethrow()\n        return json_decode(response.body)\n\n    def test_types(self):\n        cookie_value = to_unicode(\n            create_signed_value(self.COOKIE_SECRET, \"asdf\", \"qwer\")\n        )\n        response = self.fetch(\n            \"/typecheck/asdf?foo=bar\", headers={\"Cookie\": \"asdf=\" + cookie_value}\n        )\n        data = json_decode(response.body)\n        self.assertEqual(data, {})\n\n        response = self.fetch(\n            \"/typecheck/asdf?foo=bar\",\n            method=\"POST\",\n            headers={\"Cookie\": \"asdf=\" + cookie_value},\n            body=\"foo=bar\",\n        )\n\n    def test_decode_argument(self):\n        # These urls all decode to the same thing\n        urls = [\n            \"/decode_arg/%C3%A9?foo=%C3%A9&encoding=utf-8\",\n            \"/decode_arg/%E9?foo=%E9&encoding=latin1\",\n            \"/decode_arg_kw/%E9?foo=%E9&encoding=latin1\",\n        ]\n        for req_url in urls:\n            response = self.fetch(req_url)\n            response.rethrow()\n            data = json_decode(response.body)\n            self.assertEqual(\n                data,\n                {u\"path\": [u\"unicode\", u\"\\u00e9\"], u\"query\": [u\"unicode\", u\"\\u00e9\"]},\n            )\n\n        response = self.fetch(\"/decode_arg/%C3%A9?foo=%C3%A9\")\n        response.rethrow()\n        data = json_decode(response.body)\n        self.assertEqual(\n            data, {u\"path\": [u\"bytes\", u\"c3a9\"], u\"query\": [u\"bytes\", u\"c3a9\"]}\n        )\n\n    def test_decode_argument_invalid_unicode(self):\n        # test that invalid unicode in URLs causes 400, not 500\n        with ExpectLog(gen_log, \".*Invalid unicode.*\"):\n            response = self.fetch(\"/typecheck/invalid%FF\")\n            self.assertEqual(response.code, 400)\n            response = self.fetch(\"/typecheck/invalid?foo=%FF\")\n            self.assertEqual(response.code, 400)\n\n    def test_decode_argument_plus(self):\n        # These urls are all equivalent.\n        urls = [\n            \"/decode_arg/1%20%2B%201?foo=1%20%2B%201&encoding=utf-8\",\n            \"/decode_arg/1%20+%201?foo=1+%2B+1&encoding=utf-8\",\n        ]\n        for req_url in urls:\n            response = self.fetch(req_url)\n            response.rethrow()\n            data = json_decode(response.body)\n            self.assertEqual(\n                data,\n                {u\"path\": [u\"unicode\", u\"1 + 1\"], u\"query\": [u\"unicode\", u\"1 + 1\"]},\n            )\n\n    def test_reverse_url(self):\n        self.assertEqual(self.app.reverse_url(\"decode_arg\", \"foo\"), \"/decode_arg/foo\")\n        self.assertEqual(self.app.reverse_url(\"decode_arg\", 42), \"/decode_arg/42\")\n        self.assertEqual(self.app.reverse_url(\"decode_arg\", b\"\\xe9\"), \"/decode_arg/%E9\")\n        self.assertEqual(\n            self.app.reverse_url(\"decode_arg\", u\"\\u00e9\"), \"/decode_arg/%C3%A9\"\n        )\n        self.assertEqual(\n            self.app.reverse_url(\"decode_arg\", \"1 + 1\"), \"/decode_arg/1%20%2B%201\"\n        )\n\n    def test_uimodule_unescaped(self):\n        response = self.fetch(\"/linkify\")\n        self.assertEqual(\n            response.body, b'<a href=\"http://example.com\">http://example.com</a>'\n        )\n\n    def test_uimodule_resources(self):\n        response = self.fetch(\"/uimodule_resources\")\n        self.assertEqual(\n            response.body,\n            b\"\"\"\\\n<html><head><link href=\"/base.css\" type=\"text/css\" rel=\"stylesheet\"/><link href=\"/foo.css\" type=\"text/css\" rel=\"stylesheet\"/>\n<style type=\"text/css\">\n.entry { margin-bottom: 1em; }\n</style>\n<meta>\n</head><body>\n\n\n<div class=\"entry\">...</div>\n\n\n<div class=\"entry\">...</div>\n\n<script src=\"/common.js\" type=\"text/javascript\"></script>\n<script type=\"text/javascript\">\n//<![CDATA[\njs_embed()\n//]]>\n</script>\n<script src=\"/analytics.js\"/>\n</body></html>\"\"\",  # noqa: E501\n        )\n\n    def test_optional_path(self):\n        self.assertEqual(self.fetch_json(\"/optional_path/foo\"), {u\"path\": u\"foo\"})\n        self.assertEqual(self.fetch_json(\"/optional_path/\"), {u\"path\": None})\n\n    def test_multi_header(self):\n        response = self.fetch(\"/multi_header\")\n        self.assertEqual(response.headers[\"x-overwrite\"], \"2\")\n        self.assertEqual(response.headers.get_list(\"x-multi\"), [\"3\", \"4\"])\n\n    def test_redirect(self):\n        response = self.fetch(\"/redirect?permanent=1\", follow_redirects=False)\n        self.assertEqual(response.code, 301)\n        response = self.fetch(\"/redirect?permanent=0\", follow_redirects=False)\n        self.assertEqual(response.code, 302)\n        response = self.fetch(\"/redirect?status=307\", follow_redirects=False)\n        self.assertEqual(response.code, 307)\n\n    def test_web_redirect(self):\n        response = self.fetch(\"/web_redirect_permanent\", follow_redirects=False)\n        self.assertEqual(response.code, 301)\n        self.assertEqual(response.headers[\"Location\"], \"/web_redirect_newpath\")\n        response = self.fetch(\"/web_redirect\", follow_redirects=False)\n        self.assertEqual(response.code, 302)\n        self.assertEqual(response.headers[\"Location\"], \"/web_redirect_newpath\")\n\n    def test_web_redirect_double_slash(self):\n        response = self.fetch(\"//web_redirect_double_slash\", follow_redirects=False)\n        self.assertEqual(response.code, 301)\n        self.assertEqual(response.headers[\"Location\"], \"/web_redirect_newpath\")\n\n    def test_header_injection(self):\n        response = self.fetch(\"/header_injection\")\n        self.assertEqual(response.body, b\"ok\")\n\n    def test_get_argument(self):\n        response = self.fetch(\"/get_argument?foo=bar\")\n        self.assertEqual(response.body, b\"bar\")\n        response = self.fetch(\"/get_argument?foo=\")\n        self.assertEqual(response.body, b\"\")\n        response = self.fetch(\"/get_argument\")\n        self.assertEqual(response.body, b\"default\")\n\n        # Test merging of query and body arguments.\n        # In singular form, body arguments take precedence over query arguments.\n        body = urllib.parse.urlencode(dict(foo=\"hello\"))\n        response = self.fetch(\"/get_argument?foo=bar\", method=\"POST\", body=body)\n        self.assertEqual(response.body, b\"hello\")\n        # In plural methods they are merged.\n        response = self.fetch(\"/get_arguments?foo=bar\", method=\"POST\", body=body)\n        self.assertEqual(\n            json_decode(response.body),\n            dict(default=[\"bar\", \"hello\"], query=[\"bar\"], body=[\"hello\"]),\n        )\n\n    def test_get_query_arguments(self):\n        # send as a post so we can ensure the separation between query\n        # string and body arguments.\n        body = urllib.parse.urlencode(dict(foo=\"hello\"))\n        response = self.fetch(\n            \"/get_argument?source=query&foo=bar\", method=\"POST\", body=body\n        )\n        self.assertEqual(response.body, b\"bar\")\n        response = self.fetch(\n            \"/get_argument?source=query&foo=\", method=\"POST\", body=body\n        )\n        self.assertEqual(response.body, b\"\")\n        response = self.fetch(\"/get_argument?source=query\", method=\"POST\", body=body)\n        self.assertEqual(response.body, b\"default\")\n\n    def test_get_body_arguments(self):\n        body = urllib.parse.urlencode(dict(foo=\"bar\"))\n        response = self.fetch(\n            \"/get_argument?source=body&foo=hello\", method=\"POST\", body=body\n        )\n        self.assertEqual(response.body, b\"bar\")\n\n        body = urllib.parse.urlencode(dict(foo=\"\"))\n        response = self.fetch(\n            \"/get_argument?source=body&foo=hello\", method=\"POST\", body=body\n        )\n        self.assertEqual(response.body, b\"\")\n\n        body = urllib.parse.urlencode(dict())\n        response = self.fetch(\n            \"/get_argument?source=body&foo=hello\", method=\"POST\", body=body\n        )\n        self.assertEqual(response.body, b\"default\")\n\n    def test_no_gzip(self):\n        response = self.fetch(\"/get_argument\")\n        self.assertNotIn(\"Accept-Encoding\", response.headers.get(\"Vary\", \"\"))\n        self.assertNotIn(\"gzip\", response.headers.get(\"Content-Encoding\", \"\"))\n\n\nclass NonWSGIWebTests(WebTestCase):\n    def get_handlers(self):\n        return [(\"/empty_flush\", EmptyFlushCallbackHandler)]\n\n    def test_empty_flush(self):\n        response = self.fetch(\"/empty_flush\")\n        self.assertEqual(response.body, b\"ok\")\n\n\nclass ErrorResponseTest(WebTestCase):\n    def get_handlers(self):\n        class DefaultHandler(RequestHandler):\n            def get(self):\n                if self.get_argument(\"status\", None):\n                    raise HTTPError(int(self.get_argument(\"status\")))\n                1 / 0\n\n        class WriteErrorHandler(RequestHandler):\n            def get(self):\n                if self.get_argument(\"status\", None):\n                    self.send_error(int(self.get_argument(\"status\")))\n                else:\n                    1 / 0\n\n            def write_error(self, status_code, **kwargs):\n                self.set_header(\"Content-Type\", \"text/plain\")\n                if \"exc_info\" in kwargs:\n                    self.write(\"Exception: %s\" % kwargs[\"exc_info\"][0].__name__)\n                else:\n                    self.write(\"Status: %d\" % status_code)\n\n        class FailedWriteErrorHandler(RequestHandler):\n            def get(self):\n                1 / 0\n\n            def write_error(self, status_code, **kwargs):\n                raise Exception(\"exception in write_error\")\n\n        return [\n            url(\"/default\", DefaultHandler),\n            url(\"/write_error\", WriteErrorHandler),\n            url(\"/failed_write_error\", FailedWriteErrorHandler),\n        ]\n\n    def test_default(self):\n        with ExpectLog(app_log, \"Uncaught exception\"):\n            response = self.fetch(\"/default\")\n            self.assertEqual(response.code, 500)\n            self.assertTrue(b\"500: Internal Server Error\" in response.body)\n\n            response = self.fetch(\"/default?status=503\")\n            self.assertEqual(response.code, 503)\n            self.assertTrue(b\"503: Service Unavailable\" in response.body)\n\n            response = self.fetch(\"/default?status=435\")\n            self.assertEqual(response.code, 435)\n            self.assertTrue(b\"435: Unknown\" in response.body)\n\n    def test_write_error(self):\n        with ExpectLog(app_log, \"Uncaught exception\"):\n            response = self.fetch(\"/write_error\")\n            self.assertEqual(response.code, 500)\n            self.assertEqual(b\"Exception: ZeroDivisionError\", response.body)\n\n            response = self.fetch(\"/write_error?status=503\")\n            self.assertEqual(response.code, 503)\n            self.assertEqual(b\"Status: 503\", response.body)\n\n    def test_failed_write_error(self):\n        with ExpectLog(app_log, \"Uncaught exception\"):\n            response = self.fetch(\"/failed_write_error\")\n            self.assertEqual(response.code, 500)\n            self.assertEqual(b\"\", response.body)\n\n\nclass StaticFileTest(WebTestCase):\n    # The expected MD5 hash of robots.txt, used in tests that call\n    # StaticFileHandler.get_version\n    robots_txt_hash = b\"f71d20196d4caf35b6a670db8c70b03d\"\n    static_dir = os.path.join(os.path.dirname(__file__), \"static\")\n\n    def get_handlers(self):\n        class StaticUrlHandler(RequestHandler):\n            def get(self, path):\n                with_v = int(self.get_argument(\"include_version\", 1))\n                self.write(self.static_url(path, include_version=with_v))\n\n        class AbsoluteStaticUrlHandler(StaticUrlHandler):\n            include_host = True\n\n        class OverrideStaticUrlHandler(RequestHandler):\n            def get(self, path):\n                do_include = bool(self.get_argument(\"include_host\"))\n                self.include_host = not do_include\n\n                regular_url = self.static_url(path)\n                override_url = self.static_url(path, include_host=do_include)\n                if override_url == regular_url:\n                    return self.write(str(False))\n\n                protocol = self.request.protocol + \"://\"\n                protocol_length = len(protocol)\n                check_regular = regular_url.find(protocol, 0, protocol_length)\n                check_override = override_url.find(protocol, 0, protocol_length)\n\n                if do_include:\n                    result = check_override == 0 and check_regular == -1\n                else:\n                    result = check_override == -1 and check_regular == 0\n                self.write(str(result))\n\n        return [\n            (\"/static_url/(.*)\", StaticUrlHandler),\n            (\"/abs_static_url/(.*)\", AbsoluteStaticUrlHandler),\n            (\"/override_static_url/(.*)\", OverrideStaticUrlHandler),\n            (\"/root_static/(.*)\", StaticFileHandler, dict(path=\"/\")),\n        ]\n\n    def get_app_kwargs(self):\n        return dict(static_path=relpath(\"static\"))\n\n    def test_static_files(self):\n        response = self.fetch(\"/robots.txt\")\n        self.assertTrue(b\"Disallow: /\" in response.body)\n\n        response = self.fetch(\"/static/robots.txt\")\n        self.assertTrue(b\"Disallow: /\" in response.body)\n        self.assertEqual(response.headers.get(\"Content-Type\"), \"text/plain\")\n\n    def test_static_compressed_files(self):\n        response = self.fetch(\"/static/sample.xml.gz\")\n        self.assertEqual(response.headers.get(\"Content-Type\"), \"application/gzip\")\n        response = self.fetch(\"/static/sample.xml.bz2\")\n        self.assertEqual(\n            response.headers.get(\"Content-Type\"), \"application/octet-stream\"\n        )\n        # make sure the uncompressed file still has the correct type\n        response = self.fetch(\"/static/sample.xml\")\n        self.assertTrue(\n            response.headers.get(\"Content-Type\") in set((\"text/xml\", \"application/xml\"))\n        )\n\n    def test_static_url(self):\n        response = self.fetch(\"/static_url/robots.txt\")\n        self.assertEqual(response.body, b\"/static/robots.txt?v=\" + self.robots_txt_hash)\n\n    def test_absolute_static_url(self):\n        response = self.fetch(\"/abs_static_url/robots.txt\")\n        self.assertEqual(\n            response.body,\n            (utf8(self.get_url(\"/\")) + b\"static/robots.txt?v=\" + self.robots_txt_hash),\n        )\n\n    def test_relative_version_exclusion(self):\n        response = self.fetch(\"/static_url/robots.txt?include_version=0\")\n        self.assertEqual(response.body, b\"/static/robots.txt\")\n\n    def test_absolute_version_exclusion(self):\n        response = self.fetch(\"/abs_static_url/robots.txt?include_version=0\")\n        self.assertEqual(response.body, utf8(self.get_url(\"/\") + \"static/robots.txt\"))\n\n    def test_include_host_override(self):\n        self._trigger_include_host_check(False)\n        self._trigger_include_host_check(True)\n\n    def _trigger_include_host_check(self, include_host):\n        path = \"/override_static_url/robots.txt?include_host=%s\"\n        response = self.fetch(path % int(include_host))\n        self.assertEqual(response.body, utf8(str(True)))\n\n    def get_and_head(self, *args, **kwargs):\n        \"\"\"Performs a GET and HEAD request and returns the GET response.\n\n        Fails if any ``Content-*`` headers returned by the two requests\n        differ.\n        \"\"\"\n        head_response = self.fetch(*args, method=\"HEAD\", **kwargs)\n        get_response = self.fetch(*args, method=\"GET\", **kwargs)\n        content_headers = set()\n        for h in itertools.chain(head_response.headers, get_response.headers):\n            if h.startswith(\"Content-\"):\n                content_headers.add(h)\n        for h in content_headers:\n            self.assertEqual(\n                head_response.headers.get(h),\n                get_response.headers.get(h),\n                \"%s differs between GET (%s) and HEAD (%s)\"\n                % (h, head_response.headers.get(h), get_response.headers.get(h)),\n            )\n        return get_response\n\n    def test_static_304_if_modified_since(self):\n        response1 = self.get_and_head(\"/static/robots.txt\")\n        response2 = self.get_and_head(\n            \"/static/robots.txt\",\n            headers={\"If-Modified-Since\": response1.headers[\"Last-Modified\"]},\n        )\n        self.assertEqual(response2.code, 304)\n        self.assertTrue(\"Content-Length\" not in response2.headers)\n        self.assertTrue(\"Last-Modified\" not in response2.headers)\n\n    def test_static_304_if_none_match(self):\n        response1 = self.get_and_head(\"/static/robots.txt\")\n        response2 = self.get_and_head(\n            \"/static/robots.txt\", headers={\"If-None-Match\": response1.headers[\"Etag\"]}\n        )\n        self.assertEqual(response2.code, 304)\n\n    def test_static_304_etag_modified_bug(self):\n        response1 = self.get_and_head(\"/static/robots.txt\")\n        response2 = self.get_and_head(\n            \"/static/robots.txt\",\n            headers={\n                \"If-None-Match\": '\"MISMATCH\"',\n                \"If-Modified-Since\": response1.headers[\"Last-Modified\"],\n            },\n        )\n        self.assertEqual(response2.code, 200)\n\n    def test_static_if_modified_since_pre_epoch(self):\n        # On windows, the functions that work with time_t do not accept\n        # negative values, and at least one client (processing.js) seems\n        # to use if-modified-since 1/1/1960 as a cache-busting technique.\n        response = self.get_and_head(\n            \"/static/robots.txt\",\n            headers={\"If-Modified-Since\": \"Fri, 01 Jan 1960 00:00:00 GMT\"},\n        )\n        self.assertEqual(response.code, 200)\n\n    def test_static_if_modified_since_time_zone(self):\n        # Instead of the value from Last-Modified, make requests with times\n        # chosen just before and after the known modification time\n        # of the file to ensure that the right time zone is being used\n        # when parsing If-Modified-Since.\n        stat = os.stat(relpath(\"static/robots.txt\"))\n\n        response = self.get_and_head(\n            \"/static/robots.txt\",\n            headers={\"If-Modified-Since\": format_timestamp(stat.st_mtime - 1)},\n        )\n        self.assertEqual(response.code, 200)\n        response = self.get_and_head(\n            \"/static/robots.txt\",\n            headers={\"If-Modified-Since\": format_timestamp(stat.st_mtime + 1)},\n        )\n        self.assertEqual(response.code, 304)\n\n    def test_static_etag(self):\n        response = self.get_and_head(\"/static/robots.txt\")\n        self.assertEqual(\n            utf8(response.headers.get(\"Etag\")), b'\"' + self.robots_txt_hash + b'\"'\n        )\n\n    def test_static_with_range(self):\n        response = self.get_and_head(\n            \"/static/robots.txt\", headers={\"Range\": \"bytes=0-9\"}\n        )\n        self.assertEqual(response.code, 206)\n        self.assertEqual(response.body, b\"User-agent\")\n        self.assertEqual(\n            utf8(response.headers.get(\"Etag\")), b'\"' + self.robots_txt_hash + b'\"'\n        )\n        self.assertEqual(response.headers.get(\"Content-Length\"), \"10\")\n        self.assertEqual(response.headers.get(\"Content-Range\"), \"bytes 0-9/26\")\n\n    def test_static_with_range_full_file(self):\n        response = self.get_and_head(\n            \"/static/robots.txt\", headers={\"Range\": \"bytes=0-\"}\n        )\n        # Note: Chrome refuses to play audio if it gets an HTTP 206 in response\n        # to ``Range: bytes=0-`` :(\n        self.assertEqual(response.code, 200)\n        robots_file_path = os.path.join(self.static_dir, \"robots.txt\")\n        with open(robots_file_path) as f:\n            self.assertEqual(response.body, utf8(f.read()))\n        self.assertEqual(response.headers.get(\"Content-Length\"), \"26\")\n        self.assertEqual(response.headers.get(\"Content-Range\"), None)\n\n    def test_static_with_range_full_past_end(self):\n        response = self.get_and_head(\n            \"/static/robots.txt\", headers={\"Range\": \"bytes=0-10000000\"}\n        )\n        self.assertEqual(response.code, 200)\n        robots_file_path = os.path.join(self.static_dir, \"robots.txt\")\n        with open(robots_file_path) as f:\n            self.assertEqual(response.body, utf8(f.read()))\n        self.assertEqual(response.headers.get(\"Content-Length\"), \"26\")\n        self.assertEqual(response.headers.get(\"Content-Range\"), None)\n\n    def test_static_with_range_partial_past_end(self):\n        response = self.get_and_head(\n            \"/static/robots.txt\", headers={\"Range\": \"bytes=1-10000000\"}\n        )\n        self.assertEqual(response.code, 206)\n        robots_file_path = os.path.join(self.static_dir, \"robots.txt\")\n        with open(robots_file_path) as f:\n            self.assertEqual(response.body, utf8(f.read()[1:]))\n        self.assertEqual(response.headers.get(\"Content-Length\"), \"25\")\n        self.assertEqual(response.headers.get(\"Content-Range\"), \"bytes 1-25/26\")\n\n    def test_static_with_range_end_edge(self):\n        response = self.get_and_head(\n            \"/static/robots.txt\", headers={\"Range\": \"bytes=22-\"}\n        )\n        self.assertEqual(response.body, b\": /\\n\")\n        self.assertEqual(response.headers.get(\"Content-Length\"), \"4\")\n        self.assertEqual(response.headers.get(\"Content-Range\"), \"bytes 22-25/26\")\n\n    def test_static_with_range_neg_end(self):\n        response = self.get_and_head(\n            \"/static/robots.txt\", headers={\"Range\": \"bytes=-4\"}\n        )\n        self.assertEqual(response.body, b\": /\\n\")\n        self.assertEqual(response.headers.get(\"Content-Length\"), \"4\")\n        self.assertEqual(response.headers.get(\"Content-Range\"), \"bytes 22-25/26\")\n\n    def test_static_with_range_neg_past_start(self):\n        response = self.get_and_head(\n            \"/static/robots.txt\", headers={\"Range\": \"bytes=-1000000\"}\n        )\n        self.assertEqual(response.code, 200)\n        robots_file_path = os.path.join(self.static_dir, \"robots.txt\")\n        with open(robots_file_path) as f:\n            self.assertEqual(response.body, utf8(f.read()))\n        self.assertEqual(response.headers.get(\"Content-Length\"), \"26\")\n        self.assertEqual(response.headers.get(\"Content-Range\"), None)\n\n    def test_static_invalid_range(self):\n        response = self.get_and_head(\"/static/robots.txt\", headers={\"Range\": \"asdf\"})\n        self.assertEqual(response.code, 200)\n\n    def test_static_unsatisfiable_range_zero_suffix(self):\n        response = self.get_and_head(\n            \"/static/robots.txt\", headers={\"Range\": \"bytes=-0\"}\n        )\n        self.assertEqual(response.headers.get(\"Content-Range\"), \"bytes */26\")\n        self.assertEqual(response.code, 416)\n\n    def test_static_unsatisfiable_range_invalid_start(self):\n        response = self.get_and_head(\n            \"/static/robots.txt\", headers={\"Range\": \"bytes=26\"}\n        )\n        self.assertEqual(response.code, 416)\n        self.assertEqual(response.headers.get(\"Content-Range\"), \"bytes */26\")\n\n    def test_static_unsatisfiable_range_end_less_than_start(self):\n        response = self.get_and_head(\n            \"/static/robots.txt\", headers={\"Range\": \"bytes=10-3\"}\n        )\n        self.assertEqual(response.code, 416)\n        self.assertEqual(response.headers.get(\"Content-Range\"), \"bytes */26\")\n\n    def test_static_head(self):\n        response = self.fetch(\"/static/robots.txt\", method=\"HEAD\")\n        self.assertEqual(response.code, 200)\n        # No body was returned, but we did get the right content length.\n        self.assertEqual(response.body, b\"\")\n        self.assertEqual(response.headers[\"Content-Length\"], \"26\")\n        self.assertEqual(\n            utf8(response.headers[\"Etag\"]), b'\"' + self.robots_txt_hash + b'\"'\n        )\n\n    def test_static_head_range(self):\n        response = self.fetch(\n            \"/static/robots.txt\", method=\"HEAD\", headers={\"Range\": \"bytes=1-4\"}\n        )\n        self.assertEqual(response.code, 206)\n        self.assertEqual(response.body, b\"\")\n        self.assertEqual(response.headers[\"Content-Length\"], \"4\")\n        self.assertEqual(\n            utf8(response.headers[\"Etag\"]), b'\"' + self.robots_txt_hash + b'\"'\n        )\n\n    def test_static_range_if_none_match(self):\n        response = self.get_and_head(\n            \"/static/robots.txt\",\n            headers={\n                \"Range\": \"bytes=1-4\",\n                \"If-None-Match\": b'\"' + self.robots_txt_hash + b'\"',\n            },\n        )\n        self.assertEqual(response.code, 304)\n        self.assertEqual(response.body, b\"\")\n        self.assertTrue(\"Content-Length\" not in response.headers)\n        self.assertEqual(\n            utf8(response.headers[\"Etag\"]), b'\"' + self.robots_txt_hash + b'\"'\n        )\n\n    def test_static_404(self):\n        response = self.get_and_head(\"/static/blarg\")\n        self.assertEqual(response.code, 404)\n\n    def test_path_traversal_protection(self):\n        # curl_httpclient processes \"..\" on the client side, so we\n        # must test this with simple_httpclient.\n        self.http_client.close()\n        self.http_client = SimpleAsyncHTTPClient()\n        with ExpectLog(gen_log, \".*not in root static directory\"):\n            response = self.get_and_head(\"/static/../static_foo.txt\")\n        # Attempted path traversal should result in 403, not 200\n        # (which means the check failed and the file was served)\n        # or 404 (which means that the file didn't exist and\n        # is probably a packaging error).\n        self.assertEqual(response.code, 403)\n\n    @unittest.skipIf(os.name != \"posix\", \"non-posix OS\")\n    def test_root_static_path(self):\n        # Sometimes people set the StaticFileHandler's path to '/'\n        # to disable Tornado's path validation (in conjunction with\n        # their own validation in get_absolute_path). Make sure\n        # that the stricter validation in 4.2.1 doesn't break them.\n        path = os.path.join(\n            os.path.dirname(os.path.abspath(__file__)), \"static/robots.txt\"\n        )\n        response = self.get_and_head(\"/root_static\" + urllib.parse.quote(path))\n        self.assertEqual(response.code, 200)\n\n\nclass StaticDefaultFilenameTest(WebTestCase):\n    def get_app_kwargs(self):\n        return dict(\n            static_path=relpath(\"static\"),\n            static_handler_args=dict(default_filename=\"index.html\"),\n        )\n\n    def get_handlers(self):\n        return []\n\n    def test_static_default_filename(self):\n        response = self.fetch(\"/static/dir/\", follow_redirects=False)\n        self.assertEqual(response.code, 200)\n        self.assertEqual(b\"this is the index\\n\", response.body)\n\n    def test_static_default_redirect(self):\n        response = self.fetch(\"/static/dir\", follow_redirects=False)\n        self.assertEqual(response.code, 301)\n        self.assertTrue(response.headers[\"Location\"].endswith(\"/static/dir/\"))\n\n\nclass StaticFileWithPathTest(WebTestCase):\n    def get_app_kwargs(self):\n        return dict(\n            static_path=relpath(\"static\"),\n            static_handler_args=dict(default_filename=\"index.html\"),\n        )\n\n    def get_handlers(self):\n        return [(\"/foo/(.*)\", StaticFileHandler, {\"path\": relpath(\"templates/\")})]\n\n    def test_serve(self):\n        response = self.fetch(\"/foo/utf8.html\")\n        self.assertEqual(response.body, b\"H\\xc3\\xa9llo\\n\")\n\n\nclass CustomStaticFileTest(WebTestCase):\n    def get_handlers(self):\n        class MyStaticFileHandler(StaticFileHandler):\n            @classmethod\n            def make_static_url(cls, settings, path):\n                version_hash = cls.get_version(settings, path)\n                extension_index = path.rindex(\".\")\n                before_version = path[:extension_index]\n                after_version = path[(extension_index + 1) :]\n                return \"/static/%s.%s.%s\" % (\n                    before_version,\n                    version_hash,\n                    after_version,\n                )\n\n            def parse_url_path(self, url_path):\n                extension_index = url_path.rindex(\".\")\n                version_index = url_path.rindex(\".\", 0, extension_index)\n                return \"%s%s\" % (url_path[:version_index], url_path[extension_index:])\n\n            @classmethod\n            def get_absolute_path(cls, settings, path):\n                return \"CustomStaticFileTest:\" + path\n\n            def validate_absolute_path(self, root, absolute_path):\n                return absolute_path\n\n            @classmethod\n            def get_content(self, path, start=None, end=None):\n                assert start is None and end is None\n                if path == \"CustomStaticFileTest:foo.txt\":\n                    return b\"bar\"\n                raise Exception(\"unexpected path %r\" % path)\n\n            def get_content_size(self):\n                if self.absolute_path == \"CustomStaticFileTest:foo.txt\":\n                    return 3\n                raise Exception(\"unexpected path %r\" % self.absolute_path)\n\n            def get_modified_time(self):\n                return None\n\n            @classmethod\n            def get_version(cls, settings, path):\n                return \"42\"\n\n        class StaticUrlHandler(RequestHandler):\n            def get(self, path):\n                self.write(self.static_url(path))\n\n        self.static_handler_class = MyStaticFileHandler\n\n        return [(\"/static_url/(.*)\", StaticUrlHandler)]\n\n    def get_app_kwargs(self):\n        return dict(static_path=\"dummy\", static_handler_class=self.static_handler_class)\n\n    def test_serve(self):\n        response = self.fetch(\"/static/foo.42.txt\")\n        self.assertEqual(response.body, b\"bar\")\n\n    def test_static_url(self):\n        with ExpectLog(gen_log, \"Could not open static file\", required=False):\n            response = self.fetch(\"/static_url/foo.txt\")\n            self.assertEqual(response.body, b\"/static/foo.42.txt\")\n\n\nclass HostMatchingTest(WebTestCase):\n    class Handler(RequestHandler):\n        def initialize(self, reply):\n            self.reply = reply\n\n        def get(self):\n            self.write(self.reply)\n\n    def get_handlers(self):\n        return [(\"/foo\", HostMatchingTest.Handler, {\"reply\": \"wildcard\"})]\n\n    def test_host_matching(self):\n        self.app.add_handlers(\n            \"www.example.com\", [(\"/foo\", HostMatchingTest.Handler, {\"reply\": \"[0]\"})]\n        )\n        self.app.add_handlers(\n            r\"www\\.example\\.com\", [(\"/bar\", HostMatchingTest.Handler, {\"reply\": \"[1]\"})]\n        )\n        self.app.add_handlers(\n            \"www.example.com\", [(\"/baz\", HostMatchingTest.Handler, {\"reply\": \"[2]\"})]\n        )\n        self.app.add_handlers(\n            \"www.e.*e.com\", [(\"/baz\", HostMatchingTest.Handler, {\"reply\": \"[3]\"})]\n        )\n\n        response = self.fetch(\"/foo\")\n        self.assertEqual(response.body, b\"wildcard\")\n        response = self.fetch(\"/bar\")\n        self.assertEqual(response.code, 404)\n        response = self.fetch(\"/baz\")\n        self.assertEqual(response.code, 404)\n\n        response = self.fetch(\"/foo\", headers={\"Host\": \"www.example.com\"})\n        self.assertEqual(response.body, b\"[0]\")\n        response = self.fetch(\"/bar\", headers={\"Host\": \"www.example.com\"})\n        self.assertEqual(response.body, b\"[1]\")\n        response = self.fetch(\"/baz\", headers={\"Host\": \"www.example.com\"})\n        self.assertEqual(response.body, b\"[2]\")\n        response = self.fetch(\"/baz\", headers={\"Host\": \"www.exe.com\"})\n        self.assertEqual(response.body, b\"[3]\")\n\n\nclass DefaultHostMatchingTest(WebTestCase):\n    def get_handlers(self):\n        return []\n\n    def get_app_kwargs(self):\n        return {\"default_host\": \"www.example.com\"}\n\n    def test_default_host_matching(self):\n        self.app.add_handlers(\n            \"www.example.com\", [(\"/foo\", HostMatchingTest.Handler, {\"reply\": \"[0]\"})]\n        )\n        self.app.add_handlers(\n            r\"www\\.example\\.com\", [(\"/bar\", HostMatchingTest.Handler, {\"reply\": \"[1]\"})]\n        )\n        self.app.add_handlers(\n            \"www.test.com\", [(\"/baz\", HostMatchingTest.Handler, {\"reply\": \"[2]\"})]\n        )\n\n        response = self.fetch(\"/foo\")\n        self.assertEqual(response.body, b\"[0]\")\n        response = self.fetch(\"/bar\")\n        self.assertEqual(response.body, b\"[1]\")\n        response = self.fetch(\"/baz\")\n        self.assertEqual(response.code, 404)\n\n        response = self.fetch(\"/foo\", headers={\"X-Real-Ip\": \"127.0.0.1\"})\n        self.assertEqual(response.code, 404)\n\n        self.app.default_host = \"www.test.com\"\n\n        response = self.fetch(\"/baz\")\n        self.assertEqual(response.body, b\"[2]\")\n\n\nclass NamedURLSpecGroupsTest(WebTestCase):\n    def get_handlers(self):\n        class EchoHandler(RequestHandler):\n            def get(self, path):\n                self.write(path)\n\n        return [\n            (\"/str/(?P<path>.*)\", EchoHandler),\n            (u\"/unicode/(?P<path>.*)\", EchoHandler),\n        ]\n\n    def test_named_urlspec_groups(self):\n        response = self.fetch(\"/str/foo\")\n        self.assertEqual(response.body, b\"foo\")\n\n        response = self.fetch(\"/unicode/bar\")\n        self.assertEqual(response.body, b\"bar\")\n\n\nclass ClearHeaderTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            self.set_header(\"h1\", \"foo\")\n            self.set_header(\"h2\", \"bar\")\n            self.clear_header(\"h1\")\n            self.clear_header(\"nonexistent\")\n\n    def test_clear_header(self):\n        response = self.fetch(\"/\")\n        self.assertTrue(\"h1\" not in response.headers)\n        self.assertEqual(response.headers[\"h2\"], \"bar\")\n\n\nclass Header204Test(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            self.set_status(204)\n            self.finish()\n\n    def test_204_headers(self):\n        response = self.fetch(\"/\")\n        self.assertEqual(response.code, 204)\n        self.assertNotIn(\"Content-Length\", response.headers)\n        self.assertNotIn(\"Transfer-Encoding\", response.headers)\n\n\nclass Header304Test(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            self.set_header(\"Content-Language\", \"en_US\")\n            self.write(\"hello\")\n\n    def test_304_headers(self):\n        response1 = self.fetch(\"/\")\n        self.assertEqual(response1.headers[\"Content-Length\"], \"5\")\n        self.assertEqual(response1.headers[\"Content-Language\"], \"en_US\")\n\n        response2 = self.fetch(\n            \"/\", headers={\"If-None-Match\": response1.headers[\"Etag\"]}\n        )\n        self.assertEqual(response2.code, 304)\n        self.assertTrue(\"Content-Length\" not in response2.headers)\n        self.assertTrue(\"Content-Language\" not in response2.headers)\n        # Not an entity header, but should not be added to 304s by chunking\n        self.assertTrue(\"Transfer-Encoding\" not in response2.headers)\n\n\nclass StatusReasonTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            reason = self.request.arguments.get(\"reason\", [])\n            self.set_status(\n                int(self.get_argument(\"code\")), reason=reason[0] if reason else None\n            )\n\n    def get_http_client(self):\n        # simple_httpclient only: curl doesn't expose the reason string\n        return SimpleAsyncHTTPClient()\n\n    def test_status(self):\n        response = self.fetch(\"/?code=304\")\n        self.assertEqual(response.code, 304)\n        self.assertEqual(response.reason, \"Not Modified\")\n        response = self.fetch(\"/?code=304&reason=Foo\")\n        self.assertEqual(response.code, 304)\n        self.assertEqual(response.reason, \"Foo\")\n        response = self.fetch(\"/?code=682&reason=Bar\")\n        self.assertEqual(response.code, 682)\n        self.assertEqual(response.reason, \"Bar\")\n        response = self.fetch(\"/?code=682\")\n        self.assertEqual(response.code, 682)\n        self.assertEqual(response.reason, \"Unknown\")\n\n\nclass DateHeaderTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            self.write(\"hello\")\n\n    def test_date_header(self):\n        response = self.fetch(\"/\")\n        parsed = email.utils.parsedate(response.headers[\"Date\"])\n        assert parsed is not None\n        header_date = datetime.datetime(*parsed[:6])\n        self.assertTrue(\n            header_date - datetime.datetime.utcnow() < datetime.timedelta(seconds=2)\n        )\n\n\nclass RaiseWithReasonTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            raise HTTPError(682, reason=\"Foo\")\n\n    def get_http_client(self):\n        # simple_httpclient only: curl doesn't expose the reason string\n        return SimpleAsyncHTTPClient()\n\n    def test_raise_with_reason(self):\n        response = self.fetch(\"/\")\n        self.assertEqual(response.code, 682)\n        self.assertEqual(response.reason, \"Foo\")\n        self.assertIn(b\"682: Foo\", response.body)\n\n    def test_httperror_str(self):\n        self.assertEqual(str(HTTPError(682, reason=\"Foo\")), \"HTTP 682: Foo\")\n\n    def test_httperror_str_from_httputil(self):\n        self.assertEqual(str(HTTPError(682)), \"HTTP 682: Unknown\")\n\n\nclass ErrorHandlerXSRFTest(WebTestCase):\n    def get_handlers(self):\n        # note that if the handlers list is empty we get the default_host\n        # redirect fallback instead of a 404, so test with both an\n        # explicitly defined error handler and an implicit 404.\n        return [(\"/error\", ErrorHandler, dict(status_code=417))]\n\n    def get_app_kwargs(self):\n        return dict(xsrf_cookies=True)\n\n    def test_error_xsrf(self):\n        response = self.fetch(\"/error\", method=\"POST\", body=\"\")\n        self.assertEqual(response.code, 417)\n\n    def test_404_xsrf(self):\n        response = self.fetch(\"/404\", method=\"POST\", body=\"\")\n        self.assertEqual(response.code, 404)\n\n\nclass GzipTestCase(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            for v in self.get_arguments(\"vary\"):\n                self.add_header(\"Vary\", v)\n            # Must write at least MIN_LENGTH bytes to activate compression.\n            self.write(\"hello world\" + (\"!\" * GZipContentEncoding.MIN_LENGTH))\n\n    def get_app_kwargs(self):\n        return dict(\n            gzip=True, static_path=os.path.join(os.path.dirname(__file__), \"static\")\n        )\n\n    def assert_compressed(self, response):\n        # simple_httpclient renames the content-encoding header;\n        # curl_httpclient doesn't.\n        self.assertEqual(\n            response.headers.get(\n                \"Content-Encoding\", response.headers.get(\"X-Consumed-Content-Encoding\")\n            ),\n            \"gzip\",\n        )\n\n    def test_gzip(self):\n        response = self.fetch(\"/\")\n        self.assert_compressed(response)\n        self.assertEqual(response.headers[\"Vary\"], \"Accept-Encoding\")\n\n    def test_gzip_static(self):\n        # The streaming responses in StaticFileHandler have subtle\n        # interactions with the gzip output so test this case separately.\n        response = self.fetch(\"/robots.txt\")\n        self.assert_compressed(response)\n        self.assertEqual(response.headers[\"Vary\"], \"Accept-Encoding\")\n\n    def test_gzip_not_requested(self):\n        response = self.fetch(\"/\", use_gzip=False)\n        self.assertNotIn(\"Content-Encoding\", response.headers)\n        self.assertEqual(response.headers[\"Vary\"], \"Accept-Encoding\")\n\n    def test_vary_already_present(self):\n        response = self.fetch(\"/?vary=Accept-Language\")\n        self.assert_compressed(response)\n        self.assertEqual(\n            [s.strip() for s in response.headers[\"Vary\"].split(\",\")],\n            [\"Accept-Language\", \"Accept-Encoding\"],\n        )\n\n    def test_vary_already_present_multiple(self):\n        # Regression test for https://github.com/tornadoweb/tornado/issues/1670\n        response = self.fetch(\"/?vary=Accept-Language&vary=Cookie\")\n        self.assert_compressed(response)\n        self.assertEqual(\n            [s.strip() for s in response.headers[\"Vary\"].split(\",\")],\n            [\"Accept-Language\", \"Cookie\", \"Accept-Encoding\"],\n        )\n\n\nclass PathArgsInPrepareTest(WebTestCase):\n    class Handler(RequestHandler):\n        def prepare(self):\n            self.write(dict(args=self.path_args, kwargs=self.path_kwargs))\n\n        def get(self, path):\n            assert path == \"foo\"\n            self.finish()\n\n    def get_handlers(self):\n        return [(\"/pos/(.*)\", self.Handler), (\"/kw/(?P<path>.*)\", self.Handler)]\n\n    def test_pos(self):\n        response = self.fetch(\"/pos/foo\")\n        response.rethrow()\n        data = json_decode(response.body)\n        self.assertEqual(data, {\"args\": [\"foo\"], \"kwargs\": {}})\n\n    def test_kw(self):\n        response = self.fetch(\"/kw/foo\")\n        response.rethrow()\n        data = json_decode(response.body)\n        self.assertEqual(data, {\"args\": [], \"kwargs\": {\"path\": \"foo\"}})\n\n\nclass ClearAllCookiesTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            self.clear_all_cookies()\n            self.write(\"ok\")\n\n    def test_clear_all_cookies(self):\n        response = self.fetch(\"/\", headers={\"Cookie\": \"foo=bar; baz=xyzzy\"})\n        set_cookies = sorted(response.headers.get_list(\"Set-Cookie\"))\n        # Python 3.5 sends 'baz=\"\";'; older versions use 'baz=;'\n        self.assertTrue(\n            set_cookies[0].startswith(\"baz=;\") or set_cookies[0].startswith('baz=\"\";')\n        )\n        self.assertTrue(\n            set_cookies[1].startswith(\"foo=;\") or set_cookies[1].startswith('foo=\"\";')\n        )\n\n\nclass PermissionError(Exception):\n    pass\n\n\nclass ExceptionHandlerTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            exc = self.get_argument(\"exc\")\n            if exc == \"http\":\n                raise HTTPError(410, \"no longer here\")\n            elif exc == \"zero\":\n                1 / 0\n            elif exc == \"permission\":\n                raise PermissionError(\"not allowed\")\n\n        def write_error(self, status_code, **kwargs):\n            if \"exc_info\" in kwargs:\n                typ, value, tb = kwargs[\"exc_info\"]\n                if isinstance(value, PermissionError):\n                    self.set_status(403)\n                    self.write(\"PermissionError\")\n                    return\n            RequestHandler.write_error(self, status_code, **kwargs)\n\n        def log_exception(self, typ, value, tb):\n            if isinstance(value, PermissionError):\n                app_log.warning(\"custom logging for PermissionError: %s\", value.args[0])\n            else:\n                RequestHandler.log_exception(self, typ, value, tb)\n\n    def test_http_error(self):\n        # HTTPErrors are logged as warnings with no stack trace.\n        # TODO: extend ExpectLog to test this more precisely\n        with ExpectLog(gen_log, \".*no longer here\"):\n            response = self.fetch(\"/?exc=http\")\n            self.assertEqual(response.code, 410)\n\n    def test_unknown_error(self):\n        # Unknown errors are logged as errors with a stack trace.\n        with ExpectLog(app_log, \"Uncaught exception\"):\n            response = self.fetch(\"/?exc=zero\")\n            self.assertEqual(response.code, 500)\n\n    def test_known_error(self):\n        # log_exception can override logging behavior, and write_error\n        # can override the response.\n        with ExpectLog(app_log, \"custom logging for PermissionError: not allowed\"):\n            response = self.fetch(\"/?exc=permission\")\n            self.assertEqual(response.code, 403)\n\n\nclass BuggyLoggingTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            1 / 0\n\n        def log_exception(self, typ, value, tb):\n            1 / 0\n\n    def test_buggy_log_exception(self):\n        # Something gets logged even though the application's\n        # logger is broken.\n        with ExpectLog(app_log, \".*\"):\n            self.fetch(\"/\")\n\n\nclass UIMethodUIModuleTest(SimpleHandlerTestCase):\n    \"\"\"Test that UI methods and modules are created correctly and\n    associated with the handler.\n    \"\"\"\n\n    class Handler(RequestHandler):\n        def get(self):\n            self.render(\"foo.html\")\n\n        def value(self):\n            return self.get_argument(\"value\")\n\n    def get_app_kwargs(self):\n        def my_ui_method(handler, x):\n            return \"In my_ui_method(%s) with handler value %s.\" % (x, handler.value())\n\n        class MyModule(UIModule):\n            def render(self, x):\n                return \"In MyModule(%s) with handler value %s.\" % (\n                    x,\n                    self.handler.value(),\n                )\n\n        loader = DictLoader(\n            {\"foo.html\": \"{{ my_ui_method(42) }} {% module MyModule(123) %}\"}\n        )\n        return dict(\n            template_loader=loader,\n            ui_methods={\"my_ui_method\": my_ui_method},\n            ui_modules={\"MyModule\": MyModule},\n        )\n\n    def tearDown(self):\n        super(UIMethodUIModuleTest, self).tearDown()\n        # TODO: fix template loader caching so this isn't necessary.\n        RequestHandler._template_loaders.clear()\n\n    def test_ui_method(self):\n        response = self.fetch(\"/?value=asdf\")\n        self.assertEqual(\n            response.body,\n            b\"In my_ui_method(42) with handler value asdf. \"\n            b\"In MyModule(123) with handler value asdf.\",\n        )\n\n\nclass GetArgumentErrorTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            try:\n                self.get_argument(\"foo\")\n                self.write({})\n            except MissingArgumentError as e:\n                self.write({\"arg_name\": e.arg_name, \"log_message\": e.log_message})\n\n    def test_catch_error(self):\n        response = self.fetch(\"/\")\n        self.assertEqual(\n            json_decode(response.body),\n            {\"arg_name\": \"foo\", \"log_message\": \"Missing argument foo\"},\n        )\n\n\nclass SetLazyPropertiesTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def prepare(self):\n            self.current_user = \"Ben\"\n            self.locale = locale.get(\"en_US\")\n\n        def get_user_locale(self):\n            raise NotImplementedError()\n\n        def get_current_user(self):\n            raise NotImplementedError()\n\n        def get(self):\n            self.write(\"Hello %s (%s)\" % (self.current_user, self.locale.code))\n\n    def test_set_properties(self):\n        # Ensure that current_user can be assigned to normally for apps\n        # that want to forgo the lazy get_current_user property\n        response = self.fetch(\"/\")\n        self.assertEqual(response.body, b\"Hello Ben (en_US)\")\n\n\nclass GetCurrentUserTest(WebTestCase):\n    def get_app_kwargs(self):\n        class WithoutUserModule(UIModule):\n            def render(self):\n                return \"\"\n\n        class WithUserModule(UIModule):\n            def render(self):\n                return str(self.current_user)\n\n        loader = DictLoader(\n            {\n                \"without_user.html\": \"\",\n                \"with_user.html\": \"{{ current_user }}\",\n                \"without_user_module.html\": \"{% module WithoutUserModule() %}\",\n                \"with_user_module.html\": \"{% module WithUserModule() %}\",\n            }\n        )\n        return dict(\n            template_loader=loader,\n            ui_modules={\n                \"WithUserModule\": WithUserModule,\n                \"WithoutUserModule\": WithoutUserModule,\n            },\n        )\n\n    def tearDown(self):\n        super(GetCurrentUserTest, self).tearDown()\n        RequestHandler._template_loaders.clear()\n\n    def get_handlers(self):\n        class CurrentUserHandler(RequestHandler):\n            def prepare(self):\n                self.has_loaded_current_user = False\n\n            def get_current_user(self):\n                self.has_loaded_current_user = True\n                return \"\"\n\n        class WithoutUserHandler(CurrentUserHandler):\n            def get(self):\n                self.render_string(\"without_user.html\")\n                self.finish(str(self.has_loaded_current_user))\n\n        class WithUserHandler(CurrentUserHandler):\n            def get(self):\n                self.render_string(\"with_user.html\")\n                self.finish(str(self.has_loaded_current_user))\n\n        class CurrentUserModuleHandler(CurrentUserHandler):\n            def get_template_namespace(self):\n                # If RequestHandler.get_template_namespace is called, then\n                # get_current_user is evaluated. Until #820 is fixed, this\n                # is a small hack to circumvent the issue.\n                return self.ui\n\n        class WithoutUserModuleHandler(CurrentUserModuleHandler):\n            def get(self):\n                self.render_string(\"without_user_module.html\")\n                self.finish(str(self.has_loaded_current_user))\n\n        class WithUserModuleHandler(CurrentUserModuleHandler):\n            def get(self):\n                self.render_string(\"with_user_module.html\")\n                self.finish(str(self.has_loaded_current_user))\n\n        return [\n            (\"/without_user\", WithoutUserHandler),\n            (\"/with_user\", WithUserHandler),\n            (\"/without_user_module\", WithoutUserModuleHandler),\n            (\"/with_user_module\", WithUserModuleHandler),\n        ]\n\n    @unittest.skip(\"needs fix\")\n    def test_get_current_user_is_lazy(self):\n        # TODO: Make this test pass. See #820.\n        response = self.fetch(\"/without_user\")\n        self.assertEqual(response.body, b\"False\")\n\n    def test_get_current_user_works(self):\n        response = self.fetch(\"/with_user\")\n        self.assertEqual(response.body, b\"True\")\n\n    def test_get_current_user_from_ui_module_is_lazy(self):\n        response = self.fetch(\"/without_user_module\")\n        self.assertEqual(response.body, b\"False\")\n\n    def test_get_current_user_from_ui_module_works(self):\n        response = self.fetch(\"/with_user_module\")\n        self.assertEqual(response.body, b\"True\")\n\n\nclass UnimplementedHTTPMethodsTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        pass\n\n    def test_unimplemented_standard_methods(self):\n        for method in [\"HEAD\", \"GET\", \"DELETE\", \"OPTIONS\"]:\n            response = self.fetch(\"/\", method=method)\n            self.assertEqual(response.code, 405)\n        for method in [\"POST\", \"PUT\"]:\n            response = self.fetch(\"/\", method=method, body=b\"\")\n            self.assertEqual(response.code, 405)\n\n\nclass UnimplementedNonStandardMethodsTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def other(self):\n            # Even though this method exists, it won't get called automatically\n            # because it is not in SUPPORTED_METHODS.\n            self.write(\"other\")\n\n    def test_unimplemented_patch(self):\n        # PATCH is recently standardized; Tornado supports it by default\n        # but wsgiref.validate doesn't like it.\n        response = self.fetch(\"/\", method=\"PATCH\", body=b\"\")\n        self.assertEqual(response.code, 405)\n\n    def test_unimplemented_other(self):\n        response = self.fetch(\"/\", method=\"OTHER\", allow_nonstandard_methods=True)\n        self.assertEqual(response.code, 405)\n\n\nclass AllHTTPMethodsTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def method(self):\n            self.write(self.request.method)\n\n        get = delete = options = post = put = method  # type: ignore\n\n    def test_standard_methods(self):\n        response = self.fetch(\"/\", method=\"HEAD\")\n        self.assertEqual(response.body, b\"\")\n        for method in [\"GET\", \"DELETE\", \"OPTIONS\"]:\n            response = self.fetch(\"/\", method=method)\n            self.assertEqual(response.body, utf8(method))\n        for method in [\"POST\", \"PUT\"]:\n            response = self.fetch(\"/\", method=method, body=b\"\")\n            self.assertEqual(response.body, utf8(method))\n\n\nclass PatchMethodTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        SUPPORTED_METHODS = RequestHandler.SUPPORTED_METHODS + (  # type: ignore\n            \"OTHER\",\n        )\n\n        def patch(self):\n            self.write(\"patch\")\n\n        def other(self):\n            self.write(\"other\")\n\n    def test_patch(self):\n        response = self.fetch(\"/\", method=\"PATCH\", body=b\"\")\n        self.assertEqual(response.body, b\"patch\")\n\n    def test_other(self):\n        response = self.fetch(\"/\", method=\"OTHER\", allow_nonstandard_methods=True)\n        self.assertEqual(response.body, b\"other\")\n\n\nclass FinishInPrepareTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def prepare(self):\n            self.finish(\"done\")\n\n        def get(self):\n            # It's difficult to assert for certain that a method did not\n            # or will not be called in an asynchronous context, but this\n            # will be logged noisily if it is reached.\n            raise Exception(\"should not reach this method\")\n\n    def test_finish_in_prepare(self):\n        response = self.fetch(\"/\")\n        self.assertEqual(response.body, b\"done\")\n\n\nclass Default404Test(WebTestCase):\n    def get_handlers(self):\n        # If there are no handlers at all a default redirect handler gets added.\n        return [(\"/foo\", RequestHandler)]\n\n    def test_404(self):\n        response = self.fetch(\"/\")\n        self.assertEqual(response.code, 404)\n        self.assertEqual(\n            response.body,\n            b\"<html><title>404: Not Found</title>\"\n            b\"<body>404: Not Found</body></html>\",\n        )\n\n\nclass Custom404Test(WebTestCase):\n    def get_handlers(self):\n        return [(\"/foo\", RequestHandler)]\n\n    def get_app_kwargs(self):\n        class Custom404Handler(RequestHandler):\n            def get(self):\n                self.set_status(404)\n                self.write(\"custom 404 response\")\n\n        return dict(default_handler_class=Custom404Handler)\n\n    def test_404(self):\n        response = self.fetch(\"/\")\n        self.assertEqual(response.code, 404)\n        self.assertEqual(response.body, b\"custom 404 response\")\n\n\nclass DefaultHandlerArgumentsTest(WebTestCase):\n    def get_handlers(self):\n        return [(\"/foo\", RequestHandler)]\n\n    def get_app_kwargs(self):\n        return dict(\n            default_handler_class=ErrorHandler,\n            default_handler_args=dict(status_code=403),\n        )\n\n    def test_403(self):\n        response = self.fetch(\"/\")\n        self.assertEqual(response.code, 403)\n\n\nclass HandlerByNameTest(WebTestCase):\n    def get_handlers(self):\n        # All three are equivalent.\n        return [\n            (\"/hello1\", HelloHandler),\n            (\"/hello2\", \"tornado.test.web_test.HelloHandler\"),\n            url(\"/hello3\", \"tornado.test.web_test.HelloHandler\"),\n        ]\n\n    def test_handler_by_name(self):\n        resp = self.fetch(\"/hello1\")\n        self.assertEqual(resp.body, b\"hello\")\n        resp = self.fetch(\"/hello2\")\n        self.assertEqual(resp.body, b\"hello\")\n        resp = self.fetch(\"/hello3\")\n        self.assertEqual(resp.body, b\"hello\")\n\n\nclass StreamingRequestBodyTest(WebTestCase):\n    def get_handlers(self):\n        @stream_request_body\n        class StreamingBodyHandler(RequestHandler):\n            def initialize(self, test):\n                self.test = test\n\n            def prepare(self):\n                self.test.prepared.set_result(None)\n\n            def data_received(self, data):\n                self.test.data.set_result(data)\n\n            def get(self):\n                self.test.finished.set_result(None)\n                self.write({})\n\n        @stream_request_body\n        class EarlyReturnHandler(RequestHandler):\n            def prepare(self):\n                # If we finish the response in prepare, it won't continue to\n                # the (non-existent) data_received.\n                raise HTTPError(401)\n\n        @stream_request_body\n        class CloseDetectionHandler(RequestHandler):\n            def initialize(self, test):\n                self.test = test\n\n            def on_connection_close(self):\n                super(CloseDetectionHandler, self).on_connection_close()\n                self.test.close_future.set_result(None)\n\n        return [\n            (\"/stream_body\", StreamingBodyHandler, dict(test=self)),\n            (\"/early_return\", EarlyReturnHandler),\n            (\"/close_detection\", CloseDetectionHandler, dict(test=self)),\n        ]\n\n    def connect(self, url, connection_close):\n        # Use a raw connection so we can control the sending of data.\n        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)\n        s.connect((\"127.0.0.1\", self.get_http_port()))\n        stream = IOStream(s)\n        stream.write(b\"GET \" + url + b\" HTTP/1.1\\r\\n\")\n        if connection_close:\n            stream.write(b\"Connection: close\\r\\n\")\n        stream.write(b\"Transfer-Encoding: chunked\\r\\n\\r\\n\")\n        return stream\n\n    @gen_test\n    def test_streaming_body(self):\n        self.prepared = Future()  # type: Future[None]\n        self.data = Future()  # type: Future[bytes]\n        self.finished = Future()  # type: Future[None]\n\n        stream = self.connect(b\"/stream_body\", connection_close=True)\n        yield self.prepared\n        stream.write(b\"4\\r\\nasdf\\r\\n\")\n        # Ensure the first chunk is received before we send the second.\n        data = yield self.data\n        self.assertEqual(data, b\"asdf\")\n        self.data = Future()\n        stream.write(b\"4\\r\\nqwer\\r\\n\")\n        data = yield self.data\n        self.assertEquals(data, b\"qwer\")\n        stream.write(b\"0\\r\\n\\r\\n\")\n        yield self.finished\n        data = yield stream.read_until_close()\n        # This would ideally use an HTTP1Connection to read the response.\n        self.assertTrue(data.endswith(b\"{}\"))\n        stream.close()\n\n    @gen_test\n    def test_early_return(self):\n        stream = self.connect(b\"/early_return\", connection_close=False)\n        data = yield stream.read_until_close()\n        self.assertTrue(data.startswith(b\"HTTP/1.1 401\"))\n\n    @gen_test\n    def test_early_return_with_data(self):\n        stream = self.connect(b\"/early_return\", connection_close=False)\n        stream.write(b\"4\\r\\nasdf\\r\\n\")\n        data = yield stream.read_until_close()\n        self.assertTrue(data.startswith(b\"HTTP/1.1 401\"))\n\n    @gen_test\n    def test_close_during_upload(self):\n        self.close_future = Future()  # type: Future[None]\n        stream = self.connect(b\"/close_detection\", connection_close=False)\n        stream.close()\n        yield self.close_future\n\n\n# Each method in this handler returns a yieldable object and yields to the\n# IOLoop so the future is not immediately ready.  Ensure that the\n# yieldables are respected and no method is called before the previous\n# one has completed.\n@stream_request_body\nclass BaseFlowControlHandler(RequestHandler):\n    def initialize(self, test):\n        self.test = test\n        self.method = None\n        self.methods = []  # type: typing.List[str]\n\n    @contextlib.contextmanager\n    def in_method(self, method):\n        if self.method is not None:\n            self.test.fail(\"entered method %s while in %s\" % (method, self.method))\n        self.method = method\n        self.methods.append(method)\n        try:\n            yield\n        finally:\n            self.method = None\n\n    @gen.coroutine\n    def prepare(self):\n        # Note that asynchronous prepare() does not block data_received,\n        # so we don't use in_method here.\n        self.methods.append(\"prepare\")\n        yield gen.moment\n\n    @gen.coroutine\n    def post(self):\n        with self.in_method(\"post\"):\n            yield gen.moment\n        self.write(dict(methods=self.methods))\n\n\nclass BaseStreamingRequestFlowControlTest(object):\n    def get_httpserver_options(self):\n        # Use a small chunk size so flow control is relevant even though\n        # all the data arrives at once.\n        return dict(chunk_size=10, decompress_request=True)\n\n    def get_http_client(self):\n        # simple_httpclient only: curl doesn't support body_producer.\n        return SimpleAsyncHTTPClient()\n\n    # Test all the slightly different code paths for fixed, chunked, etc bodies.\n    def test_flow_control_fixed_body(self):\n        response = self.fetch(\"/\", body=\"abcdefghijklmnopqrstuvwxyz\", method=\"POST\")\n        response.rethrow()\n        self.assertEqual(\n            json_decode(response.body),\n            dict(\n                methods=[\n                    \"prepare\",\n                    \"data_received\",\n                    \"data_received\",\n                    \"data_received\",\n                    \"post\",\n                ]\n            ),\n        )\n\n    def test_flow_control_chunked_body(self):\n        chunks = [b\"abcd\", b\"efgh\", b\"ijkl\"]\n\n        @gen.coroutine\n        def body_producer(write):\n            for i in chunks:\n                yield write(i)\n\n        response = self.fetch(\"/\", body_producer=body_producer, method=\"POST\")\n        response.rethrow()\n        self.assertEqual(\n            json_decode(response.body),\n            dict(\n                methods=[\n                    \"prepare\",\n                    \"data_received\",\n                    \"data_received\",\n                    \"data_received\",\n                    \"post\",\n                ]\n            ),\n        )\n\n    def test_flow_control_compressed_body(self):\n        bytesio = BytesIO()\n        gzip_file = gzip.GzipFile(mode=\"w\", fileobj=bytesio)\n        gzip_file.write(b\"abcdefghijklmnopqrstuvwxyz\")\n        gzip_file.close()\n        compressed_body = bytesio.getvalue()\n        response = self.fetch(\n            \"/\",\n            body=compressed_body,\n            method=\"POST\",\n            headers={\"Content-Encoding\": \"gzip\"},\n        )\n        response.rethrow()\n        self.assertEqual(\n            json_decode(response.body),\n            dict(\n                methods=[\n                    \"prepare\",\n                    \"data_received\",\n                    \"data_received\",\n                    \"data_received\",\n                    \"post\",\n                ]\n            ),\n        )\n\n\nclass DecoratedStreamingRequestFlowControlTest(\n    BaseStreamingRequestFlowControlTest, WebTestCase\n):\n    def get_handlers(self):\n        class DecoratedFlowControlHandler(BaseFlowControlHandler):\n            @gen.coroutine\n            def data_received(self, data):\n                with self.in_method(\"data_received\"):\n                    yield gen.moment\n\n        return [(\"/\", DecoratedFlowControlHandler, dict(test=self))]\n\n\nclass NativeStreamingRequestFlowControlTest(\n    BaseStreamingRequestFlowControlTest, WebTestCase\n):\n    def get_handlers(self):\n        class NativeFlowControlHandler(BaseFlowControlHandler):\n            async def data_received(self, data):\n                with self.in_method(\"data_received\"):\n                    import asyncio\n\n                    await asyncio.sleep(0)\n\n        return [(\"/\", NativeFlowControlHandler, dict(test=self))]\n\n\nclass IncorrectContentLengthTest(SimpleHandlerTestCase):\n    def get_handlers(self):\n        test = self\n        self.server_error = None\n\n        # Manually set a content-length that doesn't match the actual content.\n        class TooHigh(RequestHandler):\n            def get(self):\n                self.set_header(\"Content-Length\", \"42\")\n                try:\n                    self.finish(\"ok\")\n                except Exception as e:\n                    test.server_error = e\n                    raise\n\n        class TooLow(RequestHandler):\n            def get(self):\n                self.set_header(\"Content-Length\", \"2\")\n                try:\n                    self.finish(\"hello\")\n                except Exception as e:\n                    test.server_error = e\n                    raise\n\n        return [(\"/high\", TooHigh), (\"/low\", TooLow)]\n\n    def test_content_length_too_high(self):\n        # When the content-length is too high, the connection is simply\n        # closed without completing the response.  An error is logged on\n        # the server.\n        with ExpectLog(app_log, \"(Uncaught exception|Exception in callback)\"):\n            with ExpectLog(\n                gen_log,\n                \"(Cannot send error response after headers written\"\n                \"|Failed to flush partial response)\",\n            ):\n                with self.assertRaises(HTTPClientError):\n                    self.fetch(\"/high\", raise_error=True)\n        self.assertEqual(\n            str(self.server_error), \"Tried to write 40 bytes less than Content-Length\"\n        )\n\n    def test_content_length_too_low(self):\n        # When the content-length is too low, the connection is closed\n        # without writing the last chunk, so the client never sees the request\n        # complete (which would be a framing error).\n        with ExpectLog(app_log, \"(Uncaught exception|Exception in callback)\"):\n            with ExpectLog(\n                gen_log,\n                \"(Cannot send error response after headers written\"\n                \"|Failed to flush partial response)\",\n            ):\n                with self.assertRaises(HTTPClientError):\n                    self.fetch(\"/low\", raise_error=True)\n        self.assertEqual(\n            str(self.server_error), \"Tried to write more data than Content-Length\"\n        )\n\n\nclass ClientCloseTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            if self.request.version.startswith(\"HTTP/1\"):\n                # Simulate a connection closed by the client during\n                # request processing.  The client will see an error, but the\n                # server should respond gracefully (without logging errors\n                # because we were unable to write out as many bytes as\n                # Content-Length said we would)\n                self.request.connection.stream.close()\n                self.write(\"hello\")\n            else:\n                # TODO: add a HTTP2-compatible version of this test.\n                self.write(\"requires HTTP/1.x\")\n\n    def test_client_close(self):\n        with self.assertRaises((HTTPClientError, unittest.SkipTest)):\n            response = self.fetch(\"/\", raise_error=True)\n            if response.body == b\"requires HTTP/1.x\":\n                self.skipTest(\"requires HTTP/1.x\")\n            self.assertEqual(response.code, 599)\n\n\nclass SignedValueTest(unittest.TestCase):\n    SECRET = \"It's a secret to everybody\"\n    SECRET_DICT = {0: \"asdfbasdf\", 1: \"12312312\", 2: \"2342342\"}\n\n    def past(self):\n        return self.present() - 86400 * 32\n\n    def present(self):\n        return 1300000000\n\n    def test_known_values(self):\n        signed_v1 = create_signed_value(\n            SignedValueTest.SECRET, \"key\", \"value\", version=1, clock=self.present\n        )\n        self.assertEqual(\n            signed_v1, b\"dmFsdWU=|1300000000|31c934969f53e48164c50768b40cbd7e2daaaa4f\"\n        )\n\n        signed_v2 = create_signed_value(\n            SignedValueTest.SECRET, \"key\", \"value\", version=2, clock=self.present\n        )\n        self.assertEqual(\n            signed_v2,\n            b\"2|1:0|10:1300000000|3:key|8:dmFsdWU=|\"\n            b\"3d4e60b996ff9c5d5788e333a0cba6f238a22c6c0f94788870e1a9ecd482e152\",\n        )\n\n        signed_default = create_signed_value(\n            SignedValueTest.SECRET, \"key\", \"value\", clock=self.present\n        )\n        self.assertEqual(signed_default, signed_v2)\n\n        decoded_v1 = decode_signed_value(\n            SignedValueTest.SECRET, \"key\", signed_v1, min_version=1, clock=self.present\n        )\n        self.assertEqual(decoded_v1, b\"value\")\n\n        decoded_v2 = decode_signed_value(\n            SignedValueTest.SECRET, \"key\", signed_v2, min_version=2, clock=self.present\n        )\n        self.assertEqual(decoded_v2, b\"value\")\n\n    def test_name_swap(self):\n        signed1 = create_signed_value(\n            SignedValueTest.SECRET, \"key1\", \"value\", clock=self.present\n        )\n        signed2 = create_signed_value(\n            SignedValueTest.SECRET, \"key2\", \"value\", clock=self.present\n        )\n        # Try decoding each string with the other's \"name\"\n        decoded1 = decode_signed_value(\n            SignedValueTest.SECRET, \"key2\", signed1, clock=self.present\n        )\n        self.assertIs(decoded1, None)\n        decoded2 = decode_signed_value(\n            SignedValueTest.SECRET, \"key1\", signed2, clock=self.present\n        )\n        self.assertIs(decoded2, None)\n\n    def test_expired(self):\n        signed = create_signed_value(\n            SignedValueTest.SECRET, \"key1\", \"value\", clock=self.past\n        )\n        decoded_past = decode_signed_value(\n            SignedValueTest.SECRET, \"key1\", signed, clock=self.past\n        )\n        self.assertEqual(decoded_past, b\"value\")\n        decoded_present = decode_signed_value(\n            SignedValueTest.SECRET, \"key1\", signed, clock=self.present\n        )\n        self.assertIs(decoded_present, None)\n\n    def test_payload_tampering(self):\n        # These cookies are variants of the one in test_known_values.\n        sig = \"3d4e60b996ff9c5d5788e333a0cba6f238a22c6c0f94788870e1a9ecd482e152\"\n\n        def validate(prefix):\n            return b\"value\" == decode_signed_value(\n                SignedValueTest.SECRET, \"key\", prefix + sig, clock=self.present\n            )\n\n        self.assertTrue(validate(\"2|1:0|10:1300000000|3:key|8:dmFsdWU=|\"))\n        # Change key version\n        self.assertFalse(validate(\"2|1:1|10:1300000000|3:key|8:dmFsdWU=|\"))\n        # length mismatch (field too short)\n        self.assertFalse(validate(\"2|1:0|10:130000000|3:key|8:dmFsdWU=|\"))\n        # length mismatch (field too long)\n        self.assertFalse(validate(\"2|1:0|10:1300000000|3:keey|8:dmFsdWU=|\"))\n\n    def test_signature_tampering(self):\n        prefix = \"2|1:0|10:1300000000|3:key|8:dmFsdWU=|\"\n\n        def validate(sig):\n            return b\"value\" == decode_signed_value(\n                SignedValueTest.SECRET, \"key\", prefix + sig, clock=self.present\n            )\n\n        self.assertTrue(\n            validate(\"3d4e60b996ff9c5d5788e333a0cba6f238a22c6c0f94788870e1a9ecd482e152\")\n        )\n        # All zeros\n        self.assertFalse(validate(\"0\" * 32))\n        # Change one character\n        self.assertFalse(\n            validate(\"4d4e60b996ff9c5d5788e333a0cba6f238a22c6c0f94788870e1a9ecd482e152\")\n        )\n        # Change another character\n        self.assertFalse(\n            validate(\"3d4e60b996ff9c5d5788e333a0cba6f238a22c6c0f94788870e1a9ecd482e153\")\n        )\n        # Truncate\n        self.assertFalse(\n            validate(\"3d4e60b996ff9c5d5788e333a0cba6f238a22c6c0f94788870e1a9ecd482e15\")\n        )\n        # Lengthen\n        self.assertFalse(\n            validate(\n                \"3d4e60b996ff9c5d5788e333a0cba6f238a22c6c0f94788870e1a9ecd482e1538\"\n            )\n        )\n\n    def test_non_ascii(self):\n        value = b\"\\xe9\"\n        signed = create_signed_value(\n            SignedValueTest.SECRET, \"key\", value, clock=self.present\n        )\n        decoded = decode_signed_value(\n            SignedValueTest.SECRET, \"key\", signed, clock=self.present\n        )\n        self.assertEqual(value, decoded)\n\n    def test_key_versioning_read_write_default_key(self):\n        value = b\"\\xe9\"\n        signed = create_signed_value(\n            SignedValueTest.SECRET_DICT, \"key\", value, clock=self.present, key_version=0\n        )\n        decoded = decode_signed_value(\n            SignedValueTest.SECRET_DICT, \"key\", signed, clock=self.present\n        )\n        self.assertEqual(value, decoded)\n\n    def test_key_versioning_read_write_non_default_key(self):\n        value = b\"\\xe9\"\n        signed = create_signed_value(\n            SignedValueTest.SECRET_DICT, \"key\", value, clock=self.present, key_version=1\n        )\n        decoded = decode_signed_value(\n            SignedValueTest.SECRET_DICT, \"key\", signed, clock=self.present\n        )\n        self.assertEqual(value, decoded)\n\n    def test_key_versioning_invalid_key(self):\n        value = b\"\\xe9\"\n        signed = create_signed_value(\n            SignedValueTest.SECRET_DICT, \"key\", value, clock=self.present, key_version=0\n        )\n        newkeys = SignedValueTest.SECRET_DICT.copy()\n        newkeys.pop(0)\n        decoded = decode_signed_value(newkeys, \"key\", signed, clock=self.present)\n        self.assertEqual(None, decoded)\n\n    def test_key_version_retrieval(self):\n        value = b\"\\xe9\"\n        signed = create_signed_value(\n            SignedValueTest.SECRET_DICT, \"key\", value, clock=self.present, key_version=1\n        )\n        key_version = get_signature_key_version(signed)\n        self.assertEqual(1, key_version)\n\n\nclass XSRFTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            version = int(self.get_argument(\"version\", \"2\"))\n            # This would be a bad idea in a real app, but in this test\n            # it's fine.\n            self.settings[\"xsrf_cookie_version\"] = version\n            self.write(self.xsrf_token)\n\n        def post(self):\n            self.write(\"ok\")\n\n    def get_app_kwargs(self):\n        return dict(xsrf_cookies=True)\n\n    def setUp(self):\n        super(XSRFTest, self).setUp()\n        self.xsrf_token = self.get_token()\n\n    def get_token(self, old_token=None, version=None):\n        if old_token is not None:\n            headers = self.cookie_headers(old_token)\n        else:\n            headers = None\n        response = self.fetch(\n            \"/\" if version is None else (\"/?version=%d\" % version), headers=headers\n        )\n        response.rethrow()\n        return native_str(response.body)\n\n    def cookie_headers(self, token=None):\n        if token is None:\n            token = self.xsrf_token\n        return {\"Cookie\": \"_xsrf=\" + token}\n\n    def test_xsrf_fail_no_token(self):\n        with ExpectLog(gen_log, \".*'_xsrf' argument missing\"):\n            response = self.fetch(\"/\", method=\"POST\", body=b\"\")\n        self.assertEqual(response.code, 403)\n\n    def test_xsrf_fail_body_no_cookie(self):\n        with ExpectLog(gen_log, \".*XSRF cookie does not match POST\"):\n            response = self.fetch(\n                \"/\",\n                method=\"POST\",\n                body=urllib.parse.urlencode(dict(_xsrf=self.xsrf_token)),\n            )\n        self.assertEqual(response.code, 403)\n\n    def test_xsrf_fail_argument_invalid_format(self):\n        with ExpectLog(gen_log, \".*'_xsrf' argument has invalid format\"):\n            response = self.fetch(\n                \"/\",\n                method=\"POST\",\n                headers=self.cookie_headers(),\n                body=urllib.parse.urlencode(dict(_xsrf=\"3|\")),\n            )\n        self.assertEqual(response.code, 403)\n\n    def test_xsrf_fail_cookie_invalid_format(self):\n        with ExpectLog(gen_log, \".*XSRF cookie does not match POST\"):\n            response = self.fetch(\n                \"/\",\n                method=\"POST\",\n                headers=self.cookie_headers(token=\"3|\"),\n                body=urllib.parse.urlencode(dict(_xsrf=self.xsrf_token)),\n            )\n        self.assertEqual(response.code, 403)\n\n    def test_xsrf_fail_cookie_no_body(self):\n        with ExpectLog(gen_log, \".*'_xsrf' argument missing\"):\n            response = self.fetch(\n                \"/\", method=\"POST\", body=b\"\", headers=self.cookie_headers()\n            )\n        self.assertEqual(response.code, 403)\n\n    def test_xsrf_success_short_token(self):\n        response = self.fetch(\n            \"/\",\n            method=\"POST\",\n            body=urllib.parse.urlencode(dict(_xsrf=\"deadbeef\")),\n            headers=self.cookie_headers(token=\"deadbeef\"),\n        )\n        self.assertEqual(response.code, 200)\n\n    def test_xsrf_success_non_hex_token(self):\n        response = self.fetch(\n            \"/\",\n            method=\"POST\",\n            body=urllib.parse.urlencode(dict(_xsrf=\"xoxo\")),\n            headers=self.cookie_headers(token=\"xoxo\"),\n        )\n        self.assertEqual(response.code, 200)\n\n    def test_xsrf_success_post_body(self):\n        response = self.fetch(\n            \"/\",\n            method=\"POST\",\n            body=urllib.parse.urlencode(dict(_xsrf=self.xsrf_token)),\n            headers=self.cookie_headers(),\n        )\n        self.assertEqual(response.code, 200)\n\n    def test_xsrf_success_query_string(self):\n        response = self.fetch(\n            \"/?\" + urllib.parse.urlencode(dict(_xsrf=self.xsrf_token)),\n            method=\"POST\",\n            body=b\"\",\n            headers=self.cookie_headers(),\n        )\n        self.assertEqual(response.code, 200)\n\n    def test_xsrf_success_header(self):\n        response = self.fetch(\n            \"/\",\n            method=\"POST\",\n            body=b\"\",\n            headers=dict(\n                {\"X-Xsrftoken\": self.xsrf_token},  # type: ignore\n                **self.cookie_headers()\n            ),\n        )\n        self.assertEqual(response.code, 200)\n\n    def test_distinct_tokens(self):\n        # Every request gets a distinct token.\n        NUM_TOKENS = 10\n        tokens = set()\n        for i in range(NUM_TOKENS):\n            tokens.add(self.get_token())\n        self.assertEqual(len(tokens), NUM_TOKENS)\n\n    def test_cross_user(self):\n        token2 = self.get_token()\n        # Each token can be used to authenticate its own request.\n        for token in (self.xsrf_token, token2):\n            response = self.fetch(\n                \"/\",\n                method=\"POST\",\n                body=urllib.parse.urlencode(dict(_xsrf=token)),\n                headers=self.cookie_headers(token),\n            )\n            self.assertEqual(response.code, 200)\n        # Sending one in the cookie and the other in the body is not allowed.\n        for cookie_token, body_token in (\n            (self.xsrf_token, token2),\n            (token2, self.xsrf_token),\n        ):\n            with ExpectLog(gen_log, \".*XSRF cookie does not match POST\"):\n                response = self.fetch(\n                    \"/\",\n                    method=\"POST\",\n                    body=urllib.parse.urlencode(dict(_xsrf=body_token)),\n                    headers=self.cookie_headers(cookie_token),\n                )\n            self.assertEqual(response.code, 403)\n\n    def test_refresh_token(self):\n        token = self.xsrf_token\n        tokens_seen = set([token])\n        # A user's token is stable over time.  Refreshing the page in one tab\n        # might update the cookie while an older tab still has the old cookie\n        # in its DOM.  Simulate this scenario by passing a constant token\n        # in the body and re-querying for the token.\n        for i in range(5):\n            token = self.get_token(token)\n            # Tokens are encoded uniquely each time\n            tokens_seen.add(token)\n            response = self.fetch(\n                \"/\",\n                method=\"POST\",\n                body=urllib.parse.urlencode(dict(_xsrf=self.xsrf_token)),\n                headers=self.cookie_headers(token),\n            )\n            self.assertEqual(response.code, 200)\n        self.assertEqual(len(tokens_seen), 6)\n\n    def test_versioning(self):\n        # Version 1 still produces distinct tokens per request.\n        self.assertNotEqual(self.get_token(version=1), self.get_token(version=1))\n\n        # Refreshed v1 tokens are all identical.\n        v1_token = self.get_token(version=1)\n        for i in range(5):\n            self.assertEqual(self.get_token(v1_token, version=1), v1_token)\n\n        # Upgrade to a v2 version of the same token\n        v2_token = self.get_token(v1_token)\n        self.assertNotEqual(v1_token, v2_token)\n        # Each v1 token can map to many v2 tokens.\n        self.assertNotEqual(v2_token, self.get_token(v1_token))\n\n        # The tokens are cross-compatible.\n        for cookie_token, body_token in ((v1_token, v2_token), (v2_token, v1_token)):\n            response = self.fetch(\n                \"/\",\n                method=\"POST\",\n                body=urllib.parse.urlencode(dict(_xsrf=body_token)),\n                headers=self.cookie_headers(cookie_token),\n            )\n            self.assertEqual(response.code, 200)\n\n\nclass XSRFCookieKwargsTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            self.write(self.xsrf_token)\n\n    def get_app_kwargs(self):\n        return dict(\n            xsrf_cookies=True, xsrf_cookie_kwargs=dict(httponly=True, expires_days=2)\n        )\n\n    def test_xsrf_httponly(self):\n        response = self.fetch(\"/\")\n        self.assertIn(\"httponly;\", response.headers[\"Set-Cookie\"].lower())\n        self.assertIn(\"expires=\", response.headers[\"Set-Cookie\"].lower())\n        header = response.headers.get(\"Set-Cookie\")\n        match = re.match(\".*; expires=(?P<expires>.+);.*\", header)\n        assert match is not None\n\n        expires = datetime.datetime.utcnow() + datetime.timedelta(days=2)\n        parsed = email.utils.parsedate(match.groupdict()[\"expires\"])\n        assert parsed is not None\n        header_expires = datetime.datetime(*parsed[:6])\n        self.assertTrue(abs((expires - header_expires).total_seconds()) < 10)\n\n\nclass FinishExceptionTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            self.set_status(401)\n            self.set_header(\"WWW-Authenticate\", 'Basic realm=\"something\"')\n            if self.get_argument(\"finish_value\", \"\"):\n                raise Finish(\"authentication required\")\n            else:\n                self.write(\"authentication required\")\n                raise Finish()\n\n    def test_finish_exception(self):\n        for u in [\"/\", \"/?finish_value=1\"]:\n            response = self.fetch(u)\n            self.assertEqual(response.code, 401)\n            self.assertEqual(\n                'Basic realm=\"something\"', response.headers.get(\"WWW-Authenticate\")\n            )\n            self.assertEqual(b\"authentication required\", response.body)\n\n\nclass DecoratorTest(WebTestCase):\n    def get_handlers(self):\n        class RemoveSlashHandler(RequestHandler):\n            @removeslash\n            def get(self):\n                pass\n\n        class AddSlashHandler(RequestHandler):\n            @addslash\n            def get(self):\n                pass\n\n        return [(\"/removeslash/\", RemoveSlashHandler), (\"/addslash\", AddSlashHandler)]\n\n    def test_removeslash(self):\n        response = self.fetch(\"/removeslash/\", follow_redirects=False)\n        self.assertEqual(response.code, 301)\n        self.assertEqual(response.headers[\"Location\"], \"/removeslash\")\n\n        response = self.fetch(\"/removeslash/?foo=bar\", follow_redirects=False)\n        self.assertEqual(response.code, 301)\n        self.assertEqual(response.headers[\"Location\"], \"/removeslash?foo=bar\")\n\n    def test_addslash(self):\n        response = self.fetch(\"/addslash\", follow_redirects=False)\n        self.assertEqual(response.code, 301)\n        self.assertEqual(response.headers[\"Location\"], \"/addslash/\")\n\n        response = self.fetch(\"/addslash?foo=bar\", follow_redirects=False)\n        self.assertEqual(response.code, 301)\n        self.assertEqual(response.headers[\"Location\"], \"/addslash/?foo=bar\")\n\n\nclass CacheTest(WebTestCase):\n    def get_handlers(self):\n        class EtagHandler(RequestHandler):\n            def get(self, computed_etag):\n                self.write(computed_etag)\n\n            def compute_etag(self):\n                return self._write_buffer[0]\n\n        return [(\"/etag/(.*)\", EtagHandler)]\n\n    def test_wildcard_etag(self):\n        computed_etag = '\"xyzzy\"'\n        etags = \"*\"\n        self._test_etag(computed_etag, etags, 304)\n\n    def test_strong_etag_match(self):\n        computed_etag = '\"xyzzy\"'\n        etags = '\"xyzzy\"'\n        self._test_etag(computed_etag, etags, 304)\n\n    def test_multiple_strong_etag_match(self):\n        computed_etag = '\"xyzzy1\"'\n        etags = '\"xyzzy1\", \"xyzzy2\"'\n        self._test_etag(computed_etag, etags, 304)\n\n    def test_strong_etag_not_match(self):\n        computed_etag = '\"xyzzy\"'\n        etags = '\"xyzzy1\"'\n        self._test_etag(computed_etag, etags, 200)\n\n    def test_multiple_strong_etag_not_match(self):\n        computed_etag = '\"xyzzy\"'\n        etags = '\"xyzzy1\", \"xyzzy2\"'\n        self._test_etag(computed_etag, etags, 200)\n\n    def test_weak_etag_match(self):\n        computed_etag = '\"xyzzy1\"'\n        etags = 'W/\"xyzzy1\"'\n        self._test_etag(computed_etag, etags, 304)\n\n    def test_multiple_weak_etag_match(self):\n        computed_etag = '\"xyzzy2\"'\n        etags = 'W/\"xyzzy1\", W/\"xyzzy2\"'\n        self._test_etag(computed_etag, etags, 304)\n\n    def test_weak_etag_not_match(self):\n        computed_etag = '\"xyzzy2\"'\n        etags = 'W/\"xyzzy1\"'\n        self._test_etag(computed_etag, etags, 200)\n\n    def test_multiple_weak_etag_not_match(self):\n        computed_etag = '\"xyzzy3\"'\n        etags = 'W/\"xyzzy1\", W/\"xyzzy2\"'\n        self._test_etag(computed_etag, etags, 200)\n\n    def _test_etag(self, computed_etag, etags, status_code):\n        response = self.fetch(\n            \"/etag/\" + computed_etag, headers={\"If-None-Match\": etags}\n        )\n        self.assertEqual(response.code, status_code)\n\n\nclass RequestSummaryTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            # remote_ip is optional, although it's set by\n            # both HTTPServer and WSGIAdapter.\n            # Clobber it to make sure it doesn't break logging.\n            self.request.remote_ip = None\n            self.finish(self._request_summary())\n\n    def test_missing_remote_ip(self):\n        resp = self.fetch(\"/\")\n        self.assertEqual(resp.body, b\"GET / (None)\")\n\n\nclass HTTPErrorTest(unittest.TestCase):\n    def test_copy(self):\n        e = HTTPError(403, reason=\"Go away\")\n        e2 = copy.copy(e)\n        self.assertIsNot(e, e2)\n        self.assertEqual(e.status_code, e2.status_code)\n        self.assertEqual(e.reason, e2.reason)\n\n\nclass ApplicationTest(AsyncTestCase):\n    def test_listen(self):\n        app = Application([])\n        server = app.listen(0, address=\"127.0.0.1\")\n        server.stop()\n\n\nclass URLSpecReverseTest(unittest.TestCase):\n    def test_reverse(self):\n        self.assertEqual(\"/favicon.ico\", url(r\"/favicon\\.ico\", None).reverse())\n        self.assertEqual(\"/favicon.ico\", url(r\"^/favicon\\.ico$\", None).reverse())\n\n    def test_non_reversible(self):\n        # URLSpecs are non-reversible if they include non-constant\n        # regex features outside capturing groups. Currently, this is\n        # only strictly enforced for backslash-escaped character\n        # classes.\n        paths = [r\"^/api/v\\d+/foo/(\\w+)$\"]\n        for path in paths:\n            # A URLSpec can still be created even if it cannot be reversed.\n            url_spec = url(path, None)\n            try:\n                result = url_spec.reverse()\n                self.fail(\n                    \"did not get expected exception when reversing %s. \"\n                    \"result: %s\" % (path, result)\n                )\n            except ValueError:\n                pass\n\n    def test_reverse_arguments(self):\n        self.assertEqual(\n            \"/api/v1/foo/bar\", url(r\"^/api/v1/foo/(\\w+)$\", None).reverse(\"bar\")\n        )\n\n\nclass RedirectHandlerTest(WebTestCase):\n    def get_handlers(self):\n        return [\n            (\"/src\", WebRedirectHandler, {\"url\": \"/dst\"}),\n            (\"/src2\", WebRedirectHandler, {\"url\": \"/dst2?foo=bar\"}),\n            (r\"/(.*?)/(.*?)/(.*)\", WebRedirectHandler, {\"url\": \"/{1}/{0}/{2}\"}),\n        ]\n\n    def test_basic_redirect(self):\n        response = self.fetch(\"/src\", follow_redirects=False)\n        self.assertEqual(response.code, 301)\n        self.assertEqual(response.headers[\"Location\"], \"/dst\")\n\n    def test_redirect_with_argument(self):\n        response = self.fetch(\"/src?foo=bar\", follow_redirects=False)\n        self.assertEqual(response.code, 301)\n        self.assertEqual(response.headers[\"Location\"], \"/dst?foo=bar\")\n\n    def test_redirect_with_appending_argument(self):\n        response = self.fetch(\"/src2?foo2=bar2\", follow_redirects=False)\n        self.assertEqual(response.code, 301)\n        self.assertEqual(response.headers[\"Location\"], \"/dst2?foo=bar&foo2=bar2\")\n\n    def test_redirect_pattern(self):\n        response = self.fetch(\"/a/b/c\", follow_redirects=False)\n        self.assertEqual(response.code, 301)\n        self.assertEqual(response.headers[\"Location\"], \"/b/a/c\")\n",
            "header": {
                "file": {
                    "path": "tornado/test/web_test.py"
                },
                "misc_line": null
            },
            "index_line": {
                "old_commit_hash": "a6f5d1bd",
                "new_commit_hash": "d43d46bc",
                "mode": "100644"
            },
            "is_binary_file": false,
            "binary_line": null,
            "minus_file": {
                "path": "a/tornado/test/web_test.py"
            },
            "plus_file": {
                "path": "b/tornado/test/web_test.py"
            },
            "hunks": [
                {
                    "descriptor": {
                        "old_range": {
                            "start": 224,
                            "length": 6
                        },
                        "new_range": {
                            "start": 224,
                            "length": 13
                        },
                        "section": "class FinalReturnTest(WebTestCase):"
                    },
                    "line_group": {
                        "all_lines": [
                            {
                                "content": "                test.final_return = self.finish()",
                                "type": "context"
                            },
                            {
                                "content": "                yield test.final_return",
                                "type": "context"
                            },
                            {
                                "content": "",
                                "type": "context"
                            },
                            {
                                "content": "            @gen.coroutine",
                                "type": "added"
                            },
                            {
                                "content": "            def post(self):",
                                "type": "added"
                            },
                            {
                                "content": "                self.write(\"hello,\")",
                                "type": "added"
                            },
                            {
                                "content": "                yield self.flush()",
                                "type": "added"
                            },
                            {
                                "content": "                test.final_return = self.finish(\"world\")",
                                "type": "added"
                            },
                            {
                                "content": "                yield test.final_return",
                                "type": "added"
                            },
                            {
                                "content": "",
                                "type": "added"
                            },
                            {
                                "content": "        class RenderHandler(RequestHandler):",
                                "type": "context"
                            },
                            {
                                "content": "            def create_template_loader(self, path):",
                                "type": "context"
                            },
                            {
                                "content": "                return DictLoader({\"foo.html\": \"hi\"})",
                                "type": "context"
                            }
                        ]
                    },
                    "modified_entities": [
                        {
                            "file_name": "tornado/test/web_test.py",
                            "type": "class",
                            "name": "FinalReturnTest",
                            "content": "class FinalReturnTest(WebTestCase):\n    def get_handlers(self):\n        test = self\n\n        class FinishHandler(RequestHandler):\n            @gen.coroutine\n            def get(self):\n                test.final_return = self.finish()\n                yield test.final_return\n\n            @gen.coroutine\n            def post(self):\n                self.write(\"hello,\")\n                yield self.flush()\n                test.final_return = self.finish(\"world\")\n                yield test.final_return\n\n        class RenderHandler(RequestHandler):\n            def create_template_loader(self, path):\n                return DictLoader({\"foo.html\": \"hi\"})\n\n            @gen.coroutine\n            def get(self):\n                test.final_return = self.render(\"foo.html\")\n\n        return [(\"/finish\", FinishHandler), (\"/render\", RenderHandler)]\n\n    def get_app_kwargs(self):\n        return dict(template_path=\"FinalReturnTest\")\n\n    def test_finish_method_return_future(self):\n        response = self.fetch(self.get_url(\"/finish\"))\n        self.assertEqual(response.code, 200)\n        self.assertIsInstance(self.final_return, Future)\n        self.assertTrue(self.final_return.done())\n\n        response = self.fetch(self.get_url(\"/finish\"), method=\"POST\", body=b\"\")\n        self.assertEqual(response.code, 200)\n        self.assertIsInstance(self.final_return, Future)\n        self.assertTrue(self.final_return.done())\n\n    def test_render_method_return_future(self):\n        response = self.fetch(self.get_url(\"/render\"))\n        self.assertEqual(response.code, 200)\n        self.assertIsInstance(self.final_return, Future)",
                            "ast_type_str": "ClassDef",
                            "start_lineno": 217,
                            "end_lineno": 261,
                            "parent": null
                        },
                        {
                            "file_name": "tornado/test/web_test.py",
                            "type": "method",
                            "name": "FinalReturnTest.get_handlers",
                            "content": "class FinalReturnTest(WebTestCase):\n    def get_handlers(self):\n        test = self\n\n        class FinishHandler(RequestHandler):\n            @gen.coroutine\n            def get(self):\n                test.final_return = self.finish()\n                yield test.final_return\n\n            @gen.coroutine\n            def post(self):\n                self.write(\"hello,\")\n                yield self.flush()\n                test.final_return = self.finish(\"world\")\n                yield test.final_return\n\n        class RenderHandler(RequestHandler):\n            def create_template_loader(self, path):\n                return DictLoader({\"foo.html\": \"hi\"})\n\n            @gen.coroutine\n            def get(self):\n                test.final_return = self.render(\"foo.html\")\n\n        return [(\"/finish\", FinishHandler), (\"/render\", RenderHandler)]\n\n    def get_app_kwargs(self):\n        return dict(template_path=\"FinalReturnTest\")\n\n    def test_finish_method_return_future(self):\n        response = self.fetch(self.get_url(\"/finish\"))\n        self.assertEqual(response.code, 200)\n        self.assertIsInstance(self.final_return, Future)\n        self.assertTrue(self.final_return.done())\n\n        response = self.fetch(self.get_url(\"/finish\"), method=\"POST\", body=b\"\")\n        self.assertEqual(response.code, 200)\n        self.assertIsInstance(self.final_return, Future)\n        self.assertTrue(self.final_return.done())\n\n    def test_render_method_return_future(self):\n        response = self.fetch(self.get_url(\"/render\"))\n        self.assertEqual(response.code, 200)\n        self.assertIsInstance(self.final_return, Future)",
                            "ast_type_str": "ClassDef",
                            "start_lineno": 218,
                            "end_lineno": 242,
                            "parent": {
                                "file_name": "tornado/test/web_test.py",
                                "type": "class",
                                "name": "FinalReturnTest",
                                "content": "class FinalReturnTest(WebTestCase):\n    def get_handlers(self):\n        test = self\n\n        class FinishHandler(RequestHandler):\n            @gen.coroutine\n            def get(self):\n                test.final_return = self.finish()\n                yield test.final_return\n\n            @gen.coroutine\n            def post(self):\n                self.write(\"hello,\")\n                yield self.flush()\n                test.final_return = self.finish(\"world\")\n                yield test.final_return\n\n        class RenderHandler(RequestHandler):\n            def create_template_loader(self, path):\n                return DictLoader({\"foo.html\": \"hi\"})\n\n            @gen.coroutine\n            def get(self):\n                test.final_return = self.render(\"foo.html\")\n\n        return [(\"/finish\", FinishHandler), (\"/render\", RenderHandler)]\n\n    def get_app_kwargs(self):\n        return dict(template_path=\"FinalReturnTest\")\n\n    def test_finish_method_return_future(self):\n        response = self.fetch(self.get_url(\"/finish\"))\n        self.assertEqual(response.code, 200)\n        self.assertIsInstance(self.final_return, Future)\n        self.assertTrue(self.final_return.done())\n\n        response = self.fetch(self.get_url(\"/finish\"), method=\"POST\", body=b\"\")\n        self.assertEqual(response.code, 200)\n        self.assertIsInstance(self.final_return, Future)\n        self.assertTrue(self.final_return.done())\n\n    def test_render_method_return_future(self):\n        response = self.fetch(self.get_url(\"/render\"))\n        self.assertEqual(response.code, 200)\n        self.assertIsInstance(self.final_return, Future)",
                                "ast_type_str": "ClassDef",
                                "start_lineno": 217,
                                "end_lineno": 261,
                                "parent": null
                            }
                        }
                    ],
                    "added_entities": [],
                    "deleted_entities": []
                },
                {
                    "descriptor": {
                        "old_range": {
                            "start": 243,
                            "length": 6
                        },
                        "new_range": {
                            "start": 250,
                            "length": 11
                        },
                        "section": "class FinalReturnTest(WebTestCase):"
                    },
                    "line_group": {
                        "all_lines": [
                            {
                                "content": "        self.assertIsInstance(self.final_return, Future)",
                                "type": "context"
                            },
                            {
                                "content": "        self.assertTrue(self.final_return.done())",
                                "type": "context"
                            },
                            {
                                "content": "",
                                "type": "context"
                            },
                            {
                                "content": "        response = self.fetch(self.get_url(\"/finish\"), method=\"POST\", body=b\"\")",
                                "type": "added"
                            },
                            {
                                "content": "        self.assertEqual(response.code, 200)",
                                "type": "added"
                            },
                            {
                                "content": "        self.assertIsInstance(self.final_return, Future)",
                                "type": "added"
                            },
                            {
                                "content": "        self.assertTrue(self.final_return.done())",
                                "type": "added"
                            },
                            {
                                "content": "",
                                "type": "added"
                            },
                            {
                                "content": "    def test_render_method_return_future(self):",
                                "type": "context"
                            },
                            {
                                "content": "        response = self.fetch(self.get_url(\"/render\"))",
                                "type": "context"
                            },
                            {
                                "content": "        self.assertEqual(response.code, 200)",
                                "type": "context"
                            },
                            {
                                "content": "",
                                "type": "context"
                            }
                        ]
                    },
                    "modified_entities": [
                        {
                            "file_name": "tornado/test/web_test.py",
                            "type": "method",
                            "name": "FinalReturnTest.test_finish_method_return_future",
                            "content": "class FinalReturnTest(WebTestCase):\n    def get_handlers(self):\n        test = self\n\n        class FinishHandler(RequestHandler):\n            @gen.coroutine\n            def get(self):\n                test.final_return = self.finish()\n                yield test.final_return\n\n            @gen.coroutine\n            def post(self):\n                self.write(\"hello,\")\n                yield self.flush()\n                test.final_return = self.finish(\"world\")\n                yield test.final_return\n\n        class RenderHandler(RequestHandler):\n            def create_template_loader(self, path):\n                return DictLoader({\"foo.html\": \"hi\"})\n\n            @gen.coroutine\n            def get(self):\n                test.final_return = self.render(\"foo.html\")\n\n        return [(\"/finish\", FinishHandler), (\"/render\", RenderHandler)]\n\n    def get_app_kwargs(self):\n        return dict(template_path=\"FinalReturnTest\")\n\n    def test_finish_method_return_future(self):\n        response = self.fetch(self.get_url(\"/finish\"))\n        self.assertEqual(response.code, 200)\n        self.assertIsInstance(self.final_return, Future)\n        self.assertTrue(self.final_return.done())\n\n        response = self.fetch(self.get_url(\"/finish\"), method=\"POST\", body=b\"\")\n        self.assertEqual(response.code, 200)\n        self.assertIsInstance(self.final_return, Future)\n        self.assertTrue(self.final_return.done())\n\n    def test_render_method_return_future(self):\n        response = self.fetch(self.get_url(\"/render\"))\n        self.assertEqual(response.code, 200)\n        self.assertIsInstance(self.final_return, Future)",
                            "ast_type_str": "ClassDef",
                            "start_lineno": 247,
                            "end_lineno": 256,
                            "parent": {
                                "file_name": "tornado/test/web_test.py",
                                "type": "class",
                                "name": "FinalReturnTest",
                                "content": "class FinalReturnTest(WebTestCase):\n    def get_handlers(self):\n        test = self\n\n        class FinishHandler(RequestHandler):\n            @gen.coroutine\n            def get(self):\n                test.final_return = self.finish()\n                yield test.final_return\n\n            @gen.coroutine\n            def post(self):\n                self.write(\"hello,\")\n                yield self.flush()\n                test.final_return = self.finish(\"world\")\n                yield test.final_return\n\n        class RenderHandler(RequestHandler):\n            def create_template_loader(self, path):\n                return DictLoader({\"foo.html\": \"hi\"})\n\n            @gen.coroutine\n            def get(self):\n                test.final_return = self.render(\"foo.html\")\n\n        return [(\"/finish\", FinishHandler), (\"/render\", RenderHandler)]\n\n    def get_app_kwargs(self):\n        return dict(template_path=\"FinalReturnTest\")\n\n    def test_finish_method_return_future(self):\n        response = self.fetch(self.get_url(\"/finish\"))\n        self.assertEqual(response.code, 200)\n        self.assertIsInstance(self.final_return, Future)\n        self.assertTrue(self.final_return.done())\n\n        response = self.fetch(self.get_url(\"/finish\"), method=\"POST\", body=b\"\")\n        self.assertEqual(response.code, 200)\n        self.assertIsInstance(self.final_return, Future)\n        self.assertTrue(self.final_return.done())\n\n    def test_render_method_return_future(self):\n        response = self.fetch(self.get_url(\"/render\"))\n        self.assertEqual(response.code, 200)\n        self.assertIsInstance(self.final_return, Future)",
                                "ast_type_str": "ClassDef",
                                "start_lineno": 217,
                                "end_lineno": 261,
                                "parent": null
                            }
                        },
                        {
                            "file_name": "tornado/test/web_test.py",
                            "type": "class",
                            "name": "FinalReturnTest",
                            "content": "class FinalReturnTest(WebTestCase):\n    def get_handlers(self):\n        test = self\n\n        class FinishHandler(RequestHandler):\n            @gen.coroutine\n            def get(self):\n                test.final_return = self.finish()\n                yield test.final_return\n\n            @gen.coroutine\n            def post(self):\n                self.write(\"hello,\")\n                yield self.flush()\n                test.final_return = self.finish(\"world\")\n                yield test.final_return\n\n        class RenderHandler(RequestHandler):\n            def create_template_loader(self, path):\n                return DictLoader({\"foo.html\": \"hi\"})\n\n            @gen.coroutine\n            def get(self):\n                test.final_return = self.render(\"foo.html\")\n\n        return [(\"/finish\", FinishHandler), (\"/render\", RenderHandler)]\n\n    def get_app_kwargs(self):\n        return dict(template_path=\"FinalReturnTest\")\n\n    def test_finish_method_return_future(self):\n        response = self.fetch(self.get_url(\"/finish\"))\n        self.assertEqual(response.code, 200)\n        self.assertIsInstance(self.final_return, Future)\n        self.assertTrue(self.final_return.done())\n\n        response = self.fetch(self.get_url(\"/finish\"), method=\"POST\", body=b\"\")\n        self.assertEqual(response.code, 200)\n        self.assertIsInstance(self.final_return, Future)\n        self.assertTrue(self.final_return.done())\n\n    def test_render_method_return_future(self):\n        response = self.fetch(self.get_url(\"/render\"))\n        self.assertEqual(response.code, 200)\n        self.assertIsInstance(self.final_return, Future)",
                            "ast_type_str": "ClassDef",
                            "start_lineno": 217,
                            "end_lineno": 261,
                            "parent": null
                        }
                    ],
                    "added_entities": [],
                    "deleted_entities": []
                }
            ]
        }
    ],
    "old_commit_hash": "9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9^",
    "new_commit_hash": "9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9",
    "commit_message": "Merge pull request #2549 from garenchan/bugfix-#2448\n\nweb: Use 'future_add_done_callback' to add callback.",
    "commit_date": "2018-12-15T10:41:20-05:00",
    "metadata": {}
}
{
    "repo_name": "tornado",
    "new_commit_hash": "9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9",
    "test_file_codes": [
        "from tornado.concurrent import Future\nfrom tornado import gen\nfrom tornado.escape import (\n    json_decode,\n    utf8,\n    to_unicode,\n    recursive_unicode,\n    native_str,\n    to_basestring,\n)\nfrom tornado.httpclient import HTTPClientError\nfrom tornado.httputil import format_timestamp\nfrom tornado.iostream import IOStream\nfrom tornado import locale\nfrom tornado.locks import Event\nfrom tornado.log import app_log, gen_log\nfrom tornado.simple_httpclient import SimpleAsyncHTTPClient\nfrom tornado.template import DictLoader\nfrom tornado.testing import AsyncHTTPTestCase, AsyncTestCase, ExpectLog, gen_test\nfrom tornado.util import ObjectDict, unicode_type\nfrom tornado.web import (\n    Application,\n    RequestHandler,\n    StaticFileHandler,\n    RedirectHandler as WebRedirectHandler,\n    HTTPError,\n    MissingArgumentError,\n    ErrorHandler,\n    authenticated,\n    url,\n    _create_signature_v1,\n    create_signed_value,\n    decode_signed_value,\n    get_signature_key_version,\n    UIModule,\n    Finish,\n    stream_request_body,\n    removeslash,\n    addslash,\n    GZipContentEncoding,\n)\n\nimport binascii\nimport contextlib\nimport copy\nimport datetime\nimport email.utils\nimport gzip\nfrom io import BytesIO\nimport itertools\nimport logging\nimport os\nimport re\nimport socket\nimport typing  # noqa: F401\nimport unittest\nimport urllib.parse\n\n\ndef relpath(*a):\n    return os.path.join(os.path.dirname(__file__), *a)\n\n\nclass WebTestCase(AsyncHTTPTestCase):\n    \"\"\"Base class for web tests that also supports WSGI mode.\n\n    Override get_handlers and get_app_kwargs instead of get_app.\n    This class is deprecated since WSGI mode is no longer supported.\n    \"\"\"\n\n    def get_app(self):\n        self.app = Application(self.get_handlers(), **self.get_app_kwargs())\n        return self.app\n\n    def get_handlers(self):\n        raise NotImplementedError()\n\n    def get_app_kwargs(self):\n        return {}\n\n\nclass SimpleHandlerTestCase(WebTestCase):\n    \"\"\"Simplified base class for tests that work with a single handler class.\n\n    To use, define a nested class named ``Handler``.\n    \"\"\"\n\n    def get_handlers(self):\n        return [(\"/\", self.Handler)]\n\n\nclass HelloHandler(RequestHandler):\n    def get(self):\n        self.write(\"hello\")\n\n\nclass CookieTestRequestHandler(RequestHandler):\n    # stub out enough methods to make the secure_cookie functions work\n    def __init__(self, cookie_secret=\"0123456789\", key_version=None):\n        # don't call super.__init__\n        self._cookies = {}  # type: typing.Dict[str, bytes]\n        if key_version is None:\n            self.application = ObjectDict(settings=dict(cookie_secret=cookie_secret))\n        else:\n            self.application = ObjectDict(\n                settings=dict(cookie_secret=cookie_secret, key_version=key_version)\n            )\n\n    def get_cookie(self, name):\n        return self._cookies.get(name)\n\n    def set_cookie(self, name, value, expires_days=None):\n        self._cookies[name] = value\n\n\n# See SignedValueTest below for more.\nclass SecureCookieV1Test(unittest.TestCase):\n    def test_round_trip(self):\n        handler = CookieTestRequestHandler()\n        handler.set_secure_cookie(\"foo\", b\"bar\", version=1)\n        self.assertEqual(handler.get_secure_cookie(\"foo\", min_version=1), b\"bar\")\n\n    def test_cookie_tampering_future_timestamp(self):\n        handler = CookieTestRequestHandler()\n        # this string base64-encodes to '12345678'\n        handler.set_secure_cookie(\"foo\", binascii.a2b_hex(b\"d76df8e7aefc\"), version=1)\n        cookie = handler._cookies[\"foo\"]\n        match = re.match(br\"12345678\\|([0-9]+)\\|([0-9a-f]+)\", cookie)\n        assert match is not None\n        timestamp = match.group(1)\n        sig = match.group(2)\n        self.assertEqual(\n            _create_signature_v1(\n                handler.application.settings[\"cookie_secret\"],\n                \"foo\",\n                \"12345678\",\n                timestamp,\n            ),\n            sig,\n        )\n        # shifting digits from payload to timestamp doesn't alter signature\n        # (this is not desirable behavior, just confirming that that's how it\n        # works)\n        self.assertEqual(\n            _create_signature_v1(\n                handler.application.settings[\"cookie_secret\"],\n                \"foo\",\n                \"1234\",\n                b\"5678\" + timestamp,\n            ),\n            sig,\n        )\n        # tamper with the cookie\n        handler._cookies[\"foo\"] = utf8(\n            \"1234|5678%s|%s\" % (to_basestring(timestamp), to_basestring(sig))\n        )\n        # it gets rejected\n        with ExpectLog(gen_log, \"Cookie timestamp in future\"):\n            self.assertTrue(handler.get_secure_cookie(\"foo\", min_version=1) is None)\n\n    def test_arbitrary_bytes(self):\n        # Secure cookies accept arbitrary data (which is base64 encoded).\n        # Note that normal cookies accept only a subset of ascii.\n        handler = CookieTestRequestHandler()\n        handler.set_secure_cookie(\"foo\", b\"\\xe9\", version=1)\n        self.assertEqual(handler.get_secure_cookie(\"foo\", min_version=1), b\"\\xe9\")\n\n\n# See SignedValueTest below for more.\nclass SecureCookieV2Test(unittest.TestCase):\n    KEY_VERSIONS = {0: \"ajklasdf0ojaisdf\", 1: \"aslkjasaolwkjsdf\"}\n\n    def test_round_trip(self):\n        handler = CookieTestRequestHandler()\n        handler.set_secure_cookie(\"foo\", b\"bar\", version=2)\n        self.assertEqual(handler.get_secure_cookie(\"foo\", min_version=2), b\"bar\")\n\n    def test_key_version_roundtrip(self):\n        handler = CookieTestRequestHandler(\n            cookie_secret=self.KEY_VERSIONS, key_version=0\n        )\n        handler.set_secure_cookie(\"foo\", b\"bar\")\n        self.assertEqual(handler.get_secure_cookie(\"foo\"), b\"bar\")\n\n    def test_key_version_roundtrip_differing_version(self):\n        handler = CookieTestRequestHandler(\n            cookie_secret=self.KEY_VERSIONS, key_version=1\n        )\n        handler.set_secure_cookie(\"foo\", b\"bar\")\n        self.assertEqual(handler.get_secure_cookie(\"foo\"), b\"bar\")\n\n    def test_key_version_increment_version(self):\n        handler = CookieTestRequestHandler(\n            cookie_secret=self.KEY_VERSIONS, key_version=0\n        )\n        handler.set_secure_cookie(\"foo\", b\"bar\")\n        new_handler = CookieTestRequestHandler(\n            cookie_secret=self.KEY_VERSIONS, key_version=1\n        )\n        new_handler._cookies = handler._cookies\n        self.assertEqual(new_handler.get_secure_cookie(\"foo\"), b\"bar\")\n\n    def test_key_version_invalidate_version(self):\n        handler = CookieTestRequestHandler(\n            cookie_secret=self.KEY_VERSIONS, key_version=0\n        )\n        handler.set_secure_cookie(\"foo\", b\"bar\")\n        new_key_versions = self.KEY_VERSIONS.copy()\n        new_key_versions.pop(0)\n        new_handler = CookieTestRequestHandler(\n            cookie_secret=new_key_versions, key_version=1\n        )\n        new_handler._cookies = handler._cookies\n        self.assertEqual(new_handler.get_secure_cookie(\"foo\"), None)\n\n\nclass FinalReturnTest(WebTestCase):\n    def get_handlers(self):\n        test = self\n\n        class FinishHandler(RequestHandler):\n            @gen.coroutine\n            def get(self):\n                test.final_return = self.finish()\n                yield test.final_return\n\n            @gen.coroutine\n            def post(self):\n                self.write(\"hello,\")\n                yield self.flush()\n                test.final_return = self.finish(\"world\")\n                yield test.final_return\n\n        class RenderHandler(RequestHandler):\n            def create_template_loader(self, path):\n                return DictLoader({\"foo.html\": \"hi\"})\n\n            @gen.coroutine\n            def get(self):\n                test.final_return = self.render(\"foo.html\")\n\n        return [(\"/finish\", FinishHandler), (\"/render\", RenderHandler)]\n\n    def get_app_kwargs(self):\n        return dict(template_path=\"FinalReturnTest\")\n\n    def test_finish_method_return_future(self):\n        response = self.fetch(self.get_url(\"/finish\"))\n        self.assertEqual(response.code, 200)\n        self.assertIsInstance(self.final_return, Future)\n        self.assertTrue(self.final_return.done())\n\n        response = self.fetch(self.get_url(\"/finish\"), method=\"POST\", body=b\"\")\n        self.assertEqual(response.code, 200)\n        self.assertIsInstance(self.final_return, Future)\n        self.assertTrue(self.final_return.done())\n\n    def test_render_method_return_future(self):\n        response = self.fetch(self.get_url(\"/render\"))\n        self.assertEqual(response.code, 200)\n        self.assertIsInstance(self.final_return, Future)\n\n\nclass CookieTest(WebTestCase):\n    def get_handlers(self):\n        class SetCookieHandler(RequestHandler):\n            def get(self):\n                # Try setting cookies with different argument types\n                # to ensure that everything gets encoded correctly\n                self.set_cookie(\"str\", \"asdf\")\n                self.set_cookie(\"unicode\", u\"qwer\")\n                self.set_cookie(\"bytes\", b\"zxcv\")\n\n        class GetCookieHandler(RequestHandler):\n            def get(self):\n                self.write(self.get_cookie(\"foo\", \"default\"))\n\n        class SetCookieDomainHandler(RequestHandler):\n            def get(self):\n                # unicode domain and path arguments shouldn't break things\n                # either (see bug #285)\n                self.set_cookie(\"unicode_args\", \"blah\", domain=u\"foo.com\", path=u\"/foo\")\n\n        class SetCookieSpecialCharHandler(RequestHandler):\n            def get(self):\n                self.set_cookie(\"equals\", \"a=b\")\n                self.set_cookie(\"semicolon\", \"a;b\")\n                self.set_cookie(\"quote\", 'a\"b')\n\n        class SetCookieOverwriteHandler(RequestHandler):\n            def get(self):\n                self.set_cookie(\"a\", \"b\", domain=\"example.com\")\n                self.set_cookie(\"c\", \"d\", domain=\"example.com\")\n                # A second call with the same name clobbers the first.\n                # Attributes from the first call are not carried over.\n                self.set_cookie(\"a\", \"e\")\n\n        class SetCookieMaxAgeHandler(RequestHandler):\n            def get(self):\n                self.set_cookie(\"foo\", \"bar\", max_age=10)\n\n        class SetCookieExpiresDaysHandler(RequestHandler):\n            def get(self):\n                self.set_cookie(\"foo\", \"bar\", expires_days=10)\n\n        class SetCookieFalsyFlags(RequestHandler):\n            def get(self):\n                self.set_cookie(\"a\", \"1\", secure=True)\n                self.set_cookie(\"b\", \"1\", secure=False)\n                self.set_cookie(\"c\", \"1\", httponly=True)\n                self.set_cookie(\"d\", \"1\", httponly=False)\n\n        return [\n            (\"/set\", SetCookieHandler),\n            (\"/get\", GetCookieHandler),\n            (\"/set_domain\", SetCookieDomainHandler),\n            (\"/special_char\", SetCookieSpecialCharHandler),\n            (\"/set_overwrite\", SetCookieOverwriteHandler),\n            (\"/set_max_age\", SetCookieMaxAgeHandler),\n            (\"/set_expires_days\", SetCookieExpiresDaysHandler),\n            (\"/set_falsy_flags\", SetCookieFalsyFlags),\n        ]\n\n    def test_set_cookie(self):\n        response = self.fetch(\"/set\")\n        self.assertEqual(\n            sorted(response.headers.get_list(\"Set-Cookie\")),\n            [\"bytes=zxcv; Path=/\", \"str=asdf; Path=/\", \"unicode=qwer; Path=/\"],\n        )\n\n    def test_get_cookie(self):\n        response = self.fetch(\"/get\", headers={\"Cookie\": \"foo=bar\"})\n        self.assertEqual(response.body, b\"bar\")\n\n        response = self.fetch(\"/get\", headers={\"Cookie\": 'foo=\"bar\"'})\n        self.assertEqual(response.body, b\"bar\")\n\n        response = self.fetch(\"/get\", headers={\"Cookie\": \"/=exception;\"})\n        self.assertEqual(response.body, b\"default\")\n\n    def test_set_cookie_domain(self):\n        response = self.fetch(\"/set_domain\")\n        self.assertEqual(\n            response.headers.get_list(\"Set-Cookie\"),\n            [\"unicode_args=blah; Domain=foo.com; Path=/foo\"],\n        )\n\n    def test_cookie_special_char(self):\n        response = self.fetch(\"/special_char\")\n        headers = sorted(response.headers.get_list(\"Set-Cookie\"))\n        self.assertEqual(len(headers), 3)\n        self.assertEqual(headers[0], 'equals=\"a=b\"; Path=/')\n        self.assertEqual(headers[1], 'quote=\"a\\\\\"b\"; Path=/')\n        # python 2.7 octal-escapes the semicolon; older versions leave it alone\n        self.assertTrue(\n            headers[2] in ('semicolon=\"a;b\"; Path=/', 'semicolon=\"a\\\\073b\"; Path=/'),\n            headers[2],\n        )\n\n        data = [\n            (\"foo=a=b\", \"a=b\"),\n            ('foo=\"a=b\"', \"a=b\"),\n            ('foo=\"a;b\"', '\"a'),  # even quoted, \";\" is a delimiter\n            (\"foo=a\\\\073b\", \"a\\\\073b\"),  # escapes only decoded in quotes\n            ('foo=\"a\\\\073b\"', \"a;b\"),\n            ('foo=\"a\\\\\"b\"', 'a\"b'),\n        ]\n        for header, expected in data:\n            logging.debug(\"trying %r\", header)\n            response = self.fetch(\"/get\", headers={\"Cookie\": header})\n            self.assertEqual(response.body, utf8(expected))\n\n    def test_set_cookie_overwrite(self):\n        response = self.fetch(\"/set_overwrite\")\n        headers = response.headers.get_list(\"Set-Cookie\")\n        self.assertEqual(\n            sorted(headers), [\"a=e; Path=/\", \"c=d; Domain=example.com; Path=/\"]\n        )\n\n    def test_set_cookie_max_age(self):\n        response = self.fetch(\"/set_max_age\")\n        headers = response.headers.get_list(\"Set-Cookie\")\n        self.assertEqual(sorted(headers), [\"foo=bar; Max-Age=10; Path=/\"])\n\n    def test_set_cookie_expires_days(self):\n        response = self.fetch(\"/set_expires_days\")\n        header = response.headers.get(\"Set-Cookie\")\n        match = re.match(\"foo=bar; expires=(?P<expires>.+); Path=/\", header)\n        assert match is not None\n\n        expires = datetime.datetime.utcnow() + datetime.timedelta(days=10)\n        parsed = email.utils.parsedate(match.groupdict()[\"expires\"])\n        assert parsed is not None\n        header_expires = datetime.datetime(*parsed[:6])\n        self.assertTrue(abs((expires - header_expires).total_seconds()) < 10)\n\n    def test_set_cookie_false_flags(self):\n        response = self.fetch(\"/set_falsy_flags\")\n        headers = sorted(response.headers.get_list(\"Set-Cookie\"))\n        # The secure and httponly headers are capitalized in py35 and\n        # lowercase in older versions.\n        self.assertEqual(headers[0].lower(), \"a=1; path=/; secure\")\n        self.assertEqual(headers[1].lower(), \"b=1; path=/\")\n        self.assertEqual(headers[2].lower(), \"c=1; httponly; path=/\")\n        self.assertEqual(headers[3].lower(), \"d=1; path=/\")\n\n\nclass AuthRedirectRequestHandler(RequestHandler):\n    def initialize(self, login_url):\n        self.login_url = login_url\n\n    def get_login_url(self):\n        return self.login_url\n\n    @authenticated\n    def get(self):\n        # we'll never actually get here because the test doesn't follow redirects\n        self.send_error(500)\n\n\nclass AuthRedirectTest(WebTestCase):\n    def get_handlers(self):\n        return [\n            (\"/relative\", AuthRedirectRequestHandler, dict(login_url=\"/login\")),\n            (\n                \"/absolute\",\n                AuthRedirectRequestHandler,\n                dict(login_url=\"http://example.com/login\"),\n            ),\n        ]\n\n    def test_relative_auth_redirect(self):\n        response = self.fetch(self.get_url(\"/relative\"), follow_redirects=False)\n        self.assertEqual(response.code, 302)\n        self.assertEqual(response.headers[\"Location\"], \"/login?next=%2Frelative\")\n\n    def test_absolute_auth_redirect(self):\n        response = self.fetch(self.get_url(\"/absolute\"), follow_redirects=False)\n        self.assertEqual(response.code, 302)\n        self.assertTrue(\n            re.match(\n                r\"http://example.com/login\\?next=http%3A%2F%2F127.0.0.1%3A[0-9]+%2Fabsolute\",\n                response.headers[\"Location\"],\n            ),\n            response.headers[\"Location\"],\n        )\n\n\nclass ConnectionCloseHandler(RequestHandler):\n    def initialize(self, test):\n        self.test = test\n\n    @gen.coroutine\n    def get(self):\n        self.test.on_handler_waiting()\n        yield self.test.cleanup_event.wait()\n\n    def on_connection_close(self):\n        self.test.on_connection_close()\n\n\nclass ConnectionCloseTest(WebTestCase):\n    def get_handlers(self):\n        self.cleanup_event = Event()\n        return [(\"/\", ConnectionCloseHandler, dict(test=self))]\n\n    def test_connection_close(self):\n        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)\n        s.connect((\"127.0.0.1\", self.get_http_port()))\n        self.stream = IOStream(s)\n        self.stream.write(b\"GET / HTTP/1.0\\r\\n\\r\\n\")\n        self.wait()\n        # Let the hanging coroutine clean up after itself\n        self.cleanup_event.set()\n        self.io_loop.run_sync(lambda: gen.sleep(0))\n\n    def on_handler_waiting(self):\n        logging.debug(\"handler waiting\")\n        self.stream.close()\n\n    def on_connection_close(self):\n        logging.debug(\"connection closed\")\n        self.stop()\n\n\nclass EchoHandler(RequestHandler):\n    def get(self, *path_args):\n        # Type checks: web.py interfaces convert argument values to\n        # unicode strings (by default, but see also decode_argument).\n        # In httpserver.py (i.e. self.request.arguments), they're left\n        # as bytes.  Keys are always native strings.\n        for key in self.request.arguments:\n            if type(key) != str:\n                raise Exception(\"incorrect type for key: %r\" % type(key))\n            for value in self.request.arguments[key]:\n                if type(value) != bytes:\n                    raise Exception(\"incorrect type for value: %r\" % type(value))\n            for value in self.get_arguments(key):\n                if type(value) != unicode_type:\n                    raise Exception(\"incorrect type for value: %r\" % type(value))\n        for arg in path_args:\n            if type(arg) != unicode_type:\n                raise Exception(\"incorrect type for path arg: %r\" % type(arg))\n        self.write(\n            dict(\n                path=self.request.path,\n                path_args=path_args,\n                args=recursive_unicode(self.request.arguments),\n            )\n        )\n\n\nclass RequestEncodingTest(WebTestCase):\n    def get_handlers(self):\n        return [(\"/group/(.*)\", EchoHandler), (\"/slashes/([^/]*)/([^/]*)\", EchoHandler)]\n\n    def fetch_json(self, path):\n        return json_decode(self.fetch(path).body)\n\n    def test_group_question_mark(self):\n        # Ensure that url-encoded question marks are handled properly\n        self.assertEqual(\n            self.fetch_json(\"/group/%3F\"),\n            dict(path=\"/group/%3F\", path_args=[\"?\"], args={}),\n        )\n        self.assertEqual(\n            self.fetch_json(\"/group/%3F?%3F=%3F\"),\n            dict(path=\"/group/%3F\", path_args=[\"?\"], args={\"?\": [\"?\"]}),\n        )\n\n    def test_group_encoding(self):\n        # Path components and query arguments should be decoded the same way\n        self.assertEqual(\n            self.fetch_json(\"/group/%C3%A9?arg=%C3%A9\"),\n            {\n                u\"path\": u\"/group/%C3%A9\",\n                u\"path_args\": [u\"\\u00e9\"],\n                u\"args\": {u\"arg\": [u\"\\u00e9\"]},\n            },\n        )\n\n    def test_slashes(self):\n        # Slashes may be escaped to appear as a single \"directory\" in the path,\n        # but they are then unescaped when passed to the get() method.\n        self.assertEqual(\n            self.fetch_json(\"/slashes/foo/bar\"),\n            dict(path=\"/slashes/foo/bar\", path_args=[\"foo\", \"bar\"], args={}),\n        )\n        self.assertEqual(\n            self.fetch_json(\"/slashes/a%2Fb/c%2Fd\"),\n            dict(path=\"/slashes/a%2Fb/c%2Fd\", path_args=[\"a/b\", \"c/d\"], args={}),\n        )\n\n    def test_error(self):\n        # Percent signs (encoded as %25) should not mess up printf-style\n        # messages in logs\n        with ExpectLog(gen_log, \".*Invalid unicode\"):\n            self.fetch(\"/group/?arg=%25%e9\")\n\n\nclass TypeCheckHandler(RequestHandler):\n    def prepare(self):\n        self.errors = {}  # type: typing.Dict[str, str]\n\n        self.check_type(\"status\", self.get_status(), int)\n\n        # get_argument is an exception from the general rule of using\n        # type str for non-body data mainly for historical reasons.\n        self.check_type(\"argument\", self.get_argument(\"foo\"), unicode_type)\n        self.check_type(\"cookie_key\", list(self.cookies.keys())[0], str)\n        self.check_type(\"cookie_value\", list(self.cookies.values())[0].value, str)\n\n        # Secure cookies return bytes because they can contain arbitrary\n        # data, but regular cookies are native strings.\n        if list(self.cookies.keys()) != [\"asdf\"]:\n            raise Exception(\n                \"unexpected values for cookie keys: %r\" % self.cookies.keys()\n            )\n        self.check_type(\"get_secure_cookie\", self.get_secure_cookie(\"asdf\"), bytes)\n        self.check_type(\"get_cookie\", self.get_cookie(\"asdf\"), str)\n\n        self.check_type(\"xsrf_token\", self.xsrf_token, bytes)\n        self.check_type(\"xsrf_form_html\", self.xsrf_form_html(), str)\n\n        self.check_type(\"reverse_url\", self.reverse_url(\"typecheck\", \"foo\"), str)\n\n        self.check_type(\"request_summary\", self._request_summary(), str)\n\n    def get(self, path_component):\n        # path_component uses type unicode instead of str for consistency\n        # with get_argument()\n        self.check_type(\"path_component\", path_component, unicode_type)\n        self.write(self.errors)\n\n    def post(self, path_component):\n        self.check_type(\"path_component\", path_component, unicode_type)\n        self.write(self.errors)\n\n    def check_type(self, name, obj, expected_type):\n        actual_type = type(obj)\n        if expected_type != actual_type:\n            self.errors[name] = \"expected %s, got %s\" % (expected_type, actual_type)\n\n\nclass DecodeArgHandler(RequestHandler):\n    def decode_argument(self, value, name=None):\n        if type(value) != bytes:\n            raise Exception(\"unexpected type for value: %r\" % type(value))\n        # use self.request.arguments directly to avoid recursion\n        if \"encoding\" in self.request.arguments:\n            return value.decode(to_unicode(self.request.arguments[\"encoding\"][0]))\n        else:\n            return value\n\n    def get(self, arg):\n        def describe(s):\n            if type(s) == bytes:\n                return [\"bytes\", native_str(binascii.b2a_hex(s))]\n            elif type(s) == unicode_type:\n                return [\"unicode\", s]\n            raise Exception(\"unknown type\")\n\n        self.write({\"path\": describe(arg), \"query\": describe(self.get_argument(\"foo\"))})\n\n\nclass LinkifyHandler(RequestHandler):\n    def get(self):\n        self.render(\"linkify.html\", message=\"http://example.com\")\n\n\nclass UIModuleResourceHandler(RequestHandler):\n    def get(self):\n        self.render(\"page.html\", entries=[1, 2])\n\n\nclass OptionalPathHandler(RequestHandler):\n    def get(self, path):\n        self.write({\"path\": path})\n\n\nclass MultiHeaderHandler(RequestHandler):\n    def get(self):\n        self.set_header(\"x-overwrite\", \"1\")\n        self.set_header(\"X-Overwrite\", 2)\n        self.add_header(\"x-multi\", 3)\n        self.add_header(\"X-Multi\", \"4\")\n\n\nclass RedirectHandler(RequestHandler):\n    def get(self):\n        if self.get_argument(\"permanent\", None) is not None:\n            self.redirect(\"/\", permanent=int(self.get_argument(\"permanent\")))\n        elif self.get_argument(\"status\", None) is not None:\n            self.redirect(\"/\", status=int(self.get_argument(\"status\")))\n        else:\n            raise Exception(\"didn't get permanent or status arguments\")\n\n\nclass EmptyFlushCallbackHandler(RequestHandler):\n    @gen.coroutine\n    def get(self):\n        # Ensure that the flush callback is run whether or not there\n        # was any output.  The gen.Task and direct yield forms are\n        # equivalent.\n        yield self.flush()  # \"empty\" flush, but writes headers\n        yield self.flush()  # empty flush\n        self.write(\"o\")\n        yield self.flush()  # flushes the \"o\"\n        yield self.flush()  # empty flush\n        self.finish(\"k\")\n\n\nclass HeaderInjectionHandler(RequestHandler):\n    def get(self):\n        try:\n            self.set_header(\"X-Foo\", \"foo\\r\\nX-Bar: baz\")\n            raise Exception(\"Didn't get expected exception\")\n        except ValueError as e:\n            if \"Unsafe header value\" in str(e):\n                self.finish(b\"ok\")\n            else:\n                raise\n\n\nclass GetArgumentHandler(RequestHandler):\n    def prepare(self):\n        if self.get_argument(\"source\", None) == \"query\":\n            method = self.get_query_argument\n        elif self.get_argument(\"source\", None) == \"body\":\n            method = self.get_body_argument\n        else:\n            method = self.get_argument\n        self.finish(method(\"foo\", \"default\"))\n\n\nclass GetArgumentsHandler(RequestHandler):\n    def prepare(self):\n        self.finish(\n            dict(\n                default=self.get_arguments(\"foo\"),\n                query=self.get_query_arguments(\"foo\"),\n                body=self.get_body_arguments(\"foo\"),\n            )\n        )\n\n\n# This test was shared with wsgi_test.py; now the name is meaningless.\nclass WSGISafeWebTest(WebTestCase):\n    COOKIE_SECRET = \"WebTest.COOKIE_SECRET\"\n\n    def get_app_kwargs(self):\n        loader = DictLoader(\n            {\n                \"linkify.html\": \"{% module linkify(message) %}\",\n                \"page.html\": \"\"\"\\\n<html><head></head><body>\n{% for e in entries %}\n{% module Template(\"entry.html\", entry=e) %}\n{% end %}\n</body></html>\"\"\",\n                \"entry.html\": \"\"\"\\\n{{ set_resources(embedded_css=\".entry { margin-bottom: 1em; }\",\n                 embedded_javascript=\"js_embed()\",\n                 css_files=[\"/base.css\", \"/foo.css\"],\n                 javascript_files=\"/common.js\",\n                 html_head=\"<meta>\",\n                 html_body='<script src=\"/analytics.js\"/>') }}\n<div class=\"entry\">...</div>\"\"\",\n            }\n        )\n        return dict(\n            template_loader=loader,\n            autoescape=\"xhtml_escape\",\n            cookie_secret=self.COOKIE_SECRET,\n        )\n\n    def tearDown(self):\n        super(WSGISafeWebTest, self).tearDown()\n        RequestHandler._template_loaders.clear()\n\n    def get_handlers(self):\n        urls = [\n            url(\"/typecheck/(.*)\", TypeCheckHandler, name=\"typecheck\"),\n            url(\"/decode_arg/(.*)\", DecodeArgHandler, name=\"decode_arg\"),\n            url(\"/decode_arg_kw/(?P<arg>.*)\", DecodeArgHandler),\n            url(\"/linkify\", LinkifyHandler),\n            url(\"/uimodule_resources\", UIModuleResourceHandler),\n            url(\"/optional_path/(.+)?\", OptionalPathHandler),\n            url(\"/multi_header\", MultiHeaderHandler),\n            url(\"/redirect\", RedirectHandler),\n            url(\n                \"/web_redirect_permanent\",\n                WebRedirectHandler,\n                {\"url\": \"/web_redirect_newpath\"},\n            ),\n            url(\n                \"/web_redirect\",\n                WebRedirectHandler,\n                {\"url\": \"/web_redirect_newpath\", \"permanent\": False},\n            ),\n            url(\n                \"//web_redirect_double_slash\",\n                WebRedirectHandler,\n                {\"url\": \"/web_redirect_newpath\"},\n            ),\n            url(\"/header_injection\", HeaderInjectionHandler),\n            url(\"/get_argument\", GetArgumentHandler),\n            url(\"/get_arguments\", GetArgumentsHandler),\n        ]\n        return urls\n\n    def fetch_json(self, *args, **kwargs):\n        response = self.fetch(*args, **kwargs)\n        response.rethrow()\n        return json_decode(response.body)\n\n    def test_types(self):\n        cookie_value = to_unicode(\n            create_signed_value(self.COOKIE_SECRET, \"asdf\", \"qwer\")\n        )\n        response = self.fetch(\n            \"/typecheck/asdf?foo=bar\", headers={\"Cookie\": \"asdf=\" + cookie_value}\n        )\n        data = json_decode(response.body)\n        self.assertEqual(data, {})\n\n        response = self.fetch(\n            \"/typecheck/asdf?foo=bar\",\n            method=\"POST\",\n            headers={\"Cookie\": \"asdf=\" + cookie_value},\n            body=\"foo=bar\",\n        )\n\n    def test_decode_argument(self):\n        # These urls all decode to the same thing\n        urls = [\n            \"/decode_arg/%C3%A9?foo=%C3%A9&encoding=utf-8\",\n            \"/decode_arg/%E9?foo=%E9&encoding=latin1\",\n            \"/decode_arg_kw/%E9?foo=%E9&encoding=latin1\",\n        ]\n        for req_url in urls:\n            response = self.fetch(req_url)\n            response.rethrow()\n            data = json_decode(response.body)\n            self.assertEqual(\n                data,\n                {u\"path\": [u\"unicode\", u\"\\u00e9\"], u\"query\": [u\"unicode\", u\"\\u00e9\"]},\n            )\n\n        response = self.fetch(\"/decode_arg/%C3%A9?foo=%C3%A9\")\n        response.rethrow()\n        data = json_decode(response.body)\n        self.assertEqual(\n            data, {u\"path\": [u\"bytes\", u\"c3a9\"], u\"query\": [u\"bytes\", u\"c3a9\"]}\n        )\n\n    def test_decode_argument_invalid_unicode(self):\n        # test that invalid unicode in URLs causes 400, not 500\n        with ExpectLog(gen_log, \".*Invalid unicode.*\"):\n            response = self.fetch(\"/typecheck/invalid%FF\")\n            self.assertEqual(response.code, 400)\n            response = self.fetch(\"/typecheck/invalid?foo=%FF\")\n            self.assertEqual(response.code, 400)\n\n    def test_decode_argument_plus(self):\n        # These urls are all equivalent.\n        urls = [\n            \"/decode_arg/1%20%2B%201?foo=1%20%2B%201&encoding=utf-8\",\n            \"/decode_arg/1%20+%201?foo=1+%2B+1&encoding=utf-8\",\n        ]\n        for req_url in urls:\n            response = self.fetch(req_url)\n            response.rethrow()\n            data = json_decode(response.body)\n            self.assertEqual(\n                data,\n                {u\"path\": [u\"unicode\", u\"1 + 1\"], u\"query\": [u\"unicode\", u\"1 + 1\"]},\n            )\n\n    def test_reverse_url(self):\n        self.assertEqual(self.app.reverse_url(\"decode_arg\", \"foo\"), \"/decode_arg/foo\")\n        self.assertEqual(self.app.reverse_url(\"decode_arg\", 42), \"/decode_arg/42\")\n        self.assertEqual(self.app.reverse_url(\"decode_arg\", b\"\\xe9\"), \"/decode_arg/%E9\")\n        self.assertEqual(\n            self.app.reverse_url(\"decode_arg\", u\"\\u00e9\"), \"/decode_arg/%C3%A9\"\n        )\n        self.assertEqual(\n            self.app.reverse_url(\"decode_arg\", \"1 + 1\"), \"/decode_arg/1%20%2B%201\"\n        )\n\n    def test_uimodule_unescaped(self):\n        response = self.fetch(\"/linkify\")\n        self.assertEqual(\n            response.body, b'<a href=\"http://example.com\">http://example.com</a>'\n        )\n\n    def test_uimodule_resources(self):\n        response = self.fetch(\"/uimodule_resources\")\n        self.assertEqual(\n            response.body,\n            b\"\"\"\\\n<html><head><link href=\"/base.css\" type=\"text/css\" rel=\"stylesheet\"/><link href=\"/foo.css\" type=\"text/css\" rel=\"stylesheet\"/>\n<style type=\"text/css\">\n.entry { margin-bottom: 1em; }\n</style>\n<meta>\n</head><body>\n\n\n<div class=\"entry\">...</div>\n\n\n<div class=\"entry\">...</div>\n\n<script src=\"/common.js\" type=\"text/javascript\"></script>\n<script type=\"text/javascript\">\n//<![CDATA[\njs_embed()\n//]]>\n</script>\n<script src=\"/analytics.js\"/>\n</body></html>\"\"\",  # noqa: E501\n        )\n\n    def test_optional_path(self):\n        self.assertEqual(self.fetch_json(\"/optional_path/foo\"), {u\"path\": u\"foo\"})\n        self.assertEqual(self.fetch_json(\"/optional_path/\"), {u\"path\": None})\n\n    def test_multi_header(self):\n        response = self.fetch(\"/multi_header\")\n        self.assertEqual(response.headers[\"x-overwrite\"], \"2\")\n        self.assertEqual(response.headers.get_list(\"x-multi\"), [\"3\", \"4\"])\n\n    def test_redirect(self):\n        response = self.fetch(\"/redirect?permanent=1\", follow_redirects=False)\n        self.assertEqual(response.code, 301)\n        response = self.fetch(\"/redirect?permanent=0\", follow_redirects=False)\n        self.assertEqual(response.code, 302)\n        response = self.fetch(\"/redirect?status=307\", follow_redirects=False)\n        self.assertEqual(response.code, 307)\n\n    def test_web_redirect(self):\n        response = self.fetch(\"/web_redirect_permanent\", follow_redirects=False)\n        self.assertEqual(response.code, 301)\n        self.assertEqual(response.headers[\"Location\"], \"/web_redirect_newpath\")\n        response = self.fetch(\"/web_redirect\", follow_redirects=False)\n        self.assertEqual(response.code, 302)\n        self.assertEqual(response.headers[\"Location\"], \"/web_redirect_newpath\")\n\n    def test_web_redirect_double_slash(self):\n        response = self.fetch(\"//web_redirect_double_slash\", follow_redirects=False)\n        self.assertEqual(response.code, 301)\n        self.assertEqual(response.headers[\"Location\"], \"/web_redirect_newpath\")\n\n    def test_header_injection(self):\n        response = self.fetch(\"/header_injection\")\n        self.assertEqual(response.body, b\"ok\")\n\n    def test_get_argument(self):\n        response = self.fetch(\"/get_argument?foo=bar\")\n        self.assertEqual(response.body, b\"bar\")\n        response = self.fetch(\"/get_argument?foo=\")\n        self.assertEqual(response.body, b\"\")\n        response = self.fetch(\"/get_argument\")\n        self.assertEqual(response.body, b\"default\")\n\n        # Test merging of query and body arguments.\n        # In singular form, body arguments take precedence over query arguments.\n        body = urllib.parse.urlencode(dict(foo=\"hello\"))\n        response = self.fetch(\"/get_argument?foo=bar\", method=\"POST\", body=body)\n        self.assertEqual(response.body, b\"hello\")\n        # In plural methods they are merged.\n        response = self.fetch(\"/get_arguments?foo=bar\", method=\"POST\", body=body)\n        self.assertEqual(\n            json_decode(response.body),\n            dict(default=[\"bar\", \"hello\"], query=[\"bar\"], body=[\"hello\"]),\n        )\n\n    def test_get_query_arguments(self):\n        # send as a post so we can ensure the separation between query\n        # string and body arguments.\n        body = urllib.parse.urlencode(dict(foo=\"hello\"))\n        response = self.fetch(\n            \"/get_argument?source=query&foo=bar\", method=\"POST\", body=body\n        )\n        self.assertEqual(response.body, b\"bar\")\n        response = self.fetch(\n            \"/get_argument?source=query&foo=\", method=\"POST\", body=body\n        )\n        self.assertEqual(response.body, b\"\")\n        response = self.fetch(\"/get_argument?source=query\", method=\"POST\", body=body)\n        self.assertEqual(response.body, b\"default\")\n\n    def test_get_body_arguments(self):\n        body = urllib.parse.urlencode(dict(foo=\"bar\"))\n        response = self.fetch(\n            \"/get_argument?source=body&foo=hello\", method=\"POST\", body=body\n        )\n        self.assertEqual(response.body, b\"bar\")\n\n        body = urllib.parse.urlencode(dict(foo=\"\"))\n        response = self.fetch(\n            \"/get_argument?source=body&foo=hello\", method=\"POST\", body=body\n        )\n        self.assertEqual(response.body, b\"\")\n\n        body = urllib.parse.urlencode(dict())\n        response = self.fetch(\n            \"/get_argument?source=body&foo=hello\", method=\"POST\", body=body\n        )\n        self.assertEqual(response.body, b\"default\")\n\n    def test_no_gzip(self):\n        response = self.fetch(\"/get_argument\")\n        self.assertNotIn(\"Accept-Encoding\", response.headers.get(\"Vary\", \"\"))\n        self.assertNotIn(\"gzip\", response.headers.get(\"Content-Encoding\", \"\"))\n\n\nclass NonWSGIWebTests(WebTestCase):\n    def get_handlers(self):\n        return [(\"/empty_flush\", EmptyFlushCallbackHandler)]\n\n    def test_empty_flush(self):\n        response = self.fetch(\"/empty_flush\")\n        self.assertEqual(response.body, b\"ok\")\n\n\nclass ErrorResponseTest(WebTestCase):\n    def get_handlers(self):\n        class DefaultHandler(RequestHandler):\n            def get(self):\n                if self.get_argument(\"status\", None):\n                    raise HTTPError(int(self.get_argument(\"status\")))\n                1 / 0\n\n        class WriteErrorHandler(RequestHandler):\n            def get(self):\n                if self.get_argument(\"status\", None):\n                    self.send_error(int(self.get_argument(\"status\")))\n                else:\n                    1 / 0\n\n            def write_error(self, status_code, **kwargs):\n                self.set_header(\"Content-Type\", \"text/plain\")\n                if \"exc_info\" in kwargs:\n                    self.write(\"Exception: %s\" % kwargs[\"exc_info\"][0].__name__)\n                else:\n                    self.write(\"Status: %d\" % status_code)\n\n        class FailedWriteErrorHandler(RequestHandler):\n            def get(self):\n                1 / 0\n\n            def write_error(self, status_code, **kwargs):\n                raise Exception(\"exception in write_error\")\n\n        return [\n            url(\"/default\", DefaultHandler),\n            url(\"/write_error\", WriteErrorHandler),\n            url(\"/failed_write_error\", FailedWriteErrorHandler),\n        ]\n\n    def test_default(self):\n        with ExpectLog(app_log, \"Uncaught exception\"):\n            response = self.fetch(\"/default\")\n            self.assertEqual(response.code, 500)\n            self.assertTrue(b\"500: Internal Server Error\" in response.body)\n\n            response = self.fetch(\"/default?status=503\")\n            self.assertEqual(response.code, 503)\n            self.assertTrue(b\"503: Service Unavailable\" in response.body)\n\n            response = self.fetch(\"/default?status=435\")\n            self.assertEqual(response.code, 435)\n            self.assertTrue(b\"435: Unknown\" in response.body)\n\n    def test_write_error(self):\n        with ExpectLog(app_log, \"Uncaught exception\"):\n            response = self.fetch(\"/write_error\")\n            self.assertEqual(response.code, 500)\n            self.assertEqual(b\"Exception: ZeroDivisionError\", response.body)\n\n            response = self.fetch(\"/write_error?status=503\")\n            self.assertEqual(response.code, 503)\n            self.assertEqual(b\"Status: 503\", response.body)\n\n    def test_failed_write_error(self):\n        with ExpectLog(app_log, \"Uncaught exception\"):\n            response = self.fetch(\"/failed_write_error\")\n            self.assertEqual(response.code, 500)\n            self.assertEqual(b\"\", response.body)\n\n\nclass StaticFileTest(WebTestCase):\n    # The expected MD5 hash of robots.txt, used in tests that call\n    # StaticFileHandler.get_version\n    robots_txt_hash = b\"f71d20196d4caf35b6a670db8c70b03d\"\n    static_dir = os.path.join(os.path.dirname(__file__), \"static\")\n\n    def get_handlers(self):\n        class StaticUrlHandler(RequestHandler):\n            def get(self, path):\n                with_v = int(self.get_argument(\"include_version\", 1))\n                self.write(self.static_url(path, include_version=with_v))\n\n        class AbsoluteStaticUrlHandler(StaticUrlHandler):\n            include_host = True\n\n        class OverrideStaticUrlHandler(RequestHandler):\n            def get(self, path):\n                do_include = bool(self.get_argument(\"include_host\"))\n                self.include_host = not do_include\n\n                regular_url = self.static_url(path)\n                override_url = self.static_url(path, include_host=do_include)\n                if override_url == regular_url:\n                    return self.write(str(False))\n\n                protocol = self.request.protocol + \"://\"\n                protocol_length = len(protocol)\n                check_regular = regular_url.find(protocol, 0, protocol_length)\n                check_override = override_url.find(protocol, 0, protocol_length)\n\n                if do_include:\n                    result = check_override == 0 and check_regular == -1\n                else:\n                    result = check_override == -1 and check_regular == 0\n                self.write(str(result))\n\n        return [\n            (\"/static_url/(.*)\", StaticUrlHandler),\n            (\"/abs_static_url/(.*)\", AbsoluteStaticUrlHandler),\n            (\"/override_static_url/(.*)\", OverrideStaticUrlHandler),\n            (\"/root_static/(.*)\", StaticFileHandler, dict(path=\"/\")),\n        ]\n\n    def get_app_kwargs(self):\n        return dict(static_path=relpath(\"static\"))\n\n    def test_static_files(self):\n        response = self.fetch(\"/robots.txt\")\n        self.assertTrue(b\"Disallow: /\" in response.body)\n\n        response = self.fetch(\"/static/robots.txt\")\n        self.assertTrue(b\"Disallow: /\" in response.body)\n        self.assertEqual(response.headers.get(\"Content-Type\"), \"text/plain\")\n\n    def test_static_compressed_files(self):\n        response = self.fetch(\"/static/sample.xml.gz\")\n        self.assertEqual(response.headers.get(\"Content-Type\"), \"application/gzip\")\n        response = self.fetch(\"/static/sample.xml.bz2\")\n        self.assertEqual(\n            response.headers.get(\"Content-Type\"), \"application/octet-stream\"\n        )\n        # make sure the uncompressed file still has the correct type\n        response = self.fetch(\"/static/sample.xml\")\n        self.assertTrue(\n            response.headers.get(\"Content-Type\") in set((\"text/xml\", \"application/xml\"))\n        )\n\n    def test_static_url(self):\n        response = self.fetch(\"/static_url/robots.txt\")\n        self.assertEqual(response.body, b\"/static/robots.txt?v=\" + self.robots_txt_hash)\n\n    def test_absolute_static_url(self):\n        response = self.fetch(\"/abs_static_url/robots.txt\")\n        self.assertEqual(\n            response.body,\n            (utf8(self.get_url(\"/\")) + b\"static/robots.txt?v=\" + self.robots_txt_hash),\n        )\n\n    def test_relative_version_exclusion(self):\n        response = self.fetch(\"/static_url/robots.txt?include_version=0\")\n        self.assertEqual(response.body, b\"/static/robots.txt\")\n\n    def test_absolute_version_exclusion(self):\n        response = self.fetch(\"/abs_static_url/robots.txt?include_version=0\")\n        self.assertEqual(response.body, utf8(self.get_url(\"/\") + \"static/robots.txt\"))\n\n    def test_include_host_override(self):\n        self._trigger_include_host_check(False)\n        self._trigger_include_host_check(True)\n\n    def _trigger_include_host_check(self, include_host):\n        path = \"/override_static_url/robots.txt?include_host=%s\"\n        response = self.fetch(path % int(include_host))\n        self.assertEqual(response.body, utf8(str(True)))\n\n    def get_and_head(self, *args, **kwargs):\n        \"\"\"Performs a GET and HEAD request and returns the GET response.\n\n        Fails if any ``Content-*`` headers returned by the two requests\n        differ.\n        \"\"\"\n        head_response = self.fetch(*args, method=\"HEAD\", **kwargs)\n        get_response = self.fetch(*args, method=\"GET\", **kwargs)\n        content_headers = set()\n        for h in itertools.chain(head_response.headers, get_response.headers):\n            if h.startswith(\"Content-\"):\n                content_headers.add(h)\n        for h in content_headers:\n            self.assertEqual(\n                head_response.headers.get(h),\n                get_response.headers.get(h),\n                \"%s differs between GET (%s) and HEAD (%s)\"\n                % (h, head_response.headers.get(h), get_response.headers.get(h)),\n            )\n        return get_response\n\n    def test_static_304_if_modified_since(self):\n        response1 = self.get_and_head(\"/static/robots.txt\")\n        response2 = self.get_and_head(\n            \"/static/robots.txt\",\n            headers={\"If-Modified-Since\": response1.headers[\"Last-Modified\"]},\n        )\n        self.assertEqual(response2.code, 304)\n        self.assertTrue(\"Content-Length\" not in response2.headers)\n        self.assertTrue(\"Last-Modified\" not in response2.headers)\n\n    def test_static_304_if_none_match(self):\n        response1 = self.get_and_head(\"/static/robots.txt\")\n        response2 = self.get_and_head(\n            \"/static/robots.txt\", headers={\"If-None-Match\": response1.headers[\"Etag\"]}\n        )\n        self.assertEqual(response2.code, 304)\n\n    def test_static_304_etag_modified_bug(self):\n        response1 = self.get_and_head(\"/static/robots.txt\")\n        response2 = self.get_and_head(\n            \"/static/robots.txt\",\n            headers={\n                \"If-None-Match\": '\"MISMATCH\"',\n                \"If-Modified-Since\": response1.headers[\"Last-Modified\"],\n            },\n        )\n        self.assertEqual(response2.code, 200)\n\n    def test_static_if_modified_since_pre_epoch(self):\n        # On windows, the functions that work with time_t do not accept\n        # negative values, and at least one client (processing.js) seems\n        # to use if-modified-since 1/1/1960 as a cache-busting technique.\n        response = self.get_and_head(\n            \"/static/robots.txt\",\n            headers={\"If-Modified-Since\": \"Fri, 01 Jan 1960 00:00:00 GMT\"},\n        )\n        self.assertEqual(response.code, 200)\n\n    def test_static_if_modified_since_time_zone(self):\n        # Instead of the value from Last-Modified, make requests with times\n        # chosen just before and after the known modification time\n        # of the file to ensure that the right time zone is being used\n        # when parsing If-Modified-Since.\n        stat = os.stat(relpath(\"static/robots.txt\"))\n\n        response = self.get_and_head(\n            \"/static/robots.txt\",\n            headers={\"If-Modified-Since\": format_timestamp(stat.st_mtime - 1)},\n        )\n        self.assertEqual(response.code, 200)\n        response = self.get_and_head(\n            \"/static/robots.txt\",\n            headers={\"If-Modified-Since\": format_timestamp(stat.st_mtime + 1)},\n        )\n        self.assertEqual(response.code, 304)\n\n    def test_static_etag(self):\n        response = self.get_and_head(\"/static/robots.txt\")\n        self.assertEqual(\n            utf8(response.headers.get(\"Etag\")), b'\"' + self.robots_txt_hash + b'\"'\n        )\n\n    def test_static_with_range(self):\n        response = self.get_and_head(\n            \"/static/robots.txt\", headers={\"Range\": \"bytes=0-9\"}\n        )\n        self.assertEqual(response.code, 206)\n        self.assertEqual(response.body, b\"User-agent\")\n        self.assertEqual(\n            utf8(response.headers.get(\"Etag\")), b'\"' + self.robots_txt_hash + b'\"'\n        )\n        self.assertEqual(response.headers.get(\"Content-Length\"), \"10\")\n        self.assertEqual(response.headers.get(\"Content-Range\"), \"bytes 0-9/26\")\n\n    def test_static_with_range_full_file(self):\n        response = self.get_and_head(\n            \"/static/robots.txt\", headers={\"Range\": \"bytes=0-\"}\n        )\n        # Note: Chrome refuses to play audio if it gets an HTTP 206 in response\n        # to ``Range: bytes=0-`` :(\n        self.assertEqual(response.code, 200)\n        robots_file_path = os.path.join(self.static_dir, \"robots.txt\")\n        with open(robots_file_path) as f:\n            self.assertEqual(response.body, utf8(f.read()))\n        self.assertEqual(response.headers.get(\"Content-Length\"), \"26\")\n        self.assertEqual(response.headers.get(\"Content-Range\"), None)\n\n    def test_static_with_range_full_past_end(self):\n        response = self.get_and_head(\n            \"/static/robots.txt\", headers={\"Range\": \"bytes=0-10000000\"}\n        )\n        self.assertEqual(response.code, 200)\n        robots_file_path = os.path.join(self.static_dir, \"robots.txt\")\n        with open(robots_file_path) as f:\n            self.assertEqual(response.body, utf8(f.read()))\n        self.assertEqual(response.headers.get(\"Content-Length\"), \"26\")\n        self.assertEqual(response.headers.get(\"Content-Range\"), None)\n\n    def test_static_with_range_partial_past_end(self):\n        response = self.get_and_head(\n            \"/static/robots.txt\", headers={\"Range\": \"bytes=1-10000000\"}\n        )\n        self.assertEqual(response.code, 206)\n        robots_file_path = os.path.join(self.static_dir, \"robots.txt\")\n        with open(robots_file_path) as f:\n            self.assertEqual(response.body, utf8(f.read()[1:]))\n        self.assertEqual(response.headers.get(\"Content-Length\"), \"25\")\n        self.assertEqual(response.headers.get(\"Content-Range\"), \"bytes 1-25/26\")\n\n    def test_static_with_range_end_edge(self):\n        response = self.get_and_head(\n            \"/static/robots.txt\", headers={\"Range\": \"bytes=22-\"}\n        )\n        self.assertEqual(response.body, b\": /\\n\")\n        self.assertEqual(response.headers.get(\"Content-Length\"), \"4\")\n        self.assertEqual(response.headers.get(\"Content-Range\"), \"bytes 22-25/26\")\n\n    def test_static_with_range_neg_end(self):\n        response = self.get_and_head(\n            \"/static/robots.txt\", headers={\"Range\": \"bytes=-4\"}\n        )\n        self.assertEqual(response.body, b\": /\\n\")\n        self.assertEqual(response.headers.get(\"Content-Length\"), \"4\")\n        self.assertEqual(response.headers.get(\"Content-Range\"), \"bytes 22-25/26\")\n\n    def test_static_with_range_neg_past_start(self):\n        response = self.get_and_head(\n            \"/static/robots.txt\", headers={\"Range\": \"bytes=-1000000\"}\n        )\n        self.assertEqual(response.code, 200)\n        robots_file_path = os.path.join(self.static_dir, \"robots.txt\")\n        with open(robots_file_path) as f:\n            self.assertEqual(response.body, utf8(f.read()))\n        self.assertEqual(response.headers.get(\"Content-Length\"), \"26\")\n        self.assertEqual(response.headers.get(\"Content-Range\"), None)\n\n    def test_static_invalid_range(self):\n        response = self.get_and_head(\"/static/robots.txt\", headers={\"Range\": \"asdf\"})\n        self.assertEqual(response.code, 200)\n\n    def test_static_unsatisfiable_range_zero_suffix(self):\n        response = self.get_and_head(\n            \"/static/robots.txt\", headers={\"Range\": \"bytes=-0\"}\n        )\n        self.assertEqual(response.headers.get(\"Content-Range\"), \"bytes */26\")\n        self.assertEqual(response.code, 416)\n\n    def test_static_unsatisfiable_range_invalid_start(self):\n        response = self.get_and_head(\n            \"/static/robots.txt\", headers={\"Range\": \"bytes=26\"}\n        )\n        self.assertEqual(response.code, 416)\n        self.assertEqual(response.headers.get(\"Content-Range\"), \"bytes */26\")\n\n    def test_static_unsatisfiable_range_end_less_than_start(self):\n        response = self.get_and_head(\n            \"/static/robots.txt\", headers={\"Range\": \"bytes=10-3\"}\n        )\n        self.assertEqual(response.code, 416)\n        self.assertEqual(response.headers.get(\"Content-Range\"), \"bytes */26\")\n\n    def test_static_head(self):\n        response = self.fetch(\"/static/robots.txt\", method=\"HEAD\")\n        self.assertEqual(response.code, 200)\n        # No body was returned, but we did get the right content length.\n        self.assertEqual(response.body, b\"\")\n        self.assertEqual(response.headers[\"Content-Length\"], \"26\")\n        self.assertEqual(\n            utf8(response.headers[\"Etag\"]), b'\"' + self.robots_txt_hash + b'\"'\n        )\n\n    def test_static_head_range(self):\n        response = self.fetch(\n            \"/static/robots.txt\", method=\"HEAD\", headers={\"Range\": \"bytes=1-4\"}\n        )\n        self.assertEqual(response.code, 206)\n        self.assertEqual(response.body, b\"\")\n        self.assertEqual(response.headers[\"Content-Length\"], \"4\")\n        self.assertEqual(\n            utf8(response.headers[\"Etag\"]), b'\"' + self.robots_txt_hash + b'\"'\n        )\n\n    def test_static_range_if_none_match(self):\n        response = self.get_and_head(\n            \"/static/robots.txt\",\n            headers={\n                \"Range\": \"bytes=1-4\",\n                \"If-None-Match\": b'\"' + self.robots_txt_hash + b'\"',\n            },\n        )\n        self.assertEqual(response.code, 304)\n        self.assertEqual(response.body, b\"\")\n        self.assertTrue(\"Content-Length\" not in response.headers)\n        self.assertEqual(\n            utf8(response.headers[\"Etag\"]), b'\"' + self.robots_txt_hash + b'\"'\n        )\n\n    def test_static_404(self):\n        response = self.get_and_head(\"/static/blarg\")\n        self.assertEqual(response.code, 404)\n\n    def test_path_traversal_protection(self):\n        # curl_httpclient processes \"..\" on the client side, so we\n        # must test this with simple_httpclient.\n        self.http_client.close()\n        self.http_client = SimpleAsyncHTTPClient()\n        with ExpectLog(gen_log, \".*not in root static directory\"):\n            response = self.get_and_head(\"/static/../static_foo.txt\")\n        # Attempted path traversal should result in 403, not 200\n        # (which means the check failed and the file was served)\n        # or 404 (which means that the file didn't exist and\n        # is probably a packaging error).\n        self.assertEqual(response.code, 403)\n\n    @unittest.skipIf(os.name != \"posix\", \"non-posix OS\")\n    def test_root_static_path(self):\n        # Sometimes people set the StaticFileHandler's path to '/'\n        # to disable Tornado's path validation (in conjunction with\n        # their own validation in get_absolute_path). Make sure\n        # that the stricter validation in 4.2.1 doesn't break them.\n        path = os.path.join(\n            os.path.dirname(os.path.abspath(__file__)), \"static/robots.txt\"\n        )\n        response = self.get_and_head(\"/root_static\" + urllib.parse.quote(path))\n        self.assertEqual(response.code, 200)\n\n\nclass StaticDefaultFilenameTest(WebTestCase):\n    def get_app_kwargs(self):\n        return dict(\n            static_path=relpath(\"static\"),\n            static_handler_args=dict(default_filename=\"index.html\"),\n        )\n\n    def get_handlers(self):\n        return []\n\n    def test_static_default_filename(self):\n        response = self.fetch(\"/static/dir/\", follow_redirects=False)\n        self.assertEqual(response.code, 200)\n        self.assertEqual(b\"this is the index\\n\", response.body)\n\n    def test_static_default_redirect(self):\n        response = self.fetch(\"/static/dir\", follow_redirects=False)\n        self.assertEqual(response.code, 301)\n        self.assertTrue(response.headers[\"Location\"].endswith(\"/static/dir/\"))\n\n\nclass StaticFileWithPathTest(WebTestCase):\n    def get_app_kwargs(self):\n        return dict(\n            static_path=relpath(\"static\"),\n            static_handler_args=dict(default_filename=\"index.html\"),\n        )\n\n    def get_handlers(self):\n        return [(\"/foo/(.*)\", StaticFileHandler, {\"path\": relpath(\"templates/\")})]\n\n    def test_serve(self):\n        response = self.fetch(\"/foo/utf8.html\")\n        self.assertEqual(response.body, b\"H\\xc3\\xa9llo\\n\")\n\n\nclass CustomStaticFileTest(WebTestCase):\n    def get_handlers(self):\n        class MyStaticFileHandler(StaticFileHandler):\n            @classmethod\n            def make_static_url(cls, settings, path):\n                version_hash = cls.get_version(settings, path)\n                extension_index = path.rindex(\".\")\n                before_version = path[:extension_index]\n                after_version = path[(extension_index + 1) :]\n                return \"/static/%s.%s.%s\" % (\n                    before_version,\n                    version_hash,\n                    after_version,\n                )\n\n            def parse_url_path(self, url_path):\n                extension_index = url_path.rindex(\".\")\n                version_index = url_path.rindex(\".\", 0, extension_index)\n                return \"%s%s\" % (url_path[:version_index], url_path[extension_index:])\n\n            @classmethod\n            def get_absolute_path(cls, settings, path):\n                return \"CustomStaticFileTest:\" + path\n\n            def validate_absolute_path(self, root, absolute_path):\n                return absolute_path\n\n            @classmethod\n            def get_content(self, path, start=None, end=None):\n                assert start is None and end is None\n                if path == \"CustomStaticFileTest:foo.txt\":\n                    return b\"bar\"\n                raise Exception(\"unexpected path %r\" % path)\n\n            def get_content_size(self):\n                if self.absolute_path == \"CustomStaticFileTest:foo.txt\":\n                    return 3\n                raise Exception(\"unexpected path %r\" % self.absolute_path)\n\n            def get_modified_time(self):\n                return None\n\n            @classmethod\n            def get_version(cls, settings, path):\n                return \"42\"\n\n        class StaticUrlHandler(RequestHandler):\n            def get(self, path):\n                self.write(self.static_url(path))\n\n        self.static_handler_class = MyStaticFileHandler\n\n        return [(\"/static_url/(.*)\", StaticUrlHandler)]\n\n    def get_app_kwargs(self):\n        return dict(static_path=\"dummy\", static_handler_class=self.static_handler_class)\n\n    def test_serve(self):\n        response = self.fetch(\"/static/foo.42.txt\")\n        self.assertEqual(response.body, b\"bar\")\n\n    def test_static_url(self):\n        with ExpectLog(gen_log, \"Could not open static file\", required=False):\n            response = self.fetch(\"/static_url/foo.txt\")\n            self.assertEqual(response.body, b\"/static/foo.42.txt\")\n\n\nclass HostMatchingTest(WebTestCase):\n    class Handler(RequestHandler):\n        def initialize(self, reply):\n            self.reply = reply\n\n        def get(self):\n            self.write(self.reply)\n\n    def get_handlers(self):\n        return [(\"/foo\", HostMatchingTest.Handler, {\"reply\": \"wildcard\"})]\n\n    def test_host_matching(self):\n        self.app.add_handlers(\n            \"www.example.com\", [(\"/foo\", HostMatchingTest.Handler, {\"reply\": \"[0]\"})]\n        )\n        self.app.add_handlers(\n            r\"www\\.example\\.com\", [(\"/bar\", HostMatchingTest.Handler, {\"reply\": \"[1]\"})]\n        )\n        self.app.add_handlers(\n            \"www.example.com\", [(\"/baz\", HostMatchingTest.Handler, {\"reply\": \"[2]\"})]\n        )\n        self.app.add_handlers(\n            \"www.e.*e.com\", [(\"/baz\", HostMatchingTest.Handler, {\"reply\": \"[3]\"})]\n        )\n\n        response = self.fetch(\"/foo\")\n        self.assertEqual(response.body, b\"wildcard\")\n        response = self.fetch(\"/bar\")\n        self.assertEqual(response.code, 404)\n        response = self.fetch(\"/baz\")\n        self.assertEqual(response.code, 404)\n\n        response = self.fetch(\"/foo\", headers={\"Host\": \"www.example.com\"})\n        self.assertEqual(response.body, b\"[0]\")\n        response = self.fetch(\"/bar\", headers={\"Host\": \"www.example.com\"})\n        self.assertEqual(response.body, b\"[1]\")\n        response = self.fetch(\"/baz\", headers={\"Host\": \"www.example.com\"})\n        self.assertEqual(response.body, b\"[2]\")\n        response = self.fetch(\"/baz\", headers={\"Host\": \"www.exe.com\"})\n        self.assertEqual(response.body, b\"[3]\")\n\n\nclass DefaultHostMatchingTest(WebTestCase):\n    def get_handlers(self):\n        return []\n\n    def get_app_kwargs(self):\n        return {\"default_host\": \"www.example.com\"}\n\n    def test_default_host_matching(self):\n        self.app.add_handlers(\n            \"www.example.com\", [(\"/foo\", HostMatchingTest.Handler, {\"reply\": \"[0]\"})]\n        )\n        self.app.add_handlers(\n            r\"www\\.example\\.com\", [(\"/bar\", HostMatchingTest.Handler, {\"reply\": \"[1]\"})]\n        )\n        self.app.add_handlers(\n            \"www.test.com\", [(\"/baz\", HostMatchingTest.Handler, {\"reply\": \"[2]\"})]\n        )\n\n        response = self.fetch(\"/foo\")\n        self.assertEqual(response.body, b\"[0]\")\n        response = self.fetch(\"/bar\")\n        self.assertEqual(response.body, b\"[1]\")\n        response = self.fetch(\"/baz\")\n        self.assertEqual(response.code, 404)\n\n        response = self.fetch(\"/foo\", headers={\"X-Real-Ip\": \"127.0.0.1\"})\n        self.assertEqual(response.code, 404)\n\n        self.app.default_host = \"www.test.com\"\n\n        response = self.fetch(\"/baz\")\n        self.assertEqual(response.body, b\"[2]\")\n\n\nclass NamedURLSpecGroupsTest(WebTestCase):\n    def get_handlers(self):\n        class EchoHandler(RequestHandler):\n            def get(self, path):\n                self.write(path)\n\n        return [\n            (\"/str/(?P<path>.*)\", EchoHandler),\n            (u\"/unicode/(?P<path>.*)\", EchoHandler),\n        ]\n\n    def test_named_urlspec_groups(self):\n        response = self.fetch(\"/str/foo\")\n        self.assertEqual(response.body, b\"foo\")\n\n        response = self.fetch(\"/unicode/bar\")\n        self.assertEqual(response.body, b\"bar\")\n\n\nclass ClearHeaderTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            self.set_header(\"h1\", \"foo\")\n            self.set_header(\"h2\", \"bar\")\n            self.clear_header(\"h1\")\n            self.clear_header(\"nonexistent\")\n\n    def test_clear_header(self):\n        response = self.fetch(\"/\")\n        self.assertTrue(\"h1\" not in response.headers)\n        self.assertEqual(response.headers[\"h2\"], \"bar\")\n\n\nclass Header204Test(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            self.set_status(204)\n            self.finish()\n\n    def test_204_headers(self):\n        response = self.fetch(\"/\")\n        self.assertEqual(response.code, 204)\n        self.assertNotIn(\"Content-Length\", response.headers)\n        self.assertNotIn(\"Transfer-Encoding\", response.headers)\n\n\nclass Header304Test(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            self.set_header(\"Content-Language\", \"en_US\")\n            self.write(\"hello\")\n\n    def test_304_headers(self):\n        response1 = self.fetch(\"/\")\n        self.assertEqual(response1.headers[\"Content-Length\"], \"5\")\n        self.assertEqual(response1.headers[\"Content-Language\"], \"en_US\")\n\n        response2 = self.fetch(\n            \"/\", headers={\"If-None-Match\": response1.headers[\"Etag\"]}\n        )\n        self.assertEqual(response2.code, 304)\n        self.assertTrue(\"Content-Length\" not in response2.headers)\n        self.assertTrue(\"Content-Language\" not in response2.headers)\n        # Not an entity header, but should not be added to 304s by chunking\n        self.assertTrue(\"Transfer-Encoding\" not in response2.headers)\n\n\nclass StatusReasonTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            reason = self.request.arguments.get(\"reason\", [])\n            self.set_status(\n                int(self.get_argument(\"code\")), reason=reason[0] if reason else None\n            )\n\n    def get_http_client(self):\n        # simple_httpclient only: curl doesn't expose the reason string\n        return SimpleAsyncHTTPClient()\n\n    def test_status(self):\n        response = self.fetch(\"/?code=304\")\n        self.assertEqual(response.code, 304)\n        self.assertEqual(response.reason, \"Not Modified\")\n        response = self.fetch(\"/?code=304&reason=Foo\")\n        self.assertEqual(response.code, 304)\n        self.assertEqual(response.reason, \"Foo\")\n        response = self.fetch(\"/?code=682&reason=Bar\")\n        self.assertEqual(response.code, 682)\n        self.assertEqual(response.reason, \"Bar\")\n        response = self.fetch(\"/?code=682\")\n        self.assertEqual(response.code, 682)\n        self.assertEqual(response.reason, \"Unknown\")\n\n\nclass DateHeaderTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            self.write(\"hello\")\n\n    def test_date_header(self):\n        response = self.fetch(\"/\")\n        parsed = email.utils.parsedate(response.headers[\"Date\"])\n        assert parsed is not None\n        header_date = datetime.datetime(*parsed[:6])\n        self.assertTrue(\n            header_date - datetime.datetime.utcnow() < datetime.timedelta(seconds=2)\n        )\n\n\nclass RaiseWithReasonTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            raise HTTPError(682, reason=\"Foo\")\n\n    def get_http_client(self):\n        # simple_httpclient only: curl doesn't expose the reason string\n        return SimpleAsyncHTTPClient()\n\n    def test_raise_with_reason(self):\n        response = self.fetch(\"/\")\n        self.assertEqual(response.code, 682)\n        self.assertEqual(response.reason, \"Foo\")\n        self.assertIn(b\"682: Foo\", response.body)\n\n    def test_httperror_str(self):\n        self.assertEqual(str(HTTPError(682, reason=\"Foo\")), \"HTTP 682: Foo\")\n\n    def test_httperror_str_from_httputil(self):\n        self.assertEqual(str(HTTPError(682)), \"HTTP 682: Unknown\")\n\n\nclass ErrorHandlerXSRFTest(WebTestCase):\n    def get_handlers(self):\n        # note that if the handlers list is empty we get the default_host\n        # redirect fallback instead of a 404, so test with both an\n        # explicitly defined error handler and an implicit 404.\n        return [(\"/error\", ErrorHandler, dict(status_code=417))]\n\n    def get_app_kwargs(self):\n        return dict(xsrf_cookies=True)\n\n    def test_error_xsrf(self):\n        response = self.fetch(\"/error\", method=\"POST\", body=\"\")\n        self.assertEqual(response.code, 417)\n\n    def test_404_xsrf(self):\n        response = self.fetch(\"/404\", method=\"POST\", body=\"\")\n        self.assertEqual(response.code, 404)\n\n\nclass GzipTestCase(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            for v in self.get_arguments(\"vary\"):\n                self.add_header(\"Vary\", v)\n            # Must write at least MIN_LENGTH bytes to activate compression.\n            self.write(\"hello world\" + (\"!\" * GZipContentEncoding.MIN_LENGTH))\n\n    def get_app_kwargs(self):\n        return dict(\n            gzip=True, static_path=os.path.join(os.path.dirname(__file__), \"static\")\n        )\n\n    def assert_compressed(self, response):\n        # simple_httpclient renames the content-encoding header;\n        # curl_httpclient doesn't.\n        self.assertEqual(\n            response.headers.get(\n                \"Content-Encoding\", response.headers.get(\"X-Consumed-Content-Encoding\")\n            ),\n            \"gzip\",\n        )\n\n    def test_gzip(self):\n        response = self.fetch(\"/\")\n        self.assert_compressed(response)\n        self.assertEqual(response.headers[\"Vary\"], \"Accept-Encoding\")\n\n    def test_gzip_static(self):\n        # The streaming responses in StaticFileHandler have subtle\n        # interactions with the gzip output so test this case separately.\n        response = self.fetch(\"/robots.txt\")\n        self.assert_compressed(response)\n        self.assertEqual(response.headers[\"Vary\"], \"Accept-Encoding\")\n\n    def test_gzip_not_requested(self):\n        response = self.fetch(\"/\", use_gzip=False)\n        self.assertNotIn(\"Content-Encoding\", response.headers)\n        self.assertEqual(response.headers[\"Vary\"], \"Accept-Encoding\")\n\n    def test_vary_already_present(self):\n        response = self.fetch(\"/?vary=Accept-Language\")\n        self.assert_compressed(response)\n        self.assertEqual(\n            [s.strip() for s in response.headers[\"Vary\"].split(\",\")],\n            [\"Accept-Language\", \"Accept-Encoding\"],\n        )\n\n    def test_vary_already_present_multiple(self):\n        # Regression test for https://github.com/tornadoweb/tornado/issues/1670\n        response = self.fetch(\"/?vary=Accept-Language&vary=Cookie\")\n        self.assert_compressed(response)\n        self.assertEqual(\n            [s.strip() for s in response.headers[\"Vary\"].split(\",\")],\n            [\"Accept-Language\", \"Cookie\", \"Accept-Encoding\"],\n        )\n\n\nclass PathArgsInPrepareTest(WebTestCase):\n    class Handler(RequestHandler):\n        def prepare(self):\n            self.write(dict(args=self.path_args, kwargs=self.path_kwargs))\n\n        def get(self, path):\n            assert path == \"foo\"\n            self.finish()\n\n    def get_handlers(self):\n        return [(\"/pos/(.*)\", self.Handler), (\"/kw/(?P<path>.*)\", self.Handler)]\n\n    def test_pos(self):\n        response = self.fetch(\"/pos/foo\")\n        response.rethrow()\n        data = json_decode(response.body)\n        self.assertEqual(data, {\"args\": [\"foo\"], \"kwargs\": {}})\n\n    def test_kw(self):\n        response = self.fetch(\"/kw/foo\")\n        response.rethrow()\n        data = json_decode(response.body)\n        self.assertEqual(data, {\"args\": [], \"kwargs\": {\"path\": \"foo\"}})\n\n\nclass ClearAllCookiesTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            self.clear_all_cookies()\n            self.write(\"ok\")\n\n    def test_clear_all_cookies(self):\n        response = self.fetch(\"/\", headers={\"Cookie\": \"foo=bar; baz=xyzzy\"})\n        set_cookies = sorted(response.headers.get_list(\"Set-Cookie\"))\n        # Python 3.5 sends 'baz=\"\";'; older versions use 'baz=;'\n        self.assertTrue(\n            set_cookies[0].startswith(\"baz=;\") or set_cookies[0].startswith('baz=\"\";')\n        )\n        self.assertTrue(\n            set_cookies[1].startswith(\"foo=;\") or set_cookies[1].startswith('foo=\"\";')\n        )\n\n\nclass PermissionError(Exception):\n    pass\n\n\nclass ExceptionHandlerTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            exc = self.get_argument(\"exc\")\n            if exc == \"http\":\n                raise HTTPError(410, \"no longer here\")\n            elif exc == \"zero\":\n                1 / 0\n            elif exc == \"permission\":\n                raise PermissionError(\"not allowed\")\n\n        def write_error(self, status_code, **kwargs):\n            if \"exc_info\" in kwargs:\n                typ, value, tb = kwargs[\"exc_info\"]\n                if isinstance(value, PermissionError):\n                    self.set_status(403)\n                    self.write(\"PermissionError\")\n                    return\n            RequestHandler.write_error(self, status_code, **kwargs)\n\n        def log_exception(self, typ, value, tb):\n            if isinstance(value, PermissionError):\n                app_log.warning(\"custom logging for PermissionError: %s\", value.args[0])\n            else:\n                RequestHandler.log_exception(self, typ, value, tb)\n\n    def test_http_error(self):\n        # HTTPErrors are logged as warnings with no stack trace.\n        # TODO: extend ExpectLog to test this more precisely\n        with ExpectLog(gen_log, \".*no longer here\"):\n            response = self.fetch(\"/?exc=http\")\n            self.assertEqual(response.code, 410)\n\n    def test_unknown_error(self):\n        # Unknown errors are logged as errors with a stack trace.\n        with ExpectLog(app_log, \"Uncaught exception\"):\n            response = self.fetch(\"/?exc=zero\")\n            self.assertEqual(response.code, 500)\n\n    def test_known_error(self):\n        # log_exception can override logging behavior, and write_error\n        # can override the response.\n        with ExpectLog(app_log, \"custom logging for PermissionError: not allowed\"):\n            response = self.fetch(\"/?exc=permission\")\n            self.assertEqual(response.code, 403)\n\n\nclass BuggyLoggingTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            1 / 0\n\n        def log_exception(self, typ, value, tb):\n            1 / 0\n\n    def test_buggy_log_exception(self):\n        # Something gets logged even though the application's\n        # logger is broken.\n        with ExpectLog(app_log, \".*\"):\n            self.fetch(\"/\")\n\n\nclass UIMethodUIModuleTest(SimpleHandlerTestCase):\n    \"\"\"Test that UI methods and modules are created correctly and\n    associated with the handler.\n    \"\"\"\n\n    class Handler(RequestHandler):\n        def get(self):\n            self.render(\"foo.html\")\n\n        def value(self):\n            return self.get_argument(\"value\")\n\n    def get_app_kwargs(self):\n        def my_ui_method(handler, x):\n            return \"In my_ui_method(%s) with handler value %s.\" % (x, handler.value())\n\n        class MyModule(UIModule):\n            def render(self, x):\n                return \"In MyModule(%s) with handler value %s.\" % (\n                    x,\n                    self.handler.value(),\n                )\n\n        loader = DictLoader(\n            {\"foo.html\": \"{{ my_ui_method(42) }} {% module MyModule(123) %}\"}\n        )\n        return dict(\n            template_loader=loader,\n            ui_methods={\"my_ui_method\": my_ui_method},\n            ui_modules={\"MyModule\": MyModule},\n        )\n\n    def tearDown(self):\n        super(UIMethodUIModuleTest, self).tearDown()\n        # TODO: fix template loader caching so this isn't necessary.\n        RequestHandler._template_loaders.clear()\n\n    def test_ui_method(self):\n        response = self.fetch(\"/?value=asdf\")\n        self.assertEqual(\n            response.body,\n            b\"In my_ui_method(42) with handler value asdf. \"\n            b\"In MyModule(123) with handler value asdf.\",\n        )\n\n\nclass GetArgumentErrorTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            try:\n                self.get_argument(\"foo\")\n                self.write({})\n            except MissingArgumentError as e:\n                self.write({\"arg_name\": e.arg_name, \"log_message\": e.log_message})\n\n    def test_catch_error(self):\n        response = self.fetch(\"/\")\n        self.assertEqual(\n            json_decode(response.body),\n            {\"arg_name\": \"foo\", \"log_message\": \"Missing argument foo\"},\n        )\n\n\nclass SetLazyPropertiesTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def prepare(self):\n            self.current_user = \"Ben\"\n            self.locale = locale.get(\"en_US\")\n\n        def get_user_locale(self):\n            raise NotImplementedError()\n\n        def get_current_user(self):\n            raise NotImplementedError()\n\n        def get(self):\n            self.write(\"Hello %s (%s)\" % (self.current_user, self.locale.code))\n\n    def test_set_properties(self):\n        # Ensure that current_user can be assigned to normally for apps\n        # that want to forgo the lazy get_current_user property\n        response = self.fetch(\"/\")\n        self.assertEqual(response.body, b\"Hello Ben (en_US)\")\n\n\nclass GetCurrentUserTest(WebTestCase):\n    def get_app_kwargs(self):\n        class WithoutUserModule(UIModule):\n            def render(self):\n                return \"\"\n\n        class WithUserModule(UIModule):\n            def render(self):\n                return str(self.current_user)\n\n        loader = DictLoader(\n            {\n                \"without_user.html\": \"\",\n                \"with_user.html\": \"{{ current_user }}\",\n                \"without_user_module.html\": \"{% module WithoutUserModule() %}\",\n                \"with_user_module.html\": \"{% module WithUserModule() %}\",\n            }\n        )\n        return dict(\n            template_loader=loader,\n            ui_modules={\n                \"WithUserModule\": WithUserModule,\n                \"WithoutUserModule\": WithoutUserModule,\n            },\n        )\n\n    def tearDown(self):\n        super(GetCurrentUserTest, self).tearDown()\n        RequestHandler._template_loaders.clear()\n\n    def get_handlers(self):\n        class CurrentUserHandler(RequestHandler):\n            def prepare(self):\n                self.has_loaded_current_user = False\n\n            def get_current_user(self):\n                self.has_loaded_current_user = True\n                return \"\"\n\n        class WithoutUserHandler(CurrentUserHandler):\n            def get(self):\n                self.render_string(\"without_user.html\")\n                self.finish(str(self.has_loaded_current_user))\n\n        class WithUserHandler(CurrentUserHandler):\n            def get(self):\n                self.render_string(\"with_user.html\")\n                self.finish(str(self.has_loaded_current_user))\n\n        class CurrentUserModuleHandler(CurrentUserHandler):\n            def get_template_namespace(self):\n                # If RequestHandler.get_template_namespace is called, then\n                # get_current_user is evaluated. Until #820 is fixed, this\n                # is a small hack to circumvent the issue.\n                return self.ui\n\n        class WithoutUserModuleHandler(CurrentUserModuleHandler):\n            def get(self):\n                self.render_string(\"without_user_module.html\")\n                self.finish(str(self.has_loaded_current_user))\n\n        class WithUserModuleHandler(CurrentUserModuleHandler):\n            def get(self):\n                self.render_string(\"with_user_module.html\")\n                self.finish(str(self.has_loaded_current_user))\n\n        return [\n            (\"/without_user\", WithoutUserHandler),\n            (\"/with_user\", WithUserHandler),\n            (\"/without_user_module\", WithoutUserModuleHandler),\n            (\"/with_user_module\", WithUserModuleHandler),\n        ]\n\n    @unittest.skip(\"needs fix\")\n    def test_get_current_user_is_lazy(self):\n        # TODO: Make this test pass. See #820.\n        response = self.fetch(\"/without_user\")\n        self.assertEqual(response.body, b\"False\")\n\n    def test_get_current_user_works(self):\n        response = self.fetch(\"/with_user\")\n        self.assertEqual(response.body, b\"True\")\n\n    def test_get_current_user_from_ui_module_is_lazy(self):\n        response = self.fetch(\"/without_user_module\")\n        self.assertEqual(response.body, b\"False\")\n\n    def test_get_current_user_from_ui_module_works(self):\n        response = self.fetch(\"/with_user_module\")\n        self.assertEqual(response.body, b\"True\")\n\n\nclass UnimplementedHTTPMethodsTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        pass\n\n    def test_unimplemented_standard_methods(self):\n        for method in [\"HEAD\", \"GET\", \"DELETE\", \"OPTIONS\"]:\n            response = self.fetch(\"/\", method=method)\n            self.assertEqual(response.code, 405)\n        for method in [\"POST\", \"PUT\"]:\n            response = self.fetch(\"/\", method=method, body=b\"\")\n            self.assertEqual(response.code, 405)\n\n\nclass UnimplementedNonStandardMethodsTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def other(self):\n            # Even though this method exists, it won't get called automatically\n            # because it is not in SUPPORTED_METHODS.\n            self.write(\"other\")\n\n    def test_unimplemented_patch(self):\n        # PATCH is recently standardized; Tornado supports it by default\n        # but wsgiref.validate doesn't like it.\n        response = self.fetch(\"/\", method=\"PATCH\", body=b\"\")\n        self.assertEqual(response.code, 405)\n\n    def test_unimplemented_other(self):\n        response = self.fetch(\"/\", method=\"OTHER\", allow_nonstandard_methods=True)\n        self.assertEqual(response.code, 405)\n\n\nclass AllHTTPMethodsTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def method(self):\n            self.write(self.request.method)\n\n        get = delete = options = post = put = method  # type: ignore\n\n    def test_standard_methods(self):\n        response = self.fetch(\"/\", method=\"HEAD\")\n        self.assertEqual(response.body, b\"\")\n        for method in [\"GET\", \"DELETE\", \"OPTIONS\"]:\n            response = self.fetch(\"/\", method=method)\n            self.assertEqual(response.body, utf8(method))\n        for method in [\"POST\", \"PUT\"]:\n            response = self.fetch(\"/\", method=method, body=b\"\")\n            self.assertEqual(response.body, utf8(method))\n\n\nclass PatchMethodTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        SUPPORTED_METHODS = RequestHandler.SUPPORTED_METHODS + (  # type: ignore\n            \"OTHER\",\n        )\n\n        def patch(self):\n            self.write(\"patch\")\n\n        def other(self):\n            self.write(\"other\")\n\n    def test_patch(self):\n        response = self.fetch(\"/\", method=\"PATCH\", body=b\"\")\n        self.assertEqual(response.body, b\"patch\")\n\n    def test_other(self):\n        response = self.fetch(\"/\", method=\"OTHER\", allow_nonstandard_methods=True)\n        self.assertEqual(response.body, b\"other\")\n\n\nclass FinishInPrepareTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def prepare(self):\n            self.finish(\"done\")\n\n        def get(self):\n            # It's difficult to assert for certain that a method did not\n            # or will not be called in an asynchronous context, but this\n            # will be logged noisily if it is reached.\n            raise Exception(\"should not reach this method\")\n\n    def test_finish_in_prepare(self):\n        response = self.fetch(\"/\")\n        self.assertEqual(response.body, b\"done\")\n\n\nclass Default404Test(WebTestCase):\n    def get_handlers(self):\n        # If there are no handlers at all a default redirect handler gets added.\n        return [(\"/foo\", RequestHandler)]\n\n    def test_404(self):\n        response = self.fetch(\"/\")\n        self.assertEqual(response.code, 404)\n        self.assertEqual(\n            response.body,\n            b\"<html><title>404: Not Found</title>\"\n            b\"<body>404: Not Found</body></html>\",\n        )\n\n\nclass Custom404Test(WebTestCase):\n    def get_handlers(self):\n        return [(\"/foo\", RequestHandler)]\n\n    def get_app_kwargs(self):\n        class Custom404Handler(RequestHandler):\n            def get(self):\n                self.set_status(404)\n                self.write(\"custom 404 response\")\n\n        return dict(default_handler_class=Custom404Handler)\n\n    def test_404(self):\n        response = self.fetch(\"/\")\n        self.assertEqual(response.code, 404)\n        self.assertEqual(response.body, b\"custom 404 response\")\n\n\nclass DefaultHandlerArgumentsTest(WebTestCase):\n    def get_handlers(self):\n        return [(\"/foo\", RequestHandler)]\n\n    def get_app_kwargs(self):\n        return dict(\n            default_handler_class=ErrorHandler,\n            default_handler_args=dict(status_code=403),\n        )\n\n    def test_403(self):\n        response = self.fetch(\"/\")\n        self.assertEqual(response.code, 403)\n\n\nclass HandlerByNameTest(WebTestCase):\n    def get_handlers(self):\n        # All three are equivalent.\n        return [\n            (\"/hello1\", HelloHandler),\n            (\"/hello2\", \"tornado.test.web_test.HelloHandler\"),\n            url(\"/hello3\", \"tornado.test.web_test.HelloHandler\"),\n        ]\n\n    def test_handler_by_name(self):\n        resp = self.fetch(\"/hello1\")\n        self.assertEqual(resp.body, b\"hello\")\n        resp = self.fetch(\"/hello2\")\n        self.assertEqual(resp.body, b\"hello\")\n        resp = self.fetch(\"/hello3\")\n        self.assertEqual(resp.body, b\"hello\")\n\n\nclass StreamingRequestBodyTest(WebTestCase):\n    def get_handlers(self):\n        @stream_request_body\n        class StreamingBodyHandler(RequestHandler):\n            def initialize(self, test):\n                self.test = test\n\n            def prepare(self):\n                self.test.prepared.set_result(None)\n\n            def data_received(self, data):\n                self.test.data.set_result(data)\n\n            def get(self):\n                self.test.finished.set_result(None)\n                self.write({})\n\n        @stream_request_body\n        class EarlyReturnHandler(RequestHandler):\n            def prepare(self):\n                # If we finish the response in prepare, it won't continue to\n                # the (non-existent) data_received.\n                raise HTTPError(401)\n\n        @stream_request_body\n        class CloseDetectionHandler(RequestHandler):\n            def initialize(self, test):\n                self.test = test\n\n            def on_connection_close(self):\n                super(CloseDetectionHandler, self).on_connection_close()\n                self.test.close_future.set_result(None)\n\n        return [\n            (\"/stream_body\", StreamingBodyHandler, dict(test=self)),\n            (\"/early_return\", EarlyReturnHandler),\n            (\"/close_detection\", CloseDetectionHandler, dict(test=self)),\n        ]\n\n    def connect(self, url, connection_close):\n        # Use a raw connection so we can control the sending of data.\n        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)\n        s.connect((\"127.0.0.1\", self.get_http_port()))\n        stream = IOStream(s)\n        stream.write(b\"GET \" + url + b\" HTTP/1.1\\r\\n\")\n        if connection_close:\n            stream.write(b\"Connection: close\\r\\n\")\n        stream.write(b\"Transfer-Encoding: chunked\\r\\n\\r\\n\")\n        return stream\n\n    @gen_test\n    def test_streaming_body(self):\n        self.prepared = Future()  # type: Future[None]\n        self.data = Future()  # type: Future[bytes]\n        self.finished = Future()  # type: Future[None]\n\n        stream = self.connect(b\"/stream_body\", connection_close=True)\n        yield self.prepared\n        stream.write(b\"4\\r\\nasdf\\r\\n\")\n        # Ensure the first chunk is received before we send the second.\n        data = yield self.data\n        self.assertEqual(data, b\"asdf\")\n        self.data = Future()\n        stream.write(b\"4\\r\\nqwer\\r\\n\")\n        data = yield self.data\n        self.assertEquals(data, b\"qwer\")\n        stream.write(b\"0\\r\\n\\r\\n\")\n        yield self.finished\n        data = yield stream.read_until_close()\n        # This would ideally use an HTTP1Connection to read the response.\n        self.assertTrue(data.endswith(b\"{}\"))\n        stream.close()\n\n    @gen_test\n    def test_early_return(self):\n        stream = self.connect(b\"/early_return\", connection_close=False)\n        data = yield stream.read_until_close()\n        self.assertTrue(data.startswith(b\"HTTP/1.1 401\"))\n\n    @gen_test\n    def test_early_return_with_data(self):\n        stream = self.connect(b\"/early_return\", connection_close=False)\n        stream.write(b\"4\\r\\nasdf\\r\\n\")\n        data = yield stream.read_until_close()\n        self.assertTrue(data.startswith(b\"HTTP/1.1 401\"))\n\n    @gen_test\n    def test_close_during_upload(self):\n        self.close_future = Future()  # type: Future[None]\n        stream = self.connect(b\"/close_detection\", connection_close=False)\n        stream.close()\n        yield self.close_future\n\n\n# Each method in this handler returns a yieldable object and yields to the\n# IOLoop so the future is not immediately ready.  Ensure that the\n# yieldables are respected and no method is called before the previous\n# one has completed.\n@stream_request_body\nclass BaseFlowControlHandler(RequestHandler):\n    def initialize(self, test):\n        self.test = test\n        self.method = None\n        self.methods = []  # type: typing.List[str]\n\n    @contextlib.contextmanager\n    def in_method(self, method):\n        if self.method is not None:\n            self.test.fail(\"entered method %s while in %s\" % (method, self.method))\n        self.method = method\n        self.methods.append(method)\n        try:\n            yield\n        finally:\n            self.method = None\n\n    @gen.coroutine\n    def prepare(self):\n        # Note that asynchronous prepare() does not block data_received,\n        # so we don't use in_method here.\n        self.methods.append(\"prepare\")\n        yield gen.moment\n\n    @gen.coroutine\n    def post(self):\n        with self.in_method(\"post\"):\n            yield gen.moment\n        self.write(dict(methods=self.methods))\n\n\nclass BaseStreamingRequestFlowControlTest(object):\n    def get_httpserver_options(self):\n        # Use a small chunk size so flow control is relevant even though\n        # all the data arrives at once.\n        return dict(chunk_size=10, decompress_request=True)\n\n    def get_http_client(self):\n        # simple_httpclient only: curl doesn't support body_producer.\n        return SimpleAsyncHTTPClient()\n\n    # Test all the slightly different code paths for fixed, chunked, etc bodies.\n    def test_flow_control_fixed_body(self):\n        response = self.fetch(\"/\", body=\"abcdefghijklmnopqrstuvwxyz\", method=\"POST\")\n        response.rethrow()\n        self.assertEqual(\n            json_decode(response.body),\n            dict(\n                methods=[\n                    \"prepare\",\n                    \"data_received\",\n                    \"data_received\",\n                    \"data_received\",\n                    \"post\",\n                ]\n            ),\n        )\n\n    def test_flow_control_chunked_body(self):\n        chunks = [b\"abcd\", b\"efgh\", b\"ijkl\"]\n\n        @gen.coroutine\n        def body_producer(write):\n            for i in chunks:\n                yield write(i)\n\n        response = self.fetch(\"/\", body_producer=body_producer, method=\"POST\")\n        response.rethrow()\n        self.assertEqual(\n            json_decode(response.body),\n            dict(\n                methods=[\n                    \"prepare\",\n                    \"data_received\",\n                    \"data_received\",\n                    \"data_received\",\n                    \"post\",\n                ]\n            ),\n        )\n\n    def test_flow_control_compressed_body(self):\n        bytesio = BytesIO()\n        gzip_file = gzip.GzipFile(mode=\"w\", fileobj=bytesio)\n        gzip_file.write(b\"abcdefghijklmnopqrstuvwxyz\")\n        gzip_file.close()\n        compressed_body = bytesio.getvalue()\n        response = self.fetch(\n            \"/\",\n            body=compressed_body,\n            method=\"POST\",\n            headers={\"Content-Encoding\": \"gzip\"},\n        )\n        response.rethrow()\n        self.assertEqual(\n            json_decode(response.body),\n            dict(\n                methods=[\n                    \"prepare\",\n                    \"data_received\",\n                    \"data_received\",\n                    \"data_received\",\n                    \"post\",\n                ]\n            ),\n        )\n\n\nclass DecoratedStreamingRequestFlowControlTest(\n    BaseStreamingRequestFlowControlTest, WebTestCase\n):\n    def get_handlers(self):\n        class DecoratedFlowControlHandler(BaseFlowControlHandler):\n            @gen.coroutine\n            def data_received(self, data):\n                with self.in_method(\"data_received\"):\n                    yield gen.moment\n\n        return [(\"/\", DecoratedFlowControlHandler, dict(test=self))]\n\n\nclass NativeStreamingRequestFlowControlTest(\n    BaseStreamingRequestFlowControlTest, WebTestCase\n):\n    def get_handlers(self):\n        class NativeFlowControlHandler(BaseFlowControlHandler):\n            async def data_received(self, data):\n                with self.in_method(\"data_received\"):\n                    import asyncio\n\n                    await asyncio.sleep(0)\n\n        return [(\"/\", NativeFlowControlHandler, dict(test=self))]\n\n\nclass IncorrectContentLengthTest(SimpleHandlerTestCase):\n    def get_handlers(self):\n        test = self\n        self.server_error = None\n\n        # Manually set a content-length that doesn't match the actual content.\n        class TooHigh(RequestHandler):\n            def get(self):\n                self.set_header(\"Content-Length\", \"42\")\n                try:\n                    self.finish(\"ok\")\n                except Exception as e:\n                    test.server_error = e\n                    raise\n\n        class TooLow(RequestHandler):\n            def get(self):\n                self.set_header(\"Content-Length\", \"2\")\n                try:\n                    self.finish(\"hello\")\n                except Exception as e:\n                    test.server_error = e\n                    raise\n\n        return [(\"/high\", TooHigh), (\"/low\", TooLow)]\n\n    def test_content_length_too_high(self):\n        # When the content-length is too high, the connection is simply\n        # closed without completing the response.  An error is logged on\n        # the server.\n        with ExpectLog(app_log, \"(Uncaught exception|Exception in callback)\"):\n            with ExpectLog(\n                gen_log,\n                \"(Cannot send error response after headers written\"\n                \"|Failed to flush partial response)\",\n            ):\n                with self.assertRaises(HTTPClientError):\n                    self.fetch(\"/high\", raise_error=True)\n        self.assertEqual(\n            str(self.server_error), \"Tried to write 40 bytes less than Content-Length\"\n        )\n\n    def test_content_length_too_low(self):\n        # When the content-length is too low, the connection is closed\n        # without writing the last chunk, so the client never sees the request\n        # complete (which would be a framing error).\n        with ExpectLog(app_log, \"(Uncaught exception|Exception in callback)\"):\n            with ExpectLog(\n                gen_log,\n                \"(Cannot send error response after headers written\"\n                \"|Failed to flush partial response)\",\n            ):\n                with self.assertRaises(HTTPClientError):\n                    self.fetch(\"/low\", raise_error=True)\n        self.assertEqual(\n            str(self.server_error), \"Tried to write more data than Content-Length\"\n        )\n\n\nclass ClientCloseTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            if self.request.version.startswith(\"HTTP/1\"):\n                # Simulate a connection closed by the client during\n                # request processing.  The client will see an error, but the\n                # server should respond gracefully (without logging errors\n                # because we were unable to write out as many bytes as\n                # Content-Length said we would)\n                self.request.connection.stream.close()\n                self.write(\"hello\")\n            else:\n                # TODO: add a HTTP2-compatible version of this test.\n                self.write(\"requires HTTP/1.x\")\n\n    def test_client_close(self):\n        with self.assertRaises((HTTPClientError, unittest.SkipTest)):\n            response = self.fetch(\"/\", raise_error=True)\n            if response.body == b\"requires HTTP/1.x\":\n                self.skipTest(\"requires HTTP/1.x\")\n            self.assertEqual(response.code, 599)\n\n\nclass SignedValueTest(unittest.TestCase):\n    SECRET = \"It's a secret to everybody\"\n    SECRET_DICT = {0: \"asdfbasdf\", 1: \"12312312\", 2: \"2342342\"}\n\n    def past(self):\n        return self.present() - 86400 * 32\n\n    def present(self):\n        return 1300000000\n\n    def test_known_values(self):\n        signed_v1 = create_signed_value(\n            SignedValueTest.SECRET, \"key\", \"value\", version=1, clock=self.present\n        )\n        self.assertEqual(\n            signed_v1, b\"dmFsdWU=|1300000000|31c934969f53e48164c50768b40cbd7e2daaaa4f\"\n        )\n\n        signed_v2 = create_signed_value(\n            SignedValueTest.SECRET, \"key\", \"value\", version=2, clock=self.present\n        )\n        self.assertEqual(\n            signed_v2,\n            b\"2|1:0|10:1300000000|3:key|8:dmFsdWU=|\"\n            b\"3d4e60b996ff9c5d5788e333a0cba6f238a22c6c0f94788870e1a9ecd482e152\",\n        )\n\n        signed_default = create_signed_value(\n            SignedValueTest.SECRET, \"key\", \"value\", clock=self.present\n        )\n        self.assertEqual(signed_default, signed_v2)\n\n        decoded_v1 = decode_signed_value(\n            SignedValueTest.SECRET, \"key\", signed_v1, min_version=1, clock=self.present\n        )\n        self.assertEqual(decoded_v1, b\"value\")\n\n        decoded_v2 = decode_signed_value(\n            SignedValueTest.SECRET, \"key\", signed_v2, min_version=2, clock=self.present\n        )\n        self.assertEqual(decoded_v2, b\"value\")\n\n    def test_name_swap(self):\n        signed1 = create_signed_value(\n            SignedValueTest.SECRET, \"key1\", \"value\", clock=self.present\n        )\n        signed2 = create_signed_value(\n            SignedValueTest.SECRET, \"key2\", \"value\", clock=self.present\n        )\n        # Try decoding each string with the other's \"name\"\n        decoded1 = decode_signed_value(\n            SignedValueTest.SECRET, \"key2\", signed1, clock=self.present\n        )\n        self.assertIs(decoded1, None)\n        decoded2 = decode_signed_value(\n            SignedValueTest.SECRET, \"key1\", signed2, clock=self.present\n        )\n        self.assertIs(decoded2, None)\n\n    def test_expired(self):\n        signed = create_signed_value(\n            SignedValueTest.SECRET, \"key1\", \"value\", clock=self.past\n        )\n        decoded_past = decode_signed_value(\n            SignedValueTest.SECRET, \"key1\", signed, clock=self.past\n        )\n        self.assertEqual(decoded_past, b\"value\")\n        decoded_present = decode_signed_value(\n            SignedValueTest.SECRET, \"key1\", signed, clock=self.present\n        )\n        self.assertIs(decoded_present, None)\n\n    def test_payload_tampering(self):\n        # These cookies are variants of the one in test_known_values.\n        sig = \"3d4e60b996ff9c5d5788e333a0cba6f238a22c6c0f94788870e1a9ecd482e152\"\n\n        def validate(prefix):\n            return b\"value\" == decode_signed_value(\n                SignedValueTest.SECRET, \"key\", prefix + sig, clock=self.present\n            )\n\n        self.assertTrue(validate(\"2|1:0|10:1300000000|3:key|8:dmFsdWU=|\"))\n        # Change key version\n        self.assertFalse(validate(\"2|1:1|10:1300000000|3:key|8:dmFsdWU=|\"))\n        # length mismatch (field too short)\n        self.assertFalse(validate(\"2|1:0|10:130000000|3:key|8:dmFsdWU=|\"))\n        # length mismatch (field too long)\n        self.assertFalse(validate(\"2|1:0|10:1300000000|3:keey|8:dmFsdWU=|\"))\n\n    def test_signature_tampering(self):\n        prefix = \"2|1:0|10:1300000000|3:key|8:dmFsdWU=|\"\n\n        def validate(sig):\n            return b\"value\" == decode_signed_value(\n                SignedValueTest.SECRET, \"key\", prefix + sig, clock=self.present\n            )\n\n        self.assertTrue(\n            validate(\"3d4e60b996ff9c5d5788e333a0cba6f238a22c6c0f94788870e1a9ecd482e152\")\n        )\n        # All zeros\n        self.assertFalse(validate(\"0\" * 32))\n        # Change one character\n        self.assertFalse(\n            validate(\"4d4e60b996ff9c5d5788e333a0cba6f238a22c6c0f94788870e1a9ecd482e152\")\n        )\n        # Change another character\n        self.assertFalse(\n            validate(\"3d4e60b996ff9c5d5788e333a0cba6f238a22c6c0f94788870e1a9ecd482e153\")\n        )\n        # Truncate\n        self.assertFalse(\n            validate(\"3d4e60b996ff9c5d5788e333a0cba6f238a22c6c0f94788870e1a9ecd482e15\")\n        )\n        # Lengthen\n        self.assertFalse(\n            validate(\n                \"3d4e60b996ff9c5d5788e333a0cba6f238a22c6c0f94788870e1a9ecd482e1538\"\n            )\n        )\n\n    def test_non_ascii(self):\n        value = b\"\\xe9\"\n        signed = create_signed_value(\n            SignedValueTest.SECRET, \"key\", value, clock=self.present\n        )\n        decoded = decode_signed_value(\n            SignedValueTest.SECRET, \"key\", signed, clock=self.present\n        )\n        self.assertEqual(value, decoded)\n\n    def test_key_versioning_read_write_default_key(self):\n        value = b\"\\xe9\"\n        signed = create_signed_value(\n            SignedValueTest.SECRET_DICT, \"key\", value, clock=self.present, key_version=0\n        )\n        decoded = decode_signed_value(\n            SignedValueTest.SECRET_DICT, \"key\", signed, clock=self.present\n        )\n        self.assertEqual(value, decoded)\n\n    def test_key_versioning_read_write_non_default_key(self):\n        value = b\"\\xe9\"\n        signed = create_signed_value(\n            SignedValueTest.SECRET_DICT, \"key\", value, clock=self.present, key_version=1\n        )\n        decoded = decode_signed_value(\n            SignedValueTest.SECRET_DICT, \"key\", signed, clock=self.present\n        )\n        self.assertEqual(value, decoded)\n\n    def test_key_versioning_invalid_key(self):\n        value = b\"\\xe9\"\n        signed = create_signed_value(\n            SignedValueTest.SECRET_DICT, \"key\", value, clock=self.present, key_version=0\n        )\n        newkeys = SignedValueTest.SECRET_DICT.copy()\n        newkeys.pop(0)\n        decoded = decode_signed_value(newkeys, \"key\", signed, clock=self.present)\n        self.assertEqual(None, decoded)\n\n    def test_key_version_retrieval(self):\n        value = b\"\\xe9\"\n        signed = create_signed_value(\n            SignedValueTest.SECRET_DICT, \"key\", value, clock=self.present, key_version=1\n        )\n        key_version = get_signature_key_version(signed)\n        self.assertEqual(1, key_version)\n\n\nclass XSRFTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            version = int(self.get_argument(\"version\", \"2\"))\n            # This would be a bad idea in a real app, but in this test\n            # it's fine.\n            self.settings[\"xsrf_cookie_version\"] = version\n            self.write(self.xsrf_token)\n\n        def post(self):\n            self.write(\"ok\")\n\n    def get_app_kwargs(self):\n        return dict(xsrf_cookies=True)\n\n    def setUp(self):\n        super(XSRFTest, self).setUp()\n        self.xsrf_token = self.get_token()\n\n    def get_token(self, old_token=None, version=None):\n        if old_token is not None:\n            headers = self.cookie_headers(old_token)\n        else:\n            headers = None\n        response = self.fetch(\n            \"/\" if version is None else (\"/?version=%d\" % version), headers=headers\n        )\n        response.rethrow()\n        return native_str(response.body)\n\n    def cookie_headers(self, token=None):\n        if token is None:\n            token = self.xsrf_token\n        return {\"Cookie\": \"_xsrf=\" + token}\n\n    def test_xsrf_fail_no_token(self):\n        with ExpectLog(gen_log, \".*'_xsrf' argument missing\"):\n            response = self.fetch(\"/\", method=\"POST\", body=b\"\")\n        self.assertEqual(response.code, 403)\n\n    def test_xsrf_fail_body_no_cookie(self):\n        with ExpectLog(gen_log, \".*XSRF cookie does not match POST\"):\n            response = self.fetch(\n                \"/\",\n                method=\"POST\",\n                body=urllib.parse.urlencode(dict(_xsrf=self.xsrf_token)),\n            )\n        self.assertEqual(response.code, 403)\n\n    def test_xsrf_fail_argument_invalid_format(self):\n        with ExpectLog(gen_log, \".*'_xsrf' argument has invalid format\"):\n            response = self.fetch(\n                \"/\",\n                method=\"POST\",\n                headers=self.cookie_headers(),\n                body=urllib.parse.urlencode(dict(_xsrf=\"3|\")),\n            )\n        self.assertEqual(response.code, 403)\n\n    def test_xsrf_fail_cookie_invalid_format(self):\n        with ExpectLog(gen_log, \".*XSRF cookie does not match POST\"):\n            response = self.fetch(\n                \"/\",\n                method=\"POST\",\n                headers=self.cookie_headers(token=\"3|\"),\n                body=urllib.parse.urlencode(dict(_xsrf=self.xsrf_token)),\n            )\n        self.assertEqual(response.code, 403)\n\n    def test_xsrf_fail_cookie_no_body(self):\n        with ExpectLog(gen_log, \".*'_xsrf' argument missing\"):\n            response = self.fetch(\n                \"/\", method=\"POST\", body=b\"\", headers=self.cookie_headers()\n            )\n        self.assertEqual(response.code, 403)\n\n    def test_xsrf_success_short_token(self):\n        response = self.fetch(\n            \"/\",\n            method=\"POST\",\n            body=urllib.parse.urlencode(dict(_xsrf=\"deadbeef\")),\n            headers=self.cookie_headers(token=\"deadbeef\"),\n        )\n        self.assertEqual(response.code, 200)\n\n    def test_xsrf_success_non_hex_token(self):\n        response = self.fetch(\n            \"/\",\n            method=\"POST\",\n            body=urllib.parse.urlencode(dict(_xsrf=\"xoxo\")),\n            headers=self.cookie_headers(token=\"xoxo\"),\n        )\n        self.assertEqual(response.code, 200)\n\n    def test_xsrf_success_post_body(self):\n        response = self.fetch(\n            \"/\",\n            method=\"POST\",\n            body=urllib.parse.urlencode(dict(_xsrf=self.xsrf_token)),\n            headers=self.cookie_headers(),\n        )\n        self.assertEqual(response.code, 200)\n\n    def test_xsrf_success_query_string(self):\n        response = self.fetch(\n            \"/?\" + urllib.parse.urlencode(dict(_xsrf=self.xsrf_token)),\n            method=\"POST\",\n            body=b\"\",\n            headers=self.cookie_headers(),\n        )\n        self.assertEqual(response.code, 200)\n\n    def test_xsrf_success_header(self):\n        response = self.fetch(\n            \"/\",\n            method=\"POST\",\n            body=b\"\",\n            headers=dict(\n                {\"X-Xsrftoken\": self.xsrf_token},  # type: ignore\n                **self.cookie_headers()\n            ),\n        )\n        self.assertEqual(response.code, 200)\n\n    def test_distinct_tokens(self):\n        # Every request gets a distinct token.\n        NUM_TOKENS = 10\n        tokens = set()\n        for i in range(NUM_TOKENS):\n            tokens.add(self.get_token())\n        self.assertEqual(len(tokens), NUM_TOKENS)\n\n    def test_cross_user(self):\n        token2 = self.get_token()\n        # Each token can be used to authenticate its own request.\n        for token in (self.xsrf_token, token2):\n            response = self.fetch(\n                \"/\",\n                method=\"POST\",\n                body=urllib.parse.urlencode(dict(_xsrf=token)),\n                headers=self.cookie_headers(token),\n            )\n            self.assertEqual(response.code, 200)\n        # Sending one in the cookie and the other in the body is not allowed.\n        for cookie_token, body_token in (\n            (self.xsrf_token, token2),\n            (token2, self.xsrf_token),\n        ):\n            with ExpectLog(gen_log, \".*XSRF cookie does not match POST\"):\n                response = self.fetch(\n                    \"/\",\n                    method=\"POST\",\n                    body=urllib.parse.urlencode(dict(_xsrf=body_token)),\n                    headers=self.cookie_headers(cookie_token),\n                )\n            self.assertEqual(response.code, 403)\n\n    def test_refresh_token(self):\n        token = self.xsrf_token\n        tokens_seen = set([token])\n        # A user's token is stable over time.  Refreshing the page in one tab\n        # might update the cookie while an older tab still has the old cookie\n        # in its DOM.  Simulate this scenario by passing a constant token\n        # in the body and re-querying for the token.\n        for i in range(5):\n            token = self.get_token(token)\n            # Tokens are encoded uniquely each time\n            tokens_seen.add(token)\n            response = self.fetch(\n                \"/\",\n                method=\"POST\",\n                body=urllib.parse.urlencode(dict(_xsrf=self.xsrf_token)),\n                headers=self.cookie_headers(token),\n            )\n            self.assertEqual(response.code, 200)\n        self.assertEqual(len(tokens_seen), 6)\n\n    def test_versioning(self):\n        # Version 1 still produces distinct tokens per request.\n        self.assertNotEqual(self.get_token(version=1), self.get_token(version=1))\n\n        # Refreshed v1 tokens are all identical.\n        v1_token = self.get_token(version=1)\n        for i in range(5):\n            self.assertEqual(self.get_token(v1_token, version=1), v1_token)\n\n        # Upgrade to a v2 version of the same token\n        v2_token = self.get_token(v1_token)\n        self.assertNotEqual(v1_token, v2_token)\n        # Each v1 token can map to many v2 tokens.\n        self.assertNotEqual(v2_token, self.get_token(v1_token))\n\n        # The tokens are cross-compatible.\n        for cookie_token, body_token in ((v1_token, v2_token), (v2_token, v1_token)):\n            response = self.fetch(\n                \"/\",\n                method=\"POST\",\n                body=urllib.parse.urlencode(dict(_xsrf=body_token)),\n                headers=self.cookie_headers(cookie_token),\n            )\n            self.assertEqual(response.code, 200)\n\n\nclass XSRFCookieKwargsTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            self.write(self.xsrf_token)\n\n    def get_app_kwargs(self):\n        return dict(\n            xsrf_cookies=True, xsrf_cookie_kwargs=dict(httponly=True, expires_days=2)\n        )\n\n    def test_xsrf_httponly(self):\n        response = self.fetch(\"/\")\n        self.assertIn(\"httponly;\", response.headers[\"Set-Cookie\"].lower())\n        self.assertIn(\"expires=\", response.headers[\"Set-Cookie\"].lower())\n        header = response.headers.get(\"Set-Cookie\")\n        match = re.match(\".*; expires=(?P<expires>.+);.*\", header)\n        assert match is not None\n\n        expires = datetime.datetime.utcnow() + datetime.timedelta(days=2)\n        parsed = email.utils.parsedate(match.groupdict()[\"expires\"])\n        assert parsed is not None\n        header_expires = datetime.datetime(*parsed[:6])\n        self.assertTrue(abs((expires - header_expires).total_seconds()) < 10)\n\n\nclass FinishExceptionTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            self.set_status(401)\n            self.set_header(\"WWW-Authenticate\", 'Basic realm=\"something\"')\n            if self.get_argument(\"finish_value\", \"\"):\n                raise Finish(\"authentication required\")\n            else:\n                self.write(\"authentication required\")\n                raise Finish()\n\n    def test_finish_exception(self):\n        for u in [\"/\", \"/?finish_value=1\"]:\n            response = self.fetch(u)\n            self.assertEqual(response.code, 401)\n            self.assertEqual(\n                'Basic realm=\"something\"', response.headers.get(\"WWW-Authenticate\")\n            )\n            self.assertEqual(b\"authentication required\", response.body)\n\n\nclass DecoratorTest(WebTestCase):\n    def get_handlers(self):\n        class RemoveSlashHandler(RequestHandler):\n            @removeslash\n            def get(self):\n                pass\n\n        class AddSlashHandler(RequestHandler):\n            @addslash\n            def get(self):\n                pass\n\n        return [(\"/removeslash/\", RemoveSlashHandler), (\"/addslash\", AddSlashHandler)]\n\n    def test_removeslash(self):\n        response = self.fetch(\"/removeslash/\", follow_redirects=False)\n        self.assertEqual(response.code, 301)\n        self.assertEqual(response.headers[\"Location\"], \"/removeslash\")\n\n        response = self.fetch(\"/removeslash/?foo=bar\", follow_redirects=False)\n        self.assertEqual(response.code, 301)\n        self.assertEqual(response.headers[\"Location\"], \"/removeslash?foo=bar\")\n\n    def test_addslash(self):\n        response = self.fetch(\"/addslash\", follow_redirects=False)\n        self.assertEqual(response.code, 301)\n        self.assertEqual(response.headers[\"Location\"], \"/addslash/\")\n\n        response = self.fetch(\"/addslash?foo=bar\", follow_redirects=False)\n        self.assertEqual(response.code, 301)\n        self.assertEqual(response.headers[\"Location\"], \"/addslash/?foo=bar\")\n\n\nclass CacheTest(WebTestCase):\n    def get_handlers(self):\n        class EtagHandler(RequestHandler):\n            def get(self, computed_etag):\n                self.write(computed_etag)\n\n            def compute_etag(self):\n                return self._write_buffer[0]\n\n        return [(\"/etag/(.*)\", EtagHandler)]\n\n    def test_wildcard_etag(self):\n        computed_etag = '\"xyzzy\"'\n        etags = \"*\"\n        self._test_etag(computed_etag, etags, 304)\n\n    def test_strong_etag_match(self):\n        computed_etag = '\"xyzzy\"'\n        etags = '\"xyzzy\"'\n        self._test_etag(computed_etag, etags, 304)\n\n    def test_multiple_strong_etag_match(self):\n        computed_etag = '\"xyzzy1\"'\n        etags = '\"xyzzy1\", \"xyzzy2\"'\n        self._test_etag(computed_etag, etags, 304)\n\n    def test_strong_etag_not_match(self):\n        computed_etag = '\"xyzzy\"'\n        etags = '\"xyzzy1\"'\n        self._test_etag(computed_etag, etags, 200)\n\n    def test_multiple_strong_etag_not_match(self):\n        computed_etag = '\"xyzzy\"'\n        etags = '\"xyzzy1\", \"xyzzy2\"'\n        self._test_etag(computed_etag, etags, 200)\n\n    def test_weak_etag_match(self):\n        computed_etag = '\"xyzzy1\"'\n        etags = 'W/\"xyzzy1\"'\n        self._test_etag(computed_etag, etags, 304)\n\n    def test_multiple_weak_etag_match(self):\n        computed_etag = '\"xyzzy2\"'\n        etags = 'W/\"xyzzy1\", W/\"xyzzy2\"'\n        self._test_etag(computed_etag, etags, 304)\n\n    def test_weak_etag_not_match(self):\n        computed_etag = '\"xyzzy2\"'\n        etags = 'W/\"xyzzy1\"'\n        self._test_etag(computed_etag, etags, 200)\n\n    def test_multiple_weak_etag_not_match(self):\n        computed_etag = '\"xyzzy3\"'\n        etags = 'W/\"xyzzy1\", W/\"xyzzy2\"'\n        self._test_etag(computed_etag, etags, 200)\n\n    def _test_etag(self, computed_etag, etags, status_code):\n        response = self.fetch(\n            \"/etag/\" + computed_etag, headers={\"If-None-Match\": etags}\n        )\n        self.assertEqual(response.code, status_code)\n\n\nclass RequestSummaryTest(SimpleHandlerTestCase):\n    class Handler(RequestHandler):\n        def get(self):\n            # remote_ip is optional, although it's set by\n            # both HTTPServer and WSGIAdapter.\n            # Clobber it to make sure it doesn't break logging.\n            self.request.remote_ip = None\n            self.finish(self._request_summary())\n\n    def test_missing_remote_ip(self):\n        resp = self.fetch(\"/\")\n        self.assertEqual(resp.body, b\"GET / (None)\")\n\n\nclass HTTPErrorTest(unittest.TestCase):\n    def test_copy(self):\n        e = HTTPError(403, reason=\"Go away\")\n        e2 = copy.copy(e)\n        self.assertIsNot(e, e2)\n        self.assertEqual(e.status_code, e2.status_code)\n        self.assertEqual(e.reason, e2.reason)\n\n\nclass ApplicationTest(AsyncTestCase):\n    def test_listen(self):\n        app = Application([])\n        server = app.listen(0, address=\"127.0.0.1\")\n        server.stop()\n\n\nclass URLSpecReverseTest(unittest.TestCase):\n    def test_reverse(self):\n        self.assertEqual(\"/favicon.ico\", url(r\"/favicon\\.ico\", None).reverse())\n        self.assertEqual(\"/favicon.ico\", url(r\"^/favicon\\.ico$\", None).reverse())\n\n    def test_non_reversible(self):\n        # URLSpecs are non-reversible if they include non-constant\n        # regex features outside capturing groups. Currently, this is\n        # only strictly enforced for backslash-escaped character\n        # classes.\n        paths = [r\"^/api/v\\d+/foo/(\\w+)$\"]\n        for path in paths:\n            # A URLSpec can still be created even if it cannot be reversed.\n            url_spec = url(path, None)\n            try:\n                result = url_spec.reverse()\n                self.fail(\n                    \"did not get expected exception when reversing %s. \"\n                    \"result: %s\" % (path, result)\n                )\n            except ValueError:\n                pass\n\n    def test_reverse_arguments(self):\n        self.assertEqual(\n            \"/api/v1/foo/bar\", url(r\"^/api/v1/foo/(\\w+)$\", None).reverse(\"bar\")\n        )\n\n\nclass RedirectHandlerTest(WebTestCase):\n    def get_handlers(self):\n        return [\n            (\"/src\", WebRedirectHandler, {\"url\": \"/dst\"}),\n            (\"/src2\", WebRedirectHandler, {\"url\": \"/dst2?foo=bar\"}),\n            (r\"/(.*?)/(.*?)/(.*)\", WebRedirectHandler, {\"url\": \"/{1}/{0}/{2}\"}),\n        ]\n\n    def test_basic_redirect(self):\n        response = self.fetch(\"/src\", follow_redirects=False)\n        self.assertEqual(response.code, 301)\n        self.assertEqual(response.headers[\"Location\"], \"/dst\")\n\n    def test_redirect_with_argument(self):\n        response = self.fetch(\"/src?foo=bar\", follow_redirects=False)\n        self.assertEqual(response.code, 301)\n        self.assertEqual(response.headers[\"Location\"], \"/dst?foo=bar\")\n\n    def test_redirect_with_appending_argument(self):\n        response = self.fetch(\"/src2?foo2=bar2\", follow_redirects=False)\n        self.assertEqual(response.code, 301)\n        self.assertEqual(response.headers[\"Location\"], \"/dst2?foo=bar&foo2=bar2\")\n\n    def test_redirect_pattern(self):\n        response = self.fetch(\"/a/b/c\", follow_redirects=False)\n        self.assertEqual(response.code, 301)\n        self.assertEqual(response.headers[\"Location\"], \"/b/a/c\")\n",
        "#!/usr/bin/env python\nimport unittest\nimport sys\nimport time\nimport traceback\nimport platform\nimport os\n\n\nclass PytestLikeResult(unittest.TestResult):\n    def __init__(self, *args, **kwargs):\n        super().__init__(*args, **kwargs)\n        self.start_time = None\n        # Store (test, outcome, info) for each test:\n        #   outcome ∈ { \"passed\", \"failed\", \"error\", \"skipped\" }\n        #   info can hold the exception or skip reason, etc.\n        self.results = []\n\n    def startTestRun(self):\n        \"\"\"Called once before all tests.\"\"\"\n        self.start_time = time.time()\n        # Print a header similar to pytest\n        print(\"=\" * 29 + \" test session starts \" + \"=\" * 29)\n        # You can customize the platform/versions/paths as you like:\n        print(\n            f\"platform {platform.system().lower()} -- Python {platform.python_version()}\"\n        )\n        print(f\"rootdir: {os.path.abspath(os.curdir)}\")\n\n    def stopTestRun(self):\n        \"\"\"Called once after all tests are done.\"\"\"\n        elapsed = time.time() - self.start_time\n        print()  # blank line after final dot\n\n        # Summarize\n        ntests = len(self.results)\n        print(f\"collected {ntests} items\")\n\n        # Show details of failures/errors\n        failures = [r for r in self.results if r[1] == \"failed\"]\n        errors = [r for r in self.results if r[1] == \"error\"]\n\n        if failures:\n            print(\"\\n\" + \"=\" * 35 + \" FAILURES \" + \"=\" * 35)\n            for test, outcome, err_info in failures:\n                test = test.split(\"::\")\n                if len(test) == 3:\n                    test = f\"{test[1]}.{test[2]}\"\n                else:\n                    test = f\"{test[1]}\"\n                print(f\"__________ {test} __________\")\n                self._print_traceback(err_info)\n        if errors:\n            print(\"\\n\" + \"=\" * 35 + \" ERRORS \" + \"=\" * 37)\n            for test, outcome, err_info in errors:\n                test = test.split(\"::\")\n                if len(test) == 3:\n                    test = f\"{test[1]}.{test[2]}\"\n                else:\n                    test = f\"{test[1]}\"\n                print(f\"__________ {test} __________\")\n                self._print_traceback(err_info)\n\n        # Optional short summary lines for passes/fails/skips\n        print(\"\\n==================== short test summary info ====================\")\n        for test, outcome, info in self.results:\n            if outcome in (\"passed\", \"failed\", \"error\", \"skipped\"):\n                print(f\"{outcome.upper()} {test}\")\n\n        # Final one-line summary: “1 failed, 1 errors, 10 passed in 2.04s”\n        passed_count = sum(1 for r in self.results if r[1] == \"passed\")\n        failed_count = len(failures)\n        error_count = len(errors)\n        skipped_count = sum(1 for r in self.results if r[1] == \"skipped\")\n\n        summary_bits = []\n        if failed_count:\n            summary_bits.append(f\"{failed_count} failed\")\n        if error_count:\n            summary_bits.append(f\"{error_count} error{'s' if error_count != 1 else ''}\")\n        if passed_count:\n            summary_bits.append(f\"{passed_count} passed\")\n        if skipped_count:\n            summary_bits.append(f\"{skipped_count} skipped\")\n\n        summary_str = \", \".join(summary_bits) if summary_bits else \"no tests run\"\n        print(\n            f\"=================== {summary_str} in {elapsed:.2f}s ===================\"\n        )\n\n    def startTest(self, test):\n        \"\"\"Called right before each test method.\"\"\"\n        super().startTest(test)\n        # Print a dot or some indicator\n        print(\".\", end=\"\", flush=True)\n\n    def addSuccess(self, test):\n        super().addSuccess(test)\n        self.results.append((self._test_id(test), \"passed\", None))\n\n    def addFailure(self, test, err):\n        super().addFailure(test, err)\n        self.results.append((self._test_id(test), \"failed\", err))\n\n    def addError(self, test, err):\n        super().addError(test, err)\n        self.results.append((self._test_id(test), \"error\", err))\n\n    def addSkip(self, test, reason):\n        super().addSkip(test, reason)\n        self.results.append((self._test_id(test), \"skipped\", reason))\n\n    def _print_traceback(self, err_info):\n        \"\"\"Helper to print the traceback like pytest does.\"\"\"\n        if isinstance(err_info, tuple) and len(err_info) == 3:\n            exc_type, exc_value, tb = err_info\n            traceback.print_exception(exc_type, exc_value, tb, file=sys.stdout)\n        else:\n            print(str(err_info))\n\n    def _test_id(self, test):\n        try:\n            return f\"{test.__class__.__module__}::{test.__class__.__name__}::{test._testMethodName}\"\n        except AttributeError:\n            # Fallback for _ErrorHolder objects\n            return f\"{test.__class__.__module__}::{test.__class__.__name__}\"\n\n\nclass PytestLikeRunner(unittest.TextTestRunner):\n    \"\"\"A custom TextTestRunner that uses the PytestLikeResult.\"\"\"\n\n    resultclass = PytestLikeResult\n\n    def run(self, test):\n        result = self._makeResult()\n        # Overriding run to ensure we call startTestRun/stopTestRun\n        result.startTestRun()\n        test(result)\n        result.stopTestRun()\n        return result\n\n    def tearDown(self):\n        # Reset signal handlers\n        from twisted.internet import reactor\n\n        reactor.removeAll()\n        reactor.stop()\n        super().tearDown()\n\n\ndef main():\n    loader = unittest.TestLoader()\n    suite = loader.discover(\".\", pattern=\"test_*.py\")  # discover tests in r2e_tests/\n    runner = PytestLikeRunner(verbosity=0)\n    result = runner.run(suite)\n    # Exit with code 0 if all tests passed/skipped, 1 if there were fails/errors\n    sys.exit(not result.wasSuccessful())\n\n\nif __name__ == \"__main__\":\n    main()\n"
    ],
    "test_file_names": [
        "test_1.py",
        "tornado_unittest_runner.py"
    ],
    "setup_res_code": 0,
    "setup_res_stdout": "",
    "setup_res_stderr": "Using CPython 3.9.21\nCreating virtual environment at: .venv\nActivate with: source .venv/bin/activate\nResolved 1 package in 869ms\nPrepared 1 package in 981ms\nInstalled 1 package in 1ms\n + tornado==6.0.dev1 (from file:///home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9)\nResolved 29 packages in 17ms\nInstalled 29 packages in 225ms\n + attrs==24.3.0\n + automat==24.8.1\n + cachetools==5.5.0\n + cffi==1.17.1\n + chardet==5.2.0\n + colorama==0.4.6\n + constantly==23.10.4\n + distlib==0.3.9\n + exceptiongroup==1.2.2\n + filelock==3.16.1\n + hyperlink==21.0.0\n + idna==3.10\n + incremental==24.7.2\n + iniconfig==2.0.0\n + packaging==24.2\n + platformdirs==4.3.6\n + pluggy==1.5.0\n + pycares==4.5.0\n + pycparser==2.22\n + pycurl==7.45.4\n + pyproject-api==1.8.0\n + pytest==8.3.4\n + setuptools==75.6.0\n + tomli==2.2.1\n + tox==4.23.2\n + twisted==24.11.0\n + typing-extensions==4.12.2\n + virtualenv==20.28.0\n + zope-interface==7.2\n",
    "new_commit_res_code": 1,
    "new_commit_res_stdout": "============================= test session starts =============================\nplatform linux -- Python 3.9.21\nrootdir: /home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9\n........................................................................................................................................................................................\ncollected 184 items\n\n=================================== FAILURES ===================================\n__________ GzipTestCase.test_gzip_static __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1758, in test_gzip_static\n    self.assert_compressed(response)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1742, in assert_compressed\n    self.assertEqual(\nAssertionError: None != 'gzip'\n__________ StaticDefaultFilenameTest.test_static_default_filename __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1409, in test_static_default_filename\n    self.assertEqual(response.code, 200)\nAssertionError: 404 != 200\n__________ StaticDefaultFilenameTest.test_static_default_redirect __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1414, in test_static_default_redirect\n    self.assertEqual(response.code, 301)\nAssertionError: 404 != 301\n__________ StaticFileTest.test_absolute_static_url __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1127, in test_absolute_static_url\n    self.assertEqual(\nAssertionError: b'http://127.0.0.1:44667/static/robots.txt' != b'http://127.0.0.1:44667/static/robots.txt?v=f71d20196d4caf35b6a670db8c70b03d'\n__________ StaticFileTest.test_root_static_path __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1394, in test_root_static_path\n    self.assertEqual(response.code, 200)\nAssertionError: 404 != 200\n__________ StaticFileTest.test_static_compressed_files __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1110, in test_static_compressed_files\n    self.assertEqual(response.headers.get(\"Content-Type\"), \"application/gzip\")\nAssertionError: 'text/html; charset=UTF-8' != 'application/gzip'\n- text/html; charset=UTF-8\n+ application/gzip\n\n__________ StaticFileTest.test_static_etag __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1228, in test_static_etag\n    self.assertEqual(\nAssertionError: None != b'\"f71d20196d4caf35b6a670db8c70b03d\"'\n__________ StaticFileTest.test_static_files __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1102, in test_static_files\n    self.assertTrue(b\"Disallow: /\" in response.body)\nAssertionError: False is not true\n__________ StaticFileTest.test_static_head __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1333, in test_static_head\n    self.assertEqual(response.code, 200)\nAssertionError: 404 != 200\n__________ StaticFileTest.test_static_head_range __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1345, in test_static_head_range\n    self.assertEqual(response.code, 206)\nAssertionError: 404 != 206\n__________ StaticFileTest.test_static_if_modified_since_pre_epoch __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1206, in test_static_if_modified_since_pre_epoch\n    self.assertEqual(response.code, 200)\nAssertionError: 404 != 200\n__________ StaticFileTest.test_static_invalid_range __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1308, in test_static_invalid_range\n    self.assertEqual(response.code, 200)\nAssertionError: 404 != 200\n__________ StaticFileTest.test_static_range_if_none_match __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1360, in test_static_range_if_none_match\n    self.assertEqual(response.code, 304)\nAssertionError: 404 != 304\n__________ StaticFileTest.test_static_unsatisfiable_range_end_less_than_start __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1328, in test_static_unsatisfiable_range_end_less_than_start\n    self.assertEqual(response.code, 416)\nAssertionError: 404 != 416\n__________ StaticFileTest.test_static_unsatisfiable_range_invalid_start __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1321, in test_static_unsatisfiable_range_invalid_start\n    self.assertEqual(response.code, 416)\nAssertionError: 404 != 416\n__________ StaticFileTest.test_static_unsatisfiable_range_zero_suffix __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1314, in test_static_unsatisfiable_range_zero_suffix\n    self.assertEqual(response.headers.get(\"Content-Range\"), \"bytes */26\")\nAssertionError: None != 'bytes */26'\n__________ StaticFileTest.test_static_url __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1123, in test_static_url\n    self.assertEqual(response.body, b\"/static/robots.txt?v=\" + self.robots_txt_hash)\nAssertionError: b'/static/robots.txt' != b'/static/robots.txt?v=f71d20196d4caf35b6a670db8c70b03d'\n__________ StaticFileTest.test_static_with_range __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1236, in test_static_with_range\n    self.assertEqual(response.code, 206)\nAssertionError: 404 != 206\n__________ StaticFileTest.test_static_with_range_end_edge __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1283, in test_static_with_range_end_edge\n    self.assertEqual(response.body, b\": /\\n\")\nAssertionError: b'<html><title>404: Not Found</title><body>404: Not Found</body></html>' != b': /\\n'\n__________ StaticFileTest.test_static_with_range_full_file __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1250, in test_static_with_range_full_file\n    self.assertEqual(response.code, 200)\nAssertionError: 404 != 200\n__________ StaticFileTest.test_static_with_range_full_past_end __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1261, in test_static_with_range_full_past_end\n    self.assertEqual(response.code, 200)\nAssertionError: 404 != 200\n__________ StaticFileTest.test_static_with_range_neg_end __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1291, in test_static_with_range_neg_end\n    self.assertEqual(response.body, b\": /\\n\")\nAssertionError: b'<html><title>404: Not Found</title><body>404: Not Found</body></html>' != b': /\\n'\n__________ StaticFileTest.test_static_with_range_neg_past_start __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1299, in test_static_with_range_neg_past_start\n    self.assertEqual(response.code, 200)\nAssertionError: 404 != 200\n__________ StaticFileTest.test_static_with_range_partial_past_end __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1272, in test_static_with_range_partial_past_end\n    self.assertEqual(response.code, 206)\nAssertionError: 404 != 206\n__________ StaticFileWithPathTest.test_serve __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1430, in test_serve\n    self.assertEqual(response.body, b\"H\\xc3\\xa9llo\\n\")\nAssertionError: b'<html><title>404: Not Found</title><body>404: Not Found</body></html>' != b'H\\xc3\\xa9llo\\n'\n\n=================================== ERRORS =====================================\n__________ StaticFileTest.test_static_304_etag_modified_bug __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1193, in test_static_304_etag_modified_bug\n    \"If-Modified-Since\": response1.headers[\"Last-Modified\"],\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/httputil.py\", line 239, in __getitem__\n    return self._dict[_normalized_headers[name]]\nKeyError: 'Last-Modified'\n__________ StaticFileTest.test_static_304_if_modified_since __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1174, in test_static_304_if_modified_since\n    headers={\"If-Modified-Since\": response1.headers[\"Last-Modified\"]},\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/httputil.py\", line 239, in __getitem__\n    return self._dict[_normalized_headers[name]]\nKeyError: 'Last-Modified'\n__________ StaticFileTest.test_static_304_if_none_match __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1183, in test_static_304_if_none_match\n    \"/static/robots.txt\", headers={\"If-None-Match\": response1.headers[\"Etag\"]}\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/httputil.py\", line 239, in __getitem__\n    return self._dict[_normalized_headers[name]]\nKeyError: 'Etag'\n__________ StaticFileTest.test_static_if_modified_since_time_zone __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1213, in test_static_if_modified_since_time_zone\n    stat = os.stat(relpath(\"static/robots.txt\"))\nFileNotFoundError: [Errno 2] No such file or directory: '/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/static/robots.txt'\n\n==================== short test summary info ====================\nPASSED r2e_tests.test_1::AllHTTPMethodsTest::test_standard_methods\nPASSED r2e_tests.test_1::ApplicationTest::test_listen\nPASSED r2e_tests.test_1::AuthRedirectTest::test_absolute_auth_redirect\nPASSED r2e_tests.test_1::AuthRedirectTest::test_relative_auth_redirect\nPASSED r2e_tests.test_1::BuggyLoggingTest::test_buggy_log_exception\nPASSED r2e_tests.test_1::CacheTest::test_multiple_strong_etag_match\nPASSED r2e_tests.test_1::CacheTest::test_multiple_strong_etag_not_match\nPASSED r2e_tests.test_1::CacheTest::test_multiple_weak_etag_match\nPASSED r2e_tests.test_1::CacheTest::test_multiple_weak_etag_not_match\nPASSED r2e_tests.test_1::CacheTest::test_strong_etag_match\nPASSED r2e_tests.test_1::CacheTest::test_strong_etag_not_match\nPASSED r2e_tests.test_1::CacheTest::test_weak_etag_match\nPASSED r2e_tests.test_1::CacheTest::test_weak_etag_not_match\nPASSED r2e_tests.test_1::CacheTest::test_wildcard_etag\nPASSED r2e_tests.test_1::ClearAllCookiesTest::test_clear_all_cookies\nPASSED r2e_tests.test_1::ClearHeaderTest::test_clear_header\nPASSED r2e_tests.test_1::ClientCloseTest::test_client_close\nPASSED r2e_tests.test_1::ConnectionCloseTest::test_connection_close\nPASSED r2e_tests.test_1::CookieTest::test_cookie_special_char\nPASSED r2e_tests.test_1::CookieTest::test_get_cookie\nPASSED r2e_tests.test_1::CookieTest::test_set_cookie\nPASSED r2e_tests.test_1::CookieTest::test_set_cookie_domain\nPASSED r2e_tests.test_1::CookieTest::test_set_cookie_expires_days\nPASSED r2e_tests.test_1::CookieTest::test_set_cookie_false_flags\nPASSED r2e_tests.test_1::CookieTest::test_set_cookie_max_age\nPASSED r2e_tests.test_1::CookieTest::test_set_cookie_overwrite\nPASSED r2e_tests.test_1::Custom404Test::test_404\nPASSED r2e_tests.test_1::CustomStaticFileTest::test_serve\nPASSED r2e_tests.test_1::CustomStaticFileTest::test_static_url\nPASSED r2e_tests.test_1::DateHeaderTest::test_date_header\nPASSED r2e_tests.test_1::DecoratedStreamingRequestFlowControlTest::test_flow_control_chunked_body\nPASSED r2e_tests.test_1::DecoratedStreamingRequestFlowControlTest::test_flow_control_compressed_body\nPASSED r2e_tests.test_1::DecoratedStreamingRequestFlowControlTest::test_flow_control_fixed_body\nPASSED r2e_tests.test_1::DecoratorTest::test_addslash\nPASSED r2e_tests.test_1::DecoratorTest::test_removeslash\nPASSED r2e_tests.test_1::Default404Test::test_404\nPASSED r2e_tests.test_1::DefaultHandlerArgumentsTest::test_403\nPASSED r2e_tests.test_1::DefaultHostMatchingTest::test_default_host_matching\nPASSED r2e_tests.test_1::ErrorHandlerXSRFTest::test_404_xsrf\nPASSED r2e_tests.test_1::ErrorHandlerXSRFTest::test_error_xsrf\nPASSED r2e_tests.test_1::ErrorResponseTest::test_default\nPASSED r2e_tests.test_1::ErrorResponseTest::test_failed_write_error\nPASSED r2e_tests.test_1::ErrorResponseTest::test_write_error\nPASSED r2e_tests.test_1::ExceptionHandlerTest::test_http_error\nPASSED r2e_tests.test_1::ExceptionHandlerTest::test_known_error\nPASSED r2e_tests.test_1::ExceptionHandlerTest::test_unknown_error\nPASSED r2e_tests.test_1::FinalReturnTest::test_finish_method_return_future\nPASSED r2e_tests.test_1::FinalReturnTest::test_render_method_return_future\nPASSED r2e_tests.test_1::FinishExceptionTest::test_finish_exception\nPASSED r2e_tests.test_1::FinishInPrepareTest::test_finish_in_prepare\nPASSED r2e_tests.test_1::GetArgumentErrorTest::test_catch_error\nPASSED r2e_tests.test_1::GetCurrentUserTest::test_get_current_user_from_ui_module_is_lazy\nPASSED r2e_tests.test_1::GetCurrentUserTest::test_get_current_user_from_ui_module_works\nSKIPPED r2e_tests.test_1::GetCurrentUserTest::test_get_current_user_is_lazy\nPASSED r2e_tests.test_1::GetCurrentUserTest::test_get_current_user_works\nPASSED r2e_tests.test_1::GzipTestCase::test_gzip\nPASSED r2e_tests.test_1::GzipTestCase::test_gzip_not_requested\nFAILED r2e_tests.test_1::GzipTestCase::test_gzip_static\nPASSED r2e_tests.test_1::GzipTestCase::test_vary_already_present\nPASSED r2e_tests.test_1::GzipTestCase::test_vary_already_present_multiple\nPASSED r2e_tests.test_1::HTTPErrorTest::test_copy\nPASSED r2e_tests.test_1::HandlerByNameTest::test_handler_by_name\nPASSED r2e_tests.test_1::Header204Test::test_204_headers\nPASSED r2e_tests.test_1::Header304Test::test_304_headers\nPASSED r2e_tests.test_1::HostMatchingTest::test_host_matching\nPASSED r2e_tests.test_1::IncorrectContentLengthTest::test_content_length_too_high\nPASSED r2e_tests.test_1::IncorrectContentLengthTest::test_content_length_too_low\nPASSED r2e_tests.test_1::NamedURLSpecGroupsTest::test_named_urlspec_groups\nPASSED r2e_tests.test_1::NativeStreamingRequestFlowControlTest::test_flow_control_chunked_body\nPASSED r2e_tests.test_1::NativeStreamingRequestFlowControlTest::test_flow_control_compressed_body\nPASSED r2e_tests.test_1::NativeStreamingRequestFlowControlTest::test_flow_control_fixed_body\nPASSED r2e_tests.test_1::NonWSGIWebTests::test_empty_flush\nPASSED r2e_tests.test_1::PatchMethodTest::test_other\nPASSED r2e_tests.test_1::PatchMethodTest::test_patch\nPASSED r2e_tests.test_1::PathArgsInPrepareTest::test_kw\nPASSED r2e_tests.test_1::PathArgsInPrepareTest::test_pos\nPASSED r2e_tests.test_1::RaiseWithReasonTest::test_httperror_str\nPASSED r2e_tests.test_1::RaiseWithReasonTest::test_httperror_str_from_httputil\nPASSED r2e_tests.test_1::RaiseWithReasonTest::test_raise_with_reason\nPASSED r2e_tests.test_1::RedirectHandlerTest::test_basic_redirect\nPASSED r2e_tests.test_1::RedirectHandlerTest::test_redirect_pattern\nPASSED r2e_tests.test_1::RedirectHandlerTest::test_redirect_with_appending_argument\nPASSED r2e_tests.test_1::RedirectHandlerTest::test_redirect_with_argument\nPASSED r2e_tests.test_1::RequestEncodingTest::test_error\nPASSED r2e_tests.test_1::RequestEncodingTest::test_group_encoding\nPASSED r2e_tests.test_1::RequestEncodingTest::test_group_question_mark\nPASSED r2e_tests.test_1::RequestEncodingTest::test_slashes\nPASSED r2e_tests.test_1::RequestSummaryTest::test_missing_remote_ip\nPASSED r2e_tests.test_1::SecureCookieV1Test::test_arbitrary_bytes\nPASSED r2e_tests.test_1::SecureCookieV1Test::test_cookie_tampering_future_timestamp\nPASSED r2e_tests.test_1::SecureCookieV1Test::test_round_trip\nPASSED r2e_tests.test_1::SecureCookieV2Test::test_key_version_increment_version\nPASSED r2e_tests.test_1::SecureCookieV2Test::test_key_version_invalidate_version\nPASSED r2e_tests.test_1::SecureCookieV2Test::test_key_version_roundtrip\nPASSED r2e_tests.test_1::SecureCookieV2Test::test_key_version_roundtrip_differing_version\nPASSED r2e_tests.test_1::SecureCookieV2Test::test_round_trip\nPASSED r2e_tests.test_1::SetLazyPropertiesTest::test_set_properties\nPASSED r2e_tests.test_1::SignedValueTest::test_expired\nPASSED r2e_tests.test_1::SignedValueTest::test_key_version_retrieval\nPASSED r2e_tests.test_1::SignedValueTest::test_key_versioning_invalid_key\nPASSED r2e_tests.test_1::SignedValueTest::test_key_versioning_read_write_default_key\nPASSED r2e_tests.test_1::SignedValueTest::test_key_versioning_read_write_non_default_key\nPASSED r2e_tests.test_1::SignedValueTest::test_known_values\nPASSED r2e_tests.test_1::SignedValueTest::test_name_swap\nPASSED r2e_tests.test_1::SignedValueTest::test_non_ascii\nPASSED r2e_tests.test_1::SignedValueTest::test_payload_tampering\nPASSED r2e_tests.test_1::SignedValueTest::test_signature_tampering\nFAILED r2e_tests.test_1::StaticDefaultFilenameTest::test_static_default_filename\nFAILED r2e_tests.test_1::StaticDefaultFilenameTest::test_static_default_redirect\nFAILED r2e_tests.test_1::StaticFileTest::test_absolute_static_url\nPASSED r2e_tests.test_1::StaticFileTest::test_absolute_version_exclusion\nPASSED r2e_tests.test_1::StaticFileTest::test_include_host_override\nPASSED r2e_tests.test_1::StaticFileTest::test_path_traversal_protection\nPASSED r2e_tests.test_1::StaticFileTest::test_relative_version_exclusion\nFAILED r2e_tests.test_1::StaticFileTest::test_root_static_path\nERROR r2e_tests.test_1::StaticFileTest::test_static_304_etag_modified_bug\nERROR r2e_tests.test_1::StaticFileTest::test_static_304_if_modified_since\nERROR r2e_tests.test_1::StaticFileTest::test_static_304_if_none_match\nPASSED r2e_tests.test_1::StaticFileTest::test_static_404\nFAILED r2e_tests.test_1::StaticFileTest::test_static_compressed_files\nFAILED r2e_tests.test_1::StaticFileTest::test_static_etag\nFAILED r2e_tests.test_1::StaticFileTest::test_static_files\nFAILED r2e_tests.test_1::StaticFileTest::test_static_head\nFAILED r2e_tests.test_1::StaticFileTest::test_static_head_range\nFAILED r2e_tests.test_1::StaticFileTest::test_static_if_modified_since_pre_epoch\nERROR r2e_tests.test_1::StaticFileTest::test_static_if_modified_since_time_zone\nFAILED r2e_tests.test_1::StaticFileTest::test_static_invalid_range\nFAILED r2e_tests.test_1::StaticFileTest::test_static_range_if_none_match\nFAILED r2e_tests.test_1::StaticFileTest::test_static_unsatisfiable_range_end_less_than_start\nFAILED r2e_tests.test_1::StaticFileTest::test_static_unsatisfiable_range_invalid_start\nFAILED r2e_tests.test_1::StaticFileTest::test_static_unsatisfiable_range_zero_suffix\nFAILED r2e_tests.test_1::StaticFileTest::test_static_url\nFAILED r2e_tests.test_1::StaticFileTest::test_static_with_range\nFAILED r2e_tests.test_1::StaticFileTest::test_static_with_range_end_edge\nFAILED r2e_tests.test_1::StaticFileTest::test_static_with_range_full_file\nFAILED r2e_tests.test_1::StaticFileTest::test_static_with_range_full_past_end\nFAILED r2e_tests.test_1::StaticFileTest::test_static_with_range_neg_end\nFAILED r2e_tests.test_1::StaticFileTest::test_static_with_range_neg_past_start\nFAILED r2e_tests.test_1::StaticFileTest::test_static_with_range_partial_past_end\nFAILED r2e_tests.test_1::StaticFileWithPathTest::test_serve\nPASSED r2e_tests.test_1::StatusReasonTest::test_status\nPASSED r2e_tests.test_1::StreamingRequestBodyTest::test_close_during_upload\nPASSED r2e_tests.test_1::StreamingRequestBodyTest::test_early_return\nPASSED r2e_tests.test_1::StreamingRequestBodyTest::test_early_return_with_data\nPASSED r2e_tests.test_1::StreamingRequestBodyTest::test_streaming_body\nPASSED r2e_tests.test_1::UIMethodUIModuleTest::test_ui_method\nPASSED r2e_tests.test_1::URLSpecReverseTest::test_non_reversible\nPASSED r2e_tests.test_1::URLSpecReverseTest::test_reverse\nPASSED r2e_tests.test_1::URLSpecReverseTest::test_reverse_arguments\nPASSED r2e_tests.test_1::UnimplementedHTTPMethodsTest::test_unimplemented_standard_methods\nPASSED r2e_tests.test_1::UnimplementedNonStandardMethodsTest::test_unimplemented_other\nPASSED r2e_tests.test_1::UnimplementedNonStandardMethodsTest::test_unimplemented_patch\nPASSED r2e_tests.test_1::WSGISafeWebTest::test_decode_argument\nPASSED r2e_tests.test_1::WSGISafeWebTest::test_decode_argument_invalid_unicode\nPASSED r2e_tests.test_1::WSGISafeWebTest::test_decode_argument_plus\nPASSED r2e_tests.test_1::WSGISafeWebTest::test_get_argument\nPASSED r2e_tests.test_1::WSGISafeWebTest::test_get_body_arguments\nPASSED r2e_tests.test_1::WSGISafeWebTest::test_get_query_arguments\nPASSED r2e_tests.test_1::WSGISafeWebTest::test_header_injection\nPASSED r2e_tests.test_1::WSGISafeWebTest::test_multi_header\nPASSED r2e_tests.test_1::WSGISafeWebTest::test_no_gzip\nPASSED r2e_tests.test_1::WSGISafeWebTest::test_optional_path\nPASSED r2e_tests.test_1::WSGISafeWebTest::test_redirect\nPASSED r2e_tests.test_1::WSGISafeWebTest::test_reverse_url\nPASSED r2e_tests.test_1::WSGISafeWebTest::test_types\nPASSED r2e_tests.test_1::WSGISafeWebTest::test_uimodule_resources\nPASSED r2e_tests.test_1::WSGISafeWebTest::test_uimodule_unescaped\nPASSED r2e_tests.test_1::WSGISafeWebTest::test_web_redirect\nPASSED r2e_tests.test_1::WSGISafeWebTest::test_web_redirect_double_slash\nPASSED r2e_tests.test_1::XSRFCookieKwargsTest::test_xsrf_httponly\nPASSED r2e_tests.test_1::XSRFTest::test_cross_user\nPASSED r2e_tests.test_1::XSRFTest::test_distinct_tokens\nPASSED r2e_tests.test_1::XSRFTest::test_refresh_token\nPASSED r2e_tests.test_1::XSRFTest::test_versioning\nPASSED r2e_tests.test_1::XSRFTest::test_xsrf_fail_argument_invalid_format\nPASSED r2e_tests.test_1::XSRFTest::test_xsrf_fail_body_no_cookie\nPASSED r2e_tests.test_1::XSRFTest::test_xsrf_fail_cookie_invalid_format\nPASSED r2e_tests.test_1::XSRFTest::test_xsrf_fail_cookie_no_body\nPASSED r2e_tests.test_1::XSRFTest::test_xsrf_fail_no_token\nPASSED r2e_tests.test_1::XSRFTest::test_xsrf_success_header\nPASSED r2e_tests.test_1::XSRFTest::test_xsrf_success_non_hex_token\nPASSED r2e_tests.test_1::XSRFTest::test_xsrf_success_post_body\nPASSED r2e_tests.test_1::XSRFTest::test_xsrf_success_query_string\nPASSED r2e_tests.test_1::XSRFTest::test_xsrf_success_short_token\n=================== 25 failed, 4 errors, 154 passed, 1 skipped in 0.53s ===================\n",
    "new_commit_res_stderr": "WARNING:tornado.access:405 HEAD / (127.0.0.1) 0.32ms\nERROR:tornado.access:500 GET / (127.0.0.1) 0.29ms\nWARNING:tornado.access:404 GET / (127.0.0.1) 0.19ms\nWARNING:tornado.access:404 GET / (127.0.0.1) 0.25ms\nWARNING:tornado.access:403 GET / (127.0.0.1) 0.24ms\nWARNING:tornado.access:404 GET /baz (127.0.0.1) 0.23ms\nWARNING:tornado.access:404 GET /foo (127.0.0.1) 0.24ms\nWARNING:tornado.access:404 POST /404 (127.0.0.1) 0.24ms\nWARNING:tornado.access:417 POST /error (127.0.0.1) 0.22ms\nERROR:tornado.access:500 GET /default (127.0.0.1) 0.29ms\nERROR:tornado.access:503 GET /default?status=503 (127.0.0.1) 0.27ms\nWARNING:tornado.access:435 GET /default?status=435 (127.0.0.1) 0.22ms\nERROR:tornado.access:500 GET /failed_write_error (127.0.0.1) 0.30ms\nERROR:tornado.access:500 GET /write_error (127.0.0.1) 0.27ms\nERROR:tornado.access:503 GET /write_error?status=503 (127.0.0.1) 0.24ms\nWARNING:tornado.access:410 GET /?exc=http (127.0.0.1) 0.28ms\nWARNING:tornado.access:403 GET /?exc=permission (127.0.0.1) 0.30ms\nERROR:tornado.access:500 GET /?exc=zero (127.0.0.1) 0.28ms\nWARNING:tornado.access:401 GET / (127.0.0.1) 0.21ms\nWARNING:tornado.access:401 GET /?finish_value=1 (127.0.0.1) 0.22ms\nWARNING:tornado.access:404 GET /robots.txt (127.0.0.1) 0.31ms\nWARNING:tornado.access:404 GET /bar (127.0.0.1) 0.22ms\nWARNING:tornado.access:404 GET /baz (127.0.0.1) 0.21ms\nERROR:tornado.access:682 GET / (127.0.0.1) 0.26ms\nWARNING:tornado.access:400 GET /group/?arg=%25%e9 (127.0.0.1) 0.51ms\nWARNING:tornado.access:404 GET /static/dir/ (127.0.0.1) 0.31ms\nWARNING:tornado.access:404 GET /static/dir (127.0.0.1) 0.28ms\nERROR:tornado.general:Could not open static file '/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/static/robots.txt'\nWARNING:tornado.access:403 HEAD /static/../static_foo.txt (127.0.0.1) 0.28ms\nWARNING:tornado.access:403 GET /static/../static_foo.txt (127.0.0.1) 0.29ms\nWARNING:tornado.access:404 HEAD /root_static/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/static/robots.txt (127.0.0.1) 0.26ms\nWARNING:tornado.access:404 GET /root_static/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/static/robots.txt (127.0.0.1) 0.27ms\nWARNING:tornado.access:404 HEAD /static/robots.txt (127.0.0.1) 0.40ms\nWARNING:tornado.access:404 GET /static/robots.txt (127.0.0.1) 0.25ms\nWARNING:tornado.access:404 HEAD /static/robots.txt (127.0.0.1) 0.25ms\nWARNING:tornado.access:404 GET /static/robots.txt (127.0.0.1) 0.26ms\nWARNING:tornado.access:404 HEAD /static/robots.txt (127.0.0.1) 0.27ms\nWARNING:tornado.access:404 GET /static/robots.txt (127.0.0.1) 0.25ms\nWARNING:tornado.access:404 HEAD /static/blarg (127.0.0.1) 0.24ms\nWARNING:tornado.access:404 GET /static/blarg (127.0.0.1) 0.24ms\nWARNING:tornado.access:404 GET /static/sample.xml.gz (127.0.0.1) 0.25ms\nWARNING:tornado.access:404 HEAD /static/robots.txt (127.0.0.1) 0.25ms\nWARNING:tornado.access:404 GET /static/robots.txt (127.0.0.1) 0.27ms\nWARNING:tornado.access:404 GET /robots.txt (127.0.0.1) 0.26ms\nWARNING:tornado.access:404 HEAD /static/robots.txt (127.0.0.1) 0.25ms\nWARNING:tornado.access:404 HEAD /static/robots.txt (127.0.0.1) 0.27ms\nWARNING:tornado.access:404 HEAD /static/robots.txt (127.0.0.1) 0.29ms\nWARNING:tornado.access:404 GET /static/robots.txt (127.0.0.1) 0.25ms\nWARNING:tornado.access:404 HEAD /static/robots.txt (127.0.0.1) 0.27ms\nWARNING:tornado.access:404 GET /static/robots.txt (127.0.0.1) 0.26ms\nWARNING:tornado.access:404 HEAD /static/robots.txt (127.0.0.1) 0.28ms\nWARNING:tornado.access:404 GET /static/robots.txt (127.0.0.1) 0.36ms\nWARNING:tornado.access:404 HEAD /static/robots.txt (127.0.0.1) 0.26ms\nWARNING:tornado.access:404 GET /static/robots.txt (127.0.0.1) 0.29ms\nWARNING:tornado.access:404 HEAD /static/robots.txt (127.0.0.1) 0.28ms\nWARNING:tornado.access:404 GET /static/robots.txt (127.0.0.1) 0.29ms\nWARNING:tornado.access:404 HEAD /static/robots.txt (127.0.0.1) 0.27ms\nWARNING:tornado.access:404 GET /static/robots.txt (127.0.0.1) 0.42ms\nWARNING:tornado.access:404 HEAD /static/robots.txt (127.0.0.1) 0.30ms\nWARNING:tornado.access:404 GET /static/robots.txt (127.0.0.1) 0.26ms\nWARNING:tornado.access:404 HEAD /static/robots.txt (127.0.0.1) 0.29ms\nWARNING:tornado.access:404 GET /static/robots.txt (127.0.0.1) 0.28ms\nWARNING:tornado.access:404 HEAD /static/robots.txt (127.0.0.1) 0.28ms\nWARNING:tornado.access:404 GET /static/robots.txt (127.0.0.1) 0.27ms\nWARNING:tornado.access:404 HEAD /static/robots.txt (127.0.0.1) 0.30ms\nWARNING:tornado.access:404 GET /static/robots.txt (127.0.0.1) 0.26ms\nWARNING:tornado.access:404 HEAD /static/robots.txt (127.0.0.1) 0.28ms\nWARNING:tornado.access:404 GET /static/robots.txt (127.0.0.1) 0.27ms\nWARNING:tornado.access:404 HEAD /static/robots.txt (127.0.0.1) 0.26ms\nWARNING:tornado.access:404 GET /static/robots.txt (127.0.0.1) 0.40ms\nWARNING:tornado.access:404 HEAD /static/robots.txt (127.0.0.1) 0.27ms\nWARNING:tornado.access:404 GET /static/robots.txt (127.0.0.1) 0.28ms\nWARNING:tornado.access:404 GET /foo/utf8.html (127.0.0.1) 0.31ms\nERROR:tornado.access:682 GET /?code=682&reason=Bar (127.0.0.1) 0.22ms\nERROR:tornado.access:682 GET /?code=682 (127.0.0.1) 0.19ms\nWARNING:tornado.access:401 GET /early_return (127.0.0.1) 0.20ms\nWARNING:tornado.access:401 GET /early_return (127.0.0.1) 0.23ms\nWARNING:tornado.access:405 HEAD / (127.0.0.1) 0.22ms\nWARNING:tornado.access:405 GET / (127.0.0.1) 0.22ms\nWARNING:tornado.access:405 DELETE / (127.0.0.1) 0.24ms\nWARNING:tornado.access:405 OPTIONS / (127.0.0.1) 0.25ms\nWARNING:tornado.access:405 POST / (127.0.0.1) 0.23ms\nWARNING:tornado.access:405 PUT / (127.0.0.1) 0.21ms\nWARNING:tornado.access:405 OTHER / (127.0.0.1) 0.22ms\nWARNING:tornado.access:405 PATCH / (127.0.0.1) 0.22ms\nWARNING:tornado.access:400 GET /typecheck/invalid%FF (127.0.0.1) 0.29ms\nWARNING:tornado.access:400 GET /typecheck/invalid?foo=%FF (127.0.0.1) 0.31ms\nWARNING:tornado.access:403 POST / (127.0.0.1) 0.66ms\nWARNING:tornado.access:403 POST / (127.0.0.1) 0.34ms\nWARNING:tornado.access:403 POST / (127.0.0.1) 0.39ms\nWARNING:tornado.access:403 POST / (127.0.0.1) 0.35ms\nWARNING:tornado.access:403 POST / (127.0.0.1) 0.38ms\nWARNING:tornado.access:403 POST / (127.0.0.1) 0.31ms\nWARNING:tornado.access:403 POST / (127.0.0.1) 0.26ms\n",
    "old_commit_res_code": 1,
    "old_commit_res_stdout": "============================= test session starts =============================\nplatform linux -- Python 3.9.21\nrootdir: /home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9\n........................................................................................................................................................................................\ncollected 184 items\n\n=================================== FAILURES ===================================\n__________ FinalReturnTest.test_finish_method_return_future __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 256, in test_finish_method_return_future\n    self.assertTrue(self.final_return.done())\nAssertionError: False is not true\n__________ GzipTestCase.test_gzip_static __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1758, in test_gzip_static\n    self.assert_compressed(response)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1742, in assert_compressed\n    self.assertEqual(\nAssertionError: None != 'gzip'\n__________ StaticDefaultFilenameTest.test_static_default_filename __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1409, in test_static_default_filename\n    self.assertEqual(response.code, 200)\nAssertionError: 404 != 200\n__________ StaticDefaultFilenameTest.test_static_default_redirect __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1414, in test_static_default_redirect\n    self.assertEqual(response.code, 301)\nAssertionError: 404 != 301\n__________ StaticFileTest.test_absolute_static_url __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1127, in test_absolute_static_url\n    self.assertEqual(\nAssertionError: b'http://127.0.0.1:45355/static/robots.txt' != b'http://127.0.0.1:45355/static/robots.txt?v=f71d20196d4caf35b6a670db8c70b03d'\n__________ StaticFileTest.test_root_static_path __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1394, in test_root_static_path\n    self.assertEqual(response.code, 200)\nAssertionError: 404 != 200\n__________ StaticFileTest.test_static_compressed_files __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1110, in test_static_compressed_files\n    self.assertEqual(response.headers.get(\"Content-Type\"), \"application/gzip\")\nAssertionError: 'text/html; charset=UTF-8' != 'application/gzip'\n- text/html; charset=UTF-8\n+ application/gzip\n\n__________ StaticFileTest.test_static_etag __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1228, in test_static_etag\n    self.assertEqual(\nAssertionError: None != b'\"f71d20196d4caf35b6a670db8c70b03d\"'\n__________ StaticFileTest.test_static_files __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1102, in test_static_files\n    self.assertTrue(b\"Disallow: /\" in response.body)\nAssertionError: False is not true\n__________ StaticFileTest.test_static_head __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1333, in test_static_head\n    self.assertEqual(response.code, 200)\nAssertionError: 404 != 200\n__________ StaticFileTest.test_static_head_range __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1345, in test_static_head_range\n    self.assertEqual(response.code, 206)\nAssertionError: 404 != 206\n__________ StaticFileTest.test_static_if_modified_since_pre_epoch __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1206, in test_static_if_modified_since_pre_epoch\n    self.assertEqual(response.code, 200)\nAssertionError: 404 != 200\n__________ StaticFileTest.test_static_invalid_range __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1308, in test_static_invalid_range\n    self.assertEqual(response.code, 200)\nAssertionError: 404 != 200\n__________ StaticFileTest.test_static_range_if_none_match __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1360, in test_static_range_if_none_match\n    self.assertEqual(response.code, 304)\nAssertionError: 404 != 304\n__________ StaticFileTest.test_static_unsatisfiable_range_end_less_than_start __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1328, in test_static_unsatisfiable_range_end_less_than_start\n    self.assertEqual(response.code, 416)\nAssertionError: 404 != 416\n__________ StaticFileTest.test_static_unsatisfiable_range_invalid_start __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1321, in test_static_unsatisfiable_range_invalid_start\n    self.assertEqual(response.code, 416)\nAssertionError: 404 != 416\n__________ StaticFileTest.test_static_unsatisfiable_range_zero_suffix __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1314, in test_static_unsatisfiable_range_zero_suffix\n    self.assertEqual(response.headers.get(\"Content-Range\"), \"bytes */26\")\nAssertionError: None != 'bytes */26'\n__________ StaticFileTest.test_static_url __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1123, in test_static_url\n    self.assertEqual(response.body, b\"/static/robots.txt?v=\" + self.robots_txt_hash)\nAssertionError: b'/static/robots.txt' != b'/static/robots.txt?v=f71d20196d4caf35b6a670db8c70b03d'\n__________ StaticFileTest.test_static_with_range __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1236, in test_static_with_range\n    self.assertEqual(response.code, 206)\nAssertionError: 404 != 206\n__________ StaticFileTest.test_static_with_range_end_edge __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1283, in test_static_with_range_end_edge\n    self.assertEqual(response.body, b\": /\\n\")\nAssertionError: b'<html><title>404: Not Found</title><body>404: Not Found</body></html>' != b': /\\n'\n__________ StaticFileTest.test_static_with_range_full_file __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1250, in test_static_with_range_full_file\n    self.assertEqual(response.code, 200)\nAssertionError: 404 != 200\n__________ StaticFileTest.test_static_with_range_full_past_end __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1261, in test_static_with_range_full_past_end\n    self.assertEqual(response.code, 200)\nAssertionError: 404 != 200\n__________ StaticFileTest.test_static_with_range_neg_end __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1291, in test_static_with_range_neg_end\n    self.assertEqual(response.body, b\": /\\n\")\nAssertionError: b'<html><title>404: Not Found</title><body>404: Not Found</body></html>' != b': /\\n'\n__________ StaticFileTest.test_static_with_range_neg_past_start __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1299, in test_static_with_range_neg_past_start\n    self.assertEqual(response.code, 200)\nAssertionError: 404 != 200\n__________ StaticFileTest.test_static_with_range_partial_past_end __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1272, in test_static_with_range_partial_past_end\n    self.assertEqual(response.code, 206)\nAssertionError: 404 != 206\n__________ StaticFileWithPathTest.test_serve __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1430, in test_serve\n    self.assertEqual(response.body, b\"H\\xc3\\xa9llo\\n\")\nAssertionError: b'<html><title>404: Not Found</title><body>404: Not Found</body></html>' != b'H\\xc3\\xa9llo\\n'\n\n=================================== ERRORS =====================================\n__________ StaticFileTest.test_static_304_etag_modified_bug __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1193, in test_static_304_etag_modified_bug\n    \"If-Modified-Since\": response1.headers[\"Last-Modified\"],\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/httputil.py\", line 239, in __getitem__\n    return self._dict[_normalized_headers[name]]\nKeyError: 'Last-Modified'\n__________ StaticFileTest.test_static_304_if_modified_since __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1174, in test_static_304_if_modified_since\n    headers={\"If-Modified-Since\": response1.headers[\"Last-Modified\"]},\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/httputil.py\", line 239, in __getitem__\n    return self._dict[_normalized_headers[name]]\nKeyError: 'Last-Modified'\n__________ StaticFileTest.test_static_304_if_none_match __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1183, in test_static_304_if_none_match\n    \"/static/robots.txt\", headers={\"If-None-Match\": response1.headers[\"Etag\"]}\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/httputil.py\", line 239, in __getitem__\n    return self._dict[_normalized_headers[name]]\nKeyError: 'Etag'\n__________ StaticFileTest.test_static_if_modified_since_time_zone __________\nTraceback (most recent call last):\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 59, in testPartExecutor\n    yield\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 592, in run\n    self._callTestMethod(testMethod)\n  File \"/home/gcpuser/.local/share/uv/python/cpython-3.9.21-linux-x86_64-gnu/lib/python3.9/unittest/case.py\", line 550, in _callTestMethod\n    method()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/testing.py\", line 93, in __call__\n    result = self.orig_method(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/test_1.py\", line 1213, in test_static_if_modified_since_time_zone\n    stat = os.stat(relpath(\"static/robots.txt\"))\nFileNotFoundError: [Errno 2] No such file or directory: '/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/static/robots.txt'\n\n==================== short test summary info ====================\nPASSED r2e_tests.test_1::AllHTTPMethodsTest::test_standard_methods\nPASSED r2e_tests.test_1::ApplicationTest::test_listen\nPASSED r2e_tests.test_1::AuthRedirectTest::test_absolute_auth_redirect\nPASSED r2e_tests.test_1::AuthRedirectTest::test_relative_auth_redirect\nPASSED r2e_tests.test_1::BuggyLoggingTest::test_buggy_log_exception\nPASSED r2e_tests.test_1::CacheTest::test_multiple_strong_etag_match\nPASSED r2e_tests.test_1::CacheTest::test_multiple_strong_etag_not_match\nPASSED r2e_tests.test_1::CacheTest::test_multiple_weak_etag_match\nPASSED r2e_tests.test_1::CacheTest::test_multiple_weak_etag_not_match\nPASSED r2e_tests.test_1::CacheTest::test_strong_etag_match\nPASSED r2e_tests.test_1::CacheTest::test_strong_etag_not_match\nPASSED r2e_tests.test_1::CacheTest::test_weak_etag_match\nPASSED r2e_tests.test_1::CacheTest::test_weak_etag_not_match\nPASSED r2e_tests.test_1::CacheTest::test_wildcard_etag\nPASSED r2e_tests.test_1::ClearAllCookiesTest::test_clear_all_cookies\nPASSED r2e_tests.test_1::ClearHeaderTest::test_clear_header\nPASSED r2e_tests.test_1::ClientCloseTest::test_client_close\nPASSED r2e_tests.test_1::ConnectionCloseTest::test_connection_close\nPASSED r2e_tests.test_1::CookieTest::test_cookie_special_char\nPASSED r2e_tests.test_1::CookieTest::test_get_cookie\nPASSED r2e_tests.test_1::CookieTest::test_set_cookie\nPASSED r2e_tests.test_1::CookieTest::test_set_cookie_domain\nPASSED r2e_tests.test_1::CookieTest::test_set_cookie_expires_days\nPASSED r2e_tests.test_1::CookieTest::test_set_cookie_false_flags\nPASSED r2e_tests.test_1::CookieTest::test_set_cookie_max_age\nPASSED r2e_tests.test_1::CookieTest::test_set_cookie_overwrite\nPASSED r2e_tests.test_1::Custom404Test::test_404\nPASSED r2e_tests.test_1::CustomStaticFileTest::test_serve\nPASSED r2e_tests.test_1::CustomStaticFileTest::test_static_url\nPASSED r2e_tests.test_1::DateHeaderTest::test_date_header\nPASSED r2e_tests.test_1::DecoratedStreamingRequestFlowControlTest::test_flow_control_chunked_body\nPASSED r2e_tests.test_1::DecoratedStreamingRequestFlowControlTest::test_flow_control_compressed_body\nPASSED r2e_tests.test_1::DecoratedStreamingRequestFlowControlTest::test_flow_control_fixed_body\nPASSED r2e_tests.test_1::DecoratorTest::test_addslash\nPASSED r2e_tests.test_1::DecoratorTest::test_removeslash\nPASSED r2e_tests.test_1::Default404Test::test_404\nPASSED r2e_tests.test_1::DefaultHandlerArgumentsTest::test_403\nPASSED r2e_tests.test_1::DefaultHostMatchingTest::test_default_host_matching\nPASSED r2e_tests.test_1::ErrorHandlerXSRFTest::test_404_xsrf\nPASSED r2e_tests.test_1::ErrorHandlerXSRFTest::test_error_xsrf\nPASSED r2e_tests.test_1::ErrorResponseTest::test_default\nPASSED r2e_tests.test_1::ErrorResponseTest::test_failed_write_error\nPASSED r2e_tests.test_1::ErrorResponseTest::test_write_error\nPASSED r2e_tests.test_1::ExceptionHandlerTest::test_http_error\nPASSED r2e_tests.test_1::ExceptionHandlerTest::test_known_error\nPASSED r2e_tests.test_1::ExceptionHandlerTest::test_unknown_error\nFAILED r2e_tests.test_1::FinalReturnTest::test_finish_method_return_future\nPASSED r2e_tests.test_1::FinalReturnTest::test_render_method_return_future\nPASSED r2e_tests.test_1::FinishExceptionTest::test_finish_exception\nPASSED r2e_tests.test_1::FinishInPrepareTest::test_finish_in_prepare\nPASSED r2e_tests.test_1::GetArgumentErrorTest::test_catch_error\nPASSED r2e_tests.test_1::GetCurrentUserTest::test_get_current_user_from_ui_module_is_lazy\nPASSED r2e_tests.test_1::GetCurrentUserTest::test_get_current_user_from_ui_module_works\nSKIPPED r2e_tests.test_1::GetCurrentUserTest::test_get_current_user_is_lazy\nPASSED r2e_tests.test_1::GetCurrentUserTest::test_get_current_user_works\nPASSED r2e_tests.test_1::GzipTestCase::test_gzip\nPASSED r2e_tests.test_1::GzipTestCase::test_gzip_not_requested\nFAILED r2e_tests.test_1::GzipTestCase::test_gzip_static\nPASSED r2e_tests.test_1::GzipTestCase::test_vary_already_present\nPASSED r2e_tests.test_1::GzipTestCase::test_vary_already_present_multiple\nPASSED r2e_tests.test_1::HTTPErrorTest::test_copy\nPASSED r2e_tests.test_1::HandlerByNameTest::test_handler_by_name\nPASSED r2e_tests.test_1::Header204Test::test_204_headers\nPASSED r2e_tests.test_1::Header304Test::test_304_headers\nPASSED r2e_tests.test_1::HostMatchingTest::test_host_matching\nPASSED r2e_tests.test_1::IncorrectContentLengthTest::test_content_length_too_high\nPASSED r2e_tests.test_1::IncorrectContentLengthTest::test_content_length_too_low\nPASSED r2e_tests.test_1::NamedURLSpecGroupsTest::test_named_urlspec_groups\nPASSED r2e_tests.test_1::NativeStreamingRequestFlowControlTest::test_flow_control_chunked_body\nPASSED r2e_tests.test_1::NativeStreamingRequestFlowControlTest::test_flow_control_compressed_body\nPASSED r2e_tests.test_1::NativeStreamingRequestFlowControlTest::test_flow_control_fixed_body\nPASSED r2e_tests.test_1::NonWSGIWebTests::test_empty_flush\nPASSED r2e_tests.test_1::PatchMethodTest::test_other\nPASSED r2e_tests.test_1::PatchMethodTest::test_patch\nPASSED r2e_tests.test_1::PathArgsInPrepareTest::test_kw\nPASSED r2e_tests.test_1::PathArgsInPrepareTest::test_pos\nPASSED r2e_tests.test_1::RaiseWithReasonTest::test_httperror_str\nPASSED r2e_tests.test_1::RaiseWithReasonTest::test_httperror_str_from_httputil\nPASSED r2e_tests.test_1::RaiseWithReasonTest::test_raise_with_reason\nPASSED r2e_tests.test_1::RedirectHandlerTest::test_basic_redirect\nPASSED r2e_tests.test_1::RedirectHandlerTest::test_redirect_pattern\nPASSED r2e_tests.test_1::RedirectHandlerTest::test_redirect_with_appending_argument\nPASSED r2e_tests.test_1::RedirectHandlerTest::test_redirect_with_argument\nPASSED r2e_tests.test_1::RequestEncodingTest::test_error\nPASSED r2e_tests.test_1::RequestEncodingTest::test_group_encoding\nPASSED r2e_tests.test_1::RequestEncodingTest::test_group_question_mark\nPASSED r2e_tests.test_1::RequestEncodingTest::test_slashes\nPASSED r2e_tests.test_1::RequestSummaryTest::test_missing_remote_ip\nPASSED r2e_tests.test_1::SecureCookieV1Test::test_arbitrary_bytes\nPASSED r2e_tests.test_1::SecureCookieV1Test::test_cookie_tampering_future_timestamp\nPASSED r2e_tests.test_1::SecureCookieV1Test::test_round_trip\nPASSED r2e_tests.test_1::SecureCookieV2Test::test_key_version_increment_version\nPASSED r2e_tests.test_1::SecureCookieV2Test::test_key_version_invalidate_version\nPASSED r2e_tests.test_1::SecureCookieV2Test::test_key_version_roundtrip\nPASSED r2e_tests.test_1::SecureCookieV2Test::test_key_version_roundtrip_differing_version\nPASSED r2e_tests.test_1::SecureCookieV2Test::test_round_trip\nPASSED r2e_tests.test_1::SetLazyPropertiesTest::test_set_properties\nPASSED r2e_tests.test_1::SignedValueTest::test_expired\nPASSED r2e_tests.test_1::SignedValueTest::test_key_version_retrieval\nPASSED r2e_tests.test_1::SignedValueTest::test_key_versioning_invalid_key\nPASSED r2e_tests.test_1::SignedValueTest::test_key_versioning_read_write_default_key\nPASSED r2e_tests.test_1::SignedValueTest::test_key_versioning_read_write_non_default_key\nPASSED r2e_tests.test_1::SignedValueTest::test_known_values\nPASSED r2e_tests.test_1::SignedValueTest::test_name_swap\nPASSED r2e_tests.test_1::SignedValueTest::test_non_ascii\nPASSED r2e_tests.test_1::SignedValueTest::test_payload_tampering\nPASSED r2e_tests.test_1::SignedValueTest::test_signature_tampering\nFAILED r2e_tests.test_1::StaticDefaultFilenameTest::test_static_default_filename\nFAILED r2e_tests.test_1::StaticDefaultFilenameTest::test_static_default_redirect\nFAILED r2e_tests.test_1::StaticFileTest::test_absolute_static_url\nPASSED r2e_tests.test_1::StaticFileTest::test_absolute_version_exclusion\nPASSED r2e_tests.test_1::StaticFileTest::test_include_host_override\nPASSED r2e_tests.test_1::StaticFileTest::test_path_traversal_protection\nPASSED r2e_tests.test_1::StaticFileTest::test_relative_version_exclusion\nFAILED r2e_tests.test_1::StaticFileTest::test_root_static_path\nERROR r2e_tests.test_1::StaticFileTest::test_static_304_etag_modified_bug\nERROR r2e_tests.test_1::StaticFileTest::test_static_304_if_modified_since\nERROR r2e_tests.test_1::StaticFileTest::test_static_304_if_none_match\nPASSED r2e_tests.test_1::StaticFileTest::test_static_404\nFAILED r2e_tests.test_1::StaticFileTest::test_static_compressed_files\nFAILED r2e_tests.test_1::StaticFileTest::test_static_etag\nFAILED r2e_tests.test_1::StaticFileTest::test_static_files\nFAILED r2e_tests.test_1::StaticFileTest::test_static_head\nFAILED r2e_tests.test_1::StaticFileTest::test_static_head_range\nFAILED r2e_tests.test_1::StaticFileTest::test_static_if_modified_since_pre_epoch\nERROR r2e_tests.test_1::StaticFileTest::test_static_if_modified_since_time_zone\nFAILED r2e_tests.test_1::StaticFileTest::test_static_invalid_range\nFAILED r2e_tests.test_1::StaticFileTest::test_static_range_if_none_match\nFAILED r2e_tests.test_1::StaticFileTest::test_static_unsatisfiable_range_end_less_than_start\nFAILED r2e_tests.test_1::StaticFileTest::test_static_unsatisfiable_range_invalid_start\nFAILED r2e_tests.test_1::StaticFileTest::test_static_unsatisfiable_range_zero_suffix\nFAILED r2e_tests.test_1::StaticFileTest::test_static_url\nFAILED r2e_tests.test_1::StaticFileTest::test_static_with_range\nFAILED r2e_tests.test_1::StaticFileTest::test_static_with_range_end_edge\nFAILED r2e_tests.test_1::StaticFileTest::test_static_with_range_full_file\nFAILED r2e_tests.test_1::StaticFileTest::test_static_with_range_full_past_end\nFAILED r2e_tests.test_1::StaticFileTest::test_static_with_range_neg_end\nFAILED r2e_tests.test_1::StaticFileTest::test_static_with_range_neg_past_start\nFAILED r2e_tests.test_1::StaticFileTest::test_static_with_range_partial_past_end\nFAILED r2e_tests.test_1::StaticFileWithPathTest::test_serve\nPASSED r2e_tests.test_1::StatusReasonTest::test_status\nPASSED r2e_tests.test_1::StreamingRequestBodyTest::test_close_during_upload\nPASSED r2e_tests.test_1::StreamingRequestBodyTest::test_early_return\nPASSED r2e_tests.test_1::StreamingRequestBodyTest::test_early_return_with_data\nPASSED r2e_tests.test_1::StreamingRequestBodyTest::test_streaming_body\nPASSED r2e_tests.test_1::UIMethodUIModuleTest::test_ui_method\nPASSED r2e_tests.test_1::URLSpecReverseTest::test_non_reversible\nPASSED r2e_tests.test_1::URLSpecReverseTest::test_reverse\nPASSED r2e_tests.test_1::URLSpecReverseTest::test_reverse_arguments\nPASSED r2e_tests.test_1::UnimplementedHTTPMethodsTest::test_unimplemented_standard_methods\nPASSED r2e_tests.test_1::UnimplementedNonStandardMethodsTest::test_unimplemented_other\nPASSED r2e_tests.test_1::UnimplementedNonStandardMethodsTest::test_unimplemented_patch\nPASSED r2e_tests.test_1::WSGISafeWebTest::test_decode_argument\nPASSED r2e_tests.test_1::WSGISafeWebTest::test_decode_argument_invalid_unicode\nPASSED r2e_tests.test_1::WSGISafeWebTest::test_decode_argument_plus\nPASSED r2e_tests.test_1::WSGISafeWebTest::test_get_argument\nPASSED r2e_tests.test_1::WSGISafeWebTest::test_get_body_arguments\nPASSED r2e_tests.test_1::WSGISafeWebTest::test_get_query_arguments\nPASSED r2e_tests.test_1::WSGISafeWebTest::test_header_injection\nPASSED r2e_tests.test_1::WSGISafeWebTest::test_multi_header\nPASSED r2e_tests.test_1::WSGISafeWebTest::test_no_gzip\nPASSED r2e_tests.test_1::WSGISafeWebTest::test_optional_path\nPASSED r2e_tests.test_1::WSGISafeWebTest::test_redirect\nPASSED r2e_tests.test_1::WSGISafeWebTest::test_reverse_url\nPASSED r2e_tests.test_1::WSGISafeWebTest::test_types\nPASSED r2e_tests.test_1::WSGISafeWebTest::test_uimodule_resources\nPASSED r2e_tests.test_1::WSGISafeWebTest::test_uimodule_unescaped\nPASSED r2e_tests.test_1::WSGISafeWebTest::test_web_redirect\nPASSED r2e_tests.test_1::WSGISafeWebTest::test_web_redirect_double_slash\nPASSED r2e_tests.test_1::XSRFCookieKwargsTest::test_xsrf_httponly\nPASSED r2e_tests.test_1::XSRFTest::test_cross_user\nPASSED r2e_tests.test_1::XSRFTest::test_distinct_tokens\nPASSED r2e_tests.test_1::XSRFTest::test_refresh_token\nPASSED r2e_tests.test_1::XSRFTest::test_versioning\nPASSED r2e_tests.test_1::XSRFTest::test_xsrf_fail_argument_invalid_format\nPASSED r2e_tests.test_1::XSRFTest::test_xsrf_fail_body_no_cookie\nPASSED r2e_tests.test_1::XSRFTest::test_xsrf_fail_cookie_invalid_format\nPASSED r2e_tests.test_1::XSRFTest::test_xsrf_fail_cookie_no_body\nPASSED r2e_tests.test_1::XSRFTest::test_xsrf_fail_no_token\nPASSED r2e_tests.test_1::XSRFTest::test_xsrf_success_header\nPASSED r2e_tests.test_1::XSRFTest::test_xsrf_success_non_hex_token\nPASSED r2e_tests.test_1::XSRFTest::test_xsrf_success_post_body\nPASSED r2e_tests.test_1::XSRFTest::test_xsrf_success_query_string\nPASSED r2e_tests.test_1::XSRFTest::test_xsrf_success_short_token\n=================== 26 failed, 4 errors, 153 passed, 1 skipped in 0.52s ===================\n",
    "old_commit_res_stderr": "WARNING:tornado.access:405 HEAD / (127.0.0.1) 0.36ms\nERROR:tornado.access:500 GET / (127.0.0.1) 0.26ms\nWARNING:tornado.access:404 GET / (127.0.0.1) 0.19ms\nWARNING:tornado.access:404 GET / (127.0.0.1) 0.22ms\nWARNING:tornado.access:403 GET / (127.0.0.1) 0.21ms\nWARNING:tornado.access:404 GET /baz (127.0.0.1) 0.23ms\nWARNING:tornado.access:404 GET /foo (127.0.0.1) 0.22ms\nWARNING:tornado.access:404 POST /404 (127.0.0.1) 0.22ms\nWARNING:tornado.access:417 POST /error (127.0.0.1) 0.25ms\nERROR:tornado.access:500 GET /default (127.0.0.1) 0.28ms\nERROR:tornado.access:503 GET /default?status=503 (127.0.0.1) 0.24ms\nWARNING:tornado.access:435 GET /default?status=435 (127.0.0.1) 0.36ms\nERROR:tornado.access:500 GET /failed_write_error (127.0.0.1) 0.29ms\nERROR:tornado.access:500 GET /write_error (127.0.0.1) 0.30ms\nERROR:tornado.access:503 GET /write_error?status=503 (127.0.0.1) 0.22ms\nWARNING:tornado.access:410 GET /?exc=http (127.0.0.1) 0.28ms\nWARNING:tornado.access:403 GET /?exc=permission (127.0.0.1) 0.29ms\nERROR:tornado.access:500 GET /?exc=zero (127.0.0.1) 0.28ms\nWARNING:tornado.access:401 GET / (127.0.0.1) 0.22ms\nWARNING:tornado.access:401 GET /?finish_value=1 (127.0.0.1) 0.25ms\nWARNING:tornado.access:404 GET /robots.txt (127.0.0.1) 0.32ms\nWARNING:tornado.access:404 GET /bar (127.0.0.1) 0.22ms\nWARNING:tornado.access:404 GET /baz (127.0.0.1) 0.22ms\nERROR:tornado.access:682 GET / (127.0.0.1) 0.33ms\nWARNING:tornado.access:400 GET /group/?arg=%25%e9 (127.0.0.1) 0.49ms\nWARNING:tornado.access:404 GET /static/dir/ (127.0.0.1) 0.29ms\nWARNING:tornado.access:404 GET /static/dir (127.0.0.1) 0.33ms\nERROR:tornado.general:Could not open static file '/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/static/robots.txt'\nWARNING:tornado.access:403 HEAD /static/../static_foo.txt (127.0.0.1) 0.28ms\nWARNING:tornado.access:403 GET /static/../static_foo.txt (127.0.0.1) 0.27ms\nWARNING:tornado.access:404 HEAD /root_static/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/static/robots.txt (127.0.0.1) 0.26ms\nWARNING:tornado.access:404 GET /root_static/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/r2e_tests/static/robots.txt (127.0.0.1) 0.26ms\nWARNING:tornado.access:404 HEAD /static/robots.txt (127.0.0.1) 0.38ms\nWARNING:tornado.access:404 GET /static/robots.txt (127.0.0.1) 0.25ms\nWARNING:tornado.access:404 HEAD /static/robots.txt (127.0.0.1) 0.26ms\nWARNING:tornado.access:404 GET /static/robots.txt (127.0.0.1) 0.24ms\nWARNING:tornado.access:404 HEAD /static/robots.txt (127.0.0.1) 0.26ms\nWARNING:tornado.access:404 GET /static/robots.txt (127.0.0.1) 0.30ms\nWARNING:tornado.access:404 HEAD /static/blarg (127.0.0.1) 0.28ms\nWARNING:tornado.access:404 GET /static/blarg (127.0.0.1) 0.25ms\nWARNING:tornado.access:404 GET /static/sample.xml.gz (127.0.0.1) 0.29ms\nWARNING:tornado.access:404 HEAD /static/robots.txt (127.0.0.1) 0.27ms\nWARNING:tornado.access:404 GET /static/robots.txt (127.0.0.1) 0.27ms\nWARNING:tornado.access:404 GET /robots.txt (127.0.0.1) 0.27ms\nWARNING:tornado.access:404 HEAD /static/robots.txt (127.0.0.1) 0.27ms\nWARNING:tornado.access:404 HEAD /static/robots.txt (127.0.0.1) 0.30ms\nWARNING:tornado.access:404 HEAD /static/robots.txt (127.0.0.1) 0.28ms\nWARNING:tornado.access:404 GET /static/robots.txt (127.0.0.1) 0.26ms\nWARNING:tornado.access:404 HEAD /static/robots.txt (127.0.0.1) 0.26ms\nWARNING:tornado.access:404 GET /static/robots.txt (127.0.0.1) 0.27ms\nWARNING:tornado.access:404 HEAD /static/robots.txt (127.0.0.1) 0.31ms\nWARNING:tornado.access:404 GET /static/robots.txt (127.0.0.1) 0.25ms\nWARNING:tornado.access:404 HEAD /static/robots.txt (127.0.0.1) 0.27ms\nWARNING:tornado.access:404 GET /static/robots.txt (127.0.0.1) 0.28ms\nWARNING:tornado.access:404 HEAD /static/robots.txt (127.0.0.1) 0.27ms\nWARNING:tornado.access:404 GET /static/robots.txt (127.0.0.1) 0.26ms\nWARNING:tornado.access:404 HEAD /static/robots.txt (127.0.0.1) 0.37ms\nWARNING:tornado.access:404 GET /static/robots.txt (127.0.0.1) 0.26ms\nWARNING:tornado.access:404 HEAD /static/robots.txt (127.0.0.1) 0.28ms\nWARNING:tornado.access:404 GET /static/robots.txt (127.0.0.1) 0.25ms\nWARNING:tornado.access:404 HEAD /static/robots.txt (127.0.0.1) 0.28ms\nWARNING:tornado.access:404 GET /static/robots.txt (127.0.0.1) 0.26ms\nWARNING:tornado.access:404 HEAD /static/robots.txt (127.0.0.1) 0.28ms\nWARNING:tornado.access:404 GET /static/robots.txt (127.0.0.1) 0.26ms\nWARNING:tornado.access:404 HEAD /static/robots.txt (127.0.0.1) 0.27ms\nWARNING:tornado.access:404 GET /static/robots.txt (127.0.0.1) 0.31ms\nWARNING:tornado.access:404 HEAD /static/robots.txt (127.0.0.1) 0.28ms\nWARNING:tornado.access:404 GET /static/robots.txt (127.0.0.1) 0.28ms\nWARNING:tornado.access:404 HEAD /static/robots.txt (127.0.0.1) 0.26ms\nWARNING:tornado.access:404 GET /static/robots.txt (127.0.0.1) 0.38ms\nWARNING:tornado.access:404 HEAD /static/robots.txt (127.0.0.1) 0.27ms\nWARNING:tornado.access:404 GET /static/robots.txt (127.0.0.1) 0.27ms\nWARNING:tornado.access:404 GET /foo/utf8.html (127.0.0.1) 0.34ms\nERROR:tornado.access:682 GET /?code=682&reason=Bar (127.0.0.1) 0.20ms\nERROR:tornado.access:682 GET /?code=682 (127.0.0.1) 0.20ms\nWARNING:tornado.access:401 GET /early_return (127.0.0.1) 0.20ms\nWARNING:tornado.access:401 GET /early_return (127.0.0.1) 0.20ms\nWARNING:tornado.access:405 HEAD / (127.0.0.1) 0.26ms\nWARNING:tornado.access:405 GET / (127.0.0.1) 0.23ms\nWARNING:tornado.access:405 DELETE / (127.0.0.1) 0.20ms\nWARNING:tornado.access:405 OPTIONS / (127.0.0.1) 0.22ms\nWARNING:tornado.access:405 POST / (127.0.0.1) 0.23ms\nWARNING:tornado.access:405 PUT / (127.0.0.1) 0.21ms\nWARNING:tornado.access:405 OTHER / (127.0.0.1) 0.24ms\nWARNING:tornado.access:405 PATCH / (127.0.0.1) 0.23ms\nWARNING:tornado.access:400 GET /typecheck/invalid%FF (127.0.0.1) 0.28ms\nWARNING:tornado.access:400 GET /typecheck/invalid?foo=%FF (127.0.0.1) 0.33ms\nWARNING:tornado.access:403 POST / (127.0.0.1) 0.39ms\nWARNING:tornado.access:403 POST / (127.0.0.1) 0.33ms\nWARNING:tornado.access:403 POST / (127.0.0.1) 0.37ms\nWARNING:tornado.access:403 POST / (127.0.0.1) 0.34ms\nWARNING:tornado.access:403 POST / (127.0.0.1) 0.34ms\nWARNING:tornado.access:403 POST / (127.0.0.1) 0.28ms\nWARNING:tornado.access:403 POST / (127.0.0.1) 0.25ms\nERROR:asyncio:Task was destroyed but it is pending!\ntask: <Task pending name='Task-251' coro=<RequestHandler._execute() running at /home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/web.py:1702> wait_for=<Future pending cb=[coroutine.<locals>.wrapper.<locals>.<lambda>() at /home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/gen.py:237, <TaskWakeupMethWrapper object at 0x7428f6653070>()]> cb=[_HandlerDelegate.execute.<locals>.<lambda>() at /home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_9e4ae9e9cfa64dfbf47573ef3e0996da3234d0b9/tornado/web.py:2330]>\n"
}
tornado/http1connection.py tornado/test/web_test.py
We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
[ISSUE]
**Title:** `finish()` Method Does Not Return a Completed Future, Causing Asynchronous Operations to Hang

**Description:**
When using the `finish()` method in the web module, it is expected to return a `Future` that is marked as done once the write operation completes. However, currently, the returned `Future` remains incomplete. This prevents any dependent asynchronous tasks from proceeding as they await the completion of the `Future`.

**Example Code:**
```python
class FinalReturnTest(WebTestCase):

    class FinishHandler(RequestHandler):

        @gen.coroutine
        def get(self):
            self.final_return = self.finish()
            yield self.final_return

    def test_finish_method_returns_completed_future(self):
        response = self.fetch('/finish')
        self.assertEqual(response.code, 200)
        self.assertIsInstance(self.final_return, Future)
        self.assertTrue(self.final_return.done())
```

**Expected Behavior:**
The `Future` returned by the `finish()` method should be marked as done (`self.final_return.done()` returns `True`) after the response is successfully written. This ensures that any asynchronous operations depending on this `Future` can proceed without issues.

**Actual Behavior:**
The `Future` returned by `finish()` is not marked as done (`self.final_return.done()` returns `False`), causing asynchronous operations that await this `Future` to hang indefinitely.

[/ISSUE]
--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.

--- BEGIN FILE ---
```
### tornado/http1connection.py
#
# Copyright 2014 Facebook
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

"""Client and server implementations of HTTP/1.x.

.. versionadded:: 4.0
"""

import asyncio
import logging
import re
import types

from tornado.concurrent import (
    Future,
    future_add_done_callback,
    future_set_result_unless_cancelled,
)
from tornado.escape import native_str, utf8
from tornado import gen
from tornado import httputil
from tornado import iostream
from tornado.log import gen_log, app_log
from tornado.util import GzipDecompressor


from typing import cast, Optional, Type, Awaitable, Callable, Union, Tuple


class _QuietException(Exception):
    def __init__(self) -> None:
        pass


class _ExceptionLoggingContext(object):
    """Used with the ``with`` statement when calling delegate methods to
    log any exceptions with the given logger.  Any exceptions caught are
    converted to _QuietException
    """

    def __init__(self, logger: logging.Logger) -> None:
        self.logger = logger

    def __enter__(self) -> None:
        pass

    def __exit__(
        self,
        typ: Optional[Type[BaseException]],
        value: Optional[BaseException],
        tb: types.TracebackType,
    ) -> None:
        if value is not None:
            assert typ is not None
            self.logger.error("Uncaught exception", exc_info=(typ, value, tb))
            raise _QuietException


class HTTP1ConnectionParameters(object):
    """Parameters for `.HTTP1Connection` and `.HTTP1ServerConnection`.
    """

    def __init__(
        self,
        no_keep_alive: bool = False,
        chunk_size: int = None,
        max_header_size: int = None,
        header_timeout: float = None,
        max_body_size: int = None,
        body_timeout: float = None,
        decompress: bool = False,
    ) -> None:
        """
        :arg bool no_keep_alive: If true, always close the connection after
            one request.
        :arg int chunk_size: how much data to read into memory at once
        :arg int max_header_size:  maximum amount of data for HTTP headers
        :arg float header_timeout: how long to wait for all headers (seconds)
        :arg int max_body_size: maximum amount of data for body
        :arg float body_timeout: how long to wait while reading body (seconds)
        :arg bool decompress: if true, decode incoming
            ``Content-Encoding: gzip``
        """
        self.no_keep_alive = no_keep_alive
        self.chunk_size = chunk_size or 65536
        self.max_header_size = max_header_size or 65536
        self.header_timeout = header_timeout
        self.max_body_size = max_body_size
        self.body_timeout = body_timeout
        self.decompress = decompress


class HTTP1Connection(httputil.HTTPConnection):
    """Implements the HTTP/1.x protocol.

    This class can be on its own for clients, or via `HTTP1ServerConnection`
    for servers.
    """

    def __init__(
        self,
        stream: iostream.IOStream,
        is_client: bool,
        params: HTTP1ConnectionParameters = None,
        context: object = None,
    ) -> None:
        """
        :arg stream: an `.IOStream`
        :arg bool is_client: client or server
        :arg params: a `.HTTP1ConnectionParameters` instance or ``None``
        :arg context: an opaque application-defined object that can be accessed
            as ``connection.context``.
        """
        self.is_client = is_client
        self.stream = stream
        if params is None:
            params = HTTP1ConnectionParameters()
        self.params = params
        self.context = context
        self.no_keep_alive = params.no_keep_alive
        # The body limits can be altered by the delegate, so save them
        # here instead of just referencing self.params later.
        self._max_body_size = self.params.max_body_size or self.stream.max_buffer_size
        self._body_timeout = self.params.body_timeout
        # _write_finished is set to True when finish() has been called,
        # i.e. there will be no more data sent.  Data may still be in the
        # stream's write buffer.
        self._write_finished = False
        # True when we have read the entire incoming body.
        self._read_finished = False
        # _finish_future resolves when all data has been written and flushed
        # to the IOStream.
        self._finish_future = Future()  # type: Future[None]
        # If true, the connection should be closed after this request
        # (after the response has been written in the server side,
        # and after it has been read in the client)
        self._disconnect_on_finish = False
        self._clear_callbacks()
        # Save the start lines after we read or write them; they
        # affect later processing (e.g. 304 responses and HEAD methods
        # have content-length but no bodies)
        self._request_start_line = None  # type: Optional[httputil.RequestStartLine]
        self._response_start_line = None  # type: Optional[httputil.ResponseStartLine]
        self._request_headers = None  # type: Optional[httputil.HTTPHeaders]
        # True if we are writing output with chunked encoding.
        self._chunking_output = False
        # While reading a body with a content-length, this is the
        # amount left to read.
        self._expected_content_remaining = None  # type: Optional[int]
        # A Future for our outgoing writes, returned by IOStream.write.
        self._pending_write = None  # type: Optional[Future[None]]

    def read_response(self, delegate: httputil.HTTPMessageDelegate) -> Awaitable[bool]:
        """Read a single HTTP response.

        Typical client-mode usage is to write a request using `write_headers`,
        `write`, and `finish`, and then call ``read_response``.

        :arg delegate: a `.HTTPMessageDelegate`

        Returns a `.Future` that resolves to a bool after the full response has
        been read. The result is true if the stream is still open.
        """
        if self.params.decompress:
            delegate = _GzipMessageDelegate(delegate, self.params.chunk_size)
        return self._read_message(delegate)

    async def _read_message(self, delegate: httputil.HTTPMessageDelegate) -> bool:
        need_delegate_close = False
        try:
            header_future = self.stream.read_until_regex(
                b"\r?\n\r?\n", max_bytes=self.params.max_header_size
            )
            if self.params.header_timeout is None:
                header_data = await header_future
            else:
                try:
                    header_data = await gen.with_timeout(
                        self.stream.io_loop.time() + self.params.header_timeout,
                        header_future,
                        quiet_exceptions=iostream.StreamClosedError,
                    )
                except gen.TimeoutError:
                    self.close()
                    return False
            start_line_str, headers = self._parse_headers(header_data)
            if self.is_client:
                resp_start_line = httputil.parse_response_start_line(start_line_str)
                self._response_start_line = resp_start_line
                start_line = (
                    resp_start_line
                )  # type: Union[httputil.RequestStartLine, httputil.ResponseStartLine]
                # TODO: this will need to change to support client-side keepalive
                self._disconnect_on_finish = False
            else:
                req_start_line = httputil.parse_request_start_line(start_line_str)
                self._request_start_line = req_start_line
                self._request_headers = headers
                start_line = req_start_line
                self._disconnect_on_finish = not self._can_keep_alive(
                    req_start_line, headers
                )
            need_delegate_close = True
            with _ExceptionLoggingContext(app_log):
                header_recv_future = delegate.headers_received(start_line, headers)
                if header_recv_future is not None:
                    await header_recv_future
            if self.stream is None:
                # We've been detached.
                need_delegate_close = False
                return False
            skip_body = False
            if self.is_client:
                assert isinstance(start_line, httputil.ResponseStartLine)
                if (
                    self._request_start_line is not None
                    and self._request_start_line.method == "HEAD"
                ):
                    skip_body = True
                code = start_line.code
                if code == 304:
                    # 304 responses may include the content-length header
                    # but do not actually have a body.
                    # http://tools.ietf.org/html/rfc7230#section-3.3
                    skip_body = True
                if code >= 100 and code < 200:
                    # 1xx responses should never indicate the presence of
                    # a body.
                    if "Content-Length" in headers or "Transfer-Encoding" in headers:
                        raise httputil.HTTPInputError(
                            "Response code %d cannot have body" % code
                        )
                    # TODO: client delegates will get headers_received twice
                    # in the case of a 100-continue.  Document or change?
                    await self._read_message(delegate)
            else:
                if headers.get("Expect") == "100-continue" and not self._write_finished:
                    self.stream.write(b"HTTP/1.1 100 (Continue)\r\n\r\n")
            if not skip_body:
                body_future = self._read_body(
                    resp_start_line.code if self.is_client else 0, headers, delegate
                )
                if body_future is not None:
                    if self._body_timeout is None:
                        await body_future
                    else:
                        try:
                            await gen.with_timeout(
                                self.stream.io_loop.time() + self._body_timeout,
                                body_future,
                                quiet_exceptions=iostream.StreamClosedError,
                            )
                        except gen.TimeoutError:
                            gen_log.info("Timeout reading body from %s", self.context)
                            self.stream.close()
                            return False
            self._read_finished = True
            if not self._write_finished or self.is_client:
                need_delegate_close = False
                with _ExceptionLoggingContext(app_log):
                    delegate.finish()
            # If we're waiting for the application to produce an asynchronous
            # response, and we're not detached, register a close callback
            # on the stream (we didn't need one while we were reading)
            if (
                not self._finish_future.done()
                and self.stream is not None
                and not self.stream.closed()
            ):
                self.stream.set_close_callback(self._on_connection_close)
                await self._finish_future
            if self.is_client and self._disconnect_on_finish:
                self.close()
            if self.stream is None:
                return False
        except httputil.HTTPInputError as e:
            gen_log.info("Malformed HTTP message from %s: %s", self.context, e)
            if not self.is_client:
                await self.stream.write(b"HTTP/1.1 400 Bad Request\r\n\r\n")
            self.close()
            return False
        finally:
            if need_delegate_close:
                with _ExceptionLoggingContext(app_log):
                    delegate.on_connection_close()
            header_future = None  # type: ignore
            self._clear_callbacks()
        return True

    def _clear_callbacks(self) -> None:
        """Clears the callback attributes.

        This allows the request handler to be garbage collected more
        quickly in CPython by breaking up reference cycles.
        """
        self._write_callback = None
        self._write_future = None  # type: Optional[Future[None]]
        self._close_callback = None  # type: Optional[Callable[[], None]]
        if self.stream is not None:
            self.stream.set_close_callback(None)

    def set_close_callback(self, callback: Optional[Callable[[], None]]) -> None:
        """Sets a callback that will be run when the connection is closed.

        Note that this callback is slightly different from
        `.HTTPMessageDelegate.on_connection_close`: The
        `.HTTPMessageDelegate` method is called when the connection is
        closed while recieving a message. This callback is used when
        there is not an active delegate (for example, on the server
        side this callback is used if the client closes the connection
        after sending its request but before receiving all the
        response.
        """
        self._close_callback = callback

    def _on_connection_close(self) -> None:
        # Note that this callback is only registered on the IOStream
        # when we have finished reading the request and are waiting for
        # the application to produce its response.
        if self._close_callback is not None:
            callback = self._close_callback
            self._close_callback = None
            callback()
        if not self._finish_future.done():
            future_set_result_unless_cancelled(self._finish_future, None)
        self._clear_callbacks()

    def close(self) -> None:
        if self.stream is not None:
            self.stream.close()
        self._clear_callbacks()
        if not self._finish_future.done():
            future_set_result_unless_cancelled(self._finish_future, None)

    def detach(self) -> iostream.IOStream:
        """Take control of the underlying stream.

        Returns the underlying `.IOStream` object and stops all further
        HTTP processing.  May only be called during
        `.HTTPMessageDelegate.headers_received`.  Intended for implementing
        protocols like websockets that tunnel over an HTTP handshake.
        """
        self._clear_callbacks()
        stream = self.stream
        self.stream = None  # type: ignore
        if not self._finish_future.done():
            future_set_result_unless_cancelled(self._finish_future, None)
        return stream

    def set_body_timeout(self, timeout: float) -> None:
        """Sets the body timeout for a single request.

        Overrides the value from `.HTTP1ConnectionParameters`.
        """
        self._body_timeout = timeout

    def set_max_body_size(self, max_body_size: int) -> None:
        """Sets the body size limit for a single request.

        Overrides the value from `.HTTP1ConnectionParameters`.
        """
        self._max_body_size = max_body_size

    def write_headers(
        self,
        start_line: Union[httputil.RequestStartLine, httputil.ResponseStartLine],
        headers: httputil.HTTPHeaders,
        chunk: bytes = None,
    ) -> "Future[None]":
        """Implements `.HTTPConnection.write_headers`."""
        lines = []
        if self.is_client:
            assert isinstance(start_line, httputil.RequestStartLine)
            self._request_start_line = start_line
            lines.append(utf8("%s %s HTTP/1.1" % (start_line[0], start_line[1])))
            # Client requests with a non-empty body must have either a
            # Content-Length or a Transfer-Encoding.
            self._chunking_output = (
                start_line.method in ("POST", "PUT", "PATCH")
                and "Content-Length" not in headers
                and "Transfer-Encoding" not in headers
            )
        else:
            assert isinstance(start_line, httputil.ResponseStartLine)
            assert self._request_start_line is not None
            assert self._request_headers is not None
            self._response_start_line = start_line
            lines.append(utf8("HTTP/1.1 %d %s" % (start_line[1], start_line[2])))
            self._chunking_output = (
                # TODO: should this use
                # self._request_start_line.version or
                # start_line.version?
                self._request_start_line.version == "HTTP/1.1"
                # 1xx, 204 and 304 responses have no body (not even a zero-length
                # body), and so should not have either Content-Length or
                # Transfer-Encoding headers.
                and start_line.code not in (204, 304)
                and (start_line.code < 100 or start_line.code >= 200)
                # No need to chunk the output if a Content-Length is specified.
                and "Content-Length" not in headers
                # Applications are discouraged from touching Transfer-Encoding,
                # but if they do, leave it alone.
                and "Transfer-Encoding" not in headers
            )
            # If connection to a 1.1 client will be closed, inform client
            if (
                self._request_start_line.version == "HTTP/1.1"
                and self._disconnect_on_finish
            ):
                headers["Connection"] = "close"
            # If a 1.0 client asked for keep-alive, add the header.
            if (
                self._request_start_line.version == "HTTP/1.0"
                and self._request_headers.get("Connection", "").lower() == "keep-alive"
            ):
                headers["Connection"] = "Keep-Alive"
        if self._chunking_output:
            headers["Transfer-Encoding"] = "chunked"
        if not self.is_client and (
            self._request_start_line.method == "HEAD"
            or cast(httputil.ResponseStartLine, start_line).code == 304
        ):
            self._expected_content_remaining = 0
        elif "Content-Length" in headers:
            self._expected_content_remaining = int(headers["Content-Length"])
        else:
            self._expected_content_remaining = None
        # TODO: headers are supposed to be of type str, but we still have some
        # cases that let bytes slip through. Remove these native_str calls when those
        # are fixed.
        header_lines = (
            native_str(n) + ": " + native_str(v) for n, v in headers.get_all()
        )
        lines.extend(l.encode("latin1") for l in header_lines)
        for line in lines:
            if b"\n" in line:
                raise ValueError("Newline in header: " + repr(line))
        future = None
        if self.stream.closed():
            future = self._write_future = Future()
            future.set_exception(iostream.StreamClosedError())
            future.exception()
        else:
            future = self._write_future = Future()
            data = b"\r\n".join(lines) + b"\r\n\r\n"
            if chunk:
                data += self._format_chunk(chunk)
            self._pending_write = self.stream.write(data)
            future_add_done_callback(self._pending_write, self._on_write_complete)
        return future

    def _format_chunk(self, chunk: bytes) -> bytes:
        if self._expected_content_remaining is not None:
            self._expected_content_remaining -= len(chunk)
            if self._expected_content_remaining < 0:
                # Close the stream now to stop further framing errors.
                self.stream.close()
                raise httputil.HTTPOutputError(
                    "Tried to write more data than Content-Length"
                )
        if self._chunking_output and chunk:
            # Don't write out empty chunks because that means END-OF-STREAM
            # with chunked encoding
            return utf8("%x" % len(chunk)) + b"\r\n" + chunk + b"\r\n"
        else:
            return chunk

    def write(self, chunk: bytes) -> "Future[None]":
        """Implements `.HTTPConnection.write`.

        For backwards compatibility it is allowed but deprecated to
        skip `write_headers` and instead call `write()` with a
        pre-encoded header block.
        """
        future = None
        if self.stream.closed():
            future = self._write_future = Future()
            self._write_future.set_exception(iostream.StreamClosedError())
            self._write_future.exception()
        else:
            future = self._write_future = Future()
            self._pending_write = self.stream.write(self._format_chunk(chunk))
            self._pending_write.add_done_callback(self._on_write_complete)
        return future

    def finish(self) -> None:
        """Implements `.HTTPConnection.finish`."""
        if (
            self._expected_content_remaining is not None
            and self._expected_content_remaining != 0
            and not self.stream.closed()
        ):
            self.stream.close()
            raise httputil.HTTPOutputError(
                "Tried to write %d bytes less than Content-Length"
                % self._expected_content_remaining
            )
        if self._chunking_output:
            if not self.stream.closed():
                self._pending_write = self.stream.write(b"0\r\n\r\n")
                self._pending_write.add_done_callback(self._on_write_complete)
        self._write_finished = True
        # If the app finished the request while we're still reading,
        # divert any remaining data away from the delegate and
        # close the connection when we're done sending our response.
        # Closing the connection is the only way to avoid reading the
        # whole input body.
        if not self._read_finished:
            self._disconnect_on_finish = True
        # No more data is coming, so instruct TCP to send any remaining
        # data immediately instead of waiting for a full packet or ack.
        self.stream.set_nodelay(True)
        if self._pending_write is None:
            self._finish_request(None)
        else:
            future_add_done_callback(self._pending_write, self._finish_request)

    def _on_write_complete(self, future: "Future[None]") -> None:
        exc = future.exception()
        if exc is not None and not isinstance(exc, iostream.StreamClosedError):
            future.result()
        if self._write_callback is not None:
            callback = self._write_callback
            self._write_callback = None
            self.stream.io_loop.add_callback(callback)
        if self._write_future is not None:
            future = self._write_future
            self._write_future = None
            future_set_result_unless_cancelled(future, None)

    def _can_keep_alive(
        self, start_line: httputil.RequestStartLine, headers: httputil.HTTPHeaders
    ) -> bool:
        if self.params.no_keep_alive:
            return False
        connection_header = headers.get("Connection")
        if connection_header is not None:
            connection_header = connection_header.lower()
        if start_line.version == "HTTP/1.1":
            return connection_header != "close"
        elif (
            "Content-Length" in headers
            or headers.get("Transfer-Encoding", "").lower() == "chunked"
            or getattr(start_line, "method", None) in ("HEAD", "GET")
        ):
            # start_line may be a request or response start line; only
            # the former has a method attribute.
            return connection_header == "keep-alive"
        return False

    def _finish_request(self, future: Optional["Future[None]"]) -> None:
        self._clear_callbacks()
        if not self.is_client and self._disconnect_on_finish:
            self.close()
            return
        # Turn Nagle's algorithm back on, leaving the stream in its
        # default state for the next request.
        self.stream.set_nodelay(False)
        if not self._finish_future.done():
            future_set_result_unless_cancelled(self._finish_future, None)

    def _parse_headers(self, data: bytes) -> Tuple[str, httputil.HTTPHeaders]:
        # The lstrip removes newlines that some implementations sometimes
        # insert between messages of a reused connection.  Per RFC 7230,
        # we SHOULD ignore at least one empty line before the request.
        # http://tools.ietf.org/html/rfc7230#section-3.5
        data_str = native_str(data.decode("latin1")).lstrip("\r\n")
        # RFC 7230 section allows for both CRLF and bare LF.
        eol = data_str.find("\n")
        start_line = data_str[:eol].rstrip("\r")
        headers = httputil.HTTPHeaders.parse(data_str[eol:])
        return start_line, headers

    def _read_body(
        self,
        code: int,
        headers: httputil.HTTPHeaders,
        delegate: httputil.HTTPMessageDelegate,
    ) -> Optional[Awaitable[None]]:
        if "Content-Length" in headers:
            if "Transfer-Encoding" in headers:
                # Response cannot contain both Content-Length and
                # Transfer-Encoding headers.
                # http://tools.ietf.org/html/rfc7230#section-3.3.3
                raise httputil.HTTPInputError(
                    "Response with both Transfer-Encoding and Content-Length"
                )
            if "," in headers["Content-Length"]:
                # Proxies sometimes cause Content-Length headers to get
                # duplicated.  If all the values are identical then we can
                # use them but if they differ it's an error.
                pieces = re.split(r",\s*", headers["Content-Length"])
                if any(i != pieces[0] for i in pieces):
                    raise httputil.HTTPInputError(
                        "Multiple unequal Content-Lengths: %r"
                        % headers["Content-Length"]
                    )
                headers["Content-Length"] = pieces[0]

            try:
                content_length = int(headers["Content-Length"])  # type: Optional[int]
            except ValueError:
                # Handles non-integer Content-Length value.
                raise httputil.HTTPInputError(
                    "Only integer Content-Length is allowed: %s"
                    % headers["Content-Length"]
                )

            if cast(int, content_length) > self._max_body_size:
                raise httputil.HTTPInputError("Content-Length too long")
        else:
            content_length = None

        if code == 204:
            # This response code is not allowed to have a non-empty body,
            # and has an implicit length of zero instead of read-until-close.
            # http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.3
            if "Transfer-Encoding" in headers or content_length not in (None, 0):
                raise httputil.HTTPInputError(
                    "Response with code %d should not have body" % code
                )
            content_length = 0

        if content_length is not None:
            return self._read_fixed_body(content_length, delegate)
        if headers.get("Transfer-Encoding", "").lower() == "chunked":
            return self._read_chunked_body(delegate)
        if self.is_client:
            return self._read_body_until_close(delegate)
        return None

    async def _read_fixed_body(
        self, content_length: int, delegate: httputil.HTTPMessageDelegate
    ) -> None:
        while content_length > 0:
            body = await self.stream.read_bytes(
                min(self.params.chunk_size, content_length), partial=True
            )
            content_length -= len(body)
            if not self._write_finished or self.is_client:
                with _ExceptionLoggingContext(app_log):
                    ret = delegate.data_received(body)
                    if ret is not None:
                        await ret

    async def _read_chunked_body(self, delegate: httputil.HTTPMessageDelegate) -> None:
        # TODO: "chunk extensions" http://tools.ietf.org/html/rfc2616#section-3.6.1
        total_size = 0
        while True:
            chunk_len_str = await self.stream.read_until(b"\r\n", max_bytes=64)
            chunk_len = int(chunk_len_str.strip(), 16)
            if chunk_len == 0:
                crlf = await self.stream.read_bytes(2)
                if crlf != b"\r\n":
                    raise httputil.HTTPInputError(
                        "improperly terminated chunked request"
                    )
                return
            total_size += chunk_len
            if total_size > self._max_body_size:
                raise httputil.HTTPInputError("chunked body too large")
            bytes_to_read = chunk_len
            while bytes_to_read:
                chunk = await self.stream.read_bytes(
                    min(bytes_to_read, self.params.chunk_size), partial=True
                )
                bytes_to_read -= len(chunk)
                if not self._write_finished or self.is_client:
                    with _ExceptionLoggingContext(app_log):
                        ret = delegate.data_received(chunk)
                        if ret is not None:
                            await ret
            # chunk ends with \r\n
            crlf = await self.stream.read_bytes(2)
            assert crlf == b"\r\n"

    async def _read_body_until_close(
        self, delegate: httputil.HTTPMessageDelegate
    ) -> None:
        body = await self.stream.read_until_close()
        if not self._write_finished or self.is_client:
            with _ExceptionLoggingContext(app_log):
                ret = delegate.data_received(body)
                if ret is not None:
                    await ret


class _GzipMessageDelegate(httputil.HTTPMessageDelegate):
    """Wraps an `HTTPMessageDelegate` to decode ``Content-Encoding: gzip``.
    """

    def __init__(self, delegate: httputil.HTTPMessageDelegate, chunk_size: int) -> None:
        self._delegate = delegate
        self._chunk_size = chunk_size
        self._decompressor = None  # type: Optional[GzipDecompressor]

    def headers_received(
        self,
        start_line: Union[httputil.RequestStartLine, httputil.ResponseStartLine],
        headers: httputil.HTTPHeaders,
    ) -> Optional[Awaitable[None]]:
        if headers.get("Content-Encoding") == "gzip":
            self._decompressor = GzipDecompressor()
            # Downstream delegates will only see uncompressed data,
            # so rename the content-encoding header.
            # (but note that curl_httpclient doesn't do this).
            headers.add("X-Consumed-Content-Encoding", headers["Content-Encoding"])
            del headers["Content-Encoding"]
        return self._delegate.headers_received(start_line, headers)

    async def data_received(self, chunk: bytes) -> None:
        if self._decompressor:
            compressed_data = chunk
            while compressed_data:
                decompressed = self._decompressor.decompress(
                    compressed_data, self._chunk_size
                )
                if decompressed:
                    ret = self._delegate.data_received(decompressed)
                    if ret is not None:
                        await ret
                compressed_data = self._decompressor.unconsumed_tail
        else:
            ret = self._delegate.data_received(chunk)
            if ret is not None:
                await ret

    def finish(self) -> None:
        if self._decompressor is not None:
            tail = self._decompressor.flush()
            if tail:
                # The tail should always be empty: decompress returned
                # all that it can in data_received and the only
                # purpose of the flush call is to detect errors such
                # as truncated input. If we did legitimately get a new
                # chunk at this point we'd need to change the
                # interface to make finish() a coroutine.
                raise ValueError(
                    "decompressor.flush returned data; possile truncated input"
                )
        return self._delegate.finish()

    def on_connection_close(self) -> None:
        return self._delegate.on_connection_close()


class HTTP1ServerConnection(object):
    """An HTTP/1.x server."""

    def __init__(
        self,
        stream: iostream.IOStream,
        params: HTTP1ConnectionParameters = None,
        context: object = None,
    ) -> None:
        """
        :arg stream: an `.IOStream`
        :arg params: a `.HTTP1ConnectionParameters` or None
        :arg context: an opaque application-defined object that is accessible
            as ``connection.context``
        """
        self.stream = stream
        if params is None:
            params = HTTP1ConnectionParameters()
        self.params = params
        self.context = context
        self._serving_future = None  # type: Optional[Future[None]]

    async def close(self) -> None:
        """Closes the connection.

        Returns a `.Future` that resolves after the serving loop has exited.
        """
        self.stream.close()
        # Block until the serving loop is done, but ignore any exceptions
        # (start_serving is already responsible for logging them).
        assert self._serving_future is not None
        try:
            await self._serving_future
        except Exception:
            pass

    def start_serving(self, delegate: httputil.HTTPServerConnectionDelegate) -> None:
        """Starts serving requests on this connection.

        :arg delegate: a `.HTTPServerConnectionDelegate`
        """
        assert isinstance(delegate, httputil.HTTPServerConnectionDelegate)
        fut = gen.convert_yielded(self._server_request_loop(delegate))
        self._serving_future = fut
        # Register the future on the IOLoop so its errors get logged.
        self.stream.io_loop.add_future(fut, lambda f: f.result())

    async def _server_request_loop(
        self, delegate: httputil.HTTPServerConnectionDelegate
    ) -> None:
        try:
            while True:
                conn = HTTP1Connection(self.stream, False, self.params, self.context)
                request_delegate = delegate.start_request(self, conn)
                try:
                    ret = await conn.read_response(request_delegate)
                except (iostream.StreamClosedError, iostream.UnsatisfiableReadError):
                    return
                except _QuietException:
                    # This exception was already logged.
                    conn.close()
                    return
                except Exception:
                    gen_log.error("Uncaught exception", exc_info=True)
                    conn.close()
                    return
                if not ret:
                    return
                await asyncio.sleep(0)
        finally:
            delegate.on_close(self)

```
--- END FILE ---

Please first localize the bug based on the issue statement, and then generate *SEARCH/REPLACE* edits to fix the issue.

Every *SEARCH/REPLACE* edit must use this format:
1. The file path
2. The start of search block: <<<<<<< SEARCH
3. A contiguous chunk of lines to search for in the existing source code
4. The dividing line: =======
5. The lines to replace into the source code
6. The end of the replace block: >>>>>>> REPLACE

Here is an example:

```python
### mathweb/flask/app.py
<<<<<<< SEARCH
from flask import Flask
=======
import math
from flask import Flask
>>>>>>> REPLACE
```
</solution>

Please note that the *SEARCH/REPLACE* edit REQUIRES PROPER INDENTATION. If you would like to add the line '        print(x)', you must fully write that out, with all those spaces before the code!
Wrap each *SEARCH/REPLACE* edit in a code block as shown in the example above. If you have multiple *SEARCH/REPLACE* edits, use a separate code block for each one.
[ISSUE]
**Title:** `finish()` Method Does Not Return a Completed Future, Causing Asynchronous Operations to Hang

**Description:**
When using the `finish()` method in the web module, it is expected to return a `Future` that is marked as done once the write operation completes. However, currently, the returned `Future` remains incomplete. This prevents any dependent asynchronous tasks from proceeding as they await the completion of the `Future`.

**Example Code:**
```python
class FinalReturnTest(WebTestCase):

    class FinishHandler(RequestHandler):

        @gen.coroutine
        def get(self):
            self.final_return = self.finish()
            yield self.final_return

    def test_finish_method_returns_completed_future(self):
        response = self.fetch('/finish')
        self.assertEqual(response.code, 200)
        self.assertIsInstance(self.final_return, Future)
        self.assertTrue(self.final_return.done())
```

**Expected Behavior:**
The `Future` returned by the `finish()` method should be marked as done (`self.final_return.done()` returns `True`) after the response is successfully written. This ensures that any asynchronous operations depending on this `Future` can proceed without issues.

**Actual Behavior:**
The `Future` returned by `finish()` is not marked as done (`self.final_return.done()` returns `False`), causing asynchronous operations that await this `Future` to hang indefinitely.

[/ISSUE]
{
    "AllHTTPMethodsTest.test_standard_methods": "PASSED",
    "ApplicationTest.test_listen": "PASSED",
    "AuthRedirectTest.test_absolute_auth_redirect": "PASSED",
    "AuthRedirectTest.test_relative_auth_redirect": "PASSED",
    "BuggyLoggingTest.test_buggy_log_exception": "PASSED",
    "CacheTest.test_multiple_strong_etag_match": "PASSED",
    "CacheTest.test_multiple_strong_etag_not_match": "PASSED",
    "CacheTest.test_multiple_weak_etag_match": "PASSED",
    "CacheTest.test_multiple_weak_etag_not_match": "PASSED",
    "CacheTest.test_strong_etag_match": "PASSED",
    "CacheTest.test_strong_etag_not_match": "PASSED",
    "CacheTest.test_weak_etag_match": "PASSED",
    "CacheTest.test_weak_etag_not_match": "PASSED",
    "CacheTest.test_wildcard_etag": "PASSED",
    "ClearAllCookiesTest.test_clear_all_cookies": "PASSED",
    "ClearHeaderTest.test_clear_header": "PASSED",
    "ClientCloseTest.test_client_close": "PASSED",
    "ConnectionCloseTest.test_connection_close": "PASSED",
    "CookieTest.test_cookie_special_char": "PASSED",
    "CookieTest.test_get_cookie": "PASSED",
    "CookieTest.test_set_cookie": "PASSED",
    "CookieTest.test_set_cookie_domain": "PASSED",
    "CookieTest.test_set_cookie_expires_days": "PASSED",
    "CookieTest.test_set_cookie_false_flags": "PASSED",
    "CookieTest.test_set_cookie_max_age": "PASSED",
    "CookieTest.test_set_cookie_overwrite": "PASSED",
    "Custom404Test.test_404": "PASSED",
    "CustomStaticFileTest.test_serve": "PASSED",
    "CustomStaticFileTest.test_static_url": "PASSED",
    "DateHeaderTest.test_date_header": "PASSED",
    "DecoratedStreamingRequestFlowControlTest.test_flow_control_chunked_body": "PASSED",
    "DecoratedStreamingRequestFlowControlTest.test_flow_control_compressed_body": "PASSED",
    "DecoratedStreamingRequestFlowControlTest.test_flow_control_fixed_body": "PASSED",
    "DecoratorTest.test_addslash": "PASSED",
    "DecoratorTest.test_removeslash": "PASSED",
    "Default404Test.test_404": "PASSED",
    "DefaultHandlerArgumentsTest.test_403": "PASSED",
    "DefaultHostMatchingTest.test_default_host_matching": "PASSED",
    "ErrorHandlerXSRFTest.test_404_xsrf": "PASSED",
    "ErrorHandlerXSRFTest.test_error_xsrf": "PASSED",
    "ErrorResponseTest.test_default": "PASSED",
    "ErrorResponseTest.test_failed_write_error": "PASSED",
    "ErrorResponseTest.test_write_error": "PASSED",
    "ExceptionHandlerTest.test_http_error": "PASSED",
    "ExceptionHandlerTest.test_known_error": "PASSED",
    "ExceptionHandlerTest.test_unknown_error": "PASSED",
    "FinalReturnTest.test_finish_method_return_future": "PASSED",
    "FinalReturnTest.test_render_method_return_future": "PASSED",
    "FinishExceptionTest.test_finish_exception": "PASSED",
    "FinishInPrepareTest.test_finish_in_prepare": "PASSED",
    "GetArgumentErrorTest.test_catch_error": "PASSED",
    "GetCurrentUserTest.test_get_current_user_from_ui_module_is_lazy": "PASSED",
    "GetCurrentUserTest.test_get_current_user_from_ui_module_works": "PASSED",
    "GetCurrentUserTest.test_get_current_user_works": "PASSED",
    "GzipTestCase.test_gzip": "PASSED",
    "GzipTestCase.test_gzip_not_requested": "PASSED",
    "GzipTestCase.test_gzip_static": "FAILED",
    "GzipTestCase.test_vary_already_present": "PASSED",
    "GzipTestCase.test_vary_already_present_multiple": "PASSED",
    "HTTPErrorTest.test_copy": "PASSED",
    "HandlerByNameTest.test_handler_by_name": "PASSED",
    "Header204Test.test_204_headers": "PASSED",
    "Header304Test.test_304_headers": "PASSED",
    "HostMatchingTest.test_host_matching": "PASSED",
    "IncorrectContentLengthTest.test_content_length_too_high": "PASSED",
    "IncorrectContentLengthTest.test_content_length_too_low": "PASSED",
    "NamedURLSpecGroupsTest.test_named_urlspec_groups": "PASSED",
    "NativeStreamingRequestFlowControlTest.test_flow_control_chunked_body": "PASSED",
    "NativeStreamingRequestFlowControlTest.test_flow_control_compressed_body": "PASSED",
    "NativeStreamingRequestFlowControlTest.test_flow_control_fixed_body": "PASSED",
    "NonWSGIWebTests.test_empty_flush": "PASSED",
    "PatchMethodTest.test_other": "PASSED",
    "PatchMethodTest.test_patch": "PASSED",
    "PathArgsInPrepareTest.test_kw": "PASSED",
    "PathArgsInPrepareTest.test_pos": "PASSED",
    "RaiseWithReasonTest.test_httperror_str": "PASSED",
    "RaiseWithReasonTest.test_httperror_str_from_httputil": "PASSED",
    "RaiseWithReasonTest.test_raise_with_reason": "PASSED",
    "RedirectHandlerTest.test_basic_redirect": "PASSED",
    "RedirectHandlerTest.test_redirect_pattern": "PASSED",
    "RedirectHandlerTest.test_redirect_with_appending_argument": "PASSED",
    "RedirectHandlerTest.test_redirect_with_argument": "PASSED",
    "RequestEncodingTest.test_error": "PASSED",
    "RequestEncodingTest.test_group_encoding": "PASSED",
    "RequestEncodingTest.test_group_question_mark": "PASSED",
    "RequestEncodingTest.test_slashes": "PASSED",
    "RequestSummaryTest.test_missing_remote_ip": "PASSED",
    "SecureCookieV1Test.test_arbitrary_bytes": "PASSED",
    "SecureCookieV1Test.test_cookie_tampering_future_timestamp": "PASSED",
    "SecureCookieV1Test.test_round_trip": "PASSED",
    "SecureCookieV2Test.test_key_version_increment_version": "PASSED",
    "SecureCookieV2Test.test_key_version_invalidate_version": "PASSED",
    "SecureCookieV2Test.test_key_version_roundtrip": "PASSED",
    "SecureCookieV2Test.test_key_version_roundtrip_differing_version": "PASSED",
    "SecureCookieV2Test.test_round_trip": "PASSED",
    "SetLazyPropertiesTest.test_set_properties": "PASSED",
    "SignedValueTest.test_expired": "PASSED",
    "SignedValueTest.test_key_version_retrieval": "PASSED",
    "SignedValueTest.test_key_versioning_invalid_key": "PASSED",
    "SignedValueTest.test_key_versioning_read_write_default_key": "PASSED",
    "SignedValueTest.test_key_versioning_read_write_non_default_key": "PASSED",
    "SignedValueTest.test_known_values": "PASSED",
    "SignedValueTest.test_name_swap": "PASSED",
    "SignedValueTest.test_non_ascii": "PASSED",
    "SignedValueTest.test_payload_tampering": "PASSED",
    "SignedValueTest.test_signature_tampering": "PASSED",
    "StaticDefaultFilenameTest.test_static_default_filename": "FAILED",
    "StaticDefaultFilenameTest.test_static_default_redirect": "FAILED",
    "StaticFileTest.test_absolute_static_url": "FAILED",
    "StaticFileTest.test_absolute_version_exclusion": "PASSED",
    "StaticFileTest.test_include_host_override": "PASSED",
    "StaticFileTest.test_path_traversal_protection": "PASSED",
    "StaticFileTest.test_relative_version_exclusion": "PASSED",
    "StaticFileTest.test_root_static_path": "FAILED",
    "StaticFileTest.test_static_304_etag_modified_bug": "ERROR",
    "StaticFileTest.test_static_304_if_modified_since": "ERROR",
    "StaticFileTest.test_static_304_if_none_match": "ERROR",
    "StaticFileTest.test_static_404": "PASSED",
    "StaticFileTest.test_static_compressed_files": "FAILED",
    "StaticFileTest.test_static_etag": "FAILED",
    "StaticFileTest.test_static_files": "FAILED",
    "StaticFileTest.test_static_head": "FAILED",
    "StaticFileTest.test_static_head_range": "FAILED",
    "StaticFileTest.test_static_if_modified_since_pre_epoch": "FAILED",
    "StaticFileTest.test_static_if_modified_since_time_zone": "ERROR",
    "StaticFileTest.test_static_invalid_range": "FAILED",
    "StaticFileTest.test_static_range_if_none_match": "FAILED",
    "StaticFileTest.test_static_unsatisfiable_range_end_less_than_start": "FAILED",
    "StaticFileTest.test_static_unsatisfiable_range_invalid_start": "FAILED",
    "StaticFileTest.test_static_unsatisfiable_range_zero_suffix": "FAILED",
    "StaticFileTest.test_static_url": "FAILED",
    "StaticFileTest.test_static_with_range": "FAILED",
    "StaticFileTest.test_static_with_range_end_edge": "FAILED",
    "StaticFileTest.test_static_with_range_full_file": "FAILED",
    "StaticFileTest.test_static_with_range_full_past_end": "FAILED",
    "StaticFileTest.test_static_with_range_neg_end": "FAILED",
    "StaticFileTest.test_static_with_range_neg_past_start": "FAILED",
    "StaticFileTest.test_static_with_range_partial_past_end": "FAILED",
    "StaticFileWithPathTest.test_serve": "FAILED",
    "StatusReasonTest.test_status": "PASSED",
    "StreamingRequestBodyTest.test_close_during_upload": "PASSED",
    "StreamingRequestBodyTest.test_early_return": "PASSED",
    "StreamingRequestBodyTest.test_early_return_with_data": "PASSED",
    "StreamingRequestBodyTest.test_streaming_body": "PASSED",
    "UIMethodUIModuleTest.test_ui_method": "PASSED",
    "URLSpecReverseTest.test_non_reversible": "PASSED",
    "URLSpecReverseTest.test_reverse": "PASSED",
    "URLSpecReverseTest.test_reverse_arguments": "PASSED",
    "UnimplementedHTTPMethodsTest.test_unimplemented_standard_methods": "PASSED",
    "UnimplementedNonStandardMethodsTest.test_unimplemented_other": "PASSED",
    "UnimplementedNonStandardMethodsTest.test_unimplemented_patch": "PASSED",
    "WSGISafeWebTest.test_decode_argument": "PASSED",
    "WSGISafeWebTest.test_decode_argument_invalid_unicode": "PASSED",
    "WSGISafeWebTest.test_decode_argument_plus": "PASSED",
    "WSGISafeWebTest.test_get_argument": "PASSED",
    "WSGISafeWebTest.test_get_body_arguments": "PASSED",
    "WSGISafeWebTest.test_get_query_arguments": "PASSED",
    "WSGISafeWebTest.test_header_injection": "PASSED",
    "WSGISafeWebTest.test_multi_header": "PASSED",
    "WSGISafeWebTest.test_no_gzip": "PASSED",
    "WSGISafeWebTest.test_optional_path": "PASSED",
    "WSGISafeWebTest.test_redirect": "PASSED",
    "WSGISafeWebTest.test_reverse_url": "PASSED",
    "WSGISafeWebTest.test_types": "PASSED",
    "WSGISafeWebTest.test_uimodule_resources": "PASSED",
    "WSGISafeWebTest.test_uimodule_unescaped": "PASSED",
    "WSGISafeWebTest.test_web_redirect": "PASSED",
    "WSGISafeWebTest.test_web_redirect_double_slash": "PASSED",
    "XSRFCookieKwargsTest.test_xsrf_httponly": "PASSED",
    "XSRFTest.test_cross_user": "PASSED",
    "XSRFTest.test_distinct_tokens": "PASSED",
    "XSRFTest.test_refresh_token": "PASSED",
    "XSRFTest.test_versioning": "PASSED",
    "XSRFTest.test_xsrf_fail_argument_invalid_format": "PASSED",
    "XSRFTest.test_xsrf_fail_body_no_cookie": "PASSED",
    "XSRFTest.test_xsrf_fail_cookie_invalid_format": "PASSED",
    "XSRFTest.test_xsrf_fail_cookie_no_body": "PASSED",
    "XSRFTest.test_xsrf_fail_no_token": "PASSED",
    "XSRFTest.test_xsrf_success_header": "PASSED",
    "XSRFTest.test_xsrf_success_non_hex_token": "PASSED",
    "XSRFTest.test_xsrf_success_post_body": "PASSED",
    "XSRFTest.test_xsrf_success_query_string": "PASSED",
    "XSRFTest.test_xsrf_success_short_token": "PASSED"
}
diff --git a/tornado/http1connection.py b/tornado/http1connection.py
index 10402b4b..0413fd9a 100644
--- a/tornado/http1connection.py
+++ b/tornado/http1connection.py
@@ -492,7 +492,7 @@ class HTTP1Connection(httputil.HTTPConnection):
         else:
             future = self._write_future = Future()
             self._pending_write = self.stream.write(self._format_chunk(chunk))
-            self._pending_write.add_done_callback(self._on_write_complete)
+            future_add_done_callback(self._pending_write, self._on_write_complete)
         return future
 
     def finish(self) -> None:

{"tornado/http1connection.py": "#\n# Copyright 2014 Facebook\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\"); you may\n# not use this file except in compliance with the License. You may obtain\n# a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT\n# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the\n# License for the specific language governing permissions and limitations\n# under the License.\n\n\"\"\"Client and server implementations of HTTP/1.x.\n\n.. versionadded:: 4.0\n\"\"\"\n\nimport asyncio\nimport logging\nimport re\nimport types\n\nfrom tornado.concurrent import (\n    Future,\n    future_add_done_callback,\n    future_set_result_unless_cancelled,\n)\nfrom tornado.escape import native_str, utf8\nfrom tornado import gen\nfrom tornado import httputil\nfrom tornado import iostream\nfrom tornado.log import gen_log, app_log\nfrom tornado.util import GzipDecompressor\n\n\nfrom typing import cast, Optional, Type, Awaitable, Callable, Union, Tuple\n\n\nclass _QuietException(Exception):\n    def __init__(self) -> None:\n        pass\n\n\nclass _ExceptionLoggingContext(object):\n    \"\"\"Used with the ``with`` statement when calling delegate methods to\n    log any exceptions with the given logger.  Any exceptions caught are\n    converted to _QuietException\n    \"\"\"\n\n    def __init__(self, logger: logging.Logger) -> None:\n        self.logger = logger\n\n    def __enter__(self) -> None:\n        pass\n\n    def __exit__(\n        self,\n        typ: Optional[Type[BaseException]],\n        value: Optional[BaseException],\n        tb: types.TracebackType,\n    ) -> None:\n        if value is not None:\n            assert typ is not None\n            self.logger.error(\"Uncaught exception\", exc_info=(typ, value, tb))\n            raise _QuietException\n\n\nclass HTTP1ConnectionParameters(object):\n    \"\"\"Parameters for `.HTTP1Connection` and `.HTTP1ServerConnection`.\n    \"\"\"\n\n    def __init__(\n        self,\n        no_keep_alive: bool = False,\n        chunk_size: int = None,\n        max_header_size: int = None,\n        header_timeout: float = None,\n        max_body_size: int = None,\n        body_timeout: float = None,\n        decompress: bool = False,\n    ) -> None:\n        \"\"\"\n        :arg bool no_keep_alive: If true, always close the connection after\n            one request.\n        :arg int chunk_size: how much data to read into memory at once\n        :arg int max_header_size:  maximum amount of data for HTTP headers\n        :arg float header_timeout: how long to wait for all headers (seconds)\n        :arg int max_body_size: maximum amount of data for body\n        :arg float body_timeout: how long to wait while reading body (seconds)\n        :arg bool decompress: if true, decode incoming\n            ``Content-Encoding: gzip``\n        \"\"\"\n        self.no_keep_alive = no_keep_alive\n        self.chunk_size = chunk_size or 65536\n        self.max_header_size = max_header_size or 65536\n        self.header_timeout = header_timeout\n        self.max_body_size = max_body_size\n        self.body_timeout = body_timeout\n        self.decompress = decompress\n\n\nclass HTTP1Connection(httputil.HTTPConnection):\n    \"\"\"Implements the HTTP/1.x protocol.\n\n    This class can be on its own for clients, or via `HTTP1ServerConnection`\n    for servers.\n    \"\"\"\n\n    def __init__(\n        self,\n        stream: iostream.IOStream,\n        is_client: bool,\n        params: HTTP1ConnectionParameters = None,\n        context: object = None,\n    ) -> None:\n        \"\"\"\n        :arg stream: an `.IOStream`\n        :arg bool is_client: client or server\n        :arg params: a `.HTTP1ConnectionParameters` instance or ``None``\n        :arg context: an opaque application-defined object that can be accessed\n            as ``connection.context``.\n        \"\"\"\n        self.is_client = is_client\n        self.stream = stream\n        if params is None:\n            params = HTTP1ConnectionParameters()\n        self.params = params\n        self.context = context\n        self.no_keep_alive = params.no_keep_alive\n        # The body limits can be altered by the delegate, so save them\n        # here instead of just referencing self.params later.\n        self._max_body_size = self.params.max_body_size or self.stream.max_buffer_size\n        self._body_timeout = self.params.body_timeout\n        # _write_finished is set to True when finish() has been called,\n        # i.e. there will be no more data sent.  Data may still be in the\n        # stream's write buffer.\n        self._write_finished = False\n        # True when we have read the entire incoming body.\n        self._read_finished = False\n        # _finish_future resolves when all data has been written and flushed\n        # to the IOStream.\n        self._finish_future = Future()  # type: Future[None]\n        # If true, the connection should be closed after this request\n        # (after the response has been written in the server side,\n        # and after it has been read in the client)\n        self._disconnect_on_finish = False\n        self._clear_callbacks()\n        # Save the start lines after we read or write them; they\n        # affect later processing (e.g. 304 responses and HEAD methods\n        # have content-length but no bodies)\n        self._request_start_line = None  # type: Optional[httputil.RequestStartLine]\n        self._response_start_line = None  # type: Optional[httputil.ResponseStartLine]\n        self._request_headers = None  # type: Optional[httputil.HTTPHeaders]\n        # True if we are writing output with chunked encoding.\n        self._chunking_output = False\n        # While reading a body with a content-length, this is the\n        # amount left to read.\n        self._expected_content_remaining = None  # type: Optional[int]\n        # A Future for our outgoing writes, returned by IOStream.write.\n        self._pending_write = None  # type: Optional[Future[None]]\n\n    def read_response(self, delegate: httputil.HTTPMessageDelegate) -> Awaitable[bool]:\n        \"\"\"Read a single HTTP response.\n\n        Typical client-mode usage is to write a request using `write_headers`,\n        `write`, and `finish`, and then call ``read_response``.\n\n        :arg delegate: a `.HTTPMessageDelegate`\n\n        Returns a `.Future` that resolves to a bool after the full response has\n        been read. The result is true if the stream is still open.\n        \"\"\"\n        if self.params.decompress:\n            delegate = _GzipMessageDelegate(delegate, self.params.chunk_size)\n        return self._read_message(delegate)\n\n    async def _read_message(self, delegate: httputil.HTTPMessageDelegate) -> bool:\n        need_delegate_close = False\n        try:\n            header_future = self.stream.read_until_regex(\n                b\"\\r?\\n\\r?\\n\", max_bytes=self.params.max_header_size\n            )\n            if self.params.header_timeout is None:\n                header_data = await header_future\n            else:\n                try:\n                    header_data = await gen.with_timeout(\n                        self.stream.io_loop.time() + self.params.header_timeout,\n                        header_future,\n                        quiet_exceptions=iostream.StreamClosedError,\n                    )\n                except gen.TimeoutError:\n                    self.close()\n                    return False\n            start_line_str, headers = self._parse_headers(header_data)\n            if self.is_client:\n                resp_start_line = httputil.parse_response_start_line(start_line_str)\n                self._response_start_line = resp_start_line\n                start_line = (\n                    resp_start_line\n                )  # type: Union[httputil.RequestStartLine, httputil.ResponseStartLine]\n                # TODO: this will need to change to support client-side keepalive\n                self._disconnect_on_finish = False\n            else:\n                req_start_line = httputil.parse_request_start_line(start_line_str)\n                self._request_start_line = req_start_line\n                self._request_headers = headers\n                start_line = req_start_line\n                self._disconnect_on_finish = not self._can_keep_alive(\n                    req_start_line, headers\n                )\n            need_delegate_close = True\n            with _ExceptionLoggingContext(app_log):\n                header_recv_future = delegate.headers_received(start_line, headers)\n                if header_recv_future is not None:\n                    await header_recv_future\n            if self.stream is None:\n                # We've been detached.\n                need_delegate_close = False\n                return False\n            skip_body = False\n            if self.is_client:\n                assert isinstance(start_line, httputil.ResponseStartLine)\n                if (\n                    self._request_start_line is not None\n                    and self._request_start_line.method == \"HEAD\"\n                ):\n                    skip_body = True\n                code = start_line.code\n                if code == 304:\n                    # 304 responses may include the content-length header\n                    # but do not actually have a body.\n                    # http://tools.ietf.org/html/rfc7230#section-3.3\n                    skip_body = True\n                if code >= 100 and code < 200:\n                    # 1xx responses should never indicate the presence of\n                    # a body.\n                    if \"Content-Length\" in headers or \"Transfer-Encoding\" in headers:\n                        raise httputil.HTTPInputError(\n                            \"Response code %d cannot have body\" % code\n                        )\n                    # TODO: client delegates will get headers_received twice\n                    # in the case of a 100-continue.  Document or change?\n                    await self._read_message(delegate)\n            else:\n                if headers.get(\"Expect\") == \"100-continue\" and not self._write_finished:\n                    self.stream.write(b\"HTTP/1.1 100 (Continue)\\r\\n\\r\\n\")\n            if not skip_body:\n                body_future = self._read_body(\n                    resp_start_line.code if self.is_client else 0, headers, delegate\n                )\n                if body_future is not None:\n                    if self._body_timeout is None:\n                        await body_future\n                    else:\n                        try:\n                            await gen.with_timeout(\n                                self.stream.io_loop.time() + self._body_timeout,\n                                body_future,\n                                quiet_exceptions=iostream.StreamClosedError,\n                            )\n                        except gen.TimeoutError:\n                            gen_log.info(\"Timeout reading body from %s\", self.context)\n                            self.stream.close()\n                            return False\n            self._read_finished = True\n            if not self._write_finished or self.is_client:\n                need_delegate_close = False\n                with _ExceptionLoggingContext(app_log):\n                    delegate.finish()\n            # If we're waiting for the application to produce an asynchronous\n            # response, and we're not detached, register a close callback\n            # on the stream (we didn't need one while we were reading)\n            if (\n                not self._finish_future.done()\n                and self.stream is not None\n                and not self.stream.closed()\n            ):\n                self.stream.set_close_callback(self._on_connection_close)\n                await self._finish_future\n            if self.is_client and self._disconnect_on_finish:\n                self.close()\n            if self.stream is None:\n                return False\n        except httputil.HTTPInputError as e:\n            gen_log.info(\"Malformed HTTP message from %s: %s\", self.context, e)\n            if not self.is_client:\n                await self.stream.write(b\"HTTP/1.1 400 Bad Request\\r\\n\\r\\n\")\n            self.close()\n            return False\n        finally:\n            if need_delegate_close:\n                with _ExceptionLoggingContext(app_log):\n                    delegate.on_connection_close()\n            header_future = None  # type: ignore\n            self._clear_callbacks()\n        return True\n\n    def _clear_callbacks(self) -> None:\n        \"\"\"Clears the callback attributes.\n\n        This allows the request handler to be garbage collected more\n        quickly in CPython by breaking up reference cycles.\n        \"\"\"\n        self._write_callback = None\n        self._write_future = None  # type: Optional[Future[None]]\n        self._close_callback = None  # type: Optional[Callable[[], None]]\n        if self.stream is not None:\n            self.stream.set_close_callback(None)\n\n    def set_close_callback(self, callback: Optional[Callable[[], None]]) -> None:\n        \"\"\"Sets a callback that will be run when the connection is closed.\n\n        Note that this callback is slightly different from\n        `.HTTPMessageDelegate.on_connection_close`: The\n        `.HTTPMessageDelegate` method is called when the connection is\n        closed while recieving a message. This callback is used when\n        there is not an active delegate (for example, on the server\n        side this callback is used if the client closes the connection\n        after sending its request but before receiving all the\n        response.\n        \"\"\"\n        self._close_callback = callback\n\n    def _on_connection_close(self) -> None:\n        # Note that this callback is only registered on the IOStream\n        # when we have finished reading the request and are waiting for\n        # the application to produce its response.\n        if self._close_callback is not None:\n            callback = self._close_callback\n            self._close_callback = None\n            callback()\n        if not self._finish_future.done():\n            future_set_result_unless_cancelled(self._finish_future, None)\n        self._clear_callbacks()\n\n    def close(self) -> None:\n        if self.stream is not None:\n            self.stream.close()\n        self._clear_callbacks()\n        if not self._finish_future.done():\n            future_set_result_unless_cancelled(self._finish_future, None)\n\n    def detach(self) -> iostream.IOStream:\n        \"\"\"Take control of the underlying stream.\n\n        Returns the underlying `.IOStream` object and stops all further\n        HTTP processing.  May only be called during\n        `.HTTPMessageDelegate.headers_received`.  Intended for implementing\n        protocols like websockets that tunnel over an HTTP handshake.\n        \"\"\"\n        self._clear_callbacks()\n        stream = self.stream\n        self.stream = None  # type: ignore\n        if not self._finish_future.done():\n            future_set_result_unless_cancelled(self._finish_future, None)\n        return stream\n\n    def set_body_timeout(self, timeout: float) -> None:\n        \"\"\"Sets the body timeout for a single request.\n\n        Overrides the value from `.HTTP1ConnectionParameters`.\n        \"\"\"\n        self._body_timeout = timeout\n\n    def set_max_body_size(self, max_body_size: int) -> None:\n        \"\"\"Sets the body size limit for a single request.\n\n        Overrides the value from `.HTTP1ConnectionParameters`.\n        \"\"\"\n        self._max_body_size = max_body_size\n\n    def write_headers(\n        self,\n        start_line: Union[httputil.RequestStartLine, httputil.ResponseStartLine],\n        headers: httputil.HTTPHeaders,\n        chunk: bytes = None,\n    ) -> \"Future[None]\":\n        \"\"\"Implements `.HTTPConnection.write_headers`.\"\"\"\n        lines = []\n        if self.is_client:\n            assert isinstance(start_line, httputil.RequestStartLine)\n            self._request_start_line = start_line\n            lines.append(utf8(\"%s %s HTTP/1.1\" % (start_line[0], start_line[1])))\n            # Client requests with a non-empty body must have either a\n            # Content-Length or a Transfer-Encoding.\n            self._chunking_output = (\n                start_line.method in (\"POST\", \"PUT\", \"PATCH\")\n                and \"Content-Length\" not in headers\n                and \"Transfer-Encoding\" not in headers\n            )\n        else:\n            assert isinstance(start_line, httputil.ResponseStartLine)\n            assert self._request_start_line is not None\n            assert self._request_headers is not None\n            self._response_start_line = start_line\n            lines.append(utf8(\"HTTP/1.1 %d %s\" % (start_line[1], start_line[2])))\n            self._chunking_output = (\n                # TODO: should this use\n                # self._request_start_line.version or\n                # start_line.version?\n                self._request_start_line.version == \"HTTP/1.1\"\n                # 1xx, 204 and 304 responses have no body (not even a zero-length\n                # body), and so should not have either Content-Length or\n                # Transfer-Encoding headers.\n                and start_line.code not in (204, 304)\n                and (start_line.code < 100 or start_line.code >= 200)\n                # No need to chunk the output if a Content-Length is specified.\n                and \"Content-Length\" not in headers\n                # Applications are discouraged from touching Transfer-Encoding,\n                # but if they do, leave it alone.\n                and \"Transfer-Encoding\" not in headers\n            )\n            # If connection to a 1.1 client will be closed, inform client\n            if (\n                self._request_start_line.version == \"HTTP/1.1\"\n                and self._disconnect_on_finish\n            ):\n                headers[\"Connection\"] = \"close\"\n            # If a 1.0 client asked for keep-alive, add the header.\n            if (\n                self._request_start_line.version == \"HTTP/1.0\"\n                and self._request_headers.get(\"Connection\", \"\").lower() == \"keep-alive\"\n            ):\n                headers[\"Connection\"] = \"Keep-Alive\"\n        if self._chunking_output:\n            headers[\"Transfer-Encoding\"] = \"chunked\"\n        if not self.is_client and (\n            self._request_start_line.method == \"HEAD\"\n            or cast(httputil.ResponseStartLine, start_line).code == 304\n        ):\n            self._expected_content_remaining = 0\n        elif \"Content-Length\" in headers:\n            self._expected_content_remaining = int(headers[\"Content-Length\"])\n        else:\n            self._expected_content_remaining = None\n        # TODO: headers are supposed to be of type str, but we still have some\n        # cases that let bytes slip through. Remove these native_str calls when those\n        # are fixed.\n        header_lines = (\n            native_str(n) + \": \" + native_str(v) for n, v in headers.get_all()\n        )\n        lines.extend(l.encode(\"latin1\") for l in header_lines)\n        for line in lines:\n            if b\"\\n\" in line:\n                raise ValueError(\"Newline in header: \" + repr(line))\n        future = None\n        if self.stream.closed():\n            future = self._write_future = Future()\n            future.set_exception(iostream.StreamClosedError())\n            future.exception()\n        else:\n            future = self._write_future = Future()\n            data = b\"\\r\\n\".join(lines) + b\"\\r\\n\\r\\n\"\n            if chunk:\n                data += self._format_chunk(chunk)\n            self._pending_write = self.stream.write(data)\n            future_add_done_callback(self._pending_write, self._on_write_complete)\n        return future\n\n    def _format_chunk(self, chunk: bytes) -> bytes:\n        if self._expected_content_remaining is not None:\n            self._expected_content_remaining -= len(chunk)\n            if self._expected_content_remaining < 0:\n                # Close the stream now to stop further framing errors.\n                self.stream.close()\n                raise httputil.HTTPOutputError(\n                    \"Tried to write more data than Content-Length\"\n                )\n        if self._chunking_output and chunk:\n            # Don't write out empty chunks because that means END-OF-STREAM\n            # with chunked encoding\n            return utf8(\"%x\" % len(chunk)) + b\"\\r\\n\" + chunk + b\"\\r\\n\"\n        else:\n            return chunk\n\n    def write(self, chunk: bytes) -> \"Future[None]\":\n        \"\"\"Implements `.HTTPConnection.write`.\n\n        For backwards compatibility it is allowed but deprecated to\n        skip `write_headers` and instead call `write()` with a\n        pre-encoded header block.\n        \"\"\"\n        future = None\n        if self.stream.closed():\n            future = self._write_future = Future()\n            self._write_future.set_exception(iostream.StreamClosedError())\n            self._write_future.exception()\n        else:\n            future = self._write_future = Future()\n            self._pending_write = self.stream.write(self._format_chunk(chunk))\n            self._pending_write.add_done_callback(self._on_write_complete)\n        return future\n\n    def finish(self) -> None:\n        \"\"\"Implements `.HTTPConnection.finish`.\"\"\"\n        if (\n            self._expected_content_remaining is not None\n            and self._expected_content_remaining != 0\n            and not self.stream.closed()\n        ):\n            self.stream.close()\n            raise httputil.HTTPOutputError(\n                \"Tried to write %d bytes less than Content-Length\"\n                % self._expected_content_remaining\n            )\n        if self._chunking_output:\n            if not self.stream.closed():\n                self._pending_write = self.stream.write(b\"0\\r\\n\\r\\n\")\n                self._pending_write.add_done_callback(self._on_write_complete)\n        self._write_finished = True\n        # If the app finished the request while we're still reading,\n        # divert any remaining data away from the delegate and\n        # close the connection when we're done sending our response.\n        # Closing the connection is the only way to avoid reading the\n        # whole input body.\n        if not self._read_finished:\n            self._disconnect_on_finish = True\n        # No more data is coming, so instruct TCP to send any remaining\n        # data immediately instead of waiting for a full packet or ack.\n        self.stream.set_nodelay(True)\n        if self._pending_write is None:\n            self._finish_request(None)\n        else:\n            future_add_done_callback(self._pending_write, self._finish_request)\n\n    def _on_write_complete(self, future: \"Future[None]\") -> None:\n        exc = future.exception()\n        if exc is not None and not isinstance(exc, iostream.StreamClosedError):\n            future.result()\n        if self._write_callback is not None:\n            callback = self._write_callback\n            self._write_callback = None\n            self.stream.io_loop.add_callback(callback)\n        if self._write_future is not None:\n            future = self._write_future\n            self._write_future = None\n            future_set_result_unless_cancelled(future, None)\n\n    def _can_keep_alive(\n        self, start_line: httputil.RequestStartLine, headers: httputil.HTTPHeaders\n    ) -> bool:\n        if self.params.no_keep_alive:\n            return False\n        connection_header = headers.get(\"Connection\")\n        if connection_header is not None:\n            connection_header = connection_header.lower()\n        if start_line.version == \"HTTP/1.1\":\n            return connection_header != \"close\"\n        elif (\n            \"Content-Length\" in headers\n            or headers.get(\"Transfer-Encoding\", \"\").lower() == \"chunked\"\n            or getattr(start_line, \"method\", None) in (\"HEAD\", \"GET\")\n        ):\n            # start_line may be a request or response start line; only\n            # the former has a method attribute.\n            return connection_header == \"keep-alive\"\n        return False\n\n    def _finish_request(self, future: Optional[\"Future[None]\"]) -> None:\n        self._clear_callbacks()\n        if not self.is_client and self._disconnect_on_finish:\n            self.close()\n            return\n        # Turn Nagle's algorithm back on, leaving the stream in its\n        # default state for the next request.\n        self.stream.set_nodelay(False)\n        if not self._finish_future.done():\n            future_set_result_unless_cancelled(self._finish_future, None)\n\n    def _parse_headers(self, data: bytes) -> Tuple[str, httputil.HTTPHeaders]:\n        # The lstrip removes newlines that some implementations sometimes\n        # insert between messages of a reused connection.  Per RFC 7230,\n        # we SHOULD ignore at least one empty line before the request.\n        # http://tools.ietf.org/html/rfc7230#section-3.5\n        data_str = native_str(data.decode(\"latin1\")).lstrip(\"\\r\\n\")\n        # RFC 7230 section allows for both CRLF and bare LF.\n        eol = data_str.find(\"\\n\")\n        start_line = data_str[:eol].rstrip(\"\\r\")\n        headers = httputil.HTTPHeaders.parse(data_str[eol:])\n        return start_line, headers\n\n    def _read_body(\n        self,\n        code: int,\n        headers: httputil.HTTPHeaders,\n        delegate: httputil.HTTPMessageDelegate,\n    ) -> Optional[Awaitable[None]]:\n        if \"Content-Length\" in headers:\n            if \"Transfer-Encoding\" in headers:\n                # Response cannot contain both Content-Length and\n                # Transfer-Encoding headers.\n                # http://tools.ietf.org/html/rfc7230#section-3.3.3\n                raise httputil.HTTPInputError(\n                    \"Response with both Transfer-Encoding and Content-Length\"\n                )\n            if \",\" in headers[\"Content-Length\"]:\n                # Proxies sometimes cause Content-Length headers to get\n                # duplicated.  If all the values are identical then we can\n                # use them but if they differ it's an error.\n                pieces = re.split(r\",\\s*\", headers[\"Content-Length\"])\n                if any(i != pieces[0] for i in pieces):\n                    raise httputil.HTTPInputError(\n                        \"Multiple unequal Content-Lengths: %r\"\n                        % headers[\"Content-Length\"]\n                    )\n                headers[\"Content-Length\"] = pieces[0]\n\n            try:\n                content_length = int(headers[\"Content-Length\"])  # type: Optional[int]\n            except ValueError:\n                # Handles non-integer Content-Length value.\n                raise httputil.HTTPInputError(\n                    \"Only integer Content-Length is allowed: %s\"\n                    % headers[\"Content-Length\"]\n                )\n\n            if cast(int, content_length) > self._max_body_size:\n                raise httputil.HTTPInputError(\"Content-Length too long\")\n        else:\n            content_length = None\n\n        if code == 204:\n            # This response code is not allowed to have a non-empty body,\n            # and has an implicit length of zero instead of read-until-close.\n            # http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.3\n            if \"Transfer-Encoding\" in headers or content_length not in (None, 0):\n                raise httputil.HTTPInputError(\n                    \"Response with code %d should not have body\" % code\n                )\n            content_length = 0\n\n        if content_length is not None:\n            return self._read_fixed_body(content_length, delegate)\n        if headers.get(\"Transfer-Encoding\", \"\").lower() == \"chunked\":\n            return self._read_chunked_body(delegate)\n        if self.is_client:\n            return self._read_body_until_close(delegate)\n        return None\n\n    async def _read_fixed_body(\n        self, content_length: int, delegate: httputil.HTTPMessageDelegate\n    ) -> None:\n        while content_length > 0:\n            body = await self.stream.read_bytes(\n                min(self.params.chunk_size, content_length), partial=True\n            )\n            content_length -= len(body)\n            if not self._write_finished or self.is_client:\n                with _ExceptionLoggingContext(app_log):\n                    ret = delegate.data_received(body)\n                    if ret is not None:\n                        await ret\n\n    async def _read_chunked_body(self, delegate: httputil.HTTPMessageDelegate) -> None:\n        # TODO: \"chunk extensions\" http://tools.ietf.org/html/rfc2616#section-3.6.1\n        total_size = 0\n        while True:\n            chunk_len_str = await self.stream.read_until(b\"\\r\\n\", max_bytes=64)\n            chunk_len = int(chunk_len_str.strip(), 16)\n            if chunk_len == 0:\n                crlf = await self.stream.read_bytes(2)\n                if crlf != b\"\\r\\n\":\n                    raise httputil.HTTPInputError(\n                        \"improperly terminated chunked request\"\n                    )\n                return\n            total_size += chunk_len\n            if total_size > self._max_body_size:\n                raise httputil.HTTPInputError(\"chunked body too large\")\n            bytes_to_read = chunk_len\n            while bytes_to_read:\n                chunk = await self.stream.read_bytes(\n                    min(bytes_to_read, self.params.chunk_size), partial=True\n                )\n                bytes_to_read -= len(chunk)\n                if not self._write_finished or self.is_client:\n                    with _ExceptionLoggingContext(app_log):\n                        ret = delegate.data_received(chunk)\n                        if ret is not None:\n                            await ret\n            # chunk ends with \\r\\n\n            crlf = await self.stream.read_bytes(2)\n            assert crlf == b\"\\r\\n\"\n\n    async def _read_body_until_close(\n        self, delegate: httputil.HTTPMessageDelegate\n    ) -> None:\n        body = await self.stream.read_until_close()\n        if not self._write_finished or self.is_client:\n            with _ExceptionLoggingContext(app_log):\n                ret = delegate.data_received(body)\n                if ret is not None:\n                    await ret\n\n\nclass _GzipMessageDelegate(httputil.HTTPMessageDelegate):\n    \"\"\"Wraps an `HTTPMessageDelegate` to decode ``Content-Encoding: gzip``.\n    \"\"\"\n\n    def __init__(self, delegate: httputil.HTTPMessageDelegate, chunk_size: int) -> None:\n        self._delegate = delegate\n        self._chunk_size = chunk_size\n        self._decompressor = None  # type: Optional[GzipDecompressor]\n\n    def headers_received(\n        self,\n        start_line: Union[httputil.RequestStartLine, httputil.ResponseStartLine],\n        headers: httputil.HTTPHeaders,\n    ) -> Optional[Awaitable[None]]:\n        if headers.get(\"Content-Encoding\") == \"gzip\":\n            self._decompressor = GzipDecompressor()\n            # Downstream delegates will only see uncompressed data,\n            # so rename the content-encoding header.\n            # (but note that curl_httpclient doesn't do this).\n            headers.add(\"X-Consumed-Content-Encoding\", headers[\"Content-Encoding\"])\n            del headers[\"Content-Encoding\"]\n        return self._delegate.headers_received(start_line, headers)\n\n    async def data_received(self, chunk: bytes) -> None:\n        if self._decompressor:\n            compressed_data = chunk\n            while compressed_data:\n                decompressed = self._decompressor.decompress(\n                    compressed_data, self._chunk_size\n                )\n                if decompressed:\n                    ret = self._delegate.data_received(decompressed)\n                    if ret is not None:\n                        await ret\n                compressed_data = self._decompressor.unconsumed_tail\n        else:\n            ret = self._delegate.data_received(chunk)\n            if ret is not None:\n                await ret\n\n    def finish(self) -> None:\n        if self._decompressor is not None:\n            tail = self._decompressor.flush()\n            if tail:\n                # The tail should always be empty: decompress returned\n                # all that it can in data_received and the only\n                # purpose of the flush call is to detect errors such\n                # as truncated input. If we did legitimately get a new\n                # chunk at this point we'd need to change the\n                # interface to make finish() a coroutine.\n                raise ValueError(\n                    \"decompressor.flush returned data; possile truncated input\"\n                )\n        return self._delegate.finish()\n\n    def on_connection_close(self) -> None:\n        return self._delegate.on_connection_close()\n\n\nclass HTTP1ServerConnection(object):\n    \"\"\"An HTTP/1.x server.\"\"\"\n\n    def __init__(\n        self,\n        stream: iostream.IOStream,\n        params: HTTP1ConnectionParameters = None,\n        context: object = None,\n    ) -> None:\n        \"\"\"\n        :arg stream: an `.IOStream`\n        :arg params: a `.HTTP1ConnectionParameters` or None\n        :arg context: an opaque application-defined object that is accessible\n            as ``connection.context``\n        \"\"\"\n        self.stream = stream\n        if params is None:\n            params = HTTP1ConnectionParameters()\n        self.params = params\n        self.context = context\n        self._serving_future = None  # type: Optional[Future[None]]\n\n    async def close(self) -> None:\n        \"\"\"Closes the connection.\n\n        Returns a `.Future` that resolves after the serving loop has exited.\n        \"\"\"\n        self.stream.close()\n        # Block until the serving loop is done, but ignore any exceptions\n        # (start_serving is already responsible for logging them).\n        assert self._serving_future is not None\n        try:\n            await self._serving_future\n        except Exception:\n            pass\n\n    def start_serving(self, delegate: httputil.HTTPServerConnectionDelegate) -> None:\n        \"\"\"Starts serving requests on this connection.\n\n        :arg delegate: a `.HTTPServerConnectionDelegate`\n        \"\"\"\n        assert isinstance(delegate, httputil.HTTPServerConnectionDelegate)\n        fut = gen.convert_yielded(self._server_request_loop(delegate))\n        self._serving_future = fut\n        # Register the future on the IOLoop so its errors get logged.\n        self.stream.io_loop.add_future(fut, lambda f: f.result())\n\n    async def _server_request_loop(\n        self, delegate: httputil.HTTPServerConnectionDelegate\n    ) -> None:\n        try:\n            while True:\n                conn = HTTP1Connection(self.stream, False, self.params, self.context)\n                request_delegate = delegate.start_request(self, conn)\n                try:\n                    ret = await conn.read_response(request_delegate)\n                except (iostream.StreamClosedError, iostream.UnsatisfiableReadError):\n                    return\n                except _QuietException:\n                    # This exception was already logged.\n                    conn.close()\n                    return\n                except Exception:\n                    gen_log.error(\"Uncaught exception\", exc_info=True)\n                    conn.close()\n                    return\n                if not ret:\n                    return\n                await asyncio.sleep(0)\n        finally:\n            delegate.on_close(self)\n"}
A user will ask you to solve a task. You should first draft your thinking process (inner monologue). Then, generate the solution.

Your response format must follow the template below:
<think>
Your thoughts or/and draft, like working through an exercise on scratch paper. Be as casual and as long as you want until you are confident to generate a correct solution.
</think>
<solution>
Final solution presented to the user.
</solution>
system
We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
[ISSUE]
**Title:** `finish()` Method Does Not Return a Completed Future, Causing Asynchronous Operations to Hang

**Description:**
When using the `finish()` method in the web module, it is expected to return a `Future` that is marked as done once the write operation completes. However, currently, the returned `Future` remains incomplete. This prevents any dependent asynchronous tasks from proceeding as they await the completion of the `Future`.

**Example Code:**
```python
class FinalReturnTest(WebTestCase):

    class FinishHandler(RequestHandler):

        @gen.coroutine
        def get(self):
            self.final_return = self.finish()
            yield self.final_return

    def test_finish_method_returns_completed_future(self):
        response = self.fetch('/finish')
        self.assertEqual(response.code, 200)
        self.assertIsInstance(self.final_return, Future)
        self.assertTrue(self.final_return.done())
```

**Expected Behavior:**
The `Future` returned by the `finish()` method should be marked as done (`self.final_return.done()` returns `True`) after the response is successfully written. This ensures that any asynchronous operations depending on this `Future` can proceed without issues.

**Actual Behavior:**
The `Future` returned by `finish()` is not marked as done (`self.final_return.done()` returns `False`), causing asynchronous operations that await this `Future` to hang indefinitely.

[/ISSUE]
--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.

--- BEGIN FILE ---
```
### tornado/http1connection.py
#
# Copyright 2014 Facebook
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

"""Client and server implementations of HTTP/1.x.

.. versionadded:: 4.0
"""

import asyncio
import logging
import re
import types

from tornado.concurrent import (
    Future,
    future_add_done_callback,
    future_set_result_unless_cancelled,
)
from tornado.escape import native_str, utf8
from tornado import gen
from tornado import httputil
from tornado import iostream
from tornado.log import gen_log, app_log
from tornado.util import GzipDecompressor


from typing import cast, Optional, Type, Awaitable, Callable, Union, Tuple


class _QuietException(Exception):
    def __init__(self) -> None:
        pass


class _ExceptionLoggingContext(object):
    """Used with the ``with`` statement when calling delegate methods to
    log any exceptions with the given logger.  Any exceptions caught are
    converted to _QuietException
    """

    def __init__(self, logger: logging.Logger) -> None:
        self.logger = logger

    def __enter__(self) -> None:
        pass

    def __exit__(
        self,
        typ: Optional[Type[BaseException]],
        value: Optional[BaseException],
        tb: types.TracebackType,
    ) -> None:
        if value is not None:
            assert typ is not None
            self.logger.error("Uncaught exception", exc_info=(typ, value, tb))
            raise _QuietException


class HTTP1ConnectionParameters(object):
    """Parameters for `.HTTP1Connection` and `.HTTP1ServerConnection`.
    """

    def __init__(
        self,
        no_keep_alive: bool = False,
        chunk_size: int = None,
        max_header_size: int = None,
        header_timeout: float = None,
        max_body_size: int = None,
        body_timeout: float = None,
        decompress: bool = False,
    ) -> None:
        """
        :arg bool no_keep_alive: If true, always close the connection after
            one request.
        :arg int chunk_size: how much data to read into memory at once
        :arg int max_header_size:  maximum amount of data for HTTP headers
        :arg float header_timeout: how long to wait for all headers (seconds)
        :arg int max_body_size: maximum amount of data for body
        :arg float body_timeout: how long to wait while reading body (seconds)
        :arg bool decompress: if true, decode incoming
            ``Content-Encoding: gzip``
        """
        self.no_keep_alive = no_keep_alive
        self.chunk_size = chunk_size or 65536
        self.max_header_size = max_header_size or 65536
        self.header_timeout = header_timeout
        self.max_body_size = max_body_size
        self.body_timeout = body_timeout
        self.decompress = decompress


class HTTP1Connection(httputil.HTTPConnection):
    """Implements the HTTP/1.x protocol.

    This class can be on its own for clients, or via `HTTP1ServerConnection`
    for servers.
    """

    def __init__(
        self,
        stream: iostream.IOStream,
        is_client: bool,
        params: HTTP1ConnectionParameters = None,
        context: object = None,
    ) -> None:
        """
        :arg stream: an `.IOStream`
        :arg bool is_client: client or server
        :arg params: a `.HTTP1ConnectionParameters` instance or ``None``
        :arg context: an opaque application-defined object that can be accessed
            as ``connection.context``.
        """
        self.is_client = is_client
        self.stream = stream
        if params is None:
            params = HTTP1ConnectionParameters()
        self.params = params
        self.context = context
        self.no_keep_alive = params.no_keep_alive
        # The body limits can be altered by the delegate, so save them
        # here instead of just referencing self.params later.
        self._max_body_size = self.params.max_body_size or self.stream.max_buffer_size
        self._body_timeout = self.params.body_timeout
        # _write_finished is set to True when finish() has been called,
        # i.e. there will be no more data sent.  Data may still be in the
        # stream's write buffer.
        self._write_finished = False
        # True when we have read the entire incoming body.
        self._read_finished = False
        # _finish_future resolves when all data has been written and flushed
        # to the IOStream.
        self._finish_future = Future()  # type: Future[None]
        # If true, the connection should be closed after this request
        # (after the response has been written in the server side,
        # and after it has been read in the client)
        self._disconnect_on_finish = False
        self._clear_callbacks()
        # Save the start lines after we read or write them; they
        # affect later processing (e.g. 304 responses and HEAD methods
        # have content-length but no bodies)
        self._request_start_line = None  # type: Optional[httputil.RequestStartLine]
        self._response_start_line = None  # type: Optional[httputil.ResponseStartLine]
        self._request_headers = None  # type: Optional[httputil.HTTPHeaders]
        # True if we are writing output with chunked encoding.
        self._chunking_output = False
        # While reading a body with a content-length, this is the
        # amount left to read.
        self._expected_content_remaining = None  # type: Optional[int]
        # A Future for our outgoing writes, returned by IOStream.write.
        self._pending_write = None  # type: Optional[Future[None]]

    def read_response(self, delegate: httputil.HTTPMessageDelegate) -> Awaitable[bool]:
        """Read a single HTTP response.

        Typical client-mode usage is to write a request using `write_headers`,
        `write`, and `finish`, and then call ``read_response``.

        :arg delegate: a `.HTTPMessageDelegate`

        Returns a `.Future` that resolves to a bool after the full response has
        been read. The result is true if the stream is still open.
        """
        if self.params.decompress:
            delegate = _GzipMessageDelegate(delegate, self.params.chunk_size)
        return self._read_message(delegate)

    async def _read_message(self, delegate: httputil.HTTPMessageDelegate) -> bool:
        need_delegate_close = False
        try:
            header_future = self.stream.read_until_regex(
                b"\r?\n\r?\n", max_bytes=self.params.max_header_size
            )
            if self.params.header_timeout is None:
                header_data = await header_future
            else:
                try:
                    header_data = await gen.with_timeout(
                        self.stream.io_loop.time() + self.params.header_timeout,
                        header_future,
                        quiet_exceptions=iostream.StreamClosedError,
                    )
                except gen.TimeoutError:
                    self.close()
                    return False
            start_line_str, headers = self._parse_headers(header_data)
            if self.is_client:
                resp_start_line = httputil.parse_response_start_line(start_line_str)
                self._response_start_line = resp_start_line
                start_line = (
                    resp_start_line
                )  # type: Union[httputil.RequestStartLine, httputil.ResponseStartLine]
                # TODO: this will need to change to support client-side keepalive
                self._disconnect_on_finish = False
            else:
                req_start_line = httputil.parse_request_start_line(start_line_str)
                self._request_start_line = req_start_line
                self._request_headers = headers
                start_line = req_start_line
                self._disconnect_on_finish = not self._can_keep_alive(
                    req_start_line, headers
                )
            need_delegate_close = True
            with _ExceptionLoggingContext(app_log):
                header_recv_future = delegate.headers_received(start_line, headers)
                if header_recv_future is not None:
                    await header_recv_future
            if self.stream is None:
                # We've been detached.
                need_delegate_close = False
                return False
            skip_body = False
            if self.is_client:
                assert isinstance(start_line, httputil.ResponseStartLine)
                if (
                    self._request_start_line is not None
                    and self._request_start_line.method == "HEAD"
                ):
                    skip_body = True
                code = start_line.code
                if code == 304:
                    # 304 responses may include the content-length header
                    # but do not actually have a body.
                    # http://tools.ietf.org/html/rfc7230#section-3.3
                    skip_body = True
                if code >= 100 and code < 200:
                    # 1xx responses should never indicate the presence of
                    # a body.
                    if "Content-Length" in headers or "Transfer-Encoding" in headers:
                        raise httputil.HTTPInputError(
                            "Response code %d cannot have body" % code
                        )
                    # TODO: client delegates will get headers_received twice
                    # in the case of a 100-continue.  Document or change?
                    await self._read_message(delegate)
            else:
                if headers.get("Expect") == "100-continue" and not self._write_finished:
                    self.stream.write(b"HTTP/1.1 100 (Continue)\r\n\r\n")
            if not skip_body:
                body_future = self._read_body(
                    resp_start_line.code if self.is_client else 0, headers, delegate
                )
                if body_future is not None:
                    if self._body_timeout is None:
                        await body_future
                    else:
                        try:
                            await gen.with_timeout(
                                self.stream.io_loop.time() + self._body_timeout,
                                body_future,
                                quiet_exceptions=iostream.StreamClosedError,
                            )
                        except gen.TimeoutError:
                            gen_log.info("Timeout reading body from %s", self.context)
                            self.stream.close()
                            return False
            self._read_finished = True
            if not self._write_finished or self.is_client:
                need_delegate_close = False
                with _ExceptionLoggingContext(app_log):
                    delegate.finish()
            # If we're waiting for the application to produce an asynchronous
            # response, and we're not detached, register a close callback
            # on the stream (we didn't need one while we were reading)
            if (
                not self._finish_future.done()
                and self.stream is not None
                and not self.stream.closed()
            ):
                self.stream.set_close_callback(self._on_connection_close)
                await self._finish_future
            if self.is_client and self._disconnect_on_finish:
                self.close()
            if self.stream is None:
                return False
        except httputil.HTTPInputError as e:
            gen_log.info("Malformed HTTP message from %s: %s", self.context, e)
            if not self.is_client:
                await self.stream.write(b"HTTP/1.1 400 Bad Request\r\n\r\n")
            self.close()
            return False
        finally:
            if need_delegate_close:
                with _ExceptionLoggingContext(app_log):
                    delegate.on_connection_close()
            header_future = None  # type: ignore
            self._clear_callbacks()
        return True

    def _clear_callbacks(self) -> None:
        """Clears the callback attributes.

        This allows the request handler to be garbage collected more
        quickly in CPython by breaking up reference cycles.
        """
        self._write_callback = None
        self._write_future = None  # type: Optional[Future[None]]
        self._close_callback = None  # type: Optional[Callable[[], None]]
        if self.stream is not None:
            self.stream.set_close_callback(None)

    def set_close_callback(self, callback: Optional[Callable[[], None]]) -> None:
        """Sets a callback that will be run when the connection is closed.

        Note that this callback is slightly different from
        `.HTTPMessageDelegate.on_connection_close`: The
        `.HTTPMessageDelegate` method is called when the connection is
        closed while recieving a message. This callback is used when
        there is not an active delegate (for example, on the server
        side this callback is used if the client closes the connection
        after sending its request but before receiving all the
        response.
        """
        self._close_callback = callback

    def _on_connection_close(self) -> None:
        # Note that this callback is only registered on the IOStream
        # when we have finished reading the request and are waiting for
        # the application to produce its response.
        if self._close_callback is not None:
            callback = self._close_callback
            self._close_callback = None
            callback()
        if not self._finish_future.done():
            future_set_result_unless_cancelled(self._finish_future, None)
        self._clear_callbacks()

    def close(self) -> None:
        if self.stream is not None:
            self.stream.close()
        self._clear_callbacks()
        if not self._finish_future.done():
            future_set_result_unless_cancelled(self._finish_future, None)

    def detach(self) -> iostream.IOStream:
        """Take control of the underlying stream.

        Returns the underlying `.IOStream` object and stops all further
        HTTP processing.  May only be called during
        `.HTTPMessageDelegate.headers_received`.  Intended for implementing
        protocols like websockets that tunnel over an HTTP handshake.
        """
        self._clear_callbacks()
        stream = self.stream
        self.stream = None  # type: ignore
        if not self._finish_future.done():
            future_set_result_unless_cancelled(self._finish_future, None)
        return stream

    def set_body_timeout(self, timeout: float) -> None:
        """Sets the body timeout for a single request.

        Overrides the value from `.HTTP1ConnectionParameters`.
        """
        self._body_timeout = timeout

    def set_max_body_size(self, max_body_size: int) -> None:
        """Sets the body size limit for a single request.

        Overrides the value from `.HTTP1ConnectionParameters`.
        """
        self._max_body_size = max_body_size

    def write_headers(
        self,
        start_line: Union[httputil.RequestStartLine, httputil.ResponseStartLine],
        headers: httputil.HTTPHeaders,
        chunk: bytes = None,
    ) -> "Future[None]":
        """Implements `.HTTPConnection.write_headers`."""
        lines = []
        if self.is_client:
            assert isinstance(start_line, httputil.RequestStartLine)
            self._request_start_line = start_line
            lines.append(utf8("%s %s HTTP/1.1" % (start_line[0], start_line[1])))
            # Client requests with a non-empty body must have either a
            # Content-Length or a Transfer-Encoding.
            self._chunking_output = (
                start_line.method in ("POST", "PUT", "PATCH")
                and "Content-Length" not in headers
                and "Transfer-Encoding" not in headers
            )
        else:
            assert isinstance(start_line, httputil.ResponseStartLine)
            assert self._request_start_line is not None
            assert self._request_headers is not None
            self._response_start_line = start_line
            lines.append(utf8("HTTP/1.1 %d %s" % (start_line[1], start_line[2])))
            self._chunking_output = (
                # TODO: should this use
                # self._request_start_line.version or
                # start_line.version?
                self._request_start_line.version == "HTTP/1.1"
                # 1xx, 204 and 304 responses have no body (not even a zero-length
                # body), and so should not have either Content-Length or
                # Transfer-Encoding headers.
                and start_line.code not in (204, 304)
                and (start_line.code < 100 or start_line.code >= 200)
                # No need to chunk the output if a Content-Length is specified.
                and "Content-Length" not in headers
                # Applications are discouraged from touching Transfer-Encoding,
                # but if they do, leave it alone.
                and "Transfer-Encoding" not in headers
            )
            # If connection to a 1.1 client will be closed, inform client
            if (
                self._request_start_line.version == "HTTP/1.1"
                and self._disconnect_on_finish
            ):
                headers["Connection"] = "close"
            # If a 1.0 client asked for keep-alive, add the header.
            if (
                self._request_start_line.version == "HTTP/1.0"
                and self._request_headers.get("Connection", "").lower() == "keep-alive"
            ):
                headers["Connection"] = "Keep-Alive"
        if self._chunking_output:
            headers["Transfer-Encoding"] = "chunked"
        if not self.is_client and (
            self._request_start_line.method == "HEAD"
            or cast(httputil.ResponseStartLine, start_line).code == 304
        ):
            self._expected_content_remaining = 0
        elif "Content-Length" in headers:
            self._expected_content_remaining = int(headers["Content-Length"])
        else:
            self._expected_content_remaining = None
        # TODO: headers are supposed to be of type str, but we still have some
        # cases that let bytes slip through. Remove these native_str calls when those
        # are fixed.
        header_lines = (
            native_str(n) + ": " + native_str(v) for n, v in headers.get_all()
        )
        lines.extend(l.encode("latin1") for l in header_lines)
        for line in lines:
            if b"\n" in line:
                raise ValueError("Newline in header: " + repr(line))
        future = None
        if self.stream.closed():
            future = self._write_future = Future()
            future.set_exception(iostream.StreamClosedError())
            future.exception()
        else:
            future = self._write_future = Future()
            data = b"\r\n".join(lines) + b"\r\n\r\n"
            if chunk:
                data += self._format_chunk(chunk)
            self._pending_write = self.stream.write(data)
            future_add_done_callback(self._pending_write, self._on_write_complete)
        return future

    def _format_chunk(self, chunk: bytes) -> bytes:
        if self._expected_content_remaining is not None:
            self._expected_content_remaining -= len(chunk)
            if self._expected_content_remaining < 0:
                # Close the stream now to stop further framing errors.
                self.stream.close()
                raise httputil.HTTPOutputError(
                    "Tried to write more data than Content-Length"
                )
        if self._chunking_output and chunk:
            # Don't write out empty chunks because that means END-OF-STREAM
            # with chunked encoding
            return utf8("%x" % len(chunk)) + b"\r\n" + chunk + b"\r\n"
        else:
            return chunk

    def write(self, chunk: bytes) -> "Future[None]":
        """Implements `.HTTPConnection.write`.

        For backwards compatibility it is allowed but deprecated to
        skip `write_headers` and instead call `write()` with a
        pre-encoded header block.
        """
        future = None
        if self.stream.closed():
            future = self._write_future = Future()
            self._write_future.set_exception(iostream.StreamClosedError())
            self._write_future.exception()
        else:
            future = self._write_future = Future()
            self._pending_write = self.stream.write(self._format_chunk(chunk))
            self._pending_write.add_done_callback(self._on_write_complete)
        return future

    def finish(self) -> None:
        """Implements `.HTTPConnection.finish`."""
        if (
            self._expected_content_remaining is not None
            and self._expected_content_remaining != 0
            and not self.stream.closed()
        ):
            self.stream.close()
            raise httputil.HTTPOutputError(
                "Tried to write %d bytes less than Content-Length"
                % self._expected_content_remaining
            )
        if self._chunking_output:
            if not self.stream.closed():
                self._pending_write = self.stream.write(b"0\r\n\r\n")
                self._pending_write.add_done_callback(self._on_write_complete)
        self._write_finished = True
        # If the app finished the request while we're still reading,
        # divert any remaining data away from the delegate and
        # close the connection when we're done sending our response.
        # Closing the connection is the only way to avoid reading the
        # whole input body.
        if not self._read_finished:
            self._disconnect_on_finish = True
        # No more data is coming, so instruct TCP to send any remaining
        # data immediately instead of waiting for a full packet or ack.
        self.stream.set_nodelay(True)
        if self._pending_write is None:
            self._finish_request(None)
        else:
            future_add_done_callback(self._pending_write, self._finish_request)

    def _on_write_complete(self, future: "Future[None]") -> None:
        exc = future.exception()
        if exc is not None and not isinstance(exc, iostream.StreamClosedError):
            future.result()
        if self._write_callback is not None:
            callback = self._write_callback
            self._write_callback = None
            self.stream.io_loop.add_callback(callback)
        if self._write_future is not None:
            future = self._write_future
            self._write_future = None
            future_set_result_unless_cancelled(future, None)

    def _can_keep_alive(
        self, start_line: httputil.RequestStartLine, headers: httputil.HTTPHeaders
    ) -> bool:
        if self.params.no_keep_alive:
            return False
        connection_header = headers.get("Connection")
        if connection_header is not None:
            connection_header = connection_header.lower()
        if start_line.version == "HTTP/1.1":
            return connection_header != "close"
        elif (
            "Content-Length" in headers
            or headers.get("Transfer-Encoding", "").lower() == "chunked"
            or getattr(start_line, "method", None) in ("HEAD", "GET")
        ):
            # start_line may be a request or response start line; only
            # the former has a method attribute.
            return connection_header == "keep-alive"
        return False

    def _finish_request(self, future: Optional["Future[None]"]) -> None:
        self._clear_callbacks()
        if not self.is_client and self._disconnect_on_finish:
            self.close()
            return
        # Turn Nagle's algorithm back on, leaving the stream in its
        # default state for the next request.
        self.stream.set_nodelay(False)
        if not self._finish_future.done():
            future_set_result_unless_cancelled(self._finish_future, None)

    def _parse_headers(self, data: bytes) -> Tuple[str, httputil.HTTPHeaders]:
        # The lstrip removes newlines that some implementations sometimes
        # insert between messages of a reused connection.  Per RFC 7230,
        # we SHOULD ignore at least one empty line before the request.
        # http://tools.ietf.org/html/rfc7230#section-3.5
        data_str = native_str(data.decode("latin1")).lstrip("\r\n")
        # RFC 7230 section allows for both CRLF and bare LF.
        eol = data_str.find("\n")
        start_line = data_str[:eol].rstrip("\r")
        headers = httputil.HTTPHeaders.parse(data_str[eol:])
        return start_line, headers

    def _read_body(
        self,
        code: int,
        headers: httputil.HTTPHeaders,
        delegate: httputil.HTTPMessageDelegate,
    ) -> Optional[Awaitable[None]]:
        if "Content-Length" in headers:
            if "Transfer-Encoding" in headers:
                # Response cannot contain both Content-Length and
                # Transfer-Encoding headers.
                # http://tools.ietf.org/html/rfc7230#section-3.3.3
                raise httputil.HTTPInputError(
                    "Response with both Transfer-Encoding and Content-Length"
                )
            if "," in headers["Content-Length"]:
                # Proxies sometimes cause Content-Length headers to get
                # duplicated.  If all the values are identical then we can
                # use them but if they differ it's an error.
                pieces = re.split(r",\s*", headers["Content-Length"])
                if any(i != pieces[0] for i in pieces):
                    raise httputil.HTTPInputError(
                        "Multiple unequal Content-Lengths: %r"
                        % headers["Content-Length"]
                    )
                headers["Content-Length"] = pieces[0]

            try:
                content_length = int(headers["Content-Length"])  # type: Optional[int]
            except ValueError:
                # Handles non-integer Content-Length value.
                raise httputil.HTTPInputError(
                    "Only integer Content-Length is allowed: %s"
                    % headers["Content-Length"]
                )

            if cast(int, content_length) > self._max_body_size:
                raise httputil.HTTPInputError("Content-Length too long")
        else:
            content_length = None

        if code == 204:
            # This response code is not allowed to have a non-empty body,
            # and has an implicit length of zero instead of read-until-close.
            # http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.3
            if "Transfer-Encoding" in headers or content_length not in (None, 0):
                raise httputil.HTTPInputError(
                    "Response with code %d should not have body" % code
                )
            content_length = 0

        if content_length is not None:
            return self._read_fixed_body(content_length, delegate)
        if headers.get("Transfer-Encoding", "").lower() == "chunked":
            return self._read_chunked_body(delegate)
        if self.is_client:
            return self._read_body_until_close(delegate)
        return None

    async def _read_fixed_body(
        self, content_length: int, delegate: httputil.HTTPMessageDelegate
    ) -> None:
        while content_length > 0:
            body = await self.stream.read_bytes(
                min(self.params.chunk_size, content_length), partial=True
            )
            content_length -= len(body)
            if not self._write_finished or self.is_client:
                with _ExceptionLoggingContext(app_log):
                    ret = delegate.data_received(body)
                    if ret is not None:
                        await ret

    async def _read_chunked_body(self, delegate: httputil.HTTPMessageDelegate) -> None:
        # TODO: "chunk extensions" http://tools.ietf.org/html/rfc2616#section-3.6.1
        total_size = 0
        while True:
            chunk_len_str = await self.stream.read_until(b"\r\n", max_bytes=64)
            chunk_len = int(chunk_len_str.strip(), 16)
            if chunk_len == 0:
                crlf = await self.stream.read_bytes(2)
                if crlf != b"\r\n":
                    raise httputil.HTTPInputError(
                        "improperly terminated chunked request"
                    )
                return
            total_size += chunk_len
            if total_size > self._max_body_size:
                raise httputil.HTTPInputError("chunked body too large")
            bytes_to_read = chunk_len
            while bytes_to_read:
                chunk = await self.stream.read_bytes(
                    min(bytes_to_read, self.params.chunk_size), partial=True
                )
                bytes_to_read -= len(chunk)
                if not self._write_finished or self.is_client:
                    with _ExceptionLoggingContext(app_log):
                        ret = delegate.data_received(chunk)
                        if ret is not None:
                            await ret
            # chunk ends with \r\n
            crlf = await self.stream.read_bytes(2)
            assert crlf == b"\r\n"

    async def _read_body_until_close(
        self, delegate: httputil.HTTPMessageDelegate
    ) -> None:
        body = await self.stream.read_until_close()
        if not self._write_finished or self.is_client:
            with _ExceptionLoggingContext(app_log):
                ret = delegate.data_received(body)
                if ret is not None:
                    await ret


class _GzipMessageDelegate(httputil.HTTPMessageDelegate):
    """Wraps an `HTTPMessageDelegate` to decode ``Content-Encoding: gzip``.
    """

    def __init__(self, delegate: httputil.HTTPMessageDelegate, chunk_size: int) -> None:
        self._delegate = delegate
        self._chunk_size = chunk_size
        self._decompressor = None  # type: Optional[GzipDecompressor]

    def headers_received(
        self,
        start_line: Union[httputil.RequestStartLine, httputil.ResponseStartLine],
        headers: httputil.HTTPHeaders,
    ) -> Optional[Awaitable[None]]:
        if headers.get("Content-Encoding") == "gzip":
            self._decompressor = GzipDecompressor()
            # Downstream delegates will only see uncompressed data,
            # so rename the content-encoding header.
            # (but note that curl_httpclient doesn't do this).
            headers.add("X-Consumed-Content-Encoding", headers["Content-Encoding"])
            del headers["Content-Encoding"]
        return self._delegate.headers_received(start_line, headers)

    async def data_received(self, chunk: bytes) -> None:
        if self._decompressor:
            compressed_data = chunk
            while compressed_data:
                decompressed = self._decompressor.decompress(
                    compressed_data, self._chunk_size
                )
                if decompressed:
                    ret = self._delegate.data_received(decompressed)
                    if ret is not None:
                        await ret
                compressed_data = self._decompressor.unconsumed_tail
        else:
            ret = self._delegate.data_received(chunk)
            if ret is not None:
                await ret

    def finish(self) -> None:
        if self._decompressor is not None:
            tail = self._decompressor.flush()
            if tail:
                # The tail should always be empty: decompress returned
                # all that it can in data_received and the only
                # purpose of the flush call is to detect errors such
                # as truncated input. If we did legitimately get a new
                # chunk at this point we'd need to change the
                # interface to make finish() a coroutine.
                raise ValueError(
                    "decompressor.flush returned data; possile truncated input"
                )
        return self._delegate.finish()

    def on_connection_close(self) -> None:
        return self._delegate.on_connection_close()


class HTTP1ServerConnection(object):
    """An HTTP/1.x server."""

    def __init__(
        self,
        stream: iostream.IOStream,
        params: HTTP1ConnectionParameters = None,
        context: object = None,
    ) -> None:
        """
        :arg stream: an `.IOStream`
        :arg params: a `.HTTP1ConnectionParameters` or None
        :arg context: an opaque application-defined object that is accessible
            as ``connection.context``
        """
        self.stream = stream
        if params is None:
            params = HTTP1ConnectionParameters()
        self.params = params
        self.context = context
        self._serving_future = None  # type: Optional[Future[None]]

    async def close(self) -> None:
        """Closes the connection.

        Returns a `.Future` that resolves after the serving loop has exited.
        """
        self.stream.close()
        # Block until the serving loop is done, but ignore any exceptions
        # (start_serving is already responsible for logging them).
        assert self._serving_future is not None
        try:
            await self._serving_future
        except Exception:
            pass

    def start_serving(self, delegate: httputil.HTTPServerConnectionDelegate) -> None:
        """Starts serving requests on this connection.

        :arg delegate: a `.HTTPServerConnectionDelegate`
        """
        assert isinstance(delegate, httputil.HTTPServerConnectionDelegate)
        fut = gen.convert_yielded(self._server_request_loop(delegate))
        self._serving_future = fut
        # Register the future on the IOLoop so its errors get logged.
        self.stream.io_loop.add_future(fut, lambda f: f.result())

    async def _server_request_loop(
        self, delegate: httputil.HTTPServerConnectionDelegate
    ) -> None:
        try:
            while True:
                conn = HTTP1Connection(self.stream, False, self.params, self.context)
                request_delegate = delegate.start_request(self, conn)
                try:
                    ret = await conn.read_response(request_delegate)
                except (iostream.StreamClosedError, iostream.UnsatisfiableReadError):
                    return
                except _QuietException:
                    # This exception was already logged.
                    conn.close()
                    return
                except Exception:
                    gen_log.error("Uncaught exception", exc_info=True)
                    conn.close()
                    return
                if not ret:
                    return
                await asyncio.sleep(0)
        finally:
            delegate.on_close(self)

```
--- END FILE ---

Please first localize the bug based on the issue statement, and then generate *SEARCH/REPLACE* edits to fix the issue.

Every *SEARCH/REPLACE* edit must use this format:
1. The file path
2. The start of search block: <<<<<<< SEARCH
3. A contiguous chunk of lines to search for in the existing source code
4. The dividing line: =======
5. The lines to replace into the source code
6. The end of the replace block: >>>>>>> REPLACE

Here is an example:

```python
### mathweb/flask/app.py
<<<<<<< SEARCH
from flask import Flask
=======
import math
from flask import Flask
>>>>>>> REPLACE
```
</solution>

Please note that the *SEARCH/REPLACE* edit REQUIRES PROPER INDENTATION. If you would like to add the line '        print(x)', you must fully write that out, with all those spaces before the code!
Wrap each *SEARCH/REPLACE* edit in a code block as shown in the example above. If you have multiple *SEARCH/REPLACE* edits, use a separate code block for each one.
user