{
    "file_diffs": [
        {
            "old_file_content": "\"\"\"Blocking and non-blocking HTTP client interfaces.\n\nThis module defines a common interface shared by two implementations,\n``simple_httpclient`` and ``curl_httpclient``.  Applications may either\ninstantiate their chosen implementation class directly or use the\n`AsyncHTTPClient` class from this module, which selects an implementation\nthat can be overridden with the `AsyncHTTPClient.configure` method.\n\nThe default implementation is ``simple_httpclient``, and this is expected\nto be suitable for most users' needs.  However, some applications may wish\nto switch to ``curl_httpclient`` for reasons such as the following:\n\n* ``curl_httpclient`` has some features not found in ``simple_httpclient``,\n  including support for HTTP proxies and the ability to use a specified\n  network interface.\n\n* ``curl_httpclient`` is more likely to be compatible with sites that are\n  not-quite-compliant with the HTTP spec, or sites that use little-exercised\n  features of HTTP.\n\n* ``curl_httpclient`` is faster.\n\n* ``curl_httpclient`` was the default prior to Tornado 2.0.\n\nNote that if you are using ``curl_httpclient``, it is highly recommended that\nyou use a recent version of ``libcurl`` and ``pycurl``.  Currently the minimum\nsupported version is 7.18.2, and the recommended version is 7.21.1 or newer.\n\"\"\"\n\nfrom __future__ import absolute_import, division, print_function, with_statement\n\nimport functools\nimport time\nimport weakref\n\nfrom tornado.concurrent import Future\nfrom tornado.escape import utf8\nfrom tornado import httputil, stack_context\nfrom tornado.ioloop import IOLoop\nfrom tornado.util import Configurable\n\n\nclass HTTPClient(object):\n    \"\"\"A blocking HTTP client.\n\n    This interface is provided for convenience and testing; most applications\n    that are running an IOLoop will want to use `AsyncHTTPClient` instead.\n    Typical usage looks like this::\n\n        http_client = httpclient.HTTPClient()\n        try:\n            response = http_client.fetch(\"http://www.google.com/\")\n            print response.body\n        except httpclient.HTTPError as e:\n            print \"Error:\", e\n        httpclient.close()\n    \"\"\"\n    def __init__(self, async_client_class=None, **kwargs):\n        self._io_loop = IOLoop()\n        if async_client_class is None:\n            async_client_class = AsyncHTTPClient\n        self._async_client = async_client_class(self._io_loop, **kwargs)\n        self._closed = False\n\n    def __del__(self):\n        self.close()\n\n    def close(self):\n        \"\"\"Closes the HTTPClient, freeing any resources used.\"\"\"\n        if not self._closed:\n            self._async_client.close()\n            self._io_loop.close()\n            self._closed = True\n\n    def fetch(self, request, **kwargs):\n        \"\"\"Executes a request, returning an `HTTPResponse`.\n\n        The request may be either a string URL or an `HTTPRequest` object.\n        If it is a string, we construct an `HTTPRequest` using any additional\n        kwargs: ``HTTPRequest(request, **kwargs)``\n\n        If an error occurs during the fetch, we raise an `HTTPError`.\n        \"\"\"\n        response = self._io_loop.run_sync(functools.partial(\n            self._async_client.fetch, request, **kwargs))\n        response.rethrow()\n        return response\n\n\nclass AsyncHTTPClient(Configurable):\n    \"\"\"An non-blocking HTTP client.\n\n    Example usage::\n\n        def handle_request(response):\n            if response.error:\n                print \"Error:\", response.error\n            else:\n                print response.body\n\n        http_client = AsyncHTTPClient()\n        http_client.fetch(\"http://www.google.com/\", handle_request)\n\n    The constructor for this class is magic in several respects: It\n    actually creates an instance of an implementation-specific\n    subclass, and instances are reused as a kind of pseudo-singleton\n    (one per `.IOLoop`).  The keyword argument ``force_instance=True``\n    can be used to suppress this singleton behavior.  Constructor\n    arguments other than ``io_loop`` and ``force_instance`` are\n    deprecated.  The implementation subclass as well as arguments to\n    its constructor can be set with the static method `configure()`\n    \"\"\"\n    @classmethod\n    def configurable_base(cls):\n        return AsyncHTTPClient\n\n    @classmethod\n    def configurable_default(cls):\n        from tornado.simple_httpclient import SimpleAsyncHTTPClient\n        return SimpleAsyncHTTPClient\n\n    @classmethod\n    def _async_clients(cls):\n        attr_name = '_async_client_dict_' + cls.__name__\n        if not hasattr(cls, attr_name):\n            setattr(cls, attr_name, weakref.WeakKeyDictionary())\n        return getattr(cls, attr_name)\n\n    def __new__(cls, io_loop=None, force_instance=False, **kwargs):\n        io_loop = io_loop or IOLoop.current()\n        if io_loop in cls._async_clients() and not force_instance:\n            return cls._async_clients()[io_loop]\n        instance = super(AsyncHTTPClient, cls).__new__(cls, io_loop=io_loop,\n                                                       **kwargs)\n        if not force_instance:\n            cls._async_clients()[io_loop] = instance\n        return instance\n\n    def initialize(self, io_loop, defaults=None):\n        self.io_loop = io_loop\n        self.defaults = dict(HTTPRequest._DEFAULTS)\n        if defaults is not None:\n            self.defaults.update(defaults)\n\n    def close(self):\n        \"\"\"Destroys this HTTP client, freeing any file descriptors used.\n        Not needed in normal use, but may be helpful in unittests that\n        create and destroy http clients.  No other methods may be called\n        on the `AsyncHTTPClient` after ``close()``.\n        \"\"\"\n        if self._async_clients().get(self.io_loop) is self:\n            del self._async_clients()[self.io_loop]\n\n    def fetch(self, request, callback=None, **kwargs):\n        \"\"\"Executes a request, asynchronously returning an `HTTPResponse`.\n\n        The request may be either a string URL or an `HTTPRequest` object.\n        If it is a string, we construct an `HTTPRequest` using any additional\n        kwargs: ``HTTPRequest(request, **kwargs)``\n\n        This method returns a `.Future` whose result is an\n        `HTTPResponse`.  The ``Future`` wil raise an `HTTPError` if\n        the request returned a non-200 response code.\n\n        If a ``callback`` is given, it will be invoked with the `HTTPResponse`.\n        In the callback interface, `HTTPError` is not automatically raised.\n        Instead, you must check the response's ``error`` attribute or\n        call its `~HTTPResponse.rethrow` method.\n        \"\"\"\n        if not isinstance(request, HTTPRequest):\n            request = HTTPRequest(url=request, **kwargs)\n        # We may modify this (to add Host, Accept-Encoding, etc),\n        # so make sure we don't modify the caller's object.  This is also\n        # where normal dicts get converted to HTTPHeaders objects.\n        request.headers = httputil.HTTPHeaders(request.headers)\n        request = _RequestProxy(request, self.defaults)\n        future = Future()\n        if callback is not None:\n            callback = stack_context.wrap(callback)\n\n            def handle_future(future):\n                exc = future.exception()\n                if isinstance(exc, HTTPError) and exc.response is not None:\n                    response = exc.response\n                elif exc is not None:\n                    response = HTTPResponse(\n                        request, 599, error=exc,\n                        request_time=time.time() - request.start_time)\n                else:\n                    response = future.result()\n                self.io_loop.add_callback(callback, response)\n            future.add_done_callback(handle_future)\n\n        def handle_response(response):\n            if response.error:\n                future.set_exception(response.error)\n            else:\n                future.set_result(response)\n        self.fetch_impl(request, handle_response)\n        return future\n\n    def fetch_impl(self, request, callback):\n        raise NotImplementedError()\n\n    @classmethod\n    def configure(cls, impl, **kwargs):\n        \"\"\"Configures the `AsyncHTTPClient` subclass to use.\n\n        ``AsyncHTTPClient()`` actually creates an instance of a subclass.\n        This method may be called with either a class object or the\n        fully-qualified name of such a class (or ``None`` to use the default,\n        ``SimpleAsyncHTTPClient``)\n\n        If additional keyword arguments are given, they will be passed\n        to the constructor of each subclass instance created.  The\n        keyword argument ``max_clients`` determines the maximum number\n        of simultaneous `~AsyncHTTPClient.fetch()` operations that can\n        execute in parallel on each `.IOLoop`.  Additional arguments\n        may be supported depending on the implementation class in use.\n\n        Example::\n\n           AsyncHTTPClient.configure(\"tornado.curl_httpclient.CurlAsyncHTTPClient\")\n        \"\"\"\n        super(AsyncHTTPClient, cls).configure(impl, **kwargs)\n\n\nclass HTTPRequest(object):\n    \"\"\"HTTP client request object.\"\"\"\n\n    # Default values for HTTPRequest parameters.\n    # Merged with the values on the request object by AsyncHTTPClient\n    # implementations.\n    _DEFAULTS = dict(\n        connect_timeout=20.0,\n        request_timeout=20.0,\n        follow_redirects=True,\n        max_redirects=5,\n        use_gzip=True,\n        proxy_password='',\n        allow_nonstandard_methods=False,\n        validate_cert=True)\n\n    def __init__(self, url, method=\"GET\", headers=None, body=None,\n                 auth_username=None, auth_password=None,\n                 connect_timeout=None, request_timeout=None,\n                 if_modified_since=None, follow_redirects=None,\n                 max_redirects=None, user_agent=None, use_gzip=None,\n                 network_interface=None, streaming_callback=None,\n                 header_callback=None, prepare_curl_callback=None,\n                 proxy_host=None, proxy_port=None, proxy_username=None,\n                 proxy_password=None, allow_nonstandard_methods=None,\n                 validate_cert=None, ca_certs=None,\n                 allow_ipv6=None,\n                 client_key=None, client_cert=None):\n        r\"\"\"All parameters except ``url`` are optional.\n\n        :arg string url: URL to fetch\n        :arg string method: HTTP method, e.g. \"GET\" or \"POST\"\n        :arg headers: Additional HTTP headers to pass on the request\n        :type headers: `~tornado.httputil.HTTPHeaders` or `dict`\n        :arg string auth_username: Username for HTTP \"Basic\" authentication\n        :arg string auth_password: Password for HTTP \"Basic\" authentication\n        :arg float connect_timeout: Timeout for initial connection in seconds\n        :arg float request_timeout: Timeout for entire request in seconds\n        :arg if_modified_since: Timestamp for ``If-Modified-Since`` header\n        :type if_modified_since: `datetime` or `float`\n        :arg bool follow_redirects: Should redirects be followed automatically\n           or return the 3xx response?\n        :arg int max_redirects: Limit for ``follow_redirects``\n        :arg string user_agent: String to send as ``User-Agent`` header\n        :arg bool use_gzip: Request gzip encoding from the server\n        :arg string network_interface: Network interface to use for request\n        :arg callable streaming_callback: If set, ``streaming_callback`` will\n           be run with each chunk of data as it is received, and\n           ``HTTPResponse.body`` and ``HTTPResponse.buffer`` will be empty in\n           the final response.\n        :arg callable header_callback: If set, ``header_callback`` will\n           be run with each header line as it is received (including the\n           first line, e.g. ``HTTP/1.0 200 OK\\r\\n``, and a final line\n           containing only ``\\r\\n``.  All lines include the trailing newline\n           characters).  ``HTTPResponse.headers`` will be empty in the final\n           response.  This is most useful in conjunction with\n           ``streaming_callback``, because it's the only way to get access to\n           header data while the request is in progress.\n        :arg callable prepare_curl_callback: If set, will be called with\n           a ``pycurl.Curl`` object to allow the application to make additional\n           ``setopt`` calls.\n        :arg string proxy_host: HTTP proxy hostname.  To use proxies,\n           ``proxy_host`` and ``proxy_port`` must be set; ``proxy_username`` and\n           ``proxy_pass`` are optional.  Proxies are currently only supported\n           with ``curl_httpclient``.\n        :arg int proxy_port: HTTP proxy port\n        :arg string proxy_username: HTTP proxy username\n        :arg string proxy_password: HTTP proxy password\n        :arg bool allow_nonstandard_methods: Allow unknown values for ``method``\n           argument?\n        :arg bool validate_cert: For HTTPS requests, validate the server's\n           certificate?\n        :arg string ca_certs: filename of CA certificates in PEM format,\n           or None to use defaults.  Note that in ``curl_httpclient``, if\n           any request uses a custom ``ca_certs`` file, they all must (they\n           don't have to all use the same ``ca_certs``, but it's not possible\n           to mix requests with ``ca_certs`` and requests that use the defaults.\n        :arg bool allow_ipv6: Use IPv6 when available?  Default is false in\n           ``simple_httpclient`` and true in ``curl_httpclient``\n        :arg string client_key: Filename for client SSL key, if any\n        :arg string client_cert: Filename for client SSL certificate, if any\n        \"\"\"\n        if headers is None:\n            headers = httputil.HTTPHeaders()\n        if if_modified_since:\n            headers[\"If-Modified-Since\"] = httputil.format_timestamp(\n                if_modified_since)\n        self.proxy_host = proxy_host\n        self.proxy_port = proxy_port\n        self.proxy_username = proxy_username\n        self.proxy_password = proxy_password\n        self.url = url\n        self.method = method\n        self.headers = headers\n        self.body = utf8(body)\n        self.auth_username = auth_username\n        self.auth_password = auth_password\n        self.connect_timeout = connect_timeout\n        self.request_timeout = request_timeout\n        self.follow_redirects = follow_redirects\n        self.max_redirects = max_redirects\n        self.user_agent = user_agent\n        self.use_gzip = use_gzip\n        self.network_interface = network_interface\n        self.streaming_callback = stack_context.wrap(streaming_callback)\n        self.header_callback = stack_context.wrap(header_callback)\n        self.prepare_curl_callback = stack_context.wrap(prepare_curl_callback)\n        self.allow_nonstandard_methods = allow_nonstandard_methods\n        self.validate_cert = validate_cert\n        self.ca_certs = ca_certs\n        self.allow_ipv6 = allow_ipv6\n        self.client_key = client_key\n        self.client_cert = client_cert\n        self.start_time = time.time()\n\n\nclass HTTPResponse(object):\n    \"\"\"HTTP Response object.\n\n    Attributes:\n\n    * request: HTTPRequest object\n\n    * code: numeric HTTP status code, e.g. 200 or 404\n\n    * reason: human-readable reason phrase describing the status code\n      (with curl_httpclient, this is a default value rather than the\n      server's actual response)\n\n    * headers: `tornado.httputil.HTTPHeaders` object\n\n    * buffer: ``cStringIO`` object for response body\n\n    * body: response body as string (created on demand from ``self.buffer``)\n\n    * error: Exception object, if any\n\n    * request_time: seconds from request start to finish\n\n    * time_info: dictionary of diagnostic timing information from the request.\n      Available data are subject to change, but currently uses timings\n      available from http://curl.haxx.se/libcurl/c/curl_easy_getinfo.html,\n      plus ``queue``, which is the delay (if any) introduced by waiting for\n      a slot under `AsyncHTTPClient`'s ``max_clients`` setting.\n    \"\"\"\n    def __init__(self, request, code, headers=None, buffer=None,\n                 effective_url=None, error=None, request_time=None,\n                 time_info=None, reason=None):\n        self.request = request\n        self.code = code\n        self.reason = reason or httputil.responses.get(code, \"Unknown\")\n        if headers is not None:\n            self.headers = headers\n        else:\n            self.headers = httputil.HTTPHeaders()\n        self.buffer = buffer\n        self._body = None\n        if effective_url is None:\n            self.effective_url = request.url\n        else:\n            self.effective_url = effective_url\n        if error is None:\n            if self.code < 200 or self.code >= 300:\n                self.error = HTTPError(self.code, response=self)\n            else:\n                self.error = None\n        else:\n            self.error = error\n        self.request_time = request_time\n        self.time_info = time_info or {}\n\n    def _get_body(self):\n        if self.buffer is None:\n            return None\n        elif self._body is None:\n            self._body = self.buffer.getvalue()\n\n        return self._body\n\n    body = property(_get_body)\n\n    def rethrow(self):\n        \"\"\"If there was an error on the request, raise an `HTTPError`.\"\"\"\n        if self.error:\n            raise self.error\n\n    def __repr__(self):\n        args = \",\".join(\"%s=%r\" % i for i in sorted(self.__dict__.items()))\n        return \"%s(%s)\" % (self.__class__.__name__, args)\n\n\nclass HTTPError(Exception):\n    \"\"\"Exception thrown for an unsuccessful HTTP request.\n\n    Attributes:\n\n    * ``code`` - HTTP error integer error code, e.g. 404.  Error code 599 is\n      used when no HTTP response was received, e.g. for a timeout.\n\n    * ``response`` - `HTTPResponse` object, if any.\n\n    Note that if ``follow_redirects`` is False, redirects become HTTPErrors,\n    and you can look at ``error.response.headers['Location']`` to see the\n    destination of the redirect.\n    \"\"\"\n    def __init__(self, code, message=None, response=None):\n        self.code = code\n        message = message or httputil.responses.get(code, \"Unknown\")\n        self.response = response\n        Exception.__init__(self, \"HTTP %d: %s\" % (self.code, message))\n\n\nclass _RequestProxy(object):\n    \"\"\"Combines an object with a dictionary of defaults.\n\n    Used internally by AsyncHTTPClient implementations.\n    \"\"\"\n    def __init__(self, request, defaults):\n        self.request = request\n        self.defaults = defaults\n\n    def __getattr__(self, name):\n        request_attr = getattr(self.request, name)\n        if request_attr is not None:\n            return request_attr\n        elif self.defaults is not None:\n            return self.defaults.get(name, None)\n        else:\n            return None\n\n\ndef main():\n    from tornado.options import define, options, parse_command_line\n    define(\"print_headers\", type=bool, default=False)\n    define(\"print_body\", type=bool, default=True)\n    define(\"follow_redirects\", type=bool, default=True)\n    define(\"validate_cert\", type=bool, default=True)\n    args = parse_command_line()\n    client = HTTPClient()\n    for arg in args:\n        try:\n            response = client.fetch(arg,\n                                    follow_redirects=options.follow_redirects,\n                                    validate_cert=options.validate_cert,\n                                    )\n        except HTTPError as e:\n            if e.response is not None:\n                response = e.response\n            else:\n                raise\n        if options.print_headers:\n            print(response.headers)\n        if options.print_body:\n            print(response.body)\n    client.close()\n\nif __name__ == \"__main__\":\n    main()\n",
            "new_file_content": "\"\"\"Blocking and non-blocking HTTP client interfaces.\n\nThis module defines a common interface shared by two implementations,\n``simple_httpclient`` and ``curl_httpclient``.  Applications may either\ninstantiate their chosen implementation class directly or use the\n`AsyncHTTPClient` class from this module, which selects an implementation\nthat can be overridden with the `AsyncHTTPClient.configure` method.\n\nThe default implementation is ``simple_httpclient``, and this is expected\nto be suitable for most users' needs.  However, some applications may wish\nto switch to ``curl_httpclient`` for reasons such as the following:\n\n* ``curl_httpclient`` has some features not found in ``simple_httpclient``,\n  including support for HTTP proxies and the ability to use a specified\n  network interface.\n\n* ``curl_httpclient`` is more likely to be compatible with sites that are\n  not-quite-compliant with the HTTP spec, or sites that use little-exercised\n  features of HTTP.\n\n* ``curl_httpclient`` is faster.\n\n* ``curl_httpclient`` was the default prior to Tornado 2.0.\n\nNote that if you are using ``curl_httpclient``, it is highly recommended that\nyou use a recent version of ``libcurl`` and ``pycurl``.  Currently the minimum\nsupported version is 7.18.2, and the recommended version is 7.21.1 or newer.\n\"\"\"\n\nfrom __future__ import absolute_import, division, print_function, with_statement\n\nimport functools\nimport time\nimport weakref\n\nfrom tornado.concurrent import Future\nfrom tornado.escape import utf8\nfrom tornado import httputil, stack_context\nfrom tornado.ioloop import IOLoop\nfrom tornado.util import Configurable\n\n\nclass HTTPClient(object):\n    \"\"\"A blocking HTTP client.\n\n    This interface is provided for convenience and testing; most applications\n    that are running an IOLoop will want to use `AsyncHTTPClient` instead.\n    Typical usage looks like this::\n\n        http_client = httpclient.HTTPClient()\n        try:\n            response = http_client.fetch(\"http://www.google.com/\")\n            print response.body\n        except httpclient.HTTPError as e:\n            print \"Error:\", e\n        httpclient.close()\n    \"\"\"\n    def __init__(self, async_client_class=None, **kwargs):\n        self._io_loop = IOLoop()\n        if async_client_class is None:\n            async_client_class = AsyncHTTPClient\n        self._async_client = async_client_class(self._io_loop, **kwargs)\n        self._closed = False\n\n    def __del__(self):\n        self.close()\n\n    def close(self):\n        \"\"\"Closes the HTTPClient, freeing any resources used.\"\"\"\n        if not self._closed:\n            self._async_client.close()\n            self._io_loop.close()\n            self._closed = True\n\n    def fetch(self, request, **kwargs):\n        \"\"\"Executes a request, returning an `HTTPResponse`.\n\n        The request may be either a string URL or an `HTTPRequest` object.\n        If it is a string, we construct an `HTTPRequest` using any additional\n        kwargs: ``HTTPRequest(request, **kwargs)``\n\n        If an error occurs during the fetch, we raise an `HTTPError`.\n        \"\"\"\n        response = self._io_loop.run_sync(functools.partial(\n            self._async_client.fetch, request, **kwargs))\n        response.rethrow()\n        return response\n\n\nclass AsyncHTTPClient(Configurable):\n    \"\"\"An non-blocking HTTP client.\n\n    Example usage::\n\n        def handle_request(response):\n            if response.error:\n                print \"Error:\", response.error\n            else:\n                print response.body\n\n        http_client = AsyncHTTPClient()\n        http_client.fetch(\"http://www.google.com/\", handle_request)\n\n    The constructor for this class is magic in several respects: It\n    actually creates an instance of an implementation-specific\n    subclass, and instances are reused as a kind of pseudo-singleton\n    (one per `.IOLoop`).  The keyword argument ``force_instance=True``\n    can be used to suppress this singleton behavior.  Constructor\n    arguments other than ``io_loop`` and ``force_instance`` are\n    deprecated.  The implementation subclass as well as arguments to\n    its constructor can be set with the static method `configure()`\n    \"\"\"\n    @classmethod\n    def configurable_base(cls):\n        return AsyncHTTPClient\n\n    @classmethod\n    def configurable_default(cls):\n        from tornado.simple_httpclient import SimpleAsyncHTTPClient\n        return SimpleAsyncHTTPClient\n\n    @classmethod\n    def _async_clients(cls):\n        attr_name = '_async_client_dict_' + cls.__name__\n        if not hasattr(cls, attr_name):\n            setattr(cls, attr_name, weakref.WeakKeyDictionary())\n        return getattr(cls, attr_name)\n\n    def __new__(cls, io_loop=None, force_instance=False, **kwargs):\n        io_loop = io_loop or IOLoop.current()\n        if io_loop in cls._async_clients() and not force_instance:\n            return cls._async_clients()[io_loop]\n        instance = super(AsyncHTTPClient, cls).__new__(cls, io_loop=io_loop,\n                                                       **kwargs)\n        if not force_instance:\n            cls._async_clients()[io_loop] = instance\n        return instance\n\n    def initialize(self, io_loop, defaults=None):\n        self.io_loop = io_loop\n        self.defaults = dict(HTTPRequest._DEFAULTS)\n        if defaults is not None:\n            self.defaults.update(defaults)\n\n    def close(self):\n        \"\"\"Destroys this HTTP client, freeing any file descriptors used.\n        Not needed in normal use, but may be helpful in unittests that\n        create and destroy http clients.  No other methods may be called\n        on the `AsyncHTTPClient` after ``close()``.\n        \"\"\"\n        if self._async_clients().get(self.io_loop) is self:\n            del self._async_clients()[self.io_loop]\n\n    def fetch(self, request, callback=None, **kwargs):\n        \"\"\"Executes a request, asynchronously returning an `HTTPResponse`.\n\n        The request may be either a string URL or an `HTTPRequest` object.\n        If it is a string, we construct an `HTTPRequest` using any additional\n        kwargs: ``HTTPRequest(request, **kwargs)``\n\n        This method returns a `.Future` whose result is an\n        `HTTPResponse`.  The ``Future`` wil raise an `HTTPError` if\n        the request returned a non-200 response code.\n\n        If a ``callback`` is given, it will be invoked with the `HTTPResponse`.\n        In the callback interface, `HTTPError` is not automatically raised.\n        Instead, you must check the response's ``error`` attribute or\n        call its `~HTTPResponse.rethrow` method.\n        \"\"\"\n        if not isinstance(request, HTTPRequest):\n            request = HTTPRequest(url=request, **kwargs)\n        # We may modify this (to add Host, Accept-Encoding, etc),\n        # so make sure we don't modify the caller's object.  This is also\n        # where normal dicts get converted to HTTPHeaders objects.\n        request.headers = httputil.HTTPHeaders(request.headers)\n        request = _RequestProxy(request, self.defaults)\n        future = Future()\n        if callback is not None:\n            callback = stack_context.wrap(callback)\n\n            def handle_future(future):\n                exc = future.exception()\n                if isinstance(exc, HTTPError) and exc.response is not None:\n                    response = exc.response\n                elif exc is not None:\n                    response = HTTPResponse(\n                        request, 599, error=exc,\n                        request_time=time.time() - request.start_time)\n                else:\n                    response = future.result()\n                self.io_loop.add_callback(callback, response)\n            future.add_done_callback(handle_future)\n\n        def handle_response(response):\n            if response.error:\n                future.set_exception(response.error)\n            else:\n                future.set_result(response)\n        self.fetch_impl(request, handle_response)\n        return future\n\n    def fetch_impl(self, request, callback):\n        raise NotImplementedError()\n\n    @classmethod\n    def configure(cls, impl, **kwargs):\n        \"\"\"Configures the `AsyncHTTPClient` subclass to use.\n\n        ``AsyncHTTPClient()`` actually creates an instance of a subclass.\n        This method may be called with either a class object or the\n        fully-qualified name of such a class (or ``None`` to use the default,\n        ``SimpleAsyncHTTPClient``)\n\n        If additional keyword arguments are given, they will be passed\n        to the constructor of each subclass instance created.  The\n        keyword argument ``max_clients`` determines the maximum number\n        of simultaneous `~AsyncHTTPClient.fetch()` operations that can\n        execute in parallel on each `.IOLoop`.  Additional arguments\n        may be supported depending on the implementation class in use.\n\n        Example::\n\n           AsyncHTTPClient.configure(\"tornado.curl_httpclient.CurlAsyncHTTPClient\")\n        \"\"\"\n        super(AsyncHTTPClient, cls).configure(impl, **kwargs)\n\n\nclass HTTPRequest(object):\n    \"\"\"HTTP client request object.\"\"\"\n\n    # Default values for HTTPRequest parameters.\n    # Merged with the values on the request object by AsyncHTTPClient\n    # implementations.\n    _DEFAULTS = dict(\n        connect_timeout=20.0,\n        request_timeout=20.0,\n        follow_redirects=True,\n        max_redirects=5,\n        use_gzip=True,\n        proxy_password='',\n        allow_nonstandard_methods=False,\n        validate_cert=True)\n\n    def __init__(self, url, method=\"GET\", headers=None, body=None,\n                 auth_username=None, auth_password=None,\n                 connect_timeout=None, request_timeout=None,\n                 if_modified_since=None, follow_redirects=None,\n                 max_redirects=None, user_agent=None, use_gzip=None,\n                 network_interface=None, streaming_callback=None,\n                 header_callback=None, prepare_curl_callback=None,\n                 proxy_host=None, proxy_port=None, proxy_username=None,\n                 proxy_password=None, allow_nonstandard_methods=None,\n                 validate_cert=None, ca_certs=None,\n                 allow_ipv6=None,\n                 client_key=None, client_cert=None):\n        r\"\"\"All parameters except ``url`` are optional.\n\n        :arg string url: URL to fetch\n        :arg string method: HTTP method, e.g. \"GET\" or \"POST\"\n        :arg headers: Additional HTTP headers to pass on the request\n        :type headers: `~tornado.httputil.HTTPHeaders` or `dict`\n        :arg string auth_username: Username for HTTP \"Basic\" authentication\n        :arg string auth_password: Password for HTTP \"Basic\" authentication\n        :arg float connect_timeout: Timeout for initial connection in seconds\n        :arg float request_timeout: Timeout for entire request in seconds\n        :arg if_modified_since: Timestamp for ``If-Modified-Since`` header\n        :type if_modified_since: `datetime` or `float`\n        :arg bool follow_redirects: Should redirects be followed automatically\n           or return the 3xx response?\n        :arg int max_redirects: Limit for ``follow_redirects``\n        :arg string user_agent: String to send as ``User-Agent`` header\n        :arg bool use_gzip: Request gzip encoding from the server\n        :arg string network_interface: Network interface to use for request\n        :arg callable streaming_callback: If set, ``streaming_callback`` will\n           be run with each chunk of data as it is received, and\n           ``HTTPResponse.body`` and ``HTTPResponse.buffer`` will be empty in\n           the final response.\n        :arg callable header_callback: If set, ``header_callback`` will\n           be run with each header line as it is received (including the\n           first line, e.g. ``HTTP/1.0 200 OK\\r\\n``, and a final line\n           containing only ``\\r\\n``.  All lines include the trailing newline\n           characters).  ``HTTPResponse.headers`` will be empty in the final\n           response.  This is most useful in conjunction with\n           ``streaming_callback``, because it's the only way to get access to\n           header data while the request is in progress.\n        :arg callable prepare_curl_callback: If set, will be called with\n           a ``pycurl.Curl`` object to allow the application to make additional\n           ``setopt`` calls.\n        :arg string proxy_host: HTTP proxy hostname.  To use proxies,\n           ``proxy_host`` and ``proxy_port`` must be set; ``proxy_username`` and\n           ``proxy_pass`` are optional.  Proxies are currently only supported\n           with ``curl_httpclient``.\n        :arg int proxy_port: HTTP proxy port\n        :arg string proxy_username: HTTP proxy username\n        :arg string proxy_password: HTTP proxy password\n        :arg bool allow_nonstandard_methods: Allow unknown values for ``method``\n           argument?\n        :arg bool validate_cert: For HTTPS requests, validate the server's\n           certificate?\n        :arg string ca_certs: filename of CA certificates in PEM format,\n           or None to use defaults.  Note that in ``curl_httpclient``, if\n           any request uses a custom ``ca_certs`` file, they all must (they\n           don't have to all use the same ``ca_certs``, but it's not possible\n           to mix requests with ``ca_certs`` and requests that use the defaults.\n        :arg bool allow_ipv6: Use IPv6 when available?  Default is false in\n           ``simple_httpclient`` and true in ``curl_httpclient``\n        :arg string client_key: Filename for client SSL key, if any\n        :arg string client_cert: Filename for client SSL certificate, if any\n        \"\"\"\n        if headers is None:\n            headers = httputil.HTTPHeaders()\n        if if_modified_since:\n            headers[\"If-Modified-Since\"] = httputil.format_timestamp(\n                if_modified_since)\n        self.proxy_host = proxy_host\n        self.proxy_port = proxy_port\n        self.proxy_username = proxy_username\n        self.proxy_password = proxy_password\n        self.url = url\n        self.method = method\n        self.headers = headers\n        self.body = utf8(body)\n        self.auth_username = auth_username\n        self.auth_password = auth_password\n        self.connect_timeout = connect_timeout\n        self.request_timeout = request_timeout\n        self.follow_redirects = follow_redirects\n        self.max_redirects = max_redirects\n        self.user_agent = user_agent\n        self.use_gzip = use_gzip\n        self.network_interface = network_interface\n        self.streaming_callback = stack_context.wrap(streaming_callback)\n        self.header_callback = stack_context.wrap(header_callback)\n        self.prepare_curl_callback = stack_context.wrap(prepare_curl_callback)\n        self.allow_nonstandard_methods = allow_nonstandard_methods\n        self.validate_cert = validate_cert\n        self.ca_certs = ca_certs\n        self.allow_ipv6 = allow_ipv6\n        self.client_key = client_key\n        self.client_cert = client_cert\n        self.start_time = time.time()\n\n\nclass HTTPResponse(object):\n    \"\"\"HTTP Response object.\n\n    Attributes:\n\n    * request: HTTPRequest object\n\n    * code: numeric HTTP status code, e.g. 200 or 404\n\n    * reason: human-readable reason phrase describing the status code\n      (with curl_httpclient, this is a default value rather than the\n      server's actual response)\n\n    * headers: `tornado.httputil.HTTPHeaders` object\n\n    * buffer: ``cStringIO`` object for response body\n\n    * body: response body as string (created on demand from ``self.buffer``)\n\n    * error: Exception object, if any\n\n    * request_time: seconds from request start to finish\n\n    * time_info: dictionary of diagnostic timing information from the request.\n      Available data are subject to change, but currently uses timings\n      available from http://curl.haxx.se/libcurl/c/curl_easy_getinfo.html,\n      plus ``queue``, which is the delay (if any) introduced by waiting for\n      a slot under `AsyncHTTPClient`'s ``max_clients`` setting.\n    \"\"\"\n    def __init__(self, request, code, headers=None, buffer=None,\n                 effective_url=None, error=None, request_time=None,\n                 time_info=None, reason=None):\n        if isinstance(request, _RequestProxy):\n            self.request = request.request\n        else:\n            self.request = request\n        self.code = code\n        self.reason = reason or httputil.responses.get(code, \"Unknown\")\n        if headers is not None:\n            self.headers = headers\n        else:\n            self.headers = httputil.HTTPHeaders()\n        self.buffer = buffer\n        self._body = None\n        if effective_url is None:\n            self.effective_url = request.url\n        else:\n            self.effective_url = effective_url\n        if error is None:\n            if self.code < 200 or self.code >= 300:\n                self.error = HTTPError(self.code, response=self)\n            else:\n                self.error = None\n        else:\n            self.error = error\n        self.request_time = request_time\n        self.time_info = time_info or {}\n\n    def _get_body(self):\n        if self.buffer is None:\n            return None\n        elif self._body is None:\n            self._body = self.buffer.getvalue()\n\n        return self._body\n\n    body = property(_get_body)\n\n    def rethrow(self):\n        \"\"\"If there was an error on the request, raise an `HTTPError`.\"\"\"\n        if self.error:\n            raise self.error\n\n    def __repr__(self):\n        args = \",\".join(\"%s=%r\" % i for i in sorted(self.__dict__.items()))\n        return \"%s(%s)\" % (self.__class__.__name__, args)\n\n\nclass HTTPError(Exception):\n    \"\"\"Exception thrown for an unsuccessful HTTP request.\n\n    Attributes:\n\n    * ``code`` - HTTP error integer error code, e.g. 404.  Error code 599 is\n      used when no HTTP response was received, e.g. for a timeout.\n\n    * ``response`` - `HTTPResponse` object, if any.\n\n    Note that if ``follow_redirects`` is False, redirects become HTTPErrors,\n    and you can look at ``error.response.headers['Location']`` to see the\n    destination of the redirect.\n    \"\"\"\n    def __init__(self, code, message=None, response=None):\n        self.code = code\n        message = message or httputil.responses.get(code, \"Unknown\")\n        self.response = response\n        Exception.__init__(self, \"HTTP %d: %s\" % (self.code, message))\n\n\nclass _RequestProxy(object):\n    \"\"\"Combines an object with a dictionary of defaults.\n\n    Used internally by AsyncHTTPClient implementations.\n    \"\"\"\n    def __init__(self, request, defaults):\n        self.request = request\n        self.defaults = defaults\n\n    def __getattr__(self, name):\n        request_attr = getattr(self.request, name)\n        if request_attr is not None:\n            return request_attr\n        elif self.defaults is not None:\n            return self.defaults.get(name, None)\n        else:\n            return None\n\n\ndef main():\n    from tornado.options import define, options, parse_command_line\n    define(\"print_headers\", type=bool, default=False)\n    define(\"print_body\", type=bool, default=True)\n    define(\"follow_redirects\", type=bool, default=True)\n    define(\"validate_cert\", type=bool, default=True)\n    args = parse_command_line()\n    client = HTTPClient()\n    for arg in args:\n        try:\n            response = client.fetch(arg,\n                                    follow_redirects=options.follow_redirects,\n                                    validate_cert=options.validate_cert,\n                                    )\n        except HTTPError as e:\n            if e.response is not None:\n                response = e.response\n            else:\n                raise\n        if options.print_headers:\n            print(response.headers)\n        if options.print_body:\n            print(response.body)\n    client.close()\n\nif __name__ == \"__main__\":\n    main()\n",
            "header": {
                "file": {
                    "path": "tornado/httpclient.py"
                },
                "misc_line": null
            },
            "index_line": {
                "old_commit_hash": "2a6f4467",
                "new_commit_hash": "bfdc7136",
                "mode": "100644"
            },
            "is_binary_file": false,
            "binary_line": null,
            "minus_file": {
                "path": "a/tornado/httpclient.py"
            },
            "plus_file": {
                "path": "b/tornado/httpclient.py"
            },
            "hunks": [
                {
                    "descriptor": {
                        "old_range": {
                            "start": 373,
                            "length": 7
                        },
                        "new_range": {
                            "start": 373,
                            "length": 10
                        },
                        "section": "class HTTPResponse(object):"
                    },
                    "line_group": {
                        "all_lines": [
                            {
                                "content": "    def __init__(self, request, code, headers=None, buffer=None,",
                                "type": "context"
                            },
                            {
                                "content": "                 effective_url=None, error=None, request_time=None,",
                                "type": "context"
                            },
                            {
                                "content": "                 time_info=None, reason=None):",
                                "type": "context"
                            },
                            {
                                "content": "        self.request = request",
                                "type": "deleted"
                            },
                            {
                                "content": "        if isinstance(request, _RequestProxy):",
                                "type": "added"
                            },
                            {
                                "content": "            self.request = request.request",
                                "type": "added"
                            },
                            {
                                "content": "        else:",
                                "type": "added"
                            },
                            {
                                "content": "            self.request = request",
                                "type": "added"
                            },
                            {
                                "content": "        self.code = code",
                                "type": "context"
                            },
                            {
                                "content": "        self.reason = reason or httputil.responses.get(code, \"Unknown\")",
                                "type": "context"
                            },
                            {
                                "content": "        if headers is not None:",
                                "type": "context"
                            }
                        ]
                    },
                    "modified_entities": [
                        {
                            "file_name": "tornado/httpclient.py",
                            "type": "class",
                            "name": "HTTPResponse",
                            "content": "class HTTPResponse(object):\n    \"\"\"HTTP Response object.\n\n    Attributes:\n\n    * request: HTTPRequest object\n\n    * code: numeric HTTP status code, e.g. 200 or 404\n\n    * reason: human-readable reason phrase describing the status code\n      (with curl_httpclient, this is a default value rather than the\n      server's actual response)\n\n    * headers: `tornado.httputil.HTTPHeaders` object\n\n    * buffer: ``cStringIO`` object for response body\n\n    * body: response body as string (created on demand from ``self.buffer``)\n\n    * error: Exception object, if any\n\n    * request_time: seconds from request start to finish\n\n    * time_info: dictionary of diagnostic timing information from the request.\n      Available data are subject to change, but currently uses timings\n      available from http://curl.haxx.se/libcurl/c/curl_easy_getinfo.html,\n      plus ``queue``, which is the delay (if any) introduced by waiting for\n      a slot under `AsyncHTTPClient`'s ``max_clients`` setting.\n    \"\"\"\n    def __init__(self, request, code, headers=None, buffer=None,\n                 effective_url=None, error=None, request_time=None,\n                 time_info=None, reason=None):\n        self.request = request\n        self.code = code\n        self.reason = reason or httputil.responses.get(code, \"Unknown\")\n        if headers is not None:\n            self.headers = headers\n        else:\n            self.headers = httputil.HTTPHeaders()\n        self.buffer = buffer\n        self._body = None\n        if effective_url is None:\n            self.effective_url = request.url\n        else:\n            self.effective_url = effective_url\n        if error is None:\n            if self.code < 200 or self.code >= 300:\n                self.error = HTTPError(self.code, response=self)\n            else:\n                self.error = None\n        else:\n            self.error = error\n        self.request_time = request_time\n        self.time_info = time_info or {}\n\n    def _get_body(self):\n        if self.buffer is None:\n            return None\n        elif self._body is None:\n            self._body = self.buffer.getvalue()\n\n        return self._body\n\n    body = property(_get_body)\n\n    def rethrow(self):\n        \"\"\"If there was an error on the request, raise an `HTTPError`.\"\"\"\n        if self.error:\n            raise self.error\n\n    def __repr__(self):\n        args = \",\".join(\"%s=%r\" % i for i in sorted(self.__dict__.items()))\n        return \"%s(%s)\" % (self.__class__.__name__, args)",
                            "ast_type_str": "ClassDef",
                            "start_lineno": 344,
                            "end_lineno": 416,
                            "parent": null
                        },
                        {
                            "file_name": "tornado/httpclient.py",
                            "type": "method",
                            "name": "HTTPResponse.__init__",
                            "content": "class HTTPResponse(object):\n    \"\"\"HTTP Response object.\n\n    Attributes:\n\n    * request: HTTPRequest object\n\n    * code: numeric HTTP status code, e.g. 200 or 404\n\n    * reason: human-readable reason phrase describing the status code\n      (with curl_httpclient, this is a default value rather than the\n      server's actual response)\n\n    * headers: `tornado.httputil.HTTPHeaders` object\n\n    * buffer: ``cStringIO`` object for response body\n\n    * body: response body as string (created on demand from ``self.buffer``)\n\n    * error: Exception object, if any\n\n    * request_time: seconds from request start to finish\n\n    * time_info: dictionary of diagnostic timing information from the request.\n      Available data are subject to change, but currently uses timings\n      available from http://curl.haxx.se/libcurl/c/curl_easy_getinfo.html,\n      plus ``queue``, which is the delay (if any) introduced by waiting for\n      a slot under `AsyncHTTPClient`'s ``max_clients`` setting.\n    \"\"\"\n    def __init__(self, request, code, headers=None, buffer=None,\n                 effective_url=None, error=None, request_time=None,\n                 time_info=None, reason=None):\n        self.request = request\n        self.code = code\n        self.reason = reason or httputil.responses.get(code, \"Unknown\")\n        if headers is not None:\n            self.headers = headers\n        else:\n            self.headers = httputil.HTTPHeaders()\n        self.buffer = buffer\n        self._body = None\n        if effective_url is None:\n            self.effective_url = request.url\n        else:\n            self.effective_url = effective_url\n        if error is None:\n            if self.code < 200 or self.code >= 300:\n                self.error = HTTPError(self.code, response=self)\n            else:\n                self.error = None\n        else:\n            self.error = error\n        self.request_time = request_time\n        self.time_info = time_info or {}\n\n    def _get_body(self):\n        if self.buffer is None:\n            return None\n        elif self._body is None:\n            self._body = self.buffer.getvalue()\n\n        return self._body\n\n    body = property(_get_body)\n\n    def rethrow(self):\n        \"\"\"If there was an error on the request, raise an `HTTPError`.\"\"\"\n        if self.error:\n            raise self.error\n\n    def __repr__(self):\n        args = \",\".join(\"%s=%r\" % i for i in sorted(self.__dict__.items()))\n        return \"%s(%s)\" % (self.__class__.__name__, args)",
                            "ast_type_str": "ClassDef",
                            "start_lineno": 373,
                            "end_lineno": 397,
                            "parent": {
                                "file_name": "tornado/httpclient.py",
                                "type": "class",
                                "name": "HTTPResponse",
                                "content": "class HTTPResponse(object):\n    \"\"\"HTTP Response object.\n\n    Attributes:\n\n    * request: HTTPRequest object\n\n    * code: numeric HTTP status code, e.g. 200 or 404\n\n    * reason: human-readable reason phrase describing the status code\n      (with curl_httpclient, this is a default value rather than the\n      server's actual response)\n\n    * headers: `tornado.httputil.HTTPHeaders` object\n\n    * buffer: ``cStringIO`` object for response body\n\n    * body: response body as string (created on demand from ``self.buffer``)\n\n    * error: Exception object, if any\n\n    * request_time: seconds from request start to finish\n\n    * time_info: dictionary of diagnostic timing information from the request.\n      Available data are subject to change, but currently uses timings\n      available from http://curl.haxx.se/libcurl/c/curl_easy_getinfo.html,\n      plus ``queue``, which is the delay (if any) introduced by waiting for\n      a slot under `AsyncHTTPClient`'s ``max_clients`` setting.\n    \"\"\"\n    def __init__(self, request, code, headers=None, buffer=None,\n                 effective_url=None, error=None, request_time=None,\n                 time_info=None, reason=None):\n        self.request = request\n        self.code = code\n        self.reason = reason or httputil.responses.get(code, \"Unknown\")\n        if headers is not None:\n            self.headers = headers\n        else:\n            self.headers = httputil.HTTPHeaders()\n        self.buffer = buffer\n        self._body = None\n        if effective_url is None:\n            self.effective_url = request.url\n        else:\n            self.effective_url = effective_url\n        if error is None:\n            if self.code < 200 or self.code >= 300:\n                self.error = HTTPError(self.code, response=self)\n            else:\n                self.error = None\n        else:\n            self.error = error\n        self.request_time = request_time\n        self.time_info = time_info or {}\n\n    def _get_body(self):\n        if self.buffer is None:\n            return None\n        elif self._body is None:\n            self._body = self.buffer.getvalue()\n\n        return self._body\n\n    body = property(_get_body)\n\n    def rethrow(self):\n        \"\"\"If there was an error on the request, raise an `HTTPError`.\"\"\"\n        if self.error:\n            raise self.error\n\n    def __repr__(self):\n        args = \",\".join(\"%s=%r\" % i for i in sorted(self.__dict__.items()))\n        return \"%s(%s)\" % (self.__class__.__name__, args)",
                                "ast_type_str": "ClassDef",
                                "start_lineno": 344,
                                "end_lineno": 416,
                                "parent": null
                            }
                        }
                    ],
                    "added_entities": [],
                    "deleted_entities": []
                }
            ]
        },
        {
            "old_file_content": "#!/usr/bin/env python\n\nfrom __future__ import absolute_import, division, print_function, with_statement\n\nimport base64\nimport binascii\nfrom contextlib import closing\nimport functools\nimport sys\nimport threading\n\nfrom tornado.escape import utf8\nfrom tornado.httpclient import HTTPRequest, HTTPResponse, _RequestProxy, HTTPError, HTTPClient\nfrom tornado.httpserver import HTTPServer\nfrom tornado.ioloop import IOLoop\nfrom tornado.iostream import IOStream\nfrom tornado import netutil\nfrom tornado.stack_context import ExceptionStackContext, NullContext\nfrom tornado.testing import AsyncHTTPTestCase, bind_unused_port, gen_test\nfrom tornado.test.util import unittest\nfrom tornado.util import u, bytes_type\nfrom tornado.web import Application, RequestHandler, url\n\ntry:\n    from io import BytesIO  # python 3\nexcept ImportError:\n    from cStringIO import StringIO as BytesIO\n\n\nclass HelloWorldHandler(RequestHandler):\n    def get(self):\n        name = self.get_argument(\"name\", \"world\")\n        self.set_header(\"Content-Type\", \"text/plain\")\n        self.finish(\"Hello %s!\" % name)\n\n\nclass PostHandler(RequestHandler):\n    def post(self):\n        self.finish(\"Post arg1: %s, arg2: %s\" % (\n            self.get_argument(\"arg1\"), self.get_argument(\"arg2\")))\n\n\nclass ChunkHandler(RequestHandler):\n    def get(self):\n        self.write(\"asdf\")\n        self.flush()\n        self.write(\"qwer\")\n\n\nclass AuthHandler(RequestHandler):\n    def get(self):\n        self.finish(self.request.headers[\"Authorization\"])\n\n\nclass CountdownHandler(RequestHandler):\n    def get(self, count):\n        count = int(count)\n        if count > 0:\n            self.redirect(self.reverse_url(\"countdown\", count - 1))\n        else:\n            self.write(\"Zero\")\n\n\nclass EchoPostHandler(RequestHandler):\n    def post(self):\n        self.write(self.request.body)\n\n\nclass UserAgentHandler(RequestHandler):\n    def get(self):\n        self.write(self.request.headers.get('User-Agent', 'User agent not set'))\n\n\nclass ContentLength304Handler(RequestHandler):\n    def get(self):\n        self.set_status(304)\n        self.set_header('Content-Length', 42)\n\n    def _clear_headers_for_304(self):\n        # Tornado strips content-length from 304 responses, but here we\n        # want to simulate servers that include the headers anyway.\n        pass\n\n\n# These tests end up getting run redundantly: once here with the default\n# HTTPClient implementation, and then again in each implementation's own\n# test suite.\n\n\nclass HTTPClientCommonTestCase(AsyncHTTPTestCase):\n    def get_app(self):\n        return Application([\n            url(\"/hello\", HelloWorldHandler),\n            url(\"/post\", PostHandler),\n            url(\"/chunk\", ChunkHandler),\n            url(\"/auth\", AuthHandler),\n            url(\"/countdown/([0-9]+)\", CountdownHandler, name=\"countdown\"),\n            url(\"/echopost\", EchoPostHandler),\n            url(\"/user_agent\", UserAgentHandler),\n            url(\"/304_with_content_length\", ContentLength304Handler),\n        ], gzip=True)\n\n    def test_hello_world(self):\n        response = self.fetch(\"/hello\")\n        self.assertEqual(response.code, 200)\n        self.assertEqual(response.headers[\"Content-Type\"], \"text/plain\")\n        self.assertEqual(response.body, b\"Hello world!\")\n        self.assertEqual(int(response.request_time), 0)\n\n        response = self.fetch(\"/hello?name=Ben\")\n        self.assertEqual(response.body, b\"Hello Ben!\")\n\n    def test_streaming_callback(self):\n        # streaming_callback is also tested in test_chunked\n        chunks = []\n        response = self.fetch(\"/hello\",\n                              streaming_callback=chunks.append)\n        # with streaming_callback, data goes to the callback and not response.body\n        self.assertEqual(chunks, [b\"Hello world!\"])\n        self.assertFalse(response.body)\n\n    def test_post(self):\n        response = self.fetch(\"/post\", method=\"POST\",\n                              body=\"arg1=foo&arg2=bar\")\n        self.assertEqual(response.code, 200)\n        self.assertEqual(response.body, b\"Post arg1: foo, arg2: bar\")\n\n    def test_chunked(self):\n        response = self.fetch(\"/chunk\")\n        self.assertEqual(response.body, b\"asdfqwer\")\n\n        chunks = []\n        response = self.fetch(\"/chunk\",\n                              streaming_callback=chunks.append)\n        self.assertEqual(chunks, [b\"asdf\", b\"qwer\"])\n        self.assertFalse(response.body)\n\n    def test_chunked_close(self):\n        # test case in which chunks spread read-callback processing\n        # over several ioloop iterations, but the connection is already closed.\n        sock, port = bind_unused_port()\n        with closing(sock):\n            def write_response(stream, request_data):\n                stream.write(b\"\"\"\\\nHTTP/1.1 200 OK\nTransfer-Encoding: chunked\n\n1\n1\n1\n2\n0\n\n\"\"\".replace(b\"\\n\", b\"\\r\\n\"), callback=stream.close)\n\n            def accept_callback(conn, address):\n                # fake an HTTP server using chunked encoding where the final chunks\n                # and connection close all happen at once\n                stream = IOStream(conn, io_loop=self.io_loop)\n                stream.read_until(b\"\\r\\n\\r\\n\",\n                                  functools.partial(write_response, stream))\n            netutil.add_accept_handler(sock, accept_callback, self.io_loop)\n            self.http_client.fetch(\"http://127.0.0.1:%d/\" % port, self.stop)\n            resp = self.wait()\n            resp.rethrow()\n            self.assertEqual(resp.body, b\"12\")\n            self.io_loop.remove_handler(sock.fileno())\n\n    def test_streaming_stack_context(self):\n        chunks = []\n        exc_info = []\n\n        def error_handler(typ, value, tb):\n            exc_info.append((typ, value, tb))\n            return True\n\n        def streaming_cb(chunk):\n            chunks.append(chunk)\n            if chunk == b'qwer':\n                1 / 0\n\n        with ExceptionStackContext(error_handler):\n            self.fetch('/chunk', streaming_callback=streaming_cb)\n\n        self.assertEqual(chunks, [b'asdf', b'qwer'])\n        self.assertEqual(1, len(exc_info))\n        self.assertIs(exc_info[0][0], ZeroDivisionError)\n\n    def test_basic_auth(self):\n        self.assertEqual(self.fetch(\"/auth\", auth_username=\"Aladdin\",\n                                    auth_password=\"open sesame\").body,\n                         b\"Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==\")\n\n    def test_follow_redirect(self):\n        response = self.fetch(\"/countdown/2\", follow_redirects=False)\n        self.assertEqual(302, response.code)\n        self.assertTrue(response.headers[\"Location\"].endswith(\"/countdown/1\"))\n\n        response = self.fetch(\"/countdown/2\")\n        self.assertEqual(200, response.code)\n        self.assertTrue(response.effective_url.endswith(\"/countdown/0\"))\n        self.assertEqual(b\"Zero\", response.body)\n\n    def test_credentials_in_url(self):\n        url = self.get_url(\"/auth\").replace(\"http://\", \"http://me:secret@\")\n        self.http_client.fetch(url, self.stop)\n        response = self.wait()\n        self.assertEqual(b\"Basic \" + base64.b64encode(b\"me:secret\"),\n                         response.body)\n\n    def test_body_encoding(self):\n        unicode_body = u(\"\\xe9\")\n        byte_body = binascii.a2b_hex(b\"e9\")\n\n        # unicode string in body gets converted to utf8\n        response = self.fetch(\"/echopost\", method=\"POST\", body=unicode_body,\n                              headers={\"Content-Type\": \"application/blah\"})\n        self.assertEqual(response.headers[\"Content-Length\"], \"2\")\n        self.assertEqual(response.body, utf8(unicode_body))\n\n        # byte strings pass through directly\n        response = self.fetch(\"/echopost\", method=\"POST\",\n                              body=byte_body,\n                              headers={\"Content-Type\": \"application/blah\"})\n        self.assertEqual(response.headers[\"Content-Length\"], \"1\")\n        self.assertEqual(response.body, byte_body)\n\n        # Mixing unicode in headers and byte string bodies shouldn't\n        # break anything\n        response = self.fetch(\"/echopost\", method=\"POST\", body=byte_body,\n                              headers={\"Content-Type\": \"application/blah\"},\n                              user_agent=u(\"foo\"))\n        self.assertEqual(response.headers[\"Content-Length\"], \"1\")\n        self.assertEqual(response.body, byte_body)\n\n    def test_types(self):\n        response = self.fetch(\"/hello\")\n        self.assertEqual(type(response.body), bytes_type)\n        self.assertEqual(type(response.headers[\"Content-Type\"]), str)\n        self.assertEqual(type(response.code), int)\n        self.assertEqual(type(response.effective_url), str)\n\n    def test_header_callback(self):\n        first_line = []\n        headers = {}\n        chunks = []\n\n        def header_callback(header_line):\n            if header_line.startswith('HTTP/'):\n                first_line.append(header_line)\n            elif header_line != '\\r\\n':\n                k, v = header_line.split(':', 1)\n                headers[k] = v.strip()\n\n        def streaming_callback(chunk):\n            # All header callbacks are run before any streaming callbacks,\n            # so the header data is available to process the data as it\n            # comes in.\n            self.assertEqual(headers['Content-Type'], 'text/html; charset=UTF-8')\n            chunks.append(chunk)\n\n        self.fetch('/chunk', header_callback=header_callback,\n                   streaming_callback=streaming_callback)\n        self.assertEqual(len(first_line), 1)\n        self.assertRegexpMatches(first_line[0], 'HTTP/1.[01] 200 OK\\r\\n')\n        self.assertEqual(chunks, [b'asdf', b'qwer'])\n\n    def test_header_callback_stack_context(self):\n        exc_info = []\n\n        def error_handler(typ, value, tb):\n            exc_info.append((typ, value, tb))\n            return True\n\n        def header_callback(header_line):\n            if header_line.startswith('Content-Type:'):\n                1 / 0\n\n        with ExceptionStackContext(error_handler):\n            self.fetch('/chunk', header_callback=header_callback)\n        self.assertEqual(len(exc_info), 1)\n        self.assertIs(exc_info[0][0], ZeroDivisionError)\n\n    def test_configure_defaults(self):\n        defaults = dict(user_agent='TestDefaultUserAgent')\n        # Construct a new instance of the configured client class\n        client = self.http_client.__class__(self.io_loop, force_instance=True,\n                                            defaults=defaults)\n        client.fetch(self.get_url('/user_agent'), callback=self.stop)\n        response = self.wait()\n        self.assertEqual(response.body, b'TestDefaultUserAgent')\n        client.close()\n\n    def test_304_with_content_length(self):\n        # According to the spec 304 responses SHOULD NOT include\n        # Content-Length or other entity headers, but some servers do it\n        # anyway.\n        # http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.5\n        response = self.fetch('/304_with_content_length')\n        self.assertEqual(response.code, 304)\n        self.assertEqual(response.headers['Content-Length'], '42')\n\n    def test_final_callback_stack_context(self):\n        # The final callback should be run outside of the httpclient's\n        # stack_context.  We want to ensure that there is not stack_context\n        # between the user's callback and the IOLoop, so monkey-patch\n        # IOLoop.handle_callback_exception and disable the test harness's\n        # context with a NullContext.\n        # Note that this does not apply to secondary callbacks (header\n        # and streaming_callback), as errors there must be seen as errors\n        # by the http client so it can clean up the connection.\n        exc_info = []\n\n        def handle_callback_exception(callback):\n            exc_info.append(sys.exc_info())\n            self.stop()\n        self.io_loop.handle_callback_exception = handle_callback_exception\n        with NullContext():\n            self.http_client.fetch(self.get_url('/hello'),\n                                   lambda response: 1 / 0)\n        self.wait()\n        self.assertEqual(exc_info[0][0], ZeroDivisionError)\n\n    @gen_test\n    def test_future_interface(self):\n        response = yield self.http_client.fetch(self.get_url('/hello'))\n        self.assertEqual(response.body, b'Hello world!')\n\n    @gen_test\n    def test_future_http_error(self):\n        try:\n            yield self.http_client.fetch(self.get_url('/notfound'))\n        except HTTPError as e:\n            self.assertEqual(e.code, 404)\n            self.assertEqual(e.response.code, 404)\n\n\nclass RequestProxyTest(unittest.TestCase):\n    def test_request_set(self):\n        proxy = _RequestProxy(HTTPRequest('http://example.com/',\n                                          user_agent='foo'),\n                              dict())\n        self.assertEqual(proxy.user_agent, 'foo')\n\n    def test_default_set(self):\n        proxy = _RequestProxy(HTTPRequest('http://example.com/'),\n                              dict(network_interface='foo'))\n        self.assertEqual(proxy.network_interface, 'foo')\n\n    def test_both_set(self):\n        proxy = _RequestProxy(HTTPRequest('http://example.com/',\n                                          proxy_host='foo'),\n                              dict(proxy_host='bar'))\n        self.assertEqual(proxy.proxy_host, 'foo')\n\n    def test_neither_set(self):\n        proxy = _RequestProxy(HTTPRequest('http://example.com/'),\n                              dict())\n        self.assertIs(proxy.auth_username, None)\n\n    def test_bad_attribute(self):\n        proxy = _RequestProxy(HTTPRequest('http://example.com/'),\n                              dict())\n        with self.assertRaises(AttributeError):\n            proxy.foo\n\n    def test_defaults_none(self):\n        proxy = _RequestProxy(HTTPRequest('http://example.com/'), None)\n        self.assertIs(proxy.auth_username, None)\n\n\nclass HTTPResponseTestCase(unittest.TestCase):\n    def test_str(self):\n        response = HTTPResponse(HTTPRequest('http://example.com'),\n                                200, headers={}, buffer=BytesIO())\n        s = str(response)\n        self.assertTrue(s.startswith('HTTPResponse('))\n        self.assertIn('code=200', s)\n\n\nclass SyncHTTPClientTest(unittest.TestCase):\n    def setUp(self):\n        if IOLoop.configured_class().__name__ == 'TwistedIOLoop':\n            # TwistedIOLoop only supports the global reactor, so we can't have\n            # separate IOLoops for client and server threads.\n            raise unittest.SkipTest(\n                'Sync HTTPClient not compatible with TwistedIOLoop')\n        self.server_ioloop = IOLoop()\n\n        sock, self.port = bind_unused_port()\n        app = Application([('/', HelloWorldHandler)])\n        server = HTTPServer(app, io_loop=self.server_ioloop)\n        server.add_socket(sock)\n\n        self.server_thread = threading.Thread(target=self.server_ioloop.start)\n        self.server_thread.start()\n\n        self.http_client = HTTPClient()\n\n    def tearDown(self):\n        self.server_ioloop.add_callback(self.server_ioloop.stop)\n        self.server_thread.join()\n        self.server_ioloop.close(all_fds=True)\n\n    def get_url(self, path):\n        return 'http://localhost:%d%s' % (self.port, path)\n\n    def test_sync_client(self):\n        response = self.http_client.fetch(self.get_url('/'))\n        self.assertEqual(b'Hello world!', response.body)\n\n    def test_sync_client_error(self):\n        # Synchronous HTTPClient raises errors directly; no need for\n        # response.rethrow()\n        with self.assertRaises(HTTPError) as assertion:\n            self.http_client.fetch(self.get_url('/notfound'))\n        self.assertEqual(assertion.exception.code, 404)\n",
            "new_file_content": "#!/usr/bin/env python\n\nfrom __future__ import absolute_import, division, print_function, with_statement\n\nimport base64\nimport binascii\nfrom contextlib import closing\nimport functools\nimport sys\nimport threading\n\nfrom tornado.escape import utf8\nfrom tornado.httpclient import HTTPRequest, HTTPResponse, _RequestProxy, HTTPError, HTTPClient\nfrom tornado.httpserver import HTTPServer\nfrom tornado.ioloop import IOLoop\nfrom tornado.iostream import IOStream\nfrom tornado import netutil\nfrom tornado.stack_context import ExceptionStackContext, NullContext\nfrom tornado.testing import AsyncHTTPTestCase, bind_unused_port, gen_test\nfrom tornado.test.util import unittest\nfrom tornado.util import u, bytes_type\nfrom tornado.web import Application, RequestHandler, url\n\ntry:\n    from io import BytesIO  # python 3\nexcept ImportError:\n    from cStringIO import StringIO as BytesIO\n\n\nclass HelloWorldHandler(RequestHandler):\n    def get(self):\n        name = self.get_argument(\"name\", \"world\")\n        self.set_header(\"Content-Type\", \"text/plain\")\n        self.finish(\"Hello %s!\" % name)\n\n\nclass PostHandler(RequestHandler):\n    def post(self):\n        self.finish(\"Post arg1: %s, arg2: %s\" % (\n            self.get_argument(\"arg1\"), self.get_argument(\"arg2\")))\n\n\nclass ChunkHandler(RequestHandler):\n    def get(self):\n        self.write(\"asdf\")\n        self.flush()\n        self.write(\"qwer\")\n\n\nclass AuthHandler(RequestHandler):\n    def get(self):\n        self.finish(self.request.headers[\"Authorization\"])\n\n\nclass CountdownHandler(RequestHandler):\n    def get(self, count):\n        count = int(count)\n        if count > 0:\n            self.redirect(self.reverse_url(\"countdown\", count - 1))\n        else:\n            self.write(\"Zero\")\n\n\nclass EchoPostHandler(RequestHandler):\n    def post(self):\n        self.write(self.request.body)\n\n\nclass UserAgentHandler(RequestHandler):\n    def get(self):\n        self.write(self.request.headers.get('User-Agent', 'User agent not set'))\n\n\nclass ContentLength304Handler(RequestHandler):\n    def get(self):\n        self.set_status(304)\n        self.set_header('Content-Length', 42)\n\n    def _clear_headers_for_304(self):\n        # Tornado strips content-length from 304 responses, but here we\n        # want to simulate servers that include the headers anyway.\n        pass\n\n\n# These tests end up getting run redundantly: once here with the default\n# HTTPClient implementation, and then again in each implementation's own\n# test suite.\n\n\nclass HTTPClientCommonTestCase(AsyncHTTPTestCase):\n    def get_app(self):\n        return Application([\n            url(\"/hello\", HelloWorldHandler),\n            url(\"/post\", PostHandler),\n            url(\"/chunk\", ChunkHandler),\n            url(\"/auth\", AuthHandler),\n            url(\"/countdown/([0-9]+)\", CountdownHandler, name=\"countdown\"),\n            url(\"/echopost\", EchoPostHandler),\n            url(\"/user_agent\", UserAgentHandler),\n            url(\"/304_with_content_length\", ContentLength304Handler),\n        ], gzip=True)\n\n    def test_hello_world(self):\n        response = self.fetch(\"/hello\")\n        self.assertEqual(response.code, 200)\n        self.assertEqual(response.headers[\"Content-Type\"], \"text/plain\")\n        self.assertEqual(response.body, b\"Hello world!\")\n        self.assertEqual(int(response.request_time), 0)\n\n        response = self.fetch(\"/hello?name=Ben\")\n        self.assertEqual(response.body, b\"Hello Ben!\")\n\n    def test_streaming_callback(self):\n        # streaming_callback is also tested in test_chunked\n        chunks = []\n        response = self.fetch(\"/hello\",\n                              streaming_callback=chunks.append)\n        # with streaming_callback, data goes to the callback and not response.body\n        self.assertEqual(chunks, [b\"Hello world!\"])\n        self.assertFalse(response.body)\n\n    def test_post(self):\n        response = self.fetch(\"/post\", method=\"POST\",\n                              body=\"arg1=foo&arg2=bar\")\n        self.assertEqual(response.code, 200)\n        self.assertEqual(response.body, b\"Post arg1: foo, arg2: bar\")\n\n    def test_chunked(self):\n        response = self.fetch(\"/chunk\")\n        self.assertEqual(response.body, b\"asdfqwer\")\n\n        chunks = []\n        response = self.fetch(\"/chunk\",\n                              streaming_callback=chunks.append)\n        self.assertEqual(chunks, [b\"asdf\", b\"qwer\"])\n        self.assertFalse(response.body)\n\n    def test_chunked_close(self):\n        # test case in which chunks spread read-callback processing\n        # over several ioloop iterations, but the connection is already closed.\n        sock, port = bind_unused_port()\n        with closing(sock):\n            def write_response(stream, request_data):\n                stream.write(b\"\"\"\\\nHTTP/1.1 200 OK\nTransfer-Encoding: chunked\n\n1\n1\n1\n2\n0\n\n\"\"\".replace(b\"\\n\", b\"\\r\\n\"), callback=stream.close)\n\n            def accept_callback(conn, address):\n                # fake an HTTP server using chunked encoding where the final chunks\n                # and connection close all happen at once\n                stream = IOStream(conn, io_loop=self.io_loop)\n                stream.read_until(b\"\\r\\n\\r\\n\",\n                                  functools.partial(write_response, stream))\n            netutil.add_accept_handler(sock, accept_callback, self.io_loop)\n            self.http_client.fetch(\"http://127.0.0.1:%d/\" % port, self.stop)\n            resp = self.wait()\n            resp.rethrow()\n            self.assertEqual(resp.body, b\"12\")\n            self.io_loop.remove_handler(sock.fileno())\n\n    def test_streaming_stack_context(self):\n        chunks = []\n        exc_info = []\n\n        def error_handler(typ, value, tb):\n            exc_info.append((typ, value, tb))\n            return True\n\n        def streaming_cb(chunk):\n            chunks.append(chunk)\n            if chunk == b'qwer':\n                1 / 0\n\n        with ExceptionStackContext(error_handler):\n            self.fetch('/chunk', streaming_callback=streaming_cb)\n\n        self.assertEqual(chunks, [b'asdf', b'qwer'])\n        self.assertEqual(1, len(exc_info))\n        self.assertIs(exc_info[0][0], ZeroDivisionError)\n\n    def test_basic_auth(self):\n        self.assertEqual(self.fetch(\"/auth\", auth_username=\"Aladdin\",\n                                    auth_password=\"open sesame\").body,\n                         b\"Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==\")\n\n    def test_follow_redirect(self):\n        response = self.fetch(\"/countdown/2\", follow_redirects=False)\n        self.assertEqual(302, response.code)\n        self.assertTrue(response.headers[\"Location\"].endswith(\"/countdown/1\"))\n\n        response = self.fetch(\"/countdown/2\")\n        self.assertEqual(200, response.code)\n        self.assertTrue(response.effective_url.endswith(\"/countdown/0\"))\n        self.assertEqual(b\"Zero\", response.body)\n\n    def test_credentials_in_url(self):\n        url = self.get_url(\"/auth\").replace(\"http://\", \"http://me:secret@\")\n        self.http_client.fetch(url, self.stop)\n        response = self.wait()\n        self.assertEqual(b\"Basic \" + base64.b64encode(b\"me:secret\"),\n                         response.body)\n\n    def test_body_encoding(self):\n        unicode_body = u(\"\\xe9\")\n        byte_body = binascii.a2b_hex(b\"e9\")\n\n        # unicode string in body gets converted to utf8\n        response = self.fetch(\"/echopost\", method=\"POST\", body=unicode_body,\n                              headers={\"Content-Type\": \"application/blah\"})\n        self.assertEqual(response.headers[\"Content-Length\"], \"2\")\n        self.assertEqual(response.body, utf8(unicode_body))\n\n        # byte strings pass through directly\n        response = self.fetch(\"/echopost\", method=\"POST\",\n                              body=byte_body,\n                              headers={\"Content-Type\": \"application/blah\"})\n        self.assertEqual(response.headers[\"Content-Length\"], \"1\")\n        self.assertEqual(response.body, byte_body)\n\n        # Mixing unicode in headers and byte string bodies shouldn't\n        # break anything\n        response = self.fetch(\"/echopost\", method=\"POST\", body=byte_body,\n                              headers={\"Content-Type\": \"application/blah\"},\n                              user_agent=u(\"foo\"))\n        self.assertEqual(response.headers[\"Content-Length\"], \"1\")\n        self.assertEqual(response.body, byte_body)\n\n    def test_types(self):\n        response = self.fetch(\"/hello\")\n        self.assertEqual(type(response.body), bytes_type)\n        self.assertEqual(type(response.headers[\"Content-Type\"]), str)\n        self.assertEqual(type(response.code), int)\n        self.assertEqual(type(response.effective_url), str)\n\n    def test_header_callback(self):\n        first_line = []\n        headers = {}\n        chunks = []\n\n        def header_callback(header_line):\n            if header_line.startswith('HTTP/'):\n                first_line.append(header_line)\n            elif header_line != '\\r\\n':\n                k, v = header_line.split(':', 1)\n                headers[k] = v.strip()\n\n        def streaming_callback(chunk):\n            # All header callbacks are run before any streaming callbacks,\n            # so the header data is available to process the data as it\n            # comes in.\n            self.assertEqual(headers['Content-Type'], 'text/html; charset=UTF-8')\n            chunks.append(chunk)\n\n        self.fetch('/chunk', header_callback=header_callback,\n                   streaming_callback=streaming_callback)\n        self.assertEqual(len(first_line), 1)\n        self.assertRegexpMatches(first_line[0], 'HTTP/1.[01] 200 OK\\r\\n')\n        self.assertEqual(chunks, [b'asdf', b'qwer'])\n\n    def test_header_callback_stack_context(self):\n        exc_info = []\n\n        def error_handler(typ, value, tb):\n            exc_info.append((typ, value, tb))\n            return True\n\n        def header_callback(header_line):\n            if header_line.startswith('Content-Type:'):\n                1 / 0\n\n        with ExceptionStackContext(error_handler):\n            self.fetch('/chunk', header_callback=header_callback)\n        self.assertEqual(len(exc_info), 1)\n        self.assertIs(exc_info[0][0], ZeroDivisionError)\n\n    def test_configure_defaults(self):\n        defaults = dict(user_agent='TestDefaultUserAgent')\n        # Construct a new instance of the configured client class\n        client = self.http_client.__class__(self.io_loop, force_instance=True,\n                                            defaults=defaults)\n        client.fetch(self.get_url('/user_agent'), callback=self.stop)\n        response = self.wait()\n        self.assertEqual(response.body, b'TestDefaultUserAgent')\n        client.close()\n\n    def test_304_with_content_length(self):\n        # According to the spec 304 responses SHOULD NOT include\n        # Content-Length or other entity headers, but some servers do it\n        # anyway.\n        # http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.5\n        response = self.fetch('/304_with_content_length')\n        self.assertEqual(response.code, 304)\n        self.assertEqual(response.headers['Content-Length'], '42')\n\n    def test_final_callback_stack_context(self):\n        # The final callback should be run outside of the httpclient's\n        # stack_context.  We want to ensure that there is not stack_context\n        # between the user's callback and the IOLoop, so monkey-patch\n        # IOLoop.handle_callback_exception and disable the test harness's\n        # context with a NullContext.\n        # Note that this does not apply to secondary callbacks (header\n        # and streaming_callback), as errors there must be seen as errors\n        # by the http client so it can clean up the connection.\n        exc_info = []\n\n        def handle_callback_exception(callback):\n            exc_info.append(sys.exc_info())\n            self.stop()\n        self.io_loop.handle_callback_exception = handle_callback_exception\n        with NullContext():\n            self.http_client.fetch(self.get_url('/hello'),\n                                   lambda response: 1 / 0)\n        self.wait()\n        self.assertEqual(exc_info[0][0], ZeroDivisionError)\n\n    @gen_test\n    def test_future_interface(self):\n        response = yield self.http_client.fetch(self.get_url('/hello'))\n        self.assertEqual(response.body, b'Hello world!')\n\n    @gen_test\n    def test_future_http_error(self):\n        try:\n            yield self.http_client.fetch(self.get_url('/notfound'))\n        except HTTPError as e:\n            self.assertEqual(e.code, 404)\n            self.assertEqual(e.response.code, 404)\n\n    @gen_test\n    def test_reuse_request_from_response(self):\n        # The response.request attribute should be an HTTPRequest, not\n        # a _RequestProxy.\n        # This test uses self.http_client.fetch because self.fetch calls\n        # self.get_url on the input unconditionally.\n        url = self.get_url('/hello')\n        response = yield self.http_client.fetch(url)\n        self.assertEqual(response.request.url, url)\n        self.assertTrue(isinstance(response.request, HTTPRequest))\n        response2 = yield self.http_client.fetch(response.request)\n        self.assertEqual(response2.body, b'Hello world!')\n\n\nclass RequestProxyTest(unittest.TestCase):\n    def test_request_set(self):\n        proxy = _RequestProxy(HTTPRequest('http://example.com/',\n                                          user_agent='foo'),\n                              dict())\n        self.assertEqual(proxy.user_agent, 'foo')\n\n    def test_default_set(self):\n        proxy = _RequestProxy(HTTPRequest('http://example.com/'),\n                              dict(network_interface='foo'))\n        self.assertEqual(proxy.network_interface, 'foo')\n\n    def test_both_set(self):\n        proxy = _RequestProxy(HTTPRequest('http://example.com/',\n                                          proxy_host='foo'),\n                              dict(proxy_host='bar'))\n        self.assertEqual(proxy.proxy_host, 'foo')\n\n    def test_neither_set(self):\n        proxy = _RequestProxy(HTTPRequest('http://example.com/'),\n                              dict())\n        self.assertIs(proxy.auth_username, None)\n\n    def test_bad_attribute(self):\n        proxy = _RequestProxy(HTTPRequest('http://example.com/'),\n                              dict())\n        with self.assertRaises(AttributeError):\n            proxy.foo\n\n    def test_defaults_none(self):\n        proxy = _RequestProxy(HTTPRequest('http://example.com/'), None)\n        self.assertIs(proxy.auth_username, None)\n\n\nclass HTTPResponseTestCase(unittest.TestCase):\n    def test_str(self):\n        response = HTTPResponse(HTTPRequest('http://example.com'),\n                                200, headers={}, buffer=BytesIO())\n        s = str(response)\n        self.assertTrue(s.startswith('HTTPResponse('))\n        self.assertIn('code=200', s)\n\n\nclass SyncHTTPClientTest(unittest.TestCase):\n    def setUp(self):\n        if IOLoop.configured_class().__name__ == 'TwistedIOLoop':\n            # TwistedIOLoop only supports the global reactor, so we can't have\n            # separate IOLoops for client and server threads.\n            raise unittest.SkipTest(\n                'Sync HTTPClient not compatible with TwistedIOLoop')\n        self.server_ioloop = IOLoop()\n\n        sock, self.port = bind_unused_port()\n        app = Application([('/', HelloWorldHandler)])\n        server = HTTPServer(app, io_loop=self.server_ioloop)\n        server.add_socket(sock)\n\n        self.server_thread = threading.Thread(target=self.server_ioloop.start)\n        self.server_thread.start()\n\n        self.http_client = HTTPClient()\n\n    def tearDown(self):\n        self.server_ioloop.add_callback(self.server_ioloop.stop)\n        self.server_thread.join()\n        self.server_ioloop.close(all_fds=True)\n\n    def get_url(self, path):\n        return 'http://localhost:%d%s' % (self.port, path)\n\n    def test_sync_client(self):\n        response = self.http_client.fetch(self.get_url('/'))\n        self.assertEqual(b'Hello world!', response.body)\n\n    def test_sync_client_error(self):\n        # Synchronous HTTPClient raises errors directly; no need for\n        # response.rethrow()\n        with self.assertRaises(HTTPError) as assertion:\n            self.http_client.fetch(self.get_url('/notfound'))\n        self.assertEqual(assertion.exception.code, 404)\n",
            "header": {
                "file": {
                    "path": "tornado/test/httpclient_test.py"
                },
                "misc_line": null
            },
            "index_line": {
                "old_commit_hash": "36f312d7",
                "new_commit_hash": "254c9c09",
                "mode": "100644"
            },
            "is_binary_file": false,
            "binary_line": null,
            "minus_file": {
                "path": "a/tornado/test/httpclient_test.py"
            },
            "plus_file": {
                "path": "b/tornado/test/httpclient_test.py"
            },
            "hunks": [
                {
                    "descriptor": {
                        "old_range": {
                            "start": 334,
                            "length": 6
                        },
                        "new_range": {
                            "start": 334,
                            "length": 19
                        },
                        "section": "Transfer-Encoding: chunked"
                    },
                    "line_group": {
                        "all_lines": [
                            {
                                "content": "            self.assertEqual(e.code, 404)",
                                "type": "context"
                            },
                            {
                                "content": "            self.assertEqual(e.response.code, 404)",
                                "type": "context"
                            },
                            {
                                "content": "",
                                "type": "context"
                            },
                            {
                                "content": "    @gen_test",
                                "type": "added"
                            },
                            {
                                "content": "    def test_reuse_request_from_response(self):",
                                "type": "added"
                            },
                            {
                                "content": "        # The response.request attribute should be an HTTPRequest, not",
                                "type": "added"
                            },
                            {
                                "content": "        # a _RequestProxy.",
                                "type": "added"
                            },
                            {
                                "content": "        # This test uses self.http_client.fetch because self.fetch calls",
                                "type": "added"
                            },
                            {
                                "content": "        # self.get_url on the input unconditionally.",
                                "type": "added"
                            },
                            {
                                "content": "        url = self.get_url('/hello')",
                                "type": "added"
                            },
                            {
                                "content": "        response = yield self.http_client.fetch(url)",
                                "type": "added"
                            },
                            {
                                "content": "        self.assertEqual(response.request.url, url)",
                                "type": "added"
                            },
                            {
                                "content": "        self.assertTrue(isinstance(response.request, HTTPRequest))",
                                "type": "added"
                            },
                            {
                                "content": "        response2 = yield self.http_client.fetch(response.request)",
                                "type": "added"
                            },
                            {
                                "content": "        self.assertEqual(response2.body, b'Hello world!')",
                                "type": "added"
                            },
                            {
                                "content": "",
                                "type": "added"
                            },
                            {
                                "content": "",
                                "type": "context"
                            },
                            {
                                "content": "class RequestProxyTest(unittest.TestCase):",
                                "type": "context"
                            },
                            {
                                "content": "    def test_request_set(self):",
                                "type": "context"
                            },
                            {
                                "content": "",
                                "type": "context"
                            }
                        ]
                    },
                    "modified_entities": [
                        {
                            "file_name": "tornado/test/httpclient_test.py",
                            "type": "class",
                            "name": "HTTPClientCommonTestCase",
                            "content": "class HTTPClientCommonTestCase(AsyncHTTPTestCase):\n    def get_app(self):\n        return Application([\n            url(\"/hello\", HelloWorldHandler),\n            url(\"/post\", PostHandler),\n            url(\"/chunk\", ChunkHandler),\n            url(\"/auth\", AuthHandler),\n            url(\"/countdown/([0-9]+)\", CountdownHandler, name=\"countdown\"),\n            url(\"/echopost\", EchoPostHandler),\n            url(\"/user_agent\", UserAgentHandler),\n            url(\"/304_with_content_length\", ContentLength304Handler),\n        ], gzip=True)\n\n    def test_hello_world(self):\n        response = self.fetch(\"/hello\")\n        self.assertEqual(response.code, 200)\n        self.assertEqual(response.headers[\"Content-Type\"], \"text/plain\")\n        self.assertEqual(response.body, b\"Hello world!\")\n        self.assertEqual(int(response.request_time), 0)\n\n        response = self.fetch(\"/hello?name=Ben\")\n        self.assertEqual(response.body, b\"Hello Ben!\")\n\n    def test_streaming_callback(self):\n        # streaming_callback is also tested in test_chunked\n        chunks = []\n        response = self.fetch(\"/hello\",\n                              streaming_callback=chunks.append)\n        # with streaming_callback, data goes to the callback and not response.body\n        self.assertEqual(chunks, [b\"Hello world!\"])\n        self.assertFalse(response.body)\n\n    def test_post(self):\n        response = self.fetch(\"/post\", method=\"POST\",\n                              body=\"arg1=foo&arg2=bar\")\n        self.assertEqual(response.code, 200)\n        self.assertEqual(response.body, b\"Post arg1: foo, arg2: bar\")\n\n    def test_chunked(self):\n        response = self.fetch(\"/chunk\")\n        self.assertEqual(response.body, b\"asdfqwer\")\n\n        chunks = []\n        response = self.fetch(\"/chunk\",\n                              streaming_callback=chunks.append)\n        self.assertEqual(chunks, [b\"asdf\", b\"qwer\"])\n        self.assertFalse(response.body)\n\n    def test_chunked_close(self):\n        # test case in which chunks spread read-callback processing\n        # over several ioloop iterations, but the connection is already closed.\n        sock, port = bind_unused_port()\n        with closing(sock):\n            def write_response(stream, request_data):\n                stream.write(b\"\"\"\\\nHTTP/1.1 200 OK\nTransfer-Encoding: chunked\n\n1\n1\n1\n2\n0\n\n\"\"\".replace(b\"\\n\", b\"\\r\\n\"), callback=stream.close)\n\n            def accept_callback(conn, address):\n                # fake an HTTP server using chunked encoding where the final chunks\n                # and connection close all happen at once\n                stream = IOStream(conn, io_loop=self.io_loop)\n                stream.read_until(b\"\\r\\n\\r\\n\",\n                                  functools.partial(write_response, stream))\n            netutil.add_accept_handler(sock, accept_callback, self.io_loop)\n            self.http_client.fetch(\"http://127.0.0.1:%d/\" % port, self.stop)\n            resp = self.wait()\n            resp.rethrow()\n            self.assertEqual(resp.body, b\"12\")\n            self.io_loop.remove_handler(sock.fileno())\n\n    def test_streaming_stack_context(self):\n        chunks = []\n        exc_info = []\n\n        def error_handler(typ, value, tb):\n            exc_info.append((typ, value, tb))\n            return True\n\n        def streaming_cb(chunk):\n            chunks.append(chunk)\n            if chunk == b'qwer':\n                1 / 0\n\n        with ExceptionStackContext(error_handler):\n            self.fetch('/chunk', streaming_callback=streaming_cb)\n\n        self.assertEqual(chunks, [b'asdf', b'qwer'])\n        self.assertEqual(1, len(exc_info))\n        self.assertIs(exc_info[0][0], ZeroDivisionError)\n\n    def test_basic_auth(self):\n        self.assertEqual(self.fetch(\"/auth\", auth_username=\"Aladdin\",\n                                    auth_password=\"open sesame\").body,\n                         b\"Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==\")\n\n    def test_follow_redirect(self):\n        response = self.fetch(\"/countdown/2\", follow_redirects=False)\n        self.assertEqual(302, response.code)\n        self.assertTrue(response.headers[\"Location\"].endswith(\"/countdown/1\"))\n\n        response = self.fetch(\"/countdown/2\")\n        self.assertEqual(200, response.code)\n        self.assertTrue(response.effective_url.endswith(\"/countdown/0\"))\n        self.assertEqual(b\"Zero\", response.body)\n\n    def test_credentials_in_url(self):\n        url = self.get_url(\"/auth\").replace(\"http://\", \"http://me:secret@\")\n        self.http_client.fetch(url, self.stop)\n        response = self.wait()\n        self.assertEqual(b\"Basic \" + base64.b64encode(b\"me:secret\"),\n                         response.body)\n\n    def test_body_encoding(self):\n        unicode_body = u(\"\\xe9\")\n        byte_body = binascii.a2b_hex(b\"e9\")\n\n        # unicode string in body gets converted to utf8\n        response = self.fetch(\"/echopost\", method=\"POST\", body=unicode_body,\n                              headers={\"Content-Type\": \"application/blah\"})\n        self.assertEqual(response.headers[\"Content-Length\"], \"2\")\n        self.assertEqual(response.body, utf8(unicode_body))\n\n        # byte strings pass through directly\n        response = self.fetch(\"/echopost\", method=\"POST\",\n                              body=byte_body,\n                              headers={\"Content-Type\": \"application/blah\"})\n        self.assertEqual(response.headers[\"Content-Length\"], \"1\")\n        self.assertEqual(response.body, byte_body)\n\n        # Mixing unicode in headers and byte string bodies shouldn't\n        # break anything\n        response = self.fetch(\"/echopost\", method=\"POST\", body=byte_body,\n                              headers={\"Content-Type\": \"application/blah\"},\n                              user_agent=u(\"foo\"))\n        self.assertEqual(response.headers[\"Content-Length\"], \"1\")\n        self.assertEqual(response.body, byte_body)\n\n    def test_types(self):\n        response = self.fetch(\"/hello\")\n        self.assertEqual(type(response.body), bytes_type)\n        self.assertEqual(type(response.headers[\"Content-Type\"]), str)\n        self.assertEqual(type(response.code), int)\n        self.assertEqual(type(response.effective_url), str)\n\n    def test_header_callback(self):\n        first_line = []\n        headers = {}\n        chunks = []\n\n        def header_callback(header_line):\n            if header_line.startswith('HTTP/'):\n                first_line.append(header_line)\n            elif header_line != '\\r\\n':\n                k, v = header_line.split(':', 1)\n                headers[k] = v.strip()\n\n        def streaming_callback(chunk):\n            # All header callbacks are run before any streaming callbacks,\n            # so the header data is available to process the data as it\n            # comes in.\n            self.assertEqual(headers['Content-Type'], 'text/html; charset=UTF-8')\n            chunks.append(chunk)\n\n        self.fetch('/chunk', header_callback=header_callback,\n                   streaming_callback=streaming_callback)\n        self.assertEqual(len(first_line), 1)\n        self.assertRegexpMatches(first_line[0], 'HTTP/1.[01] 200 OK\\r\\n')\n        self.assertEqual(chunks, [b'asdf', b'qwer'])\n\n    def test_header_callback_stack_context(self):\n        exc_info = []\n\n        def error_handler(typ, value, tb):\n            exc_info.append((typ, value, tb))\n            return True\n\n        def header_callback(header_line):\n            if header_line.startswith('Content-Type:'):\n                1 / 0\n\n        with ExceptionStackContext(error_handler):\n            self.fetch('/chunk', header_callback=header_callback)\n        self.assertEqual(len(exc_info), 1)\n        self.assertIs(exc_info[0][0], ZeroDivisionError)\n\n    def test_configure_defaults(self):\n        defaults = dict(user_agent='TestDefaultUserAgent')\n        # Construct a new instance of the configured client class\n        client = self.http_client.__class__(self.io_loop, force_instance=True,\n                                            defaults=defaults)\n        client.fetch(self.get_url('/user_agent'), callback=self.stop)\n        response = self.wait()\n        self.assertEqual(response.body, b'TestDefaultUserAgent')\n        client.close()\n\n    def test_304_with_content_length(self):\n        # According to the spec 304 responses SHOULD NOT include\n        # Content-Length or other entity headers, but some servers do it\n        # anyway.\n        # http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.5\n        response = self.fetch('/304_with_content_length')\n        self.assertEqual(response.code, 304)\n        self.assertEqual(response.headers['Content-Length'], '42')\n\n    def test_final_callback_stack_context(self):\n        # The final callback should be run outside of the httpclient's\n        # stack_context.  We want to ensure that there is not stack_context\n        # between the user's callback and the IOLoop, so monkey-patch\n        # IOLoop.handle_callback_exception and disable the test harness's\n        # context with a NullContext.\n        # Note that this does not apply to secondary callbacks (header\n        # and streaming_callback), as errors there must be seen as errors\n        # by the http client so it can clean up the connection.\n        exc_info = []\n\n        def handle_callback_exception(callback):\n            exc_info.append(sys.exc_info())\n            self.stop()\n        self.io_loop.handle_callback_exception = handle_callback_exception\n        with NullContext():\n            self.http_client.fetch(self.get_url('/hello'),\n                                   lambda response: 1 / 0)\n        self.wait()\n        self.assertEqual(exc_info[0][0], ZeroDivisionError)\n\n    @gen_test\n    def test_future_interface(self):\n        response = yield self.http_client.fetch(self.get_url('/hello'))\n        self.assertEqual(response.body, b'Hello world!')\n\n    @gen_test\n    def test_future_http_error(self):\n        try:\n            yield self.http_client.fetch(self.get_url('/notfound'))\n        except HTTPError as e:\n            self.assertEqual(e.code, 404)\n            self.assertEqual(e.response.code, 404)\n\n    @gen_test\n    def test_reuse_request_from_response(self):\n        # The response.request attribute should be an HTTPRequest, not\n        # a _RequestProxy.\n        # This test uses self.http_client.fetch because self.fetch calls\n        # self.get_url on the input unconditionally.\n        url = self.get_url('/hello')\n        response = yield self.http_client.fetch(url)\n        self.assertEqual(response.request.url, url)\n        self.assertTrue(isinstance(response.request, HTTPRequest))\n        response2 = yield self.http_client.fetch(response.request)\n        self.assertEqual(response2.body, b'Hello world!')",
                            "ast_type_str": "ClassDef",
                            "start_lineno": 90,
                            "end_lineno": 348,
                            "parent": null
                        }
                    ],
                    "added_entities": [
                        {
                            "file_name": "tornado/test/httpclient_test.py",
                            "type": "method",
                            "name": "HTTPClientCommonTestCase.test_reuse_request_from_response",
                            "content": "class HTTPClientCommonTestCase(AsyncHTTPTestCase):\n    def get_app(self):\n        return Application([\n            url(\"/hello\", HelloWorldHandler),\n            url(\"/post\", PostHandler),\n            url(\"/chunk\", ChunkHandler),\n            url(\"/auth\", AuthHandler),\n            url(\"/countdown/([0-9]+)\", CountdownHandler, name=\"countdown\"),\n            url(\"/echopost\", EchoPostHandler),\n            url(\"/user_agent\", UserAgentHandler),\n            url(\"/304_with_content_length\", ContentLength304Handler),\n        ], gzip=True)\n\n    def test_hello_world(self):\n        response = self.fetch(\"/hello\")\n        self.assertEqual(response.code, 200)\n        self.assertEqual(response.headers[\"Content-Type\"], \"text/plain\")\n        self.assertEqual(response.body, b\"Hello world!\")\n        self.assertEqual(int(response.request_time), 0)\n\n        response = self.fetch(\"/hello?name=Ben\")\n        self.assertEqual(response.body, b\"Hello Ben!\")\n\n    def test_streaming_callback(self):\n        # streaming_callback is also tested in test_chunked\n        chunks = []\n        response = self.fetch(\"/hello\",\n                              streaming_callback=chunks.append)\n        # with streaming_callback, data goes to the callback and not response.body\n        self.assertEqual(chunks, [b\"Hello world!\"])\n        self.assertFalse(response.body)\n\n    def test_post(self):\n        response = self.fetch(\"/post\", method=\"POST\",\n                              body=\"arg1=foo&arg2=bar\")\n        self.assertEqual(response.code, 200)\n        self.assertEqual(response.body, b\"Post arg1: foo, arg2: bar\")\n\n    def test_chunked(self):\n        response = self.fetch(\"/chunk\")\n        self.assertEqual(response.body, b\"asdfqwer\")\n\n        chunks = []\n        response = self.fetch(\"/chunk\",\n                              streaming_callback=chunks.append)\n        self.assertEqual(chunks, [b\"asdf\", b\"qwer\"])\n        self.assertFalse(response.body)\n\n    def test_chunked_close(self):\n        # test case in which chunks spread read-callback processing\n        # over several ioloop iterations, but the connection is already closed.\n        sock, port = bind_unused_port()\n        with closing(sock):\n            def write_response(stream, request_data):\n                stream.write(b\"\"\"\\\nHTTP/1.1 200 OK\nTransfer-Encoding: chunked\n\n1\n1\n1\n2\n0\n\n\"\"\".replace(b\"\\n\", b\"\\r\\n\"), callback=stream.close)\n\n            def accept_callback(conn, address):\n                # fake an HTTP server using chunked encoding where the final chunks\n                # and connection close all happen at once\n                stream = IOStream(conn, io_loop=self.io_loop)\n                stream.read_until(b\"\\r\\n\\r\\n\",\n                                  functools.partial(write_response, stream))\n            netutil.add_accept_handler(sock, accept_callback, self.io_loop)\n            self.http_client.fetch(\"http://127.0.0.1:%d/\" % port, self.stop)\n            resp = self.wait()\n            resp.rethrow()\n            self.assertEqual(resp.body, b\"12\")\n            self.io_loop.remove_handler(sock.fileno())\n\n    def test_streaming_stack_context(self):\n        chunks = []\n        exc_info = []\n\n        def error_handler(typ, value, tb):\n            exc_info.append((typ, value, tb))\n            return True\n\n        def streaming_cb(chunk):\n            chunks.append(chunk)\n            if chunk == b'qwer':\n                1 / 0\n\n        with ExceptionStackContext(error_handler):\n            self.fetch('/chunk', streaming_callback=streaming_cb)\n\n        self.assertEqual(chunks, [b'asdf', b'qwer'])\n        self.assertEqual(1, len(exc_info))\n        self.assertIs(exc_info[0][0], ZeroDivisionError)\n\n    def test_basic_auth(self):\n        self.assertEqual(self.fetch(\"/auth\", auth_username=\"Aladdin\",\n                                    auth_password=\"open sesame\").body,\n                         b\"Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==\")\n\n    def test_follow_redirect(self):\n        response = self.fetch(\"/countdown/2\", follow_redirects=False)\n        self.assertEqual(302, response.code)\n        self.assertTrue(response.headers[\"Location\"].endswith(\"/countdown/1\"))\n\n        response = self.fetch(\"/countdown/2\")\n        self.assertEqual(200, response.code)\n        self.assertTrue(response.effective_url.endswith(\"/countdown/0\"))\n        self.assertEqual(b\"Zero\", response.body)\n\n    def test_credentials_in_url(self):\n        url = self.get_url(\"/auth\").replace(\"http://\", \"http://me:secret@\")\n        self.http_client.fetch(url, self.stop)\n        response = self.wait()\n        self.assertEqual(b\"Basic \" + base64.b64encode(b\"me:secret\"),\n                         response.body)\n\n    def test_body_encoding(self):\n        unicode_body = u(\"\\xe9\")\n        byte_body = binascii.a2b_hex(b\"e9\")\n\n        # unicode string in body gets converted to utf8\n        response = self.fetch(\"/echopost\", method=\"POST\", body=unicode_body,\n                              headers={\"Content-Type\": \"application/blah\"})\n        self.assertEqual(response.headers[\"Content-Length\"], \"2\")\n        self.assertEqual(response.body, utf8(unicode_body))\n\n        # byte strings pass through directly\n        response = self.fetch(\"/echopost\", method=\"POST\",\n                              body=byte_body,\n                              headers={\"Content-Type\": \"application/blah\"})\n        self.assertEqual(response.headers[\"Content-Length\"], \"1\")\n        self.assertEqual(response.body, byte_body)\n\n        # Mixing unicode in headers and byte string bodies shouldn't\n        # break anything\n        response = self.fetch(\"/echopost\", method=\"POST\", body=byte_body,\n                              headers={\"Content-Type\": \"application/blah\"},\n                              user_agent=u(\"foo\"))\n        self.assertEqual(response.headers[\"Content-Length\"], \"1\")\n        self.assertEqual(response.body, byte_body)\n\n    def test_types(self):\n        response = self.fetch(\"/hello\")\n        self.assertEqual(type(response.body), bytes_type)\n        self.assertEqual(type(response.headers[\"Content-Type\"]), str)\n        self.assertEqual(type(response.code), int)\n        self.assertEqual(type(response.effective_url), str)\n\n    def test_header_callback(self):\n        first_line = []\n        headers = {}\n        chunks = []\n\n        def header_callback(header_line):\n            if header_line.startswith('HTTP/'):\n                first_line.append(header_line)\n            elif header_line != '\\r\\n':\n                k, v = header_line.split(':', 1)\n                headers[k] = v.strip()\n\n        def streaming_callback(chunk):\n            # All header callbacks are run before any streaming callbacks,\n            # so the header data is available to process the data as it\n            # comes in.\n            self.assertEqual(headers['Content-Type'], 'text/html; charset=UTF-8')\n            chunks.append(chunk)\n\n        self.fetch('/chunk', header_callback=header_callback,\n                   streaming_callback=streaming_callback)\n        self.assertEqual(len(first_line), 1)\n        self.assertRegexpMatches(first_line[0], 'HTTP/1.[01] 200 OK\\r\\n')\n        self.assertEqual(chunks, [b'asdf', b'qwer'])\n\n    def test_header_callback_stack_context(self):\n        exc_info = []\n\n        def error_handler(typ, value, tb):\n            exc_info.append((typ, value, tb))\n            return True\n\n        def header_callback(header_line):\n            if header_line.startswith('Content-Type:'):\n                1 / 0\n\n        with ExceptionStackContext(error_handler):\n            self.fetch('/chunk', header_callback=header_callback)\n        self.assertEqual(len(exc_info), 1)\n        self.assertIs(exc_info[0][0], ZeroDivisionError)\n\n    def test_configure_defaults(self):\n        defaults = dict(user_agent='TestDefaultUserAgent')\n        # Construct a new instance of the configured client class\n        client = self.http_client.__class__(self.io_loop, force_instance=True,\n                                            defaults=defaults)\n        client.fetch(self.get_url('/user_agent'), callback=self.stop)\n        response = self.wait()\n        self.assertEqual(response.body, b'TestDefaultUserAgent')\n        client.close()\n\n    def test_304_with_content_length(self):\n        # According to the spec 304 responses SHOULD NOT include\n        # Content-Length or other entity headers, but some servers do it\n        # anyway.\n        # http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.5\n        response = self.fetch('/304_with_content_length')\n        self.assertEqual(response.code, 304)\n        self.assertEqual(response.headers['Content-Length'], '42')\n\n    def test_final_callback_stack_context(self):\n        # The final callback should be run outside of the httpclient's\n        # stack_context.  We want to ensure that there is not stack_context\n        # between the user's callback and the IOLoop, so monkey-patch\n        # IOLoop.handle_callback_exception and disable the test harness's\n        # context with a NullContext.\n        # Note that this does not apply to secondary callbacks (header\n        # and streaming_callback), as errors there must be seen as errors\n        # by the http client so it can clean up the connection.\n        exc_info = []\n\n        def handle_callback_exception(callback):\n            exc_info.append(sys.exc_info())\n            self.stop()\n        self.io_loop.handle_callback_exception = handle_callback_exception\n        with NullContext():\n            self.http_client.fetch(self.get_url('/hello'),\n                                   lambda response: 1 / 0)\n        self.wait()\n        self.assertEqual(exc_info[0][0], ZeroDivisionError)\n\n    @gen_test\n    def test_future_interface(self):\n        response = yield self.http_client.fetch(self.get_url('/hello'))\n        self.assertEqual(response.body, b'Hello world!')\n\n    @gen_test\n    def test_future_http_error(self):\n        try:\n            yield self.http_client.fetch(self.get_url('/notfound'))\n        except HTTPError as e:\n            self.assertEqual(e.code, 404)\n            self.assertEqual(e.response.code, 404)\n\n    @gen_test\n    def test_reuse_request_from_response(self):\n        # The response.request attribute should be an HTTPRequest, not\n        # a _RequestProxy.\n        # This test uses self.http_client.fetch because self.fetch calls\n        # self.get_url on the input unconditionally.\n        url = self.get_url('/hello')\n        response = yield self.http_client.fetch(url)\n        self.assertEqual(response.request.url, url)\n        self.assertTrue(isinstance(response.request, HTTPRequest))\n        response2 = yield self.http_client.fetch(response.request)\n        self.assertEqual(response2.body, b'Hello world!')",
                            "ast_type_str": "ClassDef",
                            "start_lineno": 338,
                            "end_lineno": 348,
                            "parent": {
                                "file_name": "tornado/test/httpclient_test.py",
                                "type": "class",
                                "name": "HTTPClientCommonTestCase",
                                "content": "class HTTPClientCommonTestCase(AsyncHTTPTestCase):\n    def get_app(self):\n        return Application([\n            url(\"/hello\", HelloWorldHandler),\n            url(\"/post\", PostHandler),\n            url(\"/chunk\", ChunkHandler),\n            url(\"/auth\", AuthHandler),\n            url(\"/countdown/([0-9]+)\", CountdownHandler, name=\"countdown\"),\n            url(\"/echopost\", EchoPostHandler),\n            url(\"/user_agent\", UserAgentHandler),\n            url(\"/304_with_content_length\", ContentLength304Handler),\n        ], gzip=True)\n\n    def test_hello_world(self):\n        response = self.fetch(\"/hello\")\n        self.assertEqual(response.code, 200)\n        self.assertEqual(response.headers[\"Content-Type\"], \"text/plain\")\n        self.assertEqual(response.body, b\"Hello world!\")\n        self.assertEqual(int(response.request_time), 0)\n\n        response = self.fetch(\"/hello?name=Ben\")\n        self.assertEqual(response.body, b\"Hello Ben!\")\n\n    def test_streaming_callback(self):\n        # streaming_callback is also tested in test_chunked\n        chunks = []\n        response = self.fetch(\"/hello\",\n                              streaming_callback=chunks.append)\n        # with streaming_callback, data goes to the callback and not response.body\n        self.assertEqual(chunks, [b\"Hello world!\"])\n        self.assertFalse(response.body)\n\n    def test_post(self):\n        response = self.fetch(\"/post\", method=\"POST\",\n                              body=\"arg1=foo&arg2=bar\")\n        self.assertEqual(response.code, 200)\n        self.assertEqual(response.body, b\"Post arg1: foo, arg2: bar\")\n\n    def test_chunked(self):\n        response = self.fetch(\"/chunk\")\n        self.assertEqual(response.body, b\"asdfqwer\")\n\n        chunks = []\n        response = self.fetch(\"/chunk\",\n                              streaming_callback=chunks.append)\n        self.assertEqual(chunks, [b\"asdf\", b\"qwer\"])\n        self.assertFalse(response.body)\n\n    def test_chunked_close(self):\n        # test case in which chunks spread read-callback processing\n        # over several ioloop iterations, but the connection is already closed.\n        sock, port = bind_unused_port()\n        with closing(sock):\n            def write_response(stream, request_data):\n                stream.write(b\"\"\"\\\nHTTP/1.1 200 OK\nTransfer-Encoding: chunked\n\n1\n1\n1\n2\n0\n\n\"\"\".replace(b\"\\n\", b\"\\r\\n\"), callback=stream.close)\n\n            def accept_callback(conn, address):\n                # fake an HTTP server using chunked encoding where the final chunks\n                # and connection close all happen at once\n                stream = IOStream(conn, io_loop=self.io_loop)\n                stream.read_until(b\"\\r\\n\\r\\n\",\n                                  functools.partial(write_response, stream))\n            netutil.add_accept_handler(sock, accept_callback, self.io_loop)\n            self.http_client.fetch(\"http://127.0.0.1:%d/\" % port, self.stop)\n            resp = self.wait()\n            resp.rethrow()\n            self.assertEqual(resp.body, b\"12\")\n            self.io_loop.remove_handler(sock.fileno())\n\n    def test_streaming_stack_context(self):\n        chunks = []\n        exc_info = []\n\n        def error_handler(typ, value, tb):\n            exc_info.append((typ, value, tb))\n            return True\n\n        def streaming_cb(chunk):\n            chunks.append(chunk)\n            if chunk == b'qwer':\n                1 / 0\n\n        with ExceptionStackContext(error_handler):\n            self.fetch('/chunk', streaming_callback=streaming_cb)\n\n        self.assertEqual(chunks, [b'asdf', b'qwer'])\n        self.assertEqual(1, len(exc_info))\n        self.assertIs(exc_info[0][0], ZeroDivisionError)\n\n    def test_basic_auth(self):\n        self.assertEqual(self.fetch(\"/auth\", auth_username=\"Aladdin\",\n                                    auth_password=\"open sesame\").body,\n                         b\"Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==\")\n\n    def test_follow_redirect(self):\n        response = self.fetch(\"/countdown/2\", follow_redirects=False)\n        self.assertEqual(302, response.code)\n        self.assertTrue(response.headers[\"Location\"].endswith(\"/countdown/1\"))\n\n        response = self.fetch(\"/countdown/2\")\n        self.assertEqual(200, response.code)\n        self.assertTrue(response.effective_url.endswith(\"/countdown/0\"))\n        self.assertEqual(b\"Zero\", response.body)\n\n    def test_credentials_in_url(self):\n        url = self.get_url(\"/auth\").replace(\"http://\", \"http://me:secret@\")\n        self.http_client.fetch(url, self.stop)\n        response = self.wait()\n        self.assertEqual(b\"Basic \" + base64.b64encode(b\"me:secret\"),\n                         response.body)\n\n    def test_body_encoding(self):\n        unicode_body = u(\"\\xe9\")\n        byte_body = binascii.a2b_hex(b\"e9\")\n\n        # unicode string in body gets converted to utf8\n        response = self.fetch(\"/echopost\", method=\"POST\", body=unicode_body,\n                              headers={\"Content-Type\": \"application/blah\"})\n        self.assertEqual(response.headers[\"Content-Length\"], \"2\")\n        self.assertEqual(response.body, utf8(unicode_body))\n\n        # byte strings pass through directly\n        response = self.fetch(\"/echopost\", method=\"POST\",\n                              body=byte_body,\n                              headers={\"Content-Type\": \"application/blah\"})\n        self.assertEqual(response.headers[\"Content-Length\"], \"1\")\n        self.assertEqual(response.body, byte_body)\n\n        # Mixing unicode in headers and byte string bodies shouldn't\n        # break anything\n        response = self.fetch(\"/echopost\", method=\"POST\", body=byte_body,\n                              headers={\"Content-Type\": \"application/blah\"},\n                              user_agent=u(\"foo\"))\n        self.assertEqual(response.headers[\"Content-Length\"], \"1\")\n        self.assertEqual(response.body, byte_body)\n\n    def test_types(self):\n        response = self.fetch(\"/hello\")\n        self.assertEqual(type(response.body), bytes_type)\n        self.assertEqual(type(response.headers[\"Content-Type\"]), str)\n        self.assertEqual(type(response.code), int)\n        self.assertEqual(type(response.effective_url), str)\n\n    def test_header_callback(self):\n        first_line = []\n        headers = {}\n        chunks = []\n\n        def header_callback(header_line):\n            if header_line.startswith('HTTP/'):\n                first_line.append(header_line)\n            elif header_line != '\\r\\n':\n                k, v = header_line.split(':', 1)\n                headers[k] = v.strip()\n\n        def streaming_callback(chunk):\n            # All header callbacks are run before any streaming callbacks,\n            # so the header data is available to process the data as it\n            # comes in.\n            self.assertEqual(headers['Content-Type'], 'text/html; charset=UTF-8')\n            chunks.append(chunk)\n\n        self.fetch('/chunk', header_callback=header_callback,\n                   streaming_callback=streaming_callback)\n        self.assertEqual(len(first_line), 1)\n        self.assertRegexpMatches(first_line[0], 'HTTP/1.[01] 200 OK\\r\\n')\n        self.assertEqual(chunks, [b'asdf', b'qwer'])\n\n    def test_header_callback_stack_context(self):\n        exc_info = []\n\n        def error_handler(typ, value, tb):\n            exc_info.append((typ, value, tb))\n            return True\n\n        def header_callback(header_line):\n            if header_line.startswith('Content-Type:'):\n                1 / 0\n\n        with ExceptionStackContext(error_handler):\n            self.fetch('/chunk', header_callback=header_callback)\n        self.assertEqual(len(exc_info), 1)\n        self.assertIs(exc_info[0][0], ZeroDivisionError)\n\n    def test_configure_defaults(self):\n        defaults = dict(user_agent='TestDefaultUserAgent')\n        # Construct a new instance of the configured client class\n        client = self.http_client.__class__(self.io_loop, force_instance=True,\n                                            defaults=defaults)\n        client.fetch(self.get_url('/user_agent'), callback=self.stop)\n        response = self.wait()\n        self.assertEqual(response.body, b'TestDefaultUserAgent')\n        client.close()\n\n    def test_304_with_content_length(self):\n        # According to the spec 304 responses SHOULD NOT include\n        # Content-Length or other entity headers, but some servers do it\n        # anyway.\n        # http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.5\n        response = self.fetch('/304_with_content_length')\n        self.assertEqual(response.code, 304)\n        self.assertEqual(response.headers['Content-Length'], '42')\n\n    def test_final_callback_stack_context(self):\n        # The final callback should be run outside of the httpclient's\n        # stack_context.  We want to ensure that there is not stack_context\n        # between the user's callback and the IOLoop, so monkey-patch\n        # IOLoop.handle_callback_exception and disable the test harness's\n        # context with a NullContext.\n        # Note that this does not apply to secondary callbacks (header\n        # and streaming_callback), as errors there must be seen as errors\n        # by the http client so it can clean up the connection.\n        exc_info = []\n\n        def handle_callback_exception(callback):\n            exc_info.append(sys.exc_info())\n            self.stop()\n        self.io_loop.handle_callback_exception = handle_callback_exception\n        with NullContext():\n            self.http_client.fetch(self.get_url('/hello'),\n                                   lambda response: 1 / 0)\n        self.wait()\n        self.assertEqual(exc_info[0][0], ZeroDivisionError)\n\n    @gen_test\n    def test_future_interface(self):\n        response = yield self.http_client.fetch(self.get_url('/hello'))\n        self.assertEqual(response.body, b'Hello world!')\n\n    @gen_test\n    def test_future_http_error(self):\n        try:\n            yield self.http_client.fetch(self.get_url('/notfound'))\n        except HTTPError as e:\n            self.assertEqual(e.code, 404)\n            self.assertEqual(e.response.code, 404)\n\n    @gen_test\n    def test_reuse_request_from_response(self):\n        # The response.request attribute should be an HTTPRequest, not\n        # a _RequestProxy.\n        # This test uses self.http_client.fetch because self.fetch calls\n        # self.get_url on the input unconditionally.\n        url = self.get_url('/hello')\n        response = yield self.http_client.fetch(url)\n        self.assertEqual(response.request.url, url)\n        self.assertTrue(isinstance(response.request, HTTPRequest))\n        response2 = yield self.http_client.fetch(response.request)\n        self.assertEqual(response2.body, b'Hello world!')",
                                "ast_type_str": "ClassDef",
                                "start_lineno": 90,
                                "end_lineno": 348,
                                "parent": null
                            }
                        }
                    ],
                    "deleted_entities": []
                }
            ]
        }
    ],
    "old_commit_hash": "6e63b238c7ed7c307f1e22232afa7df7fb5901a9^",
    "new_commit_hash": "6e63b238c7ed7c307f1e22232afa7df7fb5901a9",
    "commit_message": "HTTPResponse.request should always be an HTTPRequest, never a _RequestProxy.\n\nCloses #718.",
    "commit_date": "2013-04-08T21:47:35-04:00",
    "metadata": {}
}
{
    "repo_name": "tornado",
    "new_commit_hash": "6e63b238c7ed7c307f1e22232afa7df7fb5901a9",
    "test_file_codes": [
        "#!/usr/bin/env python\n\nfrom __future__ import absolute_import, division, print_function, with_statement\n\nimport base64\nimport binascii\nfrom contextlib import closing\nimport functools\nimport sys\nimport threading\n\nfrom tornado.escape import utf8\nfrom tornado.httpclient import HTTPRequest, HTTPResponse, _RequestProxy, HTTPError, HTTPClient\nfrom tornado.httpserver import HTTPServer\nfrom tornado.ioloop import IOLoop\nfrom tornado.iostream import IOStream\nfrom tornado import netutil\nfrom tornado.stack_context import ExceptionStackContext, NullContext\nfrom tornado.testing import AsyncHTTPTestCase, bind_unused_port, gen_test\nfrom tornado.test.util import unittest\nfrom tornado.util import u, bytes_type\nfrom tornado.web import Application, RequestHandler, url\n\ntry:\n    from io import BytesIO  # python 3\nexcept ImportError:\n    from cStringIO import StringIO as BytesIO\n\n\nclass HelloWorldHandler(RequestHandler):\n    def get(self):\n        name = self.get_argument(\"name\", \"world\")\n        self.set_header(\"Content-Type\", \"text/plain\")\n        self.finish(\"Hello %s!\" % name)\n\n\nclass PostHandler(RequestHandler):\n    def post(self):\n        self.finish(\"Post arg1: %s, arg2: %s\" % (\n            self.get_argument(\"arg1\"), self.get_argument(\"arg2\")))\n\n\nclass ChunkHandler(RequestHandler):\n    def get(self):\n        self.write(\"asdf\")\n        self.flush()\n        self.write(\"qwer\")\n\n\nclass AuthHandler(RequestHandler):\n    def get(self):\n        self.finish(self.request.headers[\"Authorization\"])\n\n\nclass CountdownHandler(RequestHandler):\n    def get(self, count):\n        count = int(count)\n        if count > 0:\n            self.redirect(self.reverse_url(\"countdown\", count - 1))\n        else:\n            self.write(\"Zero\")\n\n\nclass EchoPostHandler(RequestHandler):\n    def post(self):\n        self.write(self.request.body)\n\n\nclass UserAgentHandler(RequestHandler):\n    def get(self):\n        self.write(self.request.headers.get('User-Agent', 'User agent not set'))\n\n\nclass ContentLength304Handler(RequestHandler):\n    def get(self):\n        self.set_status(304)\n        self.set_header('Content-Length', 42)\n\n    def _clear_headers_for_304(self):\n        # Tornado strips content-length from 304 responses, but here we\n        # want to simulate servers that include the headers anyway.\n        pass\n\n\n# These tests end up getting run redundantly: once here with the default\n# HTTPClient implementation, and then again in each implementation's own\n# test suite.\n\n\nclass HTTPClientCommonTestCase(AsyncHTTPTestCase):\n    def get_app(self):\n        return Application([\n            url(\"/hello\", HelloWorldHandler),\n            url(\"/post\", PostHandler),\n            url(\"/chunk\", ChunkHandler),\n            url(\"/auth\", AuthHandler),\n            url(\"/countdown/([0-9]+)\", CountdownHandler, name=\"countdown\"),\n            url(\"/echopost\", EchoPostHandler),\n            url(\"/user_agent\", UserAgentHandler),\n            url(\"/304_with_content_length\", ContentLength304Handler),\n        ], gzip=True)\n\n    def test_hello_world(self):\n        response = self.fetch(\"/hello\")\n        self.assertEqual(response.code, 200)\n        self.assertEqual(response.headers[\"Content-Type\"], \"text/plain\")\n        self.assertEqual(response.body, b\"Hello world!\")\n        self.assertEqual(int(response.request_time), 0)\n\n        response = self.fetch(\"/hello?name=Ben\")\n        self.assertEqual(response.body, b\"Hello Ben!\")\n\n    def test_streaming_callback(self):\n        # streaming_callback is also tested in test_chunked\n        chunks = []\n        response = self.fetch(\"/hello\",\n                              streaming_callback=chunks.append)\n        # with streaming_callback, data goes to the callback and not response.body\n        self.assertEqual(chunks, [b\"Hello world!\"])\n        self.assertFalse(response.body)\n\n    def test_post(self):\n        response = self.fetch(\"/post\", method=\"POST\",\n                              body=\"arg1=foo&arg2=bar\")\n        self.assertEqual(response.code, 200)\n        self.assertEqual(response.body, b\"Post arg1: foo, arg2: bar\")\n\n    def test_chunked(self):\n        response = self.fetch(\"/chunk\")\n        self.assertEqual(response.body, b\"asdfqwer\")\n\n        chunks = []\n        response = self.fetch(\"/chunk\",\n                              streaming_callback=chunks.append)\n        self.assertEqual(chunks, [b\"asdf\", b\"qwer\"])\n        self.assertFalse(response.body)\n\n    def test_chunked_close(self):\n        # test case in which chunks spread read-callback processing\n        # over several ioloop iterations, but the connection is already closed.\n        sock, port = bind_unused_port()\n        with closing(sock):\n            def write_response(stream, request_data):\n                stream.write(b\"\"\"\\\nHTTP/1.1 200 OK\nTransfer-Encoding: chunked\n\n1\n1\n1\n2\n0\n\n\"\"\".replace(b\"\\n\", b\"\\r\\n\"), callback=stream.close)\n\n            def accept_callback(conn, address):\n                # fake an HTTP server using chunked encoding where the final chunks\n                # and connection close all happen at once\n                stream = IOStream(conn, io_loop=self.io_loop)\n                stream.read_until(b\"\\r\\n\\r\\n\",\n                                  functools.partial(write_response, stream))\n            netutil.add_accept_handler(sock, accept_callback, self.io_loop)\n            self.http_client.fetch(\"http://127.0.0.1:%d/\" % port, self.stop)\n            resp = self.wait()\n            resp.rethrow()\n            self.assertEqual(resp.body, b\"12\")\n            self.io_loop.remove_handler(sock.fileno())\n\n    def test_streaming_stack_context(self):\n        chunks = []\n        exc_info = []\n\n        def error_handler(typ, value, tb):\n            exc_info.append((typ, value, tb))\n            return True\n\n        def streaming_cb(chunk):\n            chunks.append(chunk)\n            if chunk == b'qwer':\n                1 / 0\n\n        with ExceptionStackContext(error_handler):\n            self.fetch('/chunk', streaming_callback=streaming_cb)\n\n        self.assertEqual(chunks, [b'asdf', b'qwer'])\n        self.assertEqual(1, len(exc_info))\n        self.assertIs(exc_info[0][0], ZeroDivisionError)\n\n    def test_basic_auth(self):\n        self.assertEqual(self.fetch(\"/auth\", auth_username=\"Aladdin\",\n                                    auth_password=\"open sesame\").body,\n                         b\"Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==\")\n\n    def test_follow_redirect(self):\n        response = self.fetch(\"/countdown/2\", follow_redirects=False)\n        self.assertEqual(302, response.code)\n        self.assertTrue(response.headers[\"Location\"].endswith(\"/countdown/1\"))\n\n        response = self.fetch(\"/countdown/2\")\n        self.assertEqual(200, response.code)\n        self.assertTrue(response.effective_url.endswith(\"/countdown/0\"))\n        self.assertEqual(b\"Zero\", response.body)\n\n    def test_credentials_in_url(self):\n        url = self.get_url(\"/auth\").replace(\"http://\", \"http://me:secret@\")\n        self.http_client.fetch(url, self.stop)\n        response = self.wait()\n        self.assertEqual(b\"Basic \" + base64.b64encode(b\"me:secret\"),\n                         response.body)\n\n    def test_body_encoding(self):\n        unicode_body = u(\"\\xe9\")\n        byte_body = binascii.a2b_hex(b\"e9\")\n\n        # unicode string in body gets converted to utf8\n        response = self.fetch(\"/echopost\", method=\"POST\", body=unicode_body,\n                              headers={\"Content-Type\": \"application/blah\"})\n        self.assertEqual(response.headers[\"Content-Length\"], \"2\")\n        self.assertEqual(response.body, utf8(unicode_body))\n\n        # byte strings pass through directly\n        response = self.fetch(\"/echopost\", method=\"POST\",\n                              body=byte_body,\n                              headers={\"Content-Type\": \"application/blah\"})\n        self.assertEqual(response.headers[\"Content-Length\"], \"1\")\n        self.assertEqual(response.body, byte_body)\n\n        # Mixing unicode in headers and byte string bodies shouldn't\n        # break anything\n        response = self.fetch(\"/echopost\", method=\"POST\", body=byte_body,\n                              headers={\"Content-Type\": \"application/blah\"},\n                              user_agent=u(\"foo\"))\n        self.assertEqual(response.headers[\"Content-Length\"], \"1\")\n        self.assertEqual(response.body, byte_body)\n\n    def test_types(self):\n        response = self.fetch(\"/hello\")\n        self.assertEqual(type(response.body), bytes_type)\n        self.assertEqual(type(response.headers[\"Content-Type\"]), str)\n        self.assertEqual(type(response.code), int)\n        self.assertEqual(type(response.effective_url), str)\n\n    def test_header_callback(self):\n        first_line = []\n        headers = {}\n        chunks = []\n\n        def header_callback(header_line):\n            if header_line.startswith('HTTP/'):\n                first_line.append(header_line)\n            elif header_line != '\\r\\n':\n                k, v = header_line.split(':', 1)\n                headers[k] = v.strip()\n\n        def streaming_callback(chunk):\n            # All header callbacks are run before any streaming callbacks,\n            # so the header data is available to process the data as it\n            # comes in.\n            self.assertEqual(headers['Content-Type'], 'text/html; charset=UTF-8')\n            chunks.append(chunk)\n\n        self.fetch('/chunk', header_callback=header_callback,\n                   streaming_callback=streaming_callback)\n        self.assertEqual(len(first_line), 1)\n        self.assertRegexpMatches(first_line[0], 'HTTP/1.[01] 200 OK\\r\\n')\n        self.assertEqual(chunks, [b'asdf', b'qwer'])\n\n    def test_header_callback_stack_context(self):\n        exc_info = []\n\n        def error_handler(typ, value, tb):\n            exc_info.append((typ, value, tb))\n            return True\n\n        def header_callback(header_line):\n            if header_line.startswith('Content-Type:'):\n                1 / 0\n\n        with ExceptionStackContext(error_handler):\n            self.fetch('/chunk', header_callback=header_callback)\n        self.assertEqual(len(exc_info), 1)\n        self.assertIs(exc_info[0][0], ZeroDivisionError)\n\n    def test_configure_defaults(self):\n        defaults = dict(user_agent='TestDefaultUserAgent')\n        # Construct a new instance of the configured client class\n        client = self.http_client.__class__(self.io_loop, force_instance=True,\n                                            defaults=defaults)\n        client.fetch(self.get_url('/user_agent'), callback=self.stop)\n        response = self.wait()\n        self.assertEqual(response.body, b'TestDefaultUserAgent')\n        client.close()\n\n    def test_304_with_content_length(self):\n        # According to the spec 304 responses SHOULD NOT include\n        # Content-Length or other entity headers, but some servers do it\n        # anyway.\n        # http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.5\n        response = self.fetch('/304_with_content_length')\n        self.assertEqual(response.code, 304)\n        self.assertEqual(response.headers['Content-Length'], '42')\n\n    def test_final_callback_stack_context(self):\n        # The final callback should be run outside of the httpclient's\n        # stack_context.  We want to ensure that there is not stack_context\n        # between the user's callback and the IOLoop, so monkey-patch\n        # IOLoop.handle_callback_exception and disable the test harness's\n        # context with a NullContext.\n        # Note that this does not apply to secondary callbacks (header\n        # and streaming_callback), as errors there must be seen as errors\n        # by the http client so it can clean up the connection.\n        exc_info = []\n\n        def handle_callback_exception(callback):\n            exc_info.append(sys.exc_info())\n            self.stop()\n        self.io_loop.handle_callback_exception = handle_callback_exception\n        with NullContext():\n            self.http_client.fetch(self.get_url('/hello'),\n                                   lambda response: 1 / 0)\n        self.wait()\n        self.assertEqual(exc_info[0][0], ZeroDivisionError)\n\n    @gen_test\n    def test_future_interface(self):\n        response = yield self.http_client.fetch(self.get_url('/hello'))\n        self.assertEqual(response.body, b'Hello world!')\n\n    @gen_test\n    def test_future_http_error(self):\n        try:\n            yield self.http_client.fetch(self.get_url('/notfound'))\n        except HTTPError as e:\n            self.assertEqual(e.code, 404)\n            self.assertEqual(e.response.code, 404)\n\n    @gen_test\n    def test_reuse_request_from_response(self):\n        # The response.request attribute should be an HTTPRequest, not\n        # a _RequestProxy.\n        # This test uses self.http_client.fetch because self.fetch calls\n        # self.get_url on the input unconditionally.\n        url = self.get_url('/hello')\n        response = yield self.http_client.fetch(url)\n        self.assertEqual(response.request.url, url)\n        self.assertTrue(isinstance(response.request, HTTPRequest))\n        response2 = yield self.http_client.fetch(response.request)\n        self.assertEqual(response2.body, b'Hello world!')\n\n\nclass RequestProxyTest(unittest.TestCase):\n    def test_request_set(self):\n        proxy = _RequestProxy(HTTPRequest('http://example.com/',\n                                          user_agent='foo'),\n                              dict())\n        self.assertEqual(proxy.user_agent, 'foo')\n\n    def test_default_set(self):\n        proxy = _RequestProxy(HTTPRequest('http://example.com/'),\n                              dict(network_interface='foo'))\n        self.assertEqual(proxy.network_interface, 'foo')\n\n    def test_both_set(self):\n        proxy = _RequestProxy(HTTPRequest('http://example.com/',\n                                          proxy_host='foo'),\n                              dict(proxy_host='bar'))\n        self.assertEqual(proxy.proxy_host, 'foo')\n\n    def test_neither_set(self):\n        proxy = _RequestProxy(HTTPRequest('http://example.com/'),\n                              dict())\n        self.assertIs(proxy.auth_username, None)\n\n    def test_bad_attribute(self):\n        proxy = _RequestProxy(HTTPRequest('http://example.com/'),\n                              dict())\n        with self.assertRaises(AttributeError):\n            proxy.foo\n\n    def test_defaults_none(self):\n        proxy = _RequestProxy(HTTPRequest('http://example.com/'), None)\n        self.assertIs(proxy.auth_username, None)\n\n\nclass HTTPResponseTestCase(unittest.TestCase):\n    def test_str(self):\n        response = HTTPResponse(HTTPRequest('http://example.com'),\n                                200, headers={}, buffer=BytesIO())\n        s = str(response)\n        self.assertTrue(s.startswith('HTTPResponse('))\n        self.assertIn('code=200', s)\n\n\nclass SyncHTTPClientTest(unittest.TestCase):\n    def setUp(self):\n        if IOLoop.configured_class().__name__ == 'TwistedIOLoop':\n            # TwistedIOLoop only supports the global reactor, so we can't have\n            # separate IOLoops for client and server threads.\n            raise unittest.SkipTest(\n                'Sync HTTPClient not compatible with TwistedIOLoop')\n        self.server_ioloop = IOLoop()\n\n        sock, self.port = bind_unused_port()\n        app = Application([('/', HelloWorldHandler)])\n        server = HTTPServer(app, io_loop=self.server_ioloop)\n        server.add_socket(sock)\n\n        self.server_thread = threading.Thread(target=self.server_ioloop.start)\n        self.server_thread.start()\n\n        self.http_client = HTTPClient()\n\n    def tearDown(self):\n        self.server_ioloop.add_callback(self.server_ioloop.stop)\n        self.server_thread.join()\n        self.server_ioloop.close(all_fds=True)\n\n    def get_url(self, path):\n        return 'http://localhost:%d%s' % (self.port, path)\n\n    def test_sync_client(self):\n        response = self.http_client.fetch(self.get_url('/'))\n        self.assertEqual(b'Hello world!', response.body)\n\n    def test_sync_client_error(self):\n        # Synchronous HTTPClient raises errors directly; no need for\n        # response.rethrow()\n        with self.assertRaises(HTTPError) as assertion:\n            self.http_client.fetch(self.get_url('/notfound'))\n        self.assertEqual(assertion.exception.code, 404)\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 782ms\nPrepared 1 package in 534ms\nInstalled 1 package in 0.91ms\n + tornado==3.1.dev1 (from file:///home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_6e63b238c7ed7c307f1e22232afa7df7fb5901a9)\nResolved 29 packages in 13ms\nInstalled 29 packages in 54ms\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": 0,
    "new_commit_res_stdout": "============================= test session starts =============================\nplatform linux -- Python 3.9.21\nrootdir: /home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_6e63b238c7ed7c307f1e22232afa7df7fb5901a9\n............................\ncollected 28 items\n\n==================== short test summary info ====================\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_304_with_content_length\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_basic_auth\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_body_encoding\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_chunked\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_chunked_close\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_configure_defaults\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_credentials_in_url\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_final_callback_stack_context\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_follow_redirect\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_future_http_error\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_future_interface\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_header_callback\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_header_callback_stack_context\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_hello_world\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_post\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_reuse_request_from_response\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_streaming_callback\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_streaming_stack_context\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_types\nPASSED r2e_tests.test_1::HTTPResponseTestCase::test_str\nPASSED r2e_tests.test_1::RequestProxyTest::test_bad_attribute\nPASSED r2e_tests.test_1::RequestProxyTest::test_both_set\nPASSED r2e_tests.test_1::RequestProxyTest::test_default_set\nPASSED r2e_tests.test_1::RequestProxyTest::test_defaults_none\nPASSED r2e_tests.test_1::RequestProxyTest::test_neither_set\nPASSED r2e_tests.test_1::RequestProxyTest::test_request_set\nPASSED r2e_tests.test_1::SyncHTTPClientTest::test_sync_client\nPASSED r2e_tests.test_1::SyncHTTPClientTest::test_sync_client_error\n=================== 28 passed in 0.04s ===================\n",
    "new_commit_res_stderr": "WARNING:tornado.access:404 GET /notfound (127.0.0.1) 0.29ms\nWARNING:tornado.access:404 GET /notfound (127.0.0.1) 0.18ms\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_6e63b238c7ed7c307f1e22232afa7df7fb5901a9\n............................\ncollected 28 items\n\n=================================== FAILURES ===================================\n__________ HTTPClientCommonTestCase.test_reuse_request_from_response __________\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_6e63b238c7ed7c307f1e22232afa7df7fb5901a9/tornado/testing.py\", line 376, in wrapper\n    return self.io_loop.run_sync(functools.partial(f, self), timeout=5)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_6e63b238c7ed7c307f1e22232afa7df7fb5901a9/tornado/ioloop.py\", line 365, in run_sync\n    return future_cell[0].result()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_6e63b238c7ed7c307f1e22232afa7df7fb5901a9/tornado/concurrent.py\", line 129, in result\n    raise_exc_info(self.__exc_info)\n  File \"<string>\", line 3, in raise_exc_info\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_6e63b238c7ed7c307f1e22232afa7df7fb5901a9/tornado/stack_context.py\", line 263, in _nested\n    yield vars\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_6e63b238c7ed7c307f1e22232afa7df7fb5901a9/tornado/stack_context.py\", line 236, in wrapped\n    callback(*args, **kwargs)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_6e63b238c7ed7c307f1e22232afa7df7fb5901a9/tornado/gen.py\", line 551, in inner\n    self.set_result(key, result)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_6e63b238c7ed7c307f1e22232afa7df7fb5901a9/tornado/gen.py\", line 480, in set_result\n    self.run()\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_6e63b238c7ed7c307f1e22232afa7df7fb5901a9/tornado/gen.py\", line 510, in run\n    yielded = self.gen.send(next)\n  File \"/home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_6e63b238c7ed7c307f1e22232afa7df7fb5901a9/r2e_tests/test_1.py\", line 346, in test_reuse_request_from_response\n    self.assertTrue(isinstance(response.request, HTTPRequest))\nAssertionError: False is not true\n\n==================== short test summary info ====================\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_304_with_content_length\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_basic_auth\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_body_encoding\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_chunked\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_chunked_close\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_configure_defaults\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_credentials_in_url\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_final_callback_stack_context\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_follow_redirect\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_future_http_error\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_future_interface\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_header_callback\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_header_callback_stack_context\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_hello_world\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_post\nFAILED r2e_tests.test_1::HTTPClientCommonTestCase::test_reuse_request_from_response\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_streaming_callback\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_streaming_stack_context\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_types\nPASSED r2e_tests.test_1::HTTPResponseTestCase::test_str\nPASSED r2e_tests.test_1::RequestProxyTest::test_bad_attribute\nPASSED r2e_tests.test_1::RequestProxyTest::test_both_set\nPASSED r2e_tests.test_1::RequestProxyTest::test_default_set\nPASSED r2e_tests.test_1::RequestProxyTest::test_defaults_none\nPASSED r2e_tests.test_1::RequestProxyTest::test_neither_set\nPASSED r2e_tests.test_1::RequestProxyTest::test_request_set\nPASSED r2e_tests.test_1::SyncHTTPClientTest::test_sync_client\nPASSED r2e_tests.test_1::SyncHTTPClientTest::test_sync_client_error\n=================== 1 failed, 27 passed in 0.05s ===================\n",
    "old_commit_res_stderr": "WARNING:tornado.access:404 GET /notfound (127.0.0.1) 0.25ms\nWARNING:tornado.access:404 GET /notfound (127.0.0.1) 0.31ms\n"
}
tornado/httpclient.py tornado/test/httpclient_test.py
We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
[ISSUE]
**Title:** `HTTPResponse.request` Sometimes Returns a `_RequestProxy` Instead of `HTTPRequest`

**Description:**
When making an HTTP request, the `response.request` attribute is expected to be an instance of `HTTPRequest`. However, in certain scenarios, it returns a `_RequestProxy` instead. This unexpected type causes failures when attempting to reuse the request object.

**Example:**
```python
def reuse_request_from_response():
    url = get_url('/hello')
    response = http_client.fetch(url)
    print(response.request.url)  # Expected: '/hello'
    print(isinstance(response.request, HTTPRequest))  # Expected: True
    response2 = http_client.fetch(response.request)
    print(response2.body)  # Expected: b'Hello world!'
```

**Expected Behavior:**
The `response.request` should always be an instance of `HTTPRequest`, allowing it to be reused seamlessly for subsequent requests.

**Actual Behavior:**
In some cases, `response.request` is a `_RequestProxy`, causing the `isinstance` check to fail and preventing the request from being reused as intended.

[/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/httpclient.py
"""Blocking and non-blocking HTTP client interfaces.

This module defines a common interface shared by two implementations,
``simple_httpclient`` and ``curl_httpclient``.  Applications may either
instantiate their chosen implementation class directly or use the
`AsyncHTTPClient` class from this module, which selects an implementation
that can be overridden with the `AsyncHTTPClient.configure` method.

The default implementation is ``simple_httpclient``, and this is expected
to be suitable for most users' needs.  However, some applications may wish
to switch to ``curl_httpclient`` for reasons such as the following:

* ``curl_httpclient`` has some features not found in ``simple_httpclient``,
  including support for HTTP proxies and the ability to use a specified
  network interface.

* ``curl_httpclient`` is more likely to be compatible with sites that are
  not-quite-compliant with the HTTP spec, or sites that use little-exercised
  features of HTTP.

* ``curl_httpclient`` is faster.

* ``curl_httpclient`` was the default prior to Tornado 2.0.

Note that if you are using ``curl_httpclient``, it is highly recommended that
you use a recent version of ``libcurl`` and ``pycurl``.  Currently the minimum
supported version is 7.18.2, and the recommended version is 7.21.1 or newer.
"""

from __future__ import absolute_import, division, print_function, with_statement

import functools
import time
import weakref

from tornado.concurrent import Future
from tornado.escape import utf8
from tornado import httputil, stack_context
from tornado.ioloop import IOLoop
from tornado.util import Configurable


class HTTPClient(object):
    """A blocking HTTP client.

    This interface is provided for convenience and testing; most applications
    that are running an IOLoop will want to use `AsyncHTTPClient` instead.
    Typical usage looks like this::

        http_client = httpclient.HTTPClient()
        try:
            response = http_client.fetch("http://www.google.com/")
            print response.body
        except httpclient.HTTPError as e:
            print "Error:", e
        httpclient.close()
    """
    def __init__(self, async_client_class=None, **kwargs):
        self._io_loop = IOLoop()
        if async_client_class is None:
            async_client_class = AsyncHTTPClient
        self._async_client = async_client_class(self._io_loop, **kwargs)
        self._closed = False

    def __del__(self):
        self.close()

    def close(self):
        """Closes the HTTPClient, freeing any resources used."""
        if not self._closed:
            self._async_client.close()
            self._io_loop.close()
            self._closed = True

    def fetch(self, request, **kwargs):
        """Executes a request, returning an `HTTPResponse`.

        The request may be either a string URL or an `HTTPRequest` object.
        If it is a string, we construct an `HTTPRequest` using any additional
        kwargs: ``HTTPRequest(request, **kwargs)``

        If an error occurs during the fetch, we raise an `HTTPError`.
        """
        response = self._io_loop.run_sync(functools.partial(
            self._async_client.fetch, request, **kwargs))
        response.rethrow()
        return response


class AsyncHTTPClient(Configurable):
    """An non-blocking HTTP client.

    Example usage::

        def handle_request(response):
            if response.error:
                print "Error:", response.error
            else:
                print response.body

        http_client = AsyncHTTPClient()
        http_client.fetch("http://www.google.com/", handle_request)

    The constructor for this class is magic in several respects: It
    actually creates an instance of an implementation-specific
    subclass, and instances are reused as a kind of pseudo-singleton
    (one per `.IOLoop`).  The keyword argument ``force_instance=True``
    can be used to suppress this singleton behavior.  Constructor
    arguments other than ``io_loop`` and ``force_instance`` are
    deprecated.  The implementation subclass as well as arguments to
    its constructor can be set with the static method `configure()`
    """
    @classmethod
    def configurable_base(cls):
        return AsyncHTTPClient

    @classmethod
    def configurable_default(cls):
        from tornado.simple_httpclient import SimpleAsyncHTTPClient
        return SimpleAsyncHTTPClient

    @classmethod
    def _async_clients(cls):
        attr_name = '_async_client_dict_' + cls.__name__
        if not hasattr(cls, attr_name):
            setattr(cls, attr_name, weakref.WeakKeyDictionary())
        return getattr(cls, attr_name)

    def __new__(cls, io_loop=None, force_instance=False, **kwargs):
        io_loop = io_loop or IOLoop.current()
        if io_loop in cls._async_clients() and not force_instance:
            return cls._async_clients()[io_loop]
        instance = super(AsyncHTTPClient, cls).__new__(cls, io_loop=io_loop,
                                                       **kwargs)
        if not force_instance:
            cls._async_clients()[io_loop] = instance
        return instance

    def initialize(self, io_loop, defaults=None):
        self.io_loop = io_loop
        self.defaults = dict(HTTPRequest._DEFAULTS)
        if defaults is not None:
            self.defaults.update(defaults)

    def close(self):
        """Destroys this HTTP client, freeing any file descriptors used.
        Not needed in normal use, but may be helpful in unittests that
        create and destroy http clients.  No other methods may be called
        on the `AsyncHTTPClient` after ``close()``.
        """
        if self._async_clients().get(self.io_loop) is self:
            del self._async_clients()[self.io_loop]

    def fetch(self, request, callback=None, **kwargs):
        """Executes a request, asynchronously returning an `HTTPResponse`.

        The request may be either a string URL or an `HTTPRequest` object.
        If it is a string, we construct an `HTTPRequest` using any additional
        kwargs: ``HTTPRequest(request, **kwargs)``

        This method returns a `.Future` whose result is an
        `HTTPResponse`.  The ``Future`` wil raise an `HTTPError` if
        the request returned a non-200 response code.

        If a ``callback`` is given, it will be invoked with the `HTTPResponse`.
        In the callback interface, `HTTPError` is not automatically raised.
        Instead, you must check the response's ``error`` attribute or
        call its `~HTTPResponse.rethrow` method.
        """
        if not isinstance(request, HTTPRequest):
            request = HTTPRequest(url=request, **kwargs)
        # We may modify this (to add Host, Accept-Encoding, etc),
        # so make sure we don't modify the caller's object.  This is also
        # where normal dicts get converted to HTTPHeaders objects.
        request.headers = httputil.HTTPHeaders(request.headers)
        request = _RequestProxy(request, self.defaults)
        future = Future()
        if callback is not None:
            callback = stack_context.wrap(callback)

            def handle_future(future):
                exc = future.exception()
                if isinstance(exc, HTTPError) and exc.response is not None:
                    response = exc.response
                elif exc is not None:
                    response = HTTPResponse(
                        request, 599, error=exc,
                        request_time=time.time() - request.start_time)
                else:
                    response = future.result()
                self.io_loop.add_callback(callback, response)
            future.add_done_callback(handle_future)

        def handle_response(response):
            if response.error:
                future.set_exception(response.error)
            else:
                future.set_result(response)
        self.fetch_impl(request, handle_response)
        return future

    def fetch_impl(self, request, callback):
        raise NotImplementedError()

    @classmethod
    def configure(cls, impl, **kwargs):
        """Configures the `AsyncHTTPClient` subclass to use.

        ``AsyncHTTPClient()`` actually creates an instance of a subclass.
        This method may be called with either a class object or the
        fully-qualified name of such a class (or ``None`` to use the default,
        ``SimpleAsyncHTTPClient``)

        If additional keyword arguments are given, they will be passed
        to the constructor of each subclass instance created.  The
        keyword argument ``max_clients`` determines the maximum number
        of simultaneous `~AsyncHTTPClient.fetch()` operations that can
        execute in parallel on each `.IOLoop`.  Additional arguments
        may be supported depending on the implementation class in use.

        Example::

           AsyncHTTPClient.configure("tornado.curl_httpclient.CurlAsyncHTTPClient")
        """
        super(AsyncHTTPClient, cls).configure(impl, **kwargs)


class HTTPRequest(object):
    """HTTP client request object."""

    # Default values for HTTPRequest parameters.
    # Merged with the values on the request object by AsyncHTTPClient
    # implementations.
    _DEFAULTS = dict(
        connect_timeout=20.0,
        request_timeout=20.0,
        follow_redirects=True,
        max_redirects=5,
        use_gzip=True,
        proxy_password='',
        allow_nonstandard_methods=False,
        validate_cert=True)

    def __init__(self, url, method="GET", headers=None, body=None,
                 auth_username=None, auth_password=None,
                 connect_timeout=None, request_timeout=None,
                 if_modified_since=None, follow_redirects=None,
                 max_redirects=None, user_agent=None, use_gzip=None,
                 network_interface=None, streaming_callback=None,
                 header_callback=None, prepare_curl_callback=None,
                 proxy_host=None, proxy_port=None, proxy_username=None,
                 proxy_password=None, allow_nonstandard_methods=None,
                 validate_cert=None, ca_certs=None,
                 allow_ipv6=None,
                 client_key=None, client_cert=None):
        r"""All parameters except ``url`` are optional.

        :arg string url: URL to fetch
        :arg string method: HTTP method, e.g. "GET" or "POST"
        :arg headers: Additional HTTP headers to pass on the request
        :type headers: `~tornado.httputil.HTTPHeaders` or `dict`
        :arg string auth_username: Username for HTTP "Basic" authentication
        :arg string auth_password: Password for HTTP "Basic" authentication
        :arg float connect_timeout: Timeout for initial connection in seconds
        :arg float request_timeout: Timeout for entire request in seconds
        :arg if_modified_since: Timestamp for ``If-Modified-Since`` header
        :type if_modified_since: `datetime` or `float`
        :arg bool follow_redirects: Should redirects be followed automatically
           or return the 3xx response?
        :arg int max_redirects: Limit for ``follow_redirects``
        :arg string user_agent: String to send as ``User-Agent`` header
        :arg bool use_gzip: Request gzip encoding from the server
        :arg string network_interface: Network interface to use for request
        :arg callable streaming_callback: If set, ``streaming_callback`` will
           be run with each chunk of data as it is received, and
           ``HTTPResponse.body`` and ``HTTPResponse.buffer`` will be empty in
           the final response.
        :arg callable header_callback: If set, ``header_callback`` will
           be run with each header line as it is received (including the
           first line, e.g. ``HTTP/1.0 200 OK\r\n``, and a final line
           containing only ``\r\n``.  All lines include the trailing newline
           characters).  ``HTTPResponse.headers`` will be empty in the final
           response.  This is most useful in conjunction with
           ``streaming_callback``, because it's the only way to get access to
           header data while the request is in progress.
        :arg callable prepare_curl_callback: If set, will be called with
           a ``pycurl.Curl`` object to allow the application to make additional
           ``setopt`` calls.
        :arg string proxy_host: HTTP proxy hostname.  To use proxies,
           ``proxy_host`` and ``proxy_port`` must be set; ``proxy_username`` and
           ``proxy_pass`` are optional.  Proxies are currently only supported
           with ``curl_httpclient``.
        :arg int proxy_port: HTTP proxy port
        :arg string proxy_username: HTTP proxy username
        :arg string proxy_password: HTTP proxy password
        :arg bool allow_nonstandard_methods: Allow unknown values for ``method``
           argument?
        :arg bool validate_cert: For HTTPS requests, validate the server's
           certificate?
        :arg string ca_certs: filename of CA certificates in PEM format,
           or None to use defaults.  Note that in ``curl_httpclient``, if
           any request uses a custom ``ca_certs`` file, they all must (they
           don't have to all use the same ``ca_certs``, but it's not possible
           to mix requests with ``ca_certs`` and requests that use the defaults.
        :arg bool allow_ipv6: Use IPv6 when available?  Default is false in
           ``simple_httpclient`` and true in ``curl_httpclient``
        :arg string client_key: Filename for client SSL key, if any
        :arg string client_cert: Filename for client SSL certificate, if any
        """
        if headers is None:
            headers = httputil.HTTPHeaders()
        if if_modified_since:
            headers["If-Modified-Since"] = httputil.format_timestamp(
                if_modified_since)
        self.proxy_host = proxy_host
        self.proxy_port = proxy_port
        self.proxy_username = proxy_username
        self.proxy_password = proxy_password
        self.url = url
        self.method = method
        self.headers = headers
        self.body = utf8(body)
        self.auth_username = auth_username
        self.auth_password = auth_password
        self.connect_timeout = connect_timeout
        self.request_timeout = request_timeout
        self.follow_redirects = follow_redirects
        self.max_redirects = max_redirects
        self.user_agent = user_agent
        self.use_gzip = use_gzip
        self.network_interface = network_interface
        self.streaming_callback = stack_context.wrap(streaming_callback)
        self.header_callback = stack_context.wrap(header_callback)
        self.prepare_curl_callback = stack_context.wrap(prepare_curl_callback)
        self.allow_nonstandard_methods = allow_nonstandard_methods
        self.validate_cert = validate_cert
        self.ca_certs = ca_certs
        self.allow_ipv6 = allow_ipv6
        self.client_key = client_key
        self.client_cert = client_cert
        self.start_time = time.time()


class HTTPResponse(object):
    """HTTP Response object.

    Attributes:

    * request: HTTPRequest object

    * code: numeric HTTP status code, e.g. 200 or 404

    * reason: human-readable reason phrase describing the status code
      (with curl_httpclient, this is a default value rather than the
      server's actual response)

    * headers: `tornado.httputil.HTTPHeaders` object

    * buffer: ``cStringIO`` object for response body

    * body: response body as string (created on demand from ``self.buffer``)

    * error: Exception object, if any

    * request_time: seconds from request start to finish

    * time_info: dictionary of diagnostic timing information from the request.
      Available data are subject to change, but currently uses timings
      available from http://curl.haxx.se/libcurl/c/curl_easy_getinfo.html,
      plus ``queue``, which is the delay (if any) introduced by waiting for
      a slot under `AsyncHTTPClient`'s ``max_clients`` setting.
    """
    def __init__(self, request, code, headers=None, buffer=None,
                 effective_url=None, error=None, request_time=None,
                 time_info=None, reason=None):
        self.request = request
        self.code = code
        self.reason = reason or httputil.responses.get(code, "Unknown")
        if headers is not None:
            self.headers = headers
        else:
            self.headers = httputil.HTTPHeaders()
        self.buffer = buffer
        self._body = None
        if effective_url is None:
            self.effective_url = request.url
        else:
            self.effective_url = effective_url
        if error is None:
            if self.code < 200 or self.code >= 300:
                self.error = HTTPError(self.code, response=self)
            else:
                self.error = None
        else:
            self.error = error
        self.request_time = request_time
        self.time_info = time_info or {}

    def _get_body(self):
        if self.buffer is None:
            return None
        elif self._body is None:
            self._body = self.buffer.getvalue()

        return self._body

    body = property(_get_body)

    def rethrow(self):
        """If there was an error on the request, raise an `HTTPError`."""
        if self.error:
            raise self.error

    def __repr__(self):
        args = ",".join("%s=%r" % i for i in sorted(self.__dict__.items()))
        return "%s(%s)" % (self.__class__.__name__, args)


class HTTPError(Exception):
    """Exception thrown for an unsuccessful HTTP request.

    Attributes:

    * ``code`` - HTTP error integer error code, e.g. 404.  Error code 599 is
      used when no HTTP response was received, e.g. for a timeout.

    * ``response`` - `HTTPResponse` object, if any.

    Note that if ``follow_redirects`` is False, redirects become HTTPErrors,
    and you can look at ``error.response.headers['Location']`` to see the
    destination of the redirect.
    """
    def __init__(self, code, message=None, response=None):
        self.code = code
        message = message or httputil.responses.get(code, "Unknown")
        self.response = response
        Exception.__init__(self, "HTTP %d: %s" % (self.code, message))


class _RequestProxy(object):
    """Combines an object with a dictionary of defaults.

    Used internally by AsyncHTTPClient implementations.
    """
    def __init__(self, request, defaults):
        self.request = request
        self.defaults = defaults

    def __getattr__(self, name):
        request_attr = getattr(self.request, name)
        if request_attr is not None:
            return request_attr
        elif self.defaults is not None:
            return self.defaults.get(name, None)
        else:
            return None


def main():
    from tornado.options import define, options, parse_command_line
    define("print_headers", type=bool, default=False)
    define("print_body", type=bool, default=True)
    define("follow_redirects", type=bool, default=True)
    define("validate_cert", type=bool, default=True)
    args = parse_command_line()
    client = HTTPClient()
    for arg in args:
        try:
            response = client.fetch(arg,
                                    follow_redirects=options.follow_redirects,
                                    validate_cert=options.validate_cert,
                                    )
        except HTTPError as e:
            if e.response is not None:
                response = e.response
            else:
                raise
        if options.print_headers:
            print(response.headers)
        if options.print_body:
            print(response.body)
    client.close()

if __name__ == "__main__":
    main()

```
--- 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:** `HTTPResponse.request` Sometimes Returns a `_RequestProxy` Instead of `HTTPRequest`

**Description:**
When making an HTTP request, the `response.request` attribute is expected to be an instance of `HTTPRequest`. However, in certain scenarios, it returns a `_RequestProxy` instead. This unexpected type causes failures when attempting to reuse the request object.

**Example:**
```python
def reuse_request_from_response():
    url = get_url('/hello')
    response = http_client.fetch(url)
    print(response.request.url)  # Expected: '/hello'
    print(isinstance(response.request, HTTPRequest))  # Expected: True
    response2 = http_client.fetch(response.request)
    print(response2.body)  # Expected: b'Hello world!'
```

**Expected Behavior:**
The `response.request` should always be an instance of `HTTPRequest`, allowing it to be reused seamlessly for subsequent requests.

**Actual Behavior:**
In some cases, `response.request` is a `_RequestProxy`, causing the `isinstance` check to fail and preventing the request from being reused as intended.

[/ISSUE]
{
    "HTTPClientCommonTestCase.test_304_with_content_length": "PASSED",
    "HTTPClientCommonTestCase.test_basic_auth": "PASSED",
    "HTTPClientCommonTestCase.test_body_encoding": "PASSED",
    "HTTPClientCommonTestCase.test_chunked": "PASSED",
    "HTTPClientCommonTestCase.test_chunked_close": "PASSED",
    "HTTPClientCommonTestCase.test_configure_defaults": "PASSED",
    "HTTPClientCommonTestCase.test_credentials_in_url": "PASSED",
    "HTTPClientCommonTestCase.test_final_callback_stack_context": "PASSED",
    "HTTPClientCommonTestCase.test_follow_redirect": "PASSED",
    "HTTPClientCommonTestCase.test_future_http_error": "PASSED",
    "HTTPClientCommonTestCase.test_future_interface": "PASSED",
    "HTTPClientCommonTestCase.test_header_callback": "PASSED",
    "HTTPClientCommonTestCase.test_header_callback_stack_context": "PASSED",
    "HTTPClientCommonTestCase.test_hello_world": "PASSED",
    "HTTPClientCommonTestCase.test_post": "PASSED",
    "HTTPClientCommonTestCase.test_reuse_request_from_response": "PASSED",
    "HTTPClientCommonTestCase.test_streaming_callback": "PASSED",
    "HTTPClientCommonTestCase.test_streaming_stack_context": "PASSED",
    "HTTPClientCommonTestCase.test_types": "PASSED",
    "HTTPResponseTestCase.test_str": "PASSED",
    "RequestProxyTest.test_bad_attribute": "PASSED",
    "RequestProxyTest.test_both_set": "PASSED",
    "RequestProxyTest.test_default_set": "PASSED",
    "RequestProxyTest.test_defaults_none": "PASSED",
    "RequestProxyTest.test_neither_set": "PASSED",
    "RequestProxyTest.test_request_set": "PASSED",
    "SyncHTTPClientTest.test_sync_client": "PASSED",
    "SyncHTTPClientTest.test_sync_client_error": "PASSED"
}
diff --git a/tornado/httpclient.py b/tornado/httpclient.py
index 2a6f4467..bfdc7136 100644
--- a/tornado/httpclient.py
+++ b/tornado/httpclient.py
@@ -373,7 +373,10 @@ class HTTPResponse(object):
     def __init__(self, request, code, headers=None, buffer=None,
                  effective_url=None, error=None, request_time=None,
                  time_info=None, reason=None):
-        self.request = request
+        if isinstance(request, _RequestProxy):
+            self.request = request.request
+        else:
+            self.request = request
         self.code = code
         self.reason = reason or httputil.responses.get(code, "Unknown")
         if headers is not None:

{"tornado/httpclient.py": "\"\"\"Blocking and non-blocking HTTP client interfaces.\n\nThis module defines a common interface shared by two implementations,\n``simple_httpclient`` and ``curl_httpclient``.  Applications may either\ninstantiate their chosen implementation class directly or use the\n`AsyncHTTPClient` class from this module, which selects an implementation\nthat can be overridden with the `AsyncHTTPClient.configure` method.\n\nThe default implementation is ``simple_httpclient``, and this is expected\nto be suitable for most users' needs.  However, some applications may wish\nto switch to ``curl_httpclient`` for reasons such as the following:\n\n* ``curl_httpclient`` has some features not found in ``simple_httpclient``,\n  including support for HTTP proxies and the ability to use a specified\n  network interface.\n\n* ``curl_httpclient`` is more likely to be compatible with sites that are\n  not-quite-compliant with the HTTP spec, or sites that use little-exercised\n  features of HTTP.\n\n* ``curl_httpclient`` is faster.\n\n* ``curl_httpclient`` was the default prior to Tornado 2.0.\n\nNote that if you are using ``curl_httpclient``, it is highly recommended that\nyou use a recent version of ``libcurl`` and ``pycurl``.  Currently the minimum\nsupported version is 7.18.2, and the recommended version is 7.21.1 or newer.\n\"\"\"\n\nfrom __future__ import absolute_import, division, print_function, with_statement\n\nimport functools\nimport time\nimport weakref\n\nfrom tornado.concurrent import Future\nfrom tornado.escape import utf8\nfrom tornado import httputil, stack_context\nfrom tornado.ioloop import IOLoop\nfrom tornado.util import Configurable\n\n\nclass HTTPClient(object):\n    \"\"\"A blocking HTTP client.\n\n    This interface is provided for convenience and testing; most applications\n    that are running an IOLoop will want to use `AsyncHTTPClient` instead.\n    Typical usage looks like this::\n\n        http_client = httpclient.HTTPClient()\n        try:\n            response = http_client.fetch(\"http://www.google.com/\")\n            print response.body\n        except httpclient.HTTPError as e:\n            print \"Error:\", e\n        httpclient.close()\n    \"\"\"\n    def __init__(self, async_client_class=None, **kwargs):\n        self._io_loop = IOLoop()\n        if async_client_class is None:\n            async_client_class = AsyncHTTPClient\n        self._async_client = async_client_class(self._io_loop, **kwargs)\n        self._closed = False\n\n    def __del__(self):\n        self.close()\n\n    def close(self):\n        \"\"\"Closes the HTTPClient, freeing any resources used.\"\"\"\n        if not self._closed:\n            self._async_client.close()\n            self._io_loop.close()\n            self._closed = True\n\n    def fetch(self, request, **kwargs):\n        \"\"\"Executes a request, returning an `HTTPResponse`.\n\n        The request may be either a string URL or an `HTTPRequest` object.\n        If it is a string, we construct an `HTTPRequest` using any additional\n        kwargs: ``HTTPRequest(request, **kwargs)``\n\n        If an error occurs during the fetch, we raise an `HTTPError`.\n        \"\"\"\n        response = self._io_loop.run_sync(functools.partial(\n            self._async_client.fetch, request, **kwargs))\n        response.rethrow()\n        return response\n\n\nclass AsyncHTTPClient(Configurable):\n    \"\"\"An non-blocking HTTP client.\n\n    Example usage::\n\n        def handle_request(response):\n            if response.error:\n                print \"Error:\", response.error\n            else:\n                print response.body\n\n        http_client = AsyncHTTPClient()\n        http_client.fetch(\"http://www.google.com/\", handle_request)\n\n    The constructor for this class is magic in several respects: It\n    actually creates an instance of an implementation-specific\n    subclass, and instances are reused as a kind of pseudo-singleton\n    (one per `.IOLoop`).  The keyword argument ``force_instance=True``\n    can be used to suppress this singleton behavior.  Constructor\n    arguments other than ``io_loop`` and ``force_instance`` are\n    deprecated.  The implementation subclass as well as arguments to\n    its constructor can be set with the static method `configure()`\n    \"\"\"\n    @classmethod\n    def configurable_base(cls):\n        return AsyncHTTPClient\n\n    @classmethod\n    def configurable_default(cls):\n        from tornado.simple_httpclient import SimpleAsyncHTTPClient\n        return SimpleAsyncHTTPClient\n\n    @classmethod\n    def _async_clients(cls):\n        attr_name = '_async_client_dict_' + cls.__name__\n        if not hasattr(cls, attr_name):\n            setattr(cls, attr_name, weakref.WeakKeyDictionary())\n        return getattr(cls, attr_name)\n\n    def __new__(cls, io_loop=None, force_instance=False, **kwargs):\n        io_loop = io_loop or IOLoop.current()\n        if io_loop in cls._async_clients() and not force_instance:\n            return cls._async_clients()[io_loop]\n        instance = super(AsyncHTTPClient, cls).__new__(cls, io_loop=io_loop,\n                                                       **kwargs)\n        if not force_instance:\n            cls._async_clients()[io_loop] = instance\n        return instance\n\n    def initialize(self, io_loop, defaults=None):\n        self.io_loop = io_loop\n        self.defaults = dict(HTTPRequest._DEFAULTS)\n        if defaults is not None:\n            self.defaults.update(defaults)\n\n    def close(self):\n        \"\"\"Destroys this HTTP client, freeing any file descriptors used.\n        Not needed in normal use, but may be helpful in unittests that\n        create and destroy http clients.  No other methods may be called\n        on the `AsyncHTTPClient` after ``close()``.\n        \"\"\"\n        if self._async_clients().get(self.io_loop) is self:\n            del self._async_clients()[self.io_loop]\n\n    def fetch(self, request, callback=None, **kwargs):\n        \"\"\"Executes a request, asynchronously returning an `HTTPResponse`.\n\n        The request may be either a string URL or an `HTTPRequest` object.\n        If it is a string, we construct an `HTTPRequest` using any additional\n        kwargs: ``HTTPRequest(request, **kwargs)``\n\n        This method returns a `.Future` whose result is an\n        `HTTPResponse`.  The ``Future`` wil raise an `HTTPError` if\n        the request returned a non-200 response code.\n\n        If a ``callback`` is given, it will be invoked with the `HTTPResponse`.\n        In the callback interface, `HTTPError` is not automatically raised.\n        Instead, you must check the response's ``error`` attribute or\n        call its `~HTTPResponse.rethrow` method.\n        \"\"\"\n        if not isinstance(request, HTTPRequest):\n            request = HTTPRequest(url=request, **kwargs)\n        # We may modify this (to add Host, Accept-Encoding, etc),\n        # so make sure we don't modify the caller's object.  This is also\n        # where normal dicts get converted to HTTPHeaders objects.\n        request.headers = httputil.HTTPHeaders(request.headers)\n        request = _RequestProxy(request, self.defaults)\n        future = Future()\n        if callback is not None:\n            callback = stack_context.wrap(callback)\n\n            def handle_future(future):\n                exc = future.exception()\n                if isinstance(exc, HTTPError) and exc.response is not None:\n                    response = exc.response\n                elif exc is not None:\n                    response = HTTPResponse(\n                        request, 599, error=exc,\n                        request_time=time.time() - request.start_time)\n                else:\n                    response = future.result()\n                self.io_loop.add_callback(callback, response)\n            future.add_done_callback(handle_future)\n\n        def handle_response(response):\n            if response.error:\n                future.set_exception(response.error)\n            else:\n                future.set_result(response)\n        self.fetch_impl(request, handle_response)\n        return future\n\n    def fetch_impl(self, request, callback):\n        raise NotImplementedError()\n\n    @classmethod\n    def configure(cls, impl, **kwargs):\n        \"\"\"Configures the `AsyncHTTPClient` subclass to use.\n\n        ``AsyncHTTPClient()`` actually creates an instance of a subclass.\n        This method may be called with either a class object or the\n        fully-qualified name of such a class (or ``None`` to use the default,\n        ``SimpleAsyncHTTPClient``)\n\n        If additional keyword arguments are given, they will be passed\n        to the constructor of each subclass instance created.  The\n        keyword argument ``max_clients`` determines the maximum number\n        of simultaneous `~AsyncHTTPClient.fetch()` operations that can\n        execute in parallel on each `.IOLoop`.  Additional arguments\n        may be supported depending on the implementation class in use.\n\n        Example::\n\n           AsyncHTTPClient.configure(\"tornado.curl_httpclient.CurlAsyncHTTPClient\")\n        \"\"\"\n        super(AsyncHTTPClient, cls).configure(impl, **kwargs)\n\n\nclass HTTPRequest(object):\n    \"\"\"HTTP client request object.\"\"\"\n\n    # Default values for HTTPRequest parameters.\n    # Merged with the values on the request object by AsyncHTTPClient\n    # implementations.\n    _DEFAULTS = dict(\n        connect_timeout=20.0,\n        request_timeout=20.0,\n        follow_redirects=True,\n        max_redirects=5,\n        use_gzip=True,\n        proxy_password='',\n        allow_nonstandard_methods=False,\n        validate_cert=True)\n\n    def __init__(self, url, method=\"GET\", headers=None, body=None,\n                 auth_username=None, auth_password=None,\n                 connect_timeout=None, request_timeout=None,\n                 if_modified_since=None, follow_redirects=None,\n                 max_redirects=None, user_agent=None, use_gzip=None,\n                 network_interface=None, streaming_callback=None,\n                 header_callback=None, prepare_curl_callback=None,\n                 proxy_host=None, proxy_port=None, proxy_username=None,\n                 proxy_password=None, allow_nonstandard_methods=None,\n                 validate_cert=None, ca_certs=None,\n                 allow_ipv6=None,\n                 client_key=None, client_cert=None):\n        r\"\"\"All parameters except ``url`` are optional.\n\n        :arg string url: URL to fetch\n        :arg string method: HTTP method, e.g. \"GET\" or \"POST\"\n        :arg headers: Additional HTTP headers to pass on the request\n        :type headers: `~tornado.httputil.HTTPHeaders` or `dict`\n        :arg string auth_username: Username for HTTP \"Basic\" authentication\n        :arg string auth_password: Password for HTTP \"Basic\" authentication\n        :arg float connect_timeout: Timeout for initial connection in seconds\n        :arg float request_timeout: Timeout for entire request in seconds\n        :arg if_modified_since: Timestamp for ``If-Modified-Since`` header\n        :type if_modified_since: `datetime` or `float`\n        :arg bool follow_redirects: Should redirects be followed automatically\n           or return the 3xx response?\n        :arg int max_redirects: Limit for ``follow_redirects``\n        :arg string user_agent: String to send as ``User-Agent`` header\n        :arg bool use_gzip: Request gzip encoding from the server\n        :arg string network_interface: Network interface to use for request\n        :arg callable streaming_callback: If set, ``streaming_callback`` will\n           be run with each chunk of data as it is received, and\n           ``HTTPResponse.body`` and ``HTTPResponse.buffer`` will be empty in\n           the final response.\n        :arg callable header_callback: If set, ``header_callback`` will\n           be run with each header line as it is received (including the\n           first line, e.g. ``HTTP/1.0 200 OK\\r\\n``, and a final line\n           containing only ``\\r\\n``.  All lines include the trailing newline\n           characters).  ``HTTPResponse.headers`` will be empty in the final\n           response.  This is most useful in conjunction with\n           ``streaming_callback``, because it's the only way to get access to\n           header data while the request is in progress.\n        :arg callable prepare_curl_callback: If set, will be called with\n           a ``pycurl.Curl`` object to allow the application to make additional\n           ``setopt`` calls.\n        :arg string proxy_host: HTTP proxy hostname.  To use proxies,\n           ``proxy_host`` and ``proxy_port`` must be set; ``proxy_username`` and\n           ``proxy_pass`` are optional.  Proxies are currently only supported\n           with ``curl_httpclient``.\n        :arg int proxy_port: HTTP proxy port\n        :arg string proxy_username: HTTP proxy username\n        :arg string proxy_password: HTTP proxy password\n        :arg bool allow_nonstandard_methods: Allow unknown values for ``method``\n           argument?\n        :arg bool validate_cert: For HTTPS requests, validate the server's\n           certificate?\n        :arg string ca_certs: filename of CA certificates in PEM format,\n           or None to use defaults.  Note that in ``curl_httpclient``, if\n           any request uses a custom ``ca_certs`` file, they all must (they\n           don't have to all use the same ``ca_certs``, but it's not possible\n           to mix requests with ``ca_certs`` and requests that use the defaults.\n        :arg bool allow_ipv6: Use IPv6 when available?  Default is false in\n           ``simple_httpclient`` and true in ``curl_httpclient``\n        :arg string client_key: Filename for client SSL key, if any\n        :arg string client_cert: Filename for client SSL certificate, if any\n        \"\"\"\n        if headers is None:\n            headers = httputil.HTTPHeaders()\n        if if_modified_since:\n            headers[\"If-Modified-Since\"] = httputil.format_timestamp(\n                if_modified_since)\n        self.proxy_host = proxy_host\n        self.proxy_port = proxy_port\n        self.proxy_username = proxy_username\n        self.proxy_password = proxy_password\n        self.url = url\n        self.method = method\n        self.headers = headers\n        self.body = utf8(body)\n        self.auth_username = auth_username\n        self.auth_password = auth_password\n        self.connect_timeout = connect_timeout\n        self.request_timeout = request_timeout\n        self.follow_redirects = follow_redirects\n        self.max_redirects = max_redirects\n        self.user_agent = user_agent\n        self.use_gzip = use_gzip\n        self.network_interface = network_interface\n        self.streaming_callback = stack_context.wrap(streaming_callback)\n        self.header_callback = stack_context.wrap(header_callback)\n        self.prepare_curl_callback = stack_context.wrap(prepare_curl_callback)\n        self.allow_nonstandard_methods = allow_nonstandard_methods\n        self.validate_cert = validate_cert\n        self.ca_certs = ca_certs\n        self.allow_ipv6 = allow_ipv6\n        self.client_key = client_key\n        self.client_cert = client_cert\n        self.start_time = time.time()\n\n\nclass HTTPResponse(object):\n    \"\"\"HTTP Response object.\n\n    Attributes:\n\n    * request: HTTPRequest object\n\n    * code: numeric HTTP status code, e.g. 200 or 404\n\n    * reason: human-readable reason phrase describing the status code\n      (with curl_httpclient, this is a default value rather than the\n      server's actual response)\n\n    * headers: `tornado.httputil.HTTPHeaders` object\n\n    * buffer: ``cStringIO`` object for response body\n\n    * body: response body as string (created on demand from ``self.buffer``)\n\n    * error: Exception object, if any\n\n    * request_time: seconds from request start to finish\n\n    * time_info: dictionary of diagnostic timing information from the request.\n      Available data are subject to change, but currently uses timings\n      available from http://curl.haxx.se/libcurl/c/curl_easy_getinfo.html,\n      plus ``queue``, which is the delay (if any) introduced by waiting for\n      a slot under `AsyncHTTPClient`'s ``max_clients`` setting.\n    \"\"\"\n    def __init__(self, request, code, headers=None, buffer=None,\n                 effective_url=None, error=None, request_time=None,\n                 time_info=None, reason=None):\n        self.request = request\n        self.code = code\n        self.reason = reason or httputil.responses.get(code, \"Unknown\")\n        if headers is not None:\n            self.headers = headers\n        else:\n            self.headers = httputil.HTTPHeaders()\n        self.buffer = buffer\n        self._body = None\n        if effective_url is None:\n            self.effective_url = request.url\n        else:\n            self.effective_url = effective_url\n        if error is None:\n            if self.code < 200 or self.code >= 300:\n                self.error = HTTPError(self.code, response=self)\n            else:\n                self.error = None\n        else:\n            self.error = error\n        self.request_time = request_time\n        self.time_info = time_info or {}\n\n    def _get_body(self):\n        if self.buffer is None:\n            return None\n        elif self._body is None:\n            self._body = self.buffer.getvalue()\n\n        return self._body\n\n    body = property(_get_body)\n\n    def rethrow(self):\n        \"\"\"If there was an error on the request, raise an `HTTPError`.\"\"\"\n        if self.error:\n            raise self.error\n\n    def __repr__(self):\n        args = \",\".join(\"%s=%r\" % i for i in sorted(self.__dict__.items()))\n        return \"%s(%s)\" % (self.__class__.__name__, args)\n\n\nclass HTTPError(Exception):\n    \"\"\"Exception thrown for an unsuccessful HTTP request.\n\n    Attributes:\n\n    * ``code`` - HTTP error integer error code, e.g. 404.  Error code 599 is\n      used when no HTTP response was received, e.g. for a timeout.\n\n    * ``response`` - `HTTPResponse` object, if any.\n\n    Note that if ``follow_redirects`` is False, redirects become HTTPErrors,\n    and you can look at ``error.response.headers['Location']`` to see the\n    destination of the redirect.\n    \"\"\"\n    def __init__(self, code, message=None, response=None):\n        self.code = code\n        message = message or httputil.responses.get(code, \"Unknown\")\n        self.response = response\n        Exception.__init__(self, \"HTTP %d: %s\" % (self.code, message))\n\n\nclass _RequestProxy(object):\n    \"\"\"Combines an object with a dictionary of defaults.\n\n    Used internally by AsyncHTTPClient implementations.\n    \"\"\"\n    def __init__(self, request, defaults):\n        self.request = request\n        self.defaults = defaults\n\n    def __getattr__(self, name):\n        request_attr = getattr(self.request, name)\n        if request_attr is not None:\n            return request_attr\n        elif self.defaults is not None:\n            return self.defaults.get(name, None)\n        else:\n            return None\n\n\ndef main():\n    from tornado.options import define, options, parse_command_line\n    define(\"print_headers\", type=bool, default=False)\n    define(\"print_body\", type=bool, default=True)\n    define(\"follow_redirects\", type=bool, default=True)\n    define(\"validate_cert\", type=bool, default=True)\n    args = parse_command_line()\n    client = HTTPClient()\n    for arg in args:\n        try:\n            response = client.fetch(arg,\n                                    follow_redirects=options.follow_redirects,\n                                    validate_cert=options.validate_cert,\n                                    )\n        except HTTPError as e:\n            if e.response is not None:\n                response = e.response\n            else:\n                raise\n        if options.print_headers:\n            print(response.headers)\n        if options.print_body:\n            print(response.body)\n    client.close()\n\nif __name__ == \"__main__\":\n    main()\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:** `HTTPResponse.request` Sometimes Returns a `_RequestProxy` Instead of `HTTPRequest`

**Description:**
When making an HTTP request, the `response.request` attribute is expected to be an instance of `HTTPRequest`. However, in certain scenarios, it returns a `_RequestProxy` instead. This unexpected type causes failures when attempting to reuse the request object.

**Example:**
```python
def reuse_request_from_response():
    url = get_url('/hello')
    response = http_client.fetch(url)
    print(response.request.url)  # Expected: '/hello'
    print(isinstance(response.request, HTTPRequest))  # Expected: True
    response2 = http_client.fetch(response.request)
    print(response2.body)  # Expected: b'Hello world!'
```

**Expected Behavior:**
The `response.request` should always be an instance of `HTTPRequest`, allowing it to be reused seamlessly for subsequent requests.

**Actual Behavior:**
In some cases, `response.request` is a `_RequestProxy`, causing the `isinstance` check to fail and preventing the request from being reused as intended.

[/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/httpclient.py
"""Blocking and non-blocking HTTP client interfaces.

This module defines a common interface shared by two implementations,
``simple_httpclient`` and ``curl_httpclient``.  Applications may either
instantiate their chosen implementation class directly or use the
`AsyncHTTPClient` class from this module, which selects an implementation
that can be overridden with the `AsyncHTTPClient.configure` method.

The default implementation is ``simple_httpclient``, and this is expected
to be suitable for most users' needs.  However, some applications may wish
to switch to ``curl_httpclient`` for reasons such as the following:

* ``curl_httpclient`` has some features not found in ``simple_httpclient``,
  including support for HTTP proxies and the ability to use a specified
  network interface.

* ``curl_httpclient`` is more likely to be compatible with sites that are
  not-quite-compliant with the HTTP spec, or sites that use little-exercised
  features of HTTP.

* ``curl_httpclient`` is faster.

* ``curl_httpclient`` was the default prior to Tornado 2.0.

Note that if you are using ``curl_httpclient``, it is highly recommended that
you use a recent version of ``libcurl`` and ``pycurl``.  Currently the minimum
supported version is 7.18.2, and the recommended version is 7.21.1 or newer.
"""

from __future__ import absolute_import, division, print_function, with_statement

import functools
import time
import weakref

from tornado.concurrent import Future
from tornado.escape import utf8
from tornado import httputil, stack_context
from tornado.ioloop import IOLoop
from tornado.util import Configurable


class HTTPClient(object):
    """A blocking HTTP client.

    This interface is provided for convenience and testing; most applications
    that are running an IOLoop will want to use `AsyncHTTPClient` instead.
    Typical usage looks like this::

        http_client = httpclient.HTTPClient()
        try:
            response = http_client.fetch("http://www.google.com/")
            print response.body
        except httpclient.HTTPError as e:
            print "Error:", e
        httpclient.close()
    """
    def __init__(self, async_client_class=None, **kwargs):
        self._io_loop = IOLoop()
        if async_client_class is None:
            async_client_class = AsyncHTTPClient
        self._async_client = async_client_class(self._io_loop, **kwargs)
        self._closed = False

    def __del__(self):
        self.close()

    def close(self):
        """Closes the HTTPClient, freeing any resources used."""
        if not self._closed:
            self._async_client.close()
            self._io_loop.close()
            self._closed = True

    def fetch(self, request, **kwargs):
        """Executes a request, returning an `HTTPResponse`.

        The request may be either a string URL or an `HTTPRequest` object.
        If it is a string, we construct an `HTTPRequest` using any additional
        kwargs: ``HTTPRequest(request, **kwargs)``

        If an error occurs during the fetch, we raise an `HTTPError`.
        """
        response = self._io_loop.run_sync(functools.partial(
            self._async_client.fetch, request, **kwargs))
        response.rethrow()
        return response


class AsyncHTTPClient(Configurable):
    """An non-blocking HTTP client.

    Example usage::

        def handle_request(response):
            if response.error:
                print "Error:", response.error
            else:
                print response.body

        http_client = AsyncHTTPClient()
        http_client.fetch("http://www.google.com/", handle_request)

    The constructor for this class is magic in several respects: It
    actually creates an instance of an implementation-specific
    subclass, and instances are reused as a kind of pseudo-singleton
    (one per `.IOLoop`).  The keyword argument ``force_instance=True``
    can be used to suppress this singleton behavior.  Constructor
    arguments other than ``io_loop`` and ``force_instance`` are
    deprecated.  The implementation subclass as well as arguments to
    its constructor can be set with the static method `configure()`
    """
    @classmethod
    def configurable_base(cls):
        return AsyncHTTPClient

    @classmethod
    def configurable_default(cls):
        from tornado.simple_httpclient import SimpleAsyncHTTPClient
        return SimpleAsyncHTTPClient

    @classmethod
    def _async_clients(cls):
        attr_name = '_async_client_dict_' + cls.__name__
        if not hasattr(cls, attr_name):
            setattr(cls, attr_name, weakref.WeakKeyDictionary())
        return getattr(cls, attr_name)

    def __new__(cls, io_loop=None, force_instance=False, **kwargs):
        io_loop = io_loop or IOLoop.current()
        if io_loop in cls._async_clients() and not force_instance:
            return cls._async_clients()[io_loop]
        instance = super(AsyncHTTPClient, cls).__new__(cls, io_loop=io_loop,
                                                       **kwargs)
        if not force_instance:
            cls._async_clients()[io_loop] = instance
        return instance

    def initialize(self, io_loop, defaults=None):
        self.io_loop = io_loop
        self.defaults = dict(HTTPRequest._DEFAULTS)
        if defaults is not None:
            self.defaults.update(defaults)

    def close(self):
        """Destroys this HTTP client, freeing any file descriptors used.
        Not needed in normal use, but may be helpful in unittests that
        create and destroy http clients.  No other methods may be called
        on the `AsyncHTTPClient` after ``close()``.
        """
        if self._async_clients().get(self.io_loop) is self:
            del self._async_clients()[self.io_loop]

    def fetch(self, request, callback=None, **kwargs):
        """Executes a request, asynchronously returning an `HTTPResponse`.

        The request may be either a string URL or an `HTTPRequest` object.
        If it is a string, we construct an `HTTPRequest` using any additional
        kwargs: ``HTTPRequest(request, **kwargs)``

        This method returns a `.Future` whose result is an
        `HTTPResponse`.  The ``Future`` wil raise an `HTTPError` if
        the request returned a non-200 response code.

        If a ``callback`` is given, it will be invoked with the `HTTPResponse`.
        In the callback interface, `HTTPError` is not automatically raised.
        Instead, you must check the response's ``error`` attribute or
        call its `~HTTPResponse.rethrow` method.
        """
        if not isinstance(request, HTTPRequest):
            request = HTTPRequest(url=request, **kwargs)
        # We may modify this (to add Host, Accept-Encoding, etc),
        # so make sure we don't modify the caller's object.  This is also
        # where normal dicts get converted to HTTPHeaders objects.
        request.headers = httputil.HTTPHeaders(request.headers)
        request = _RequestProxy(request, self.defaults)
        future = Future()
        if callback is not None:
            callback = stack_context.wrap(callback)

            def handle_future(future):
                exc = future.exception()
                if isinstance(exc, HTTPError) and exc.response is not None:
                    response = exc.response
                elif exc is not None:
                    response = HTTPResponse(
                        request, 599, error=exc,
                        request_time=time.time() - request.start_time)
                else:
                    response = future.result()
                self.io_loop.add_callback(callback, response)
            future.add_done_callback(handle_future)

        def handle_response(response):
            if response.error:
                future.set_exception(response.error)
            else:
                future.set_result(response)
        self.fetch_impl(request, handle_response)
        return future

    def fetch_impl(self, request, callback):
        raise NotImplementedError()

    @classmethod
    def configure(cls, impl, **kwargs):
        """Configures the `AsyncHTTPClient` subclass to use.

        ``AsyncHTTPClient()`` actually creates an instance of a subclass.
        This method may be called with either a class object or the
        fully-qualified name of such a class (or ``None`` to use the default,
        ``SimpleAsyncHTTPClient``)

        If additional keyword arguments are given, they will be passed
        to the constructor of each subclass instance created.  The
        keyword argument ``max_clients`` determines the maximum number
        of simultaneous `~AsyncHTTPClient.fetch()` operations that can
        execute in parallel on each `.IOLoop`.  Additional arguments
        may be supported depending on the implementation class in use.

        Example::

           AsyncHTTPClient.configure("tornado.curl_httpclient.CurlAsyncHTTPClient")
        """
        super(AsyncHTTPClient, cls).configure(impl, **kwargs)


class HTTPRequest(object):
    """HTTP client request object."""

    # Default values for HTTPRequest parameters.
    # Merged with the values on the request object by AsyncHTTPClient
    # implementations.
    _DEFAULTS = dict(
        connect_timeout=20.0,
        request_timeout=20.0,
        follow_redirects=True,
        max_redirects=5,
        use_gzip=True,
        proxy_password='',
        allow_nonstandard_methods=False,
        validate_cert=True)

    def __init__(self, url, method="GET", headers=None, body=None,
                 auth_username=None, auth_password=None,
                 connect_timeout=None, request_timeout=None,
                 if_modified_since=None, follow_redirects=None,
                 max_redirects=None, user_agent=None, use_gzip=None,
                 network_interface=None, streaming_callback=None,
                 header_callback=None, prepare_curl_callback=None,
                 proxy_host=None, proxy_port=None, proxy_username=None,
                 proxy_password=None, allow_nonstandard_methods=None,
                 validate_cert=None, ca_certs=None,
                 allow_ipv6=None,
                 client_key=None, client_cert=None):
        r"""All parameters except ``url`` are optional.

        :arg string url: URL to fetch
        :arg string method: HTTP method, e.g. "GET" or "POST"
        :arg headers: Additional HTTP headers to pass on the request
        :type headers: `~tornado.httputil.HTTPHeaders` or `dict`
        :arg string auth_username: Username for HTTP "Basic" authentication
        :arg string auth_password: Password for HTTP "Basic" authentication
        :arg float connect_timeout: Timeout for initial connection in seconds
        :arg float request_timeout: Timeout for entire request in seconds
        :arg if_modified_since: Timestamp for ``If-Modified-Since`` header
        :type if_modified_since: `datetime` or `float`
        :arg bool follow_redirects: Should redirects be followed automatically
           or return the 3xx response?
        :arg int max_redirects: Limit for ``follow_redirects``
        :arg string user_agent: String to send as ``User-Agent`` header
        :arg bool use_gzip: Request gzip encoding from the server
        :arg string network_interface: Network interface to use for request
        :arg callable streaming_callback: If set, ``streaming_callback`` will
           be run with each chunk of data as it is received, and
           ``HTTPResponse.body`` and ``HTTPResponse.buffer`` will be empty in
           the final response.
        :arg callable header_callback: If set, ``header_callback`` will
           be run with each header line as it is received (including the
           first line, e.g. ``HTTP/1.0 200 OK\r\n``, and a final line
           containing only ``\r\n``.  All lines include the trailing newline
           characters).  ``HTTPResponse.headers`` will be empty in the final
           response.  This is most useful in conjunction with
           ``streaming_callback``, because it's the only way to get access to
           header data while the request is in progress.
        :arg callable prepare_curl_callback: If set, will be called with
           a ``pycurl.Curl`` object to allow the application to make additional
           ``setopt`` calls.
        :arg string proxy_host: HTTP proxy hostname.  To use proxies,
           ``proxy_host`` and ``proxy_port`` must be set; ``proxy_username`` and
           ``proxy_pass`` are optional.  Proxies are currently only supported
           with ``curl_httpclient``.
        :arg int proxy_port: HTTP proxy port
        :arg string proxy_username: HTTP proxy username
        :arg string proxy_password: HTTP proxy password
        :arg bool allow_nonstandard_methods: Allow unknown values for ``method``
           argument?
        :arg bool validate_cert: For HTTPS requests, validate the server's
           certificate?
        :arg string ca_certs: filename of CA certificates in PEM format,
           or None to use defaults.  Note that in ``curl_httpclient``, if
           any request uses a custom ``ca_certs`` file, they all must (they
           don't have to all use the same ``ca_certs``, but it's not possible
           to mix requests with ``ca_certs`` and requests that use the defaults.
        :arg bool allow_ipv6: Use IPv6 when available?  Default is false in
           ``simple_httpclient`` and true in ``curl_httpclient``
        :arg string client_key: Filename for client SSL key, if any
        :arg string client_cert: Filename for client SSL certificate, if any
        """
        if headers is None:
            headers = httputil.HTTPHeaders()
        if if_modified_since:
            headers["If-Modified-Since"] = httputil.format_timestamp(
                if_modified_since)
        self.proxy_host = proxy_host
        self.proxy_port = proxy_port
        self.proxy_username = proxy_username
        self.proxy_password = proxy_password
        self.url = url
        self.method = method
        self.headers = headers
        self.body = utf8(body)
        self.auth_username = auth_username
        self.auth_password = auth_password
        self.connect_timeout = connect_timeout
        self.request_timeout = request_timeout
        self.follow_redirects = follow_redirects
        self.max_redirects = max_redirects
        self.user_agent = user_agent
        self.use_gzip = use_gzip
        self.network_interface = network_interface
        self.streaming_callback = stack_context.wrap(streaming_callback)
        self.header_callback = stack_context.wrap(header_callback)
        self.prepare_curl_callback = stack_context.wrap(prepare_curl_callback)
        self.allow_nonstandard_methods = allow_nonstandard_methods
        self.validate_cert = validate_cert
        self.ca_certs = ca_certs
        self.allow_ipv6 = allow_ipv6
        self.client_key = client_key
        self.client_cert = client_cert
        self.start_time = time.time()


class HTTPResponse(object):
    """HTTP Response object.

    Attributes:

    * request: HTTPRequest object

    * code: numeric HTTP status code, e.g. 200 or 404

    * reason: human-readable reason phrase describing the status code
      (with curl_httpclient, this is a default value rather than the
      server's actual response)

    * headers: `tornado.httputil.HTTPHeaders` object

    * buffer: ``cStringIO`` object for response body

    * body: response body as string (created on demand from ``self.buffer``)

    * error: Exception object, if any

    * request_time: seconds from request start to finish

    * time_info: dictionary of diagnostic timing information from the request.
      Available data are subject to change, but currently uses timings
      available from http://curl.haxx.se/libcurl/c/curl_easy_getinfo.html,
      plus ``queue``, which is the delay (if any) introduced by waiting for
      a slot under `AsyncHTTPClient`'s ``max_clients`` setting.
    """
    def __init__(self, request, code, headers=None, buffer=None,
                 effective_url=None, error=None, request_time=None,
                 time_info=None, reason=None):
        self.request = request
        self.code = code
        self.reason = reason or httputil.responses.get(code, "Unknown")
        if headers is not None:
            self.headers = headers
        else:
            self.headers = httputil.HTTPHeaders()
        self.buffer = buffer
        self._body = None
        if effective_url is None:
            self.effective_url = request.url
        else:
            self.effective_url = effective_url
        if error is None:
            if self.code < 200 or self.code >= 300:
                self.error = HTTPError(self.code, response=self)
            else:
                self.error = None
        else:
            self.error = error
        self.request_time = request_time
        self.time_info = time_info or {}

    def _get_body(self):
        if self.buffer is None:
            return None
        elif self._body is None:
            self._body = self.buffer.getvalue()

        return self._body

    body = property(_get_body)

    def rethrow(self):
        """If there was an error on the request, raise an `HTTPError`."""
        if self.error:
            raise self.error

    def __repr__(self):
        args = ",".join("%s=%r" % i for i in sorted(self.__dict__.items()))
        return "%s(%s)" % (self.__class__.__name__, args)


class HTTPError(Exception):
    """Exception thrown for an unsuccessful HTTP request.

    Attributes:

    * ``code`` - HTTP error integer error code, e.g. 404.  Error code 599 is
      used when no HTTP response was received, e.g. for a timeout.

    * ``response`` - `HTTPResponse` object, if any.

    Note that if ``follow_redirects`` is False, redirects become HTTPErrors,
    and you can look at ``error.response.headers['Location']`` to see the
    destination of the redirect.
    """
    def __init__(self, code, message=None, response=None):
        self.code = code
        message = message or httputil.responses.get(code, "Unknown")
        self.response = response
        Exception.__init__(self, "HTTP %d: %s" % (self.code, message))


class _RequestProxy(object):
    """Combines an object with a dictionary of defaults.

    Used internally by AsyncHTTPClient implementations.
    """
    def __init__(self, request, defaults):
        self.request = request
        self.defaults = defaults

    def __getattr__(self, name):
        request_attr = getattr(self.request, name)
        if request_attr is not None:
            return request_attr
        elif self.defaults is not None:
            return self.defaults.get(name, None)
        else:
            return None


def main():
    from tornado.options import define, options, parse_command_line
    define("print_headers", type=bool, default=False)
    define("print_body", type=bool, default=True)
    define("follow_redirects", type=bool, default=True)
    define("validate_cert", type=bool, default=True)
    args = parse_command_line()
    client = HTTPClient()
    for arg in args:
        try:
            response = client.fetch(arg,
                                    follow_redirects=options.follow_redirects,
                                    validate_cert=options.validate_cert,
                                    )
        except HTTPError as e:
            if e.response is not None:
                response = e.response
            else:
                raise
        if options.print_headers:
            print(response.headers)
        if options.print_body:
            print(response.body)
    client.close()

if __name__ == "__main__":
    main()

```
--- 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