{
    "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\nrecommended that you use a recent version of ``libcurl`` and\n``pycurl``.  Currently the minimum supported version of libcurl is\n7.21.1, and the minimum version of pycurl is 7.18.2.  It is highly\nrecommended that your ``libcurl`` installation is built with\nasynchronous DNS resolver (threaded or c-ares), otherwise you may\nencounter various problems with request timeouts (for more\ninformation, see\nhttp://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTCONNECTTIMEOUTMS\nand comments in curl_httpclient.py).\n\nTo select ``curl_httpclient``, call `AsyncHTTPClient.configure` at startup::\n\n    AsyncHTTPClient.configure(\"tornado.curl_httpclient.CurlAsyncHTTPClient\")\n\"\"\"\n\nfrom __future__ import absolute_import, division, print_function, with_statement\n\nimport functools\nimport time\nimport weakref\n\nfrom tornado.concurrent import TracebackFuture\nfrom tornado.escape import utf8, native_str\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            # HTTPError is raised for non-200 responses; the response\n            # can be found in e.response.\n            print(\"Error: \" + str(e))\n        except Exception as e:\n            # Other errors are possible, such as IOError.\n            print(\"Error: \" + str(e))\n        http_client.close()\n    \"\"\"\n    def __init__(self, async_client_class=None, **kwargs):\n        self._io_loop = IOLoop(make_current=False)\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` unless\n        the ``raise_error`` keyword argument is set to False.\n        \"\"\"\n        response = self._io_loop.run_sync(functools.partial(\n            self._async_client.fetch, request, **kwargs))\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.  Unless\n    ``force_instance=True`` is used, no arguments other than\n    ``io_loop`` should be passed to the `AsyncHTTPClient` constructor.\n    The implementation subclass as well as arguments to its\n    constructor can be set with the static method `configure()`\n\n    All `AsyncHTTPClient` implementations support a ``defaults``\n    keyword argument, which can be used to set default values for\n    `HTTPRequest` attributes.  For example::\n\n        AsyncHTTPClient.configure(\n            None, defaults=dict(user_agent=\"MyUserAgent\"))\n        # or with force_instance:\n        client = AsyncHTTPClient(force_instance=True,\n            defaults=dict(user_agent=\"MyUserAgent\"))\n\n    .. versionchanged:: 4.1\n       The ``io_loop`` argument is deprecated.\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 force_instance:\n            instance_cache = None\n        else:\n            instance_cache = cls._async_clients()\n        if instance_cache is not None and io_loop in instance_cache:\n            return instance_cache[io_loop]\n        instance = super(AsyncHTTPClient, cls).__new__(cls, io_loop=io_loop,\n                                                       **kwargs)\n        # Make sure the instance knows which cache to remove itself from.\n        # It can't simply call _async_clients() because we may be in\n        # __new__(AsyncHTTPClient) but instance.__class__ may be\n        # SimpleAsyncHTTPClient.\n        instance._instance_cache = instance_cache\n        if instance_cache is not None:\n            instance_cache[instance.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        self._closed = False\n\n    def close(self):\n        \"\"\"Destroys this HTTP client, freeing any file descriptors used.\n\n        This method is **not needed in normal use** due to the way\n        that `AsyncHTTPClient` objects are transparently reused.\n        ``close()`` is generally only necessary when either the\n        `.IOLoop` is also being closed, or the ``force_instance=True``\n        argument was used when creating the `AsyncHTTPClient`.\n\n        No other methods may be called on the `AsyncHTTPClient` after\n        ``close()``.\n\n        \"\"\"\n        if self._closed:\n            return\n        self._closed = True\n        if self._instance_cache is not None:\n            if self._instance_cache.get(self.io_loop) is not self:\n                raise RuntimeError(\"inconsistent AsyncHTTPClient cache\")\n            del self._instance_cache[self.io_loop]\n\n    def fetch(self, request, callback=None, raise_error=True, **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`. By default, the ``Future`` will raise an\n        `HTTPError` if the request returned a non-200 response code\n        (other errors may also be raised if the server could not be\n        contacted). Instead, if ``raise_error`` is set to False, the\n        response will always be returned regardless of the response\n        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 self._closed:\n            raise RuntimeError(\"fetch() called on closed AsyncHTTPClient\")\n        if not isinstance(request, HTTPRequest):\n            request = HTTPRequest(url=request, **kwargs)\n        else:\n            if kwargs:\n                raise ValueError(\"kwargs can't be used if request is an HTTPRequest object\")\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 = TracebackFuture()\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 raise_error and 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        decompress_response=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, auth_mode=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, body_producer=None,\n                 expect_100_continue=False, decompress_response=None,\n                 ssl_options=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 body: HTTP request body as a string (byte or unicode; if unicode\n           the utf-8 encoding will be used)\n        :arg body_producer: Callable used for lazy/asynchronous request bodies.\n           It is called with one argument, a ``write`` function, and should\n           return a `.Future`.  It should call the write function with new\n           data as it becomes available.  The write function returns a\n           `.Future` which can be used for flow control.\n           Only one of ``body`` and ``body_producer`` may\n           be specified.  ``body_producer`` is not supported on\n           ``curl_httpclient``.  When using ``body_producer`` it is recommended\n           to pass a ``Content-Length`` in the headers as otherwise chunked\n           encoding will be used, and many servers do not support chunked\n           encoding on requests.  New in Tornado 4.0\n        :arg string auth_username: Username for HTTP authentication\n        :arg string auth_password: Password for HTTP authentication\n        :arg string auth_mode: Authentication mode; default is \"basic\".\n           Allowed values are implementation-defined; ``curl_httpclient``\n           supports \"basic\" and \"digest\"; ``simple_httpclient`` only supports\n           \"basic\"\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 decompress_response: Request a compressed response from\n           the server and decompress it after downloading.  Default is True.\n           New in Tornado 4.0.\n        :arg bool use_gzip: Deprecated alias for ``decompress_response``\n           since Tornado 4.0.\n        :arg string network_interface: Network interface to use for request.\n           ``curl_httpclient`` only; see note below.\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.  See note below when used with\n           ``curl_httpclient``.\n        :arg string client_key: Filename for client SSL key, if any.  See\n           note below when used with ``curl_httpclient``.\n        :arg string client_cert: Filename for client SSL certificate, if any.\n           See note below when used with ``curl_httpclient``.\n        :arg ssl.SSLContext ssl_options: `ssl.SSLContext` object for use in\n           ``simple_httpclient`` (unsupported by ``curl_httpclient``).\n           Overrides ``validate_cert``, ``ca_certs``, ``client_key``,\n           and ``client_cert``.\n        :arg bool allow_ipv6: Use IPv6 when available?  Default is true.\n        :arg bool expect_100_continue: If true, send the\n           ``Expect: 100-continue`` header and wait for a continue response\n           before sending the request body.  Only supported with\n           simple_httpclient.\n\n        .. note::\n\n            When using ``curl_httpclient`` certain options may be\n            inherited by subsequent fetches because ``pycurl`` does\n            not allow them to be cleanly reset.  This applies to the\n            ``ca_certs``, ``client_key``, ``client_cert``, and\n            ``network_interface`` arguments.  If you use these\n            options, you should pass them on every request (you don't\n            have to always use the same values, but it's not possible\n            to mix requests that specify these options with ones that\n            use the defaults).\n\n        .. versionadded:: 3.1\n           The ``auth_mode`` argument.\n\n        .. versionadded:: 4.0\n           The ``body_producer`` and ``expect_100_continue`` arguments.\n\n        .. versionadded:: 4.2\n           The ``ssl_options`` argument.\n        \"\"\"\n        # Note that some of these attributes go through property setters\n        # defined below.\n        self.headers = headers\n        if if_modified_since:\n            self.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.body = body\n        self.body_producer = body_producer\n        self.auth_username = auth_username\n        self.auth_password = auth_password\n        self.auth_mode = auth_mode\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        if decompress_response is not None:\n            self.decompress_response = decompress_response\n        else:\n            self.decompress_response = use_gzip\n        self.network_interface = network_interface\n        self.streaming_callback = streaming_callback\n        self.header_callback = header_callback\n        self.prepare_curl_callback = 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.ssl_options = ssl_options\n        self.expect_100_continue = expect_100_continue\n        self.start_time = time.time()\n\n    @property\n    def headers(self):\n        return self._headers\n\n    @headers.setter\n    def headers(self, value):\n        if value is None:\n            self._headers = httputil.HTTPHeaders()\n        else:\n            self._headers = value\n\n    @property\n    def body(self):\n        return self._body\n\n    @body.setter\n    def body(self, value):\n        self._body = utf8(value)\n\n    @property\n    def body_producer(self):\n        return self._body_producer\n\n    @body_producer.setter\n    def body_producer(self, value):\n        self._body_producer = stack_context.wrap(value)\n\n    @property\n    def streaming_callback(self):\n        return self._streaming_callback\n\n    @streaming_callback.setter\n    def streaming_callback(self, value):\n        self._streaming_callback = stack_context.wrap(value)\n\n    @property\n    def header_callback(self):\n        return self._header_callback\n\n    @header_callback.setter\n    def header_callback(self, value):\n        self._header_callback = stack_context.wrap(value)\n\n    @property\n    def prepare_curl_callback(self):\n        return self._prepare_curl_callback\n\n    @prepare_curl_callback.setter\n    def prepare_curl_callback(self, value):\n        self._prepare_curl_callback = stack_context.wrap(value)\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\n    * headers: `tornado.httputil.HTTPHeaders` object\n\n    * effective_url: final location of the resource after following any\n      redirects\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, message=self.reason,\n                                       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        self.message = message or httputil.responses.get(code, \"Unknown\")\n        self.response = response\n        super(HTTPError, self).__init__(code, message, response)\n\n    def __str__(self):\n        return \"HTTP %d: %s\" % (self.code, self.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(native_str(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\nrecommended that you use a recent version of ``libcurl`` and\n``pycurl``.  Currently the minimum supported version of libcurl is\n7.21.1, and the minimum version of pycurl is 7.18.2.  It is highly\nrecommended that your ``libcurl`` installation is built with\nasynchronous DNS resolver (threaded or c-ares), otherwise you may\nencounter various problems with request timeouts (for more\ninformation, see\nhttp://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTCONNECTTIMEOUTMS\nand comments in curl_httpclient.py).\n\nTo select ``curl_httpclient``, call `AsyncHTTPClient.configure` at startup::\n\n    AsyncHTTPClient.configure(\"tornado.curl_httpclient.CurlAsyncHTTPClient\")\n\"\"\"\n\nfrom __future__ import absolute_import, division, print_function, with_statement\n\nimport functools\nimport time\nimport weakref\n\nfrom tornado.concurrent import TracebackFuture\nfrom tornado.escape import utf8, native_str\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            # HTTPError is raised for non-200 responses; the response\n            # can be found in e.response.\n            print(\"Error: \" + str(e))\n        except Exception as e:\n            # Other errors are possible, such as IOError.\n            print(\"Error: \" + str(e))\n        http_client.close()\n    \"\"\"\n    def __init__(self, async_client_class=None, **kwargs):\n        self._io_loop = IOLoop(make_current=False)\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` unless\n        the ``raise_error`` keyword argument is set to False.\n        \"\"\"\n        response = self._io_loop.run_sync(functools.partial(\n            self._async_client.fetch, request, **kwargs))\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.  Unless\n    ``force_instance=True`` is used, no arguments other than\n    ``io_loop`` should be passed to the `AsyncHTTPClient` constructor.\n    The implementation subclass as well as arguments to its\n    constructor can be set with the static method `configure()`\n\n    All `AsyncHTTPClient` implementations support a ``defaults``\n    keyword argument, which can be used to set default values for\n    `HTTPRequest` attributes.  For example::\n\n        AsyncHTTPClient.configure(\n            None, defaults=dict(user_agent=\"MyUserAgent\"))\n        # or with force_instance:\n        client = AsyncHTTPClient(force_instance=True,\n            defaults=dict(user_agent=\"MyUserAgent\"))\n\n    .. versionchanged:: 4.1\n       The ``io_loop`` argument is deprecated.\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 force_instance:\n            instance_cache = None\n        else:\n            instance_cache = cls._async_clients()\n        if instance_cache is not None and io_loop in instance_cache:\n            return instance_cache[io_loop]\n        instance = super(AsyncHTTPClient, cls).__new__(cls, io_loop=io_loop,\n                                                       **kwargs)\n        # Make sure the instance knows which cache to remove itself from.\n        # It can't simply call _async_clients() because we may be in\n        # __new__(AsyncHTTPClient) but instance.__class__ may be\n        # SimpleAsyncHTTPClient.\n        instance._instance_cache = instance_cache\n        if instance_cache is not None:\n            instance_cache[instance.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        self._closed = False\n\n    def close(self):\n        \"\"\"Destroys this HTTP client, freeing any file descriptors used.\n\n        This method is **not needed in normal use** due to the way\n        that `AsyncHTTPClient` objects are transparently reused.\n        ``close()`` is generally only necessary when either the\n        `.IOLoop` is also being closed, or the ``force_instance=True``\n        argument was used when creating the `AsyncHTTPClient`.\n\n        No other methods may be called on the `AsyncHTTPClient` after\n        ``close()``.\n\n        \"\"\"\n        if self._closed:\n            return\n        self._closed = True\n        if self._instance_cache is not None:\n            if self._instance_cache.get(self.io_loop) is not self:\n                raise RuntimeError(\"inconsistent AsyncHTTPClient cache\")\n            del self._instance_cache[self.io_loop]\n\n    def fetch(self, request, callback=None, raise_error=True, **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`. By default, the ``Future`` will raise an\n        `HTTPError` if the request returned a non-200 response code\n        (other errors may also be raised if the server could not be\n        contacted). Instead, if ``raise_error`` is set to False, the\n        response will always be returned regardless of the response\n        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 self._closed:\n            raise RuntimeError(\"fetch() called on closed AsyncHTTPClient\")\n        if not isinstance(request, HTTPRequest):\n            request = HTTPRequest(url=request, **kwargs)\n        else:\n            if kwargs:\n                raise ValueError(\"kwargs can't be used if request is an HTTPRequest object\")\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 = TracebackFuture()\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 raise_error and 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        decompress_response=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, auth_mode=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, body_producer=None,\n                 expect_100_continue=False, decompress_response=None,\n                 ssl_options=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 body: HTTP request body as a string (byte or unicode; if unicode\n           the utf-8 encoding will be used)\n        :arg body_producer: Callable used for lazy/asynchronous request bodies.\n           It is called with one argument, a ``write`` function, and should\n           return a `.Future`.  It should call the write function with new\n           data as it becomes available.  The write function returns a\n           `.Future` which can be used for flow control.\n           Only one of ``body`` and ``body_producer`` may\n           be specified.  ``body_producer`` is not supported on\n           ``curl_httpclient``.  When using ``body_producer`` it is recommended\n           to pass a ``Content-Length`` in the headers as otherwise chunked\n           encoding will be used, and many servers do not support chunked\n           encoding on requests.  New in Tornado 4.0\n        :arg string auth_username: Username for HTTP authentication\n        :arg string auth_password: Password for HTTP authentication\n        :arg string auth_mode: Authentication mode; default is \"basic\".\n           Allowed values are implementation-defined; ``curl_httpclient``\n           supports \"basic\" and \"digest\"; ``simple_httpclient`` only supports\n           \"basic\"\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 decompress_response: Request a compressed response from\n           the server and decompress it after downloading.  Default is True.\n           New in Tornado 4.0.\n        :arg bool use_gzip: Deprecated alias for ``decompress_response``\n           since Tornado 4.0.\n        :arg string network_interface: Network interface to use for request.\n           ``curl_httpclient`` only; see note below.\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.  See note below when used with\n           ``curl_httpclient``.\n        :arg string client_key: Filename for client SSL key, if any.  See\n           note below when used with ``curl_httpclient``.\n        :arg string client_cert: Filename for client SSL certificate, if any.\n           See note below when used with ``curl_httpclient``.\n        :arg ssl.SSLContext ssl_options: `ssl.SSLContext` object for use in\n           ``simple_httpclient`` (unsupported by ``curl_httpclient``).\n           Overrides ``validate_cert``, ``ca_certs``, ``client_key``,\n           and ``client_cert``.\n        :arg bool allow_ipv6: Use IPv6 when available?  Default is true.\n        :arg bool expect_100_continue: If true, send the\n           ``Expect: 100-continue`` header and wait for a continue response\n           before sending the request body.  Only supported with\n           simple_httpclient.\n\n        .. note::\n\n            When using ``curl_httpclient`` certain options may be\n            inherited by subsequent fetches because ``pycurl`` does\n            not allow them to be cleanly reset.  This applies to the\n            ``ca_certs``, ``client_key``, ``client_cert``, and\n            ``network_interface`` arguments.  If you use these\n            options, you should pass them on every request (you don't\n            have to always use the same values, but it's not possible\n            to mix requests that specify these options with ones that\n            use the defaults).\n\n        .. versionadded:: 3.1\n           The ``auth_mode`` argument.\n\n        .. versionadded:: 4.0\n           The ``body_producer`` and ``expect_100_continue`` arguments.\n\n        .. versionadded:: 4.2\n           The ``ssl_options`` argument.\n        \"\"\"\n        # Note that some of these attributes go through property setters\n        # defined below.\n        self.headers = headers\n        if if_modified_since:\n            self.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.body = body\n        self.body_producer = body_producer\n        self.auth_username = auth_username\n        self.auth_password = auth_password\n        self.auth_mode = auth_mode\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        if decompress_response is not None:\n            self.decompress_response = decompress_response\n        else:\n            self.decompress_response = use_gzip\n        self.network_interface = network_interface\n        self.streaming_callback = streaming_callback\n        self.header_callback = header_callback\n        self.prepare_curl_callback = 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.ssl_options = ssl_options\n        self.expect_100_continue = expect_100_continue\n        self.start_time = time.time()\n\n    @property\n    def headers(self):\n        return self._headers\n\n    @headers.setter\n    def headers(self, value):\n        if value is None:\n            self._headers = httputil.HTTPHeaders()\n        else:\n            self._headers = value\n\n    @property\n    def body(self):\n        return self._body\n\n    @body.setter\n    def body(self, value):\n        self._body = utf8(value)\n\n    @property\n    def body_producer(self):\n        return self._body_producer\n\n    @body_producer.setter\n    def body_producer(self, value):\n        self._body_producer = stack_context.wrap(value)\n\n    @property\n    def streaming_callback(self):\n        return self._streaming_callback\n\n    @streaming_callback.setter\n    def streaming_callback(self, value):\n        self._streaming_callback = stack_context.wrap(value)\n\n    @property\n    def header_callback(self):\n        return self._header_callback\n\n    @header_callback.setter\n    def header_callback(self, value):\n        self._header_callback = stack_context.wrap(value)\n\n    @property\n    def prepare_curl_callback(self):\n        return self._prepare_curl_callback\n\n    @prepare_curl_callback.setter\n    def prepare_curl_callback(self, value):\n        self._prepare_curl_callback = stack_context.wrap(value)\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\n    * headers: `tornado.httputil.HTTPHeaders` object\n\n    * effective_url: final location of the resource after following any\n      redirects\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, message=self.reason,\n                                       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        self.message = message or httputil.responses.get(code, \"Unknown\")\n        self.response = response\n        super(HTTPError, self).__init__(code, message, response)\n\n    def __str__(self):\n        return \"HTTP %d: %s\" % (self.code, self.message)\n\n    # There is a cyclic reference between self and self.response,\n    # which breaks the default __repr__ implementation.\n    # (especially on pypy, which doesn't have the same recursion\n    # detection as cpython).\n    __repr__ = __str__\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(native_str(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": "90c78119",
                "new_commit_hash": "32ce1e0b",
                "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": 615,
                            "length": 6
                        },
                        "new_range": {
                            "start": 615,
                            "length": 12
                        },
                        "section": "class HTTPError(Exception):"
                    },
                    "line_group": {
                        "all_lines": [
                            {
                                "content": "    def __str__(self):",
                                "type": "context"
                            },
                            {
                                "content": "        return \"HTTP %d: %s\" % (self.code, self.message)",
                                "type": "context"
                            },
                            {
                                "content": "",
                                "type": "context"
                            },
                            {
                                "content": "    # There is a cyclic reference between self and self.response,",
                                "type": "added"
                            },
                            {
                                "content": "    # which breaks the default __repr__ implementation.",
                                "type": "added"
                            },
                            {
                                "content": "    # (especially on pypy, which doesn't have the same recursion",
                                "type": "added"
                            },
                            {
                                "content": "    # detection as cpython).",
                                "type": "added"
                            },
                            {
                                "content": "    __repr__ = __str__",
                                "type": "added"
                            },
                            {
                                "content": "",
                                "type": "added"
                            },
                            {
                                "content": "",
                                "type": "context"
                            },
                            {
                                "content": "class _RequestProxy(object):",
                                "type": "context"
                            },
                            {
                                "content": "    \"\"\"Combines an object with a dictionary of defaults.",
                                "type": "context"
                            }
                        ]
                    },
                    "modified_entities": [
                        {
                            "file_name": "tornado/httpclient.py",
                            "type": "class",
                            "name": "HTTPError",
                            "content": "class 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        self.message = message or httputil.responses.get(code, \"Unknown\")\n        self.response = response\n        super(HTTPError, self).__init__(code, message, response)\n\n    def __str__(self):\n        return \"HTTP %d: %s\" % (self.code, self.message)\n\n    # There is a cyclic reference between self and self.response,\n    # which breaks the default __repr__ implementation.\n    # (especially on pypy, which doesn't have the same recursion\n    # detection as cpython).\n    __repr__ = __str__",
                            "ast_type_str": "ClassDef",
                            "start_lineno": 595,
                            "end_lineno": 622,
                            "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 copy\nimport functools\nimport sys\nimport threading\nimport datetime\nfrom io import BytesIO\n\nfrom tornado.escape import utf8\nfrom tornado import gen\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.log import gen_log\nfrom tornado import netutil\nfrom tornado.stack_context import ExceptionStackContext, NullContext\nfrom tornado.testing import AsyncHTTPTestCase, bind_unused_port, gen_test, ExpectLog\nfrom tornado.test.util import unittest, skipOnTravis\nfrom tornado.web import Application, RequestHandler, url\nfrom tornado.httputil import format_timestamp, HTTPHeaders\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 PutHandler(RequestHandler):\n    def put(self):\n        self.write(\"Put body: \")\n        self.write(self.request.body)\n\n\nclass RedirectHandler(RequestHandler):\n    def prepare(self):\n        self.write('redirects can have bodies too')\n        self.redirect(self.get_argument(\"url\"),\n                      status=int(self.get_argument(\"status\", \"302\")))\n\n\nclass ChunkHandler(RequestHandler):\n    @gen.coroutine\n    def get(self):\n        self.write(\"asdf\")\n        self.flush()\n        # Wait a bit to ensure the chunks are sent and received separately.\n        yield gen.sleep(0.01)\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\nclass PatchHandler(RequestHandler):\n\n    def patch(self):\n        \"Return the request payload - so we can check it is being kept\"\n        self.write(self.request.body)\n\n\nclass AllMethodsHandler(RequestHandler):\n    SUPPORTED_METHODS = RequestHandler.SUPPORTED_METHODS + ('OTHER',)\n\n    def method(self):\n        self.write(self.request.method)\n\n    get = post = put = delete = options = patch = other = method\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(\"/put\", PutHandler),\n            url(\"/redirect\", RedirectHandler),\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            url(\"/all_methods\", AllMethodsHandler),\n            url('/patch', PatchHandler),\n        ], gzip=True)\n\n    def test_patch_receives_payload(self):\n        body = b\"some patch data\"\n        response = self.fetch(\"/patch\", method='PATCH', body=body)\n        self.assertEqual(response.code, 200)\n        self.assertEqual(response.body, body)\n\n    @skipOnTravis\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                if b\"HTTP/1.\" not in request_data:\n                    self.skipTest(\"requires HTTP/1.x\")\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_basic_auth_explicit_mode(self):\n        self.assertEqual(self.fetch(\"/auth\", auth_username=\"Aladdin\",\n                                    auth_password=\"open sesame\",\n                                    auth_mode=\"basic\").body,\n                         b\"Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==\")\n\n    def test_unsupported_auth_mode(self):\n        # curl and simple clients handle errors a bit differently; the\n        # important thing is that they don't fall back to basic auth\n        # on an unknown mode.\n        with ExpectLog(gen_log, \"uncaught exception\", required=False):\n            with self.assertRaises((ValueError, HTTPError)):\n                response = self.fetch(\"/auth\", auth_username=\"Aladdin\",\n                                      auth_password=\"open sesame\",\n                                      auth_mode=\"asdf\")\n                response.rethrow()\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)\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/1.1 101'):\n                # Upgrading to HTTP/2\n                pass\n            elif 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.lower()] = 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, first_line)\n        self.assertRegexpMatches(first_line[0], 'HTTP/[0-9]\\\\.[0-9] 200.*\\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.lower().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', allow_ipv6=False)\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        try:\n            client.fetch(self.get_url('/user_agent'), callback=self.stop)\n            response = self.wait()\n            self.assertEqual(response.body, b'TestDefaultUserAgent')\n        finally:\n            client.close()\n\n    def test_header_types(self):\n        # Header values may be passed as character or utf8 byte strings,\n        # in a plain dictionary or an HTTPHeaders object.\n        # Keys must always be the native str type.\n        # All combinations should have the same results on the wire.\n        for value in [u\"MyUserAgent\", b\"MyUserAgent\"]:\n            for container in [dict, HTTPHeaders]:\n                headers = container()\n                headers['User-Agent'] = value\n                resp = self.fetch('/user_agent', headers=headers)\n                self.assertEqual(\n                    resp.body, b\"MyUserAgent\",\n                    \"response=%r, value=%r, container=%r\" %\n                    (resp.body, value, container))\n\n    def test_multi_line_headers(self):\n        # Multi-line http headers are rare but rfc-allowed\n        # http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2\n        sock, port = bind_unused_port()\n        with closing(sock):\n            def write_response(stream, request_data):\n                if b\"HTTP/1.\" not in request_data:\n                    self.skipTest(\"requires HTTP/1.x\")\n                stream.write(b\"\"\"\\\nHTTP/1.1 200 OK\nX-XSS-Protection: 1;\n\\tmode=block\n\n\"\"\".replace(b\"\\n\", b\"\\r\\n\"), callback=stream.close)\n\n            def accept_callback(conn, address):\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.headers['X-XSS-Protection'], \"1; mode=block\")\n            self.io_loop.remove_handler(sock.fileno())\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        with self.assertRaises(HTTPError) as context:\n            yield self.http_client.fetch(self.get_url('/notfound'))\n        self.assertEqual(context.exception.code, 404)\n        self.assertEqual(context.exception.response.code, 404)\n\n    @gen_test\n    def test_future_http_error_no_raise(self):\n        response = yield self.http_client.fetch(self.get_url('/notfound'), raise_error=False)\n        self.assertEqual(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    def test_all_methods(self):\n        for method in ['GET', 'DELETE', 'OPTIONS']:\n            response = self.fetch('/all_methods', method=method)\n            self.assertEqual(response.body, utf8(method))\n        for method in ['POST', 'PUT', 'PATCH']:\n            response = self.fetch('/all_methods', method=method, body=b'')\n            self.assertEqual(response.body, utf8(method))\n        response = self.fetch('/all_methods', method='HEAD')\n        self.assertEqual(response.body, b'')\n        response = self.fetch('/all_methods', method='OTHER',\n                              allow_nonstandard_methods=True)\n        self.assertEqual(response.body, b'OTHER')\n\n    def test_body_sanity_checks(self):\n        # These methods require a body.\n        for method in ('POST', 'PUT', 'PATCH'):\n            with self.assertRaises(ValueError) as context:\n                resp = self.fetch('/all_methods', method=method)\n                resp.rethrow()\n            self.assertIn('must not be None', str(context.exception))\n\n            resp = self.fetch('/all_methods', method=method,\n                              allow_nonstandard_methods=True)\n            self.assertEqual(resp.code, 200)\n\n        # These methods don't allow a body.\n        for method in ('GET', 'DELETE', 'OPTIONS'):\n            with self.assertRaises(ValueError) as context:\n                resp = self.fetch('/all_methods', method=method, body=b'asdf')\n                resp.rethrow()\n            self.assertIn('must be None', str(context.exception))\n\n            # In most cases this can be overridden, but curl_httpclient\n            # does not allow body with a GET at all.\n            if method != 'GET':\n                resp = self.fetch('/all_methods', method=method, body=b'asdf',\n                                  allow_nonstandard_methods=True)\n                resp.rethrow()\n                self.assertEqual(resp.code, 200)\n\n    # This test causes odd failures with the combination of\n    # curl_httpclient (at least with the version of libcurl available\n    # on ubuntu 12.04), TwistedIOLoop, and epoll.  For POST (but not PUT),\n    # curl decides the response came back too soon and closes the connection\n    # to start again.  It does this *before* telling the socket callback to\n    # unregister the FD.  Some IOLoop implementations have special kernel\n    # integration to discover this immediately.  Tornado's IOLoops\n    # ignore errors on remove_handler to accommodate this behavior, but\n    # Twisted's reactor does not.  The removeReader call fails and so\n    # do all future removeAll calls (which our tests do at cleanup).\n    #\n    # def test_post_307(self):\n    #    response = self.fetch(\"/redirect?status=307&url=/post\",\n    #                          method=\"POST\", body=b\"arg1=foo&arg2=bar\")\n    #    self.assertEqual(response.body, b\"Post arg1: foo, arg2: bar\")\n\n    def test_put_307(self):\n        response = self.fetch(\"/redirect?status=307&url=/put\",\n                              method=\"PUT\", body=b\"hello\")\n        response.rethrow()\n        self.assertEqual(response.body, b\"Put body: hello\")\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__ in ('TwistedIOLoop',\n                                                  'AsyncIOMainLoop'):\n            # TwistedIOLoop only supports the global reactor, so we can't have\n            # separate IOLoops for client and server threads.\n            # AsyncIOMainLoop doesn't work with the default policy\n            # (although it could with some tweaks to this test and a\n            # policy that created loops for non-main threads).\n            raise unittest.SkipTest(\n                'Sync HTTPClient not compatible with TwistedIOLoop or '\n                'AsyncIOMainLoop')\n        self.server_ioloop = IOLoop()\n\n        sock, self.port = bind_unused_port()\n        app = Application([('/', HelloWorldHandler)])\n        self.server = HTTPServer(app, io_loop=self.server_ioloop)\n        self.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        def stop_server():\n            self.server.stop()\n            # Delay the shutdown of the IOLoop by one iteration because\n            # the server may still have some cleanup work left when\n            # the client finishes with the response (this is noticable\n            # with http/2, which leaves a Future with an unexamined\n            # StreamClosedError on the loop).\n            self.server_ioloop.add_callback(self.server_ioloop.stop)\n        self.server_ioloop.add_callback(stop_server)\n        self.server_thread.join()\n        self.http_client.close()\n        self.server_ioloop.close(all_fds=True)\n\n    def get_url(self, path):\n        return 'http://127.0.0.1:%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\n\nclass HTTPRequestTestCase(unittest.TestCase):\n    def test_headers(self):\n        request = HTTPRequest('http://example.com', headers={'foo': 'bar'})\n        self.assertEqual(request.headers, {'foo': 'bar'})\n\n    def test_headers_setter(self):\n        request = HTTPRequest('http://example.com')\n        request.headers = {'bar': 'baz'}\n        self.assertEqual(request.headers, {'bar': 'baz'})\n\n    def test_null_headers_setter(self):\n        request = HTTPRequest('http://example.com')\n        request.headers = None\n        self.assertEqual(request.headers, {})\n\n    def test_body(self):\n        request = HTTPRequest('http://example.com', body='foo')\n        self.assertEqual(request.body, utf8('foo'))\n\n    def test_body_setter(self):\n        request = HTTPRequest('http://example.com')\n        request.body = 'foo'\n        self.assertEqual(request.body, utf8('foo'))\n\n    def test_if_modified_since(self):\n        http_date = datetime.datetime.utcnow()\n        request = HTTPRequest('http://example.com', if_modified_since=http_date)\n        self.assertEqual(request.headers,\n                         {'If-Modified-Since': format_timestamp(http_date)})\n\n\nclass HTTPErrorTestCase(unittest.TestCase):\n    def test_copy(self):\n        e = HTTPError(403)\n        e2 = copy.copy(e)\n        self.assertIsNot(e, e2)\n        self.assertEqual(e.code, e2.code)\n\n    def test_str(self):\n        e = HTTPError(403)\n        self.assertEqual(str(e), \"HTTP 403: Forbidden\")\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 copy\nimport functools\nimport sys\nimport threading\nimport datetime\nfrom io import BytesIO\n\nfrom tornado.escape import utf8\nfrom tornado import gen\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.log import gen_log\nfrom tornado import netutil\nfrom tornado.stack_context import ExceptionStackContext, NullContext\nfrom tornado.testing import AsyncHTTPTestCase, bind_unused_port, gen_test, ExpectLog\nfrom tornado.test.util import unittest, skipOnTravis\nfrom tornado.web import Application, RequestHandler, url\nfrom tornado.httputil import format_timestamp, HTTPHeaders\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 PutHandler(RequestHandler):\n    def put(self):\n        self.write(\"Put body: \")\n        self.write(self.request.body)\n\n\nclass RedirectHandler(RequestHandler):\n    def prepare(self):\n        self.write('redirects can have bodies too')\n        self.redirect(self.get_argument(\"url\"),\n                      status=int(self.get_argument(\"status\", \"302\")))\n\n\nclass ChunkHandler(RequestHandler):\n    @gen.coroutine\n    def get(self):\n        self.write(\"asdf\")\n        self.flush()\n        # Wait a bit to ensure the chunks are sent and received separately.\n        yield gen.sleep(0.01)\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\nclass PatchHandler(RequestHandler):\n\n    def patch(self):\n        \"Return the request payload - so we can check it is being kept\"\n        self.write(self.request.body)\n\n\nclass AllMethodsHandler(RequestHandler):\n    SUPPORTED_METHODS = RequestHandler.SUPPORTED_METHODS + ('OTHER',)\n\n    def method(self):\n        self.write(self.request.method)\n\n    get = post = put = delete = options = patch = other = method\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(\"/put\", PutHandler),\n            url(\"/redirect\", RedirectHandler),\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            url(\"/all_methods\", AllMethodsHandler),\n            url('/patch', PatchHandler),\n        ], gzip=True)\n\n    def test_patch_receives_payload(self):\n        body = b\"some patch data\"\n        response = self.fetch(\"/patch\", method='PATCH', body=body)\n        self.assertEqual(response.code, 200)\n        self.assertEqual(response.body, body)\n\n    @skipOnTravis\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                if b\"HTTP/1.\" not in request_data:\n                    self.skipTest(\"requires HTTP/1.x\")\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_basic_auth_explicit_mode(self):\n        self.assertEqual(self.fetch(\"/auth\", auth_username=\"Aladdin\",\n                                    auth_password=\"open sesame\",\n                                    auth_mode=\"basic\").body,\n                         b\"Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==\")\n\n    def test_unsupported_auth_mode(self):\n        # curl and simple clients handle errors a bit differently; the\n        # important thing is that they don't fall back to basic auth\n        # on an unknown mode.\n        with ExpectLog(gen_log, \"uncaught exception\", required=False):\n            with self.assertRaises((ValueError, HTTPError)):\n                response = self.fetch(\"/auth\", auth_username=\"Aladdin\",\n                                      auth_password=\"open sesame\",\n                                      auth_mode=\"asdf\")\n                response.rethrow()\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)\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/1.1 101'):\n                # Upgrading to HTTP/2\n                pass\n            elif 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.lower()] = 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, first_line)\n        self.assertRegexpMatches(first_line[0], 'HTTP/[0-9]\\\\.[0-9] 200.*\\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.lower().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', allow_ipv6=False)\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        try:\n            client.fetch(self.get_url('/user_agent'), callback=self.stop)\n            response = self.wait()\n            self.assertEqual(response.body, b'TestDefaultUserAgent')\n        finally:\n            client.close()\n\n    def test_header_types(self):\n        # Header values may be passed as character or utf8 byte strings,\n        # in a plain dictionary or an HTTPHeaders object.\n        # Keys must always be the native str type.\n        # All combinations should have the same results on the wire.\n        for value in [u\"MyUserAgent\", b\"MyUserAgent\"]:\n            for container in [dict, HTTPHeaders]:\n                headers = container()\n                headers['User-Agent'] = value\n                resp = self.fetch('/user_agent', headers=headers)\n                self.assertEqual(\n                    resp.body, b\"MyUserAgent\",\n                    \"response=%r, value=%r, container=%r\" %\n                    (resp.body, value, container))\n\n    def test_multi_line_headers(self):\n        # Multi-line http headers are rare but rfc-allowed\n        # http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2\n        sock, port = bind_unused_port()\n        with closing(sock):\n            def write_response(stream, request_data):\n                if b\"HTTP/1.\" not in request_data:\n                    self.skipTest(\"requires HTTP/1.x\")\n                stream.write(b\"\"\"\\\nHTTP/1.1 200 OK\nX-XSS-Protection: 1;\n\\tmode=block\n\n\"\"\".replace(b\"\\n\", b\"\\r\\n\"), callback=stream.close)\n\n            def accept_callback(conn, address):\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.headers['X-XSS-Protection'], \"1; mode=block\")\n            self.io_loop.remove_handler(sock.fileno())\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        with self.assertRaises(HTTPError) as context:\n            yield self.http_client.fetch(self.get_url('/notfound'))\n        self.assertEqual(context.exception.code, 404)\n        self.assertEqual(context.exception.response.code, 404)\n\n    @gen_test\n    def test_future_http_error_no_raise(self):\n        response = yield self.http_client.fetch(self.get_url('/notfound'), raise_error=False)\n        self.assertEqual(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    def test_all_methods(self):\n        for method in ['GET', 'DELETE', 'OPTIONS']:\n            response = self.fetch('/all_methods', method=method)\n            self.assertEqual(response.body, utf8(method))\n        for method in ['POST', 'PUT', 'PATCH']:\n            response = self.fetch('/all_methods', method=method, body=b'')\n            self.assertEqual(response.body, utf8(method))\n        response = self.fetch('/all_methods', method='HEAD')\n        self.assertEqual(response.body, b'')\n        response = self.fetch('/all_methods', method='OTHER',\n                              allow_nonstandard_methods=True)\n        self.assertEqual(response.body, b'OTHER')\n\n    def test_body_sanity_checks(self):\n        # These methods require a body.\n        for method in ('POST', 'PUT', 'PATCH'):\n            with self.assertRaises(ValueError) as context:\n                resp = self.fetch('/all_methods', method=method)\n                resp.rethrow()\n            self.assertIn('must not be None', str(context.exception))\n\n            resp = self.fetch('/all_methods', method=method,\n                              allow_nonstandard_methods=True)\n            self.assertEqual(resp.code, 200)\n\n        # These methods don't allow a body.\n        for method in ('GET', 'DELETE', 'OPTIONS'):\n            with self.assertRaises(ValueError) as context:\n                resp = self.fetch('/all_methods', method=method, body=b'asdf')\n                resp.rethrow()\n            self.assertIn('must be None', str(context.exception))\n\n            # In most cases this can be overridden, but curl_httpclient\n            # does not allow body with a GET at all.\n            if method != 'GET':\n                resp = self.fetch('/all_methods', method=method, body=b'asdf',\n                                  allow_nonstandard_methods=True)\n                resp.rethrow()\n                self.assertEqual(resp.code, 200)\n\n    # This test causes odd failures with the combination of\n    # curl_httpclient (at least with the version of libcurl available\n    # on ubuntu 12.04), TwistedIOLoop, and epoll.  For POST (but not PUT),\n    # curl decides the response came back too soon and closes the connection\n    # to start again.  It does this *before* telling the socket callback to\n    # unregister the FD.  Some IOLoop implementations have special kernel\n    # integration to discover this immediately.  Tornado's IOLoops\n    # ignore errors on remove_handler to accommodate this behavior, but\n    # Twisted's reactor does not.  The removeReader call fails and so\n    # do all future removeAll calls (which our tests do at cleanup).\n    #\n    # def test_post_307(self):\n    #    response = self.fetch(\"/redirect?status=307&url=/post\",\n    #                          method=\"POST\", body=b\"arg1=foo&arg2=bar\")\n    #    self.assertEqual(response.body, b\"Post arg1: foo, arg2: bar\")\n\n    def test_put_307(self):\n        response = self.fetch(\"/redirect?status=307&url=/put\",\n                              method=\"PUT\", body=b\"hello\")\n        response.rethrow()\n        self.assertEqual(response.body, b\"Put body: hello\")\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__ in ('TwistedIOLoop',\n                                                  'AsyncIOMainLoop'):\n            # TwistedIOLoop only supports the global reactor, so we can't have\n            # separate IOLoops for client and server threads.\n            # AsyncIOMainLoop doesn't work with the default policy\n            # (although it could with some tweaks to this test and a\n            # policy that created loops for non-main threads).\n            raise unittest.SkipTest(\n                'Sync HTTPClient not compatible with TwistedIOLoop or '\n                'AsyncIOMainLoop')\n        self.server_ioloop = IOLoop()\n\n        sock, self.port = bind_unused_port()\n        app = Application([('/', HelloWorldHandler)])\n        self.server = HTTPServer(app, io_loop=self.server_ioloop)\n        self.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        def stop_server():\n            self.server.stop()\n            # Delay the shutdown of the IOLoop by one iteration because\n            # the server may still have some cleanup work left when\n            # the client finishes with the response (this is noticable\n            # with http/2, which leaves a Future with an unexamined\n            # StreamClosedError on the loop).\n            self.server_ioloop.add_callback(self.server_ioloop.stop)\n        self.server_ioloop.add_callback(stop_server)\n        self.server_thread.join()\n        self.http_client.close()\n        self.server_ioloop.close(all_fds=True)\n\n    def get_url(self, path):\n        return 'http://127.0.0.1:%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\n\nclass HTTPRequestTestCase(unittest.TestCase):\n    def test_headers(self):\n        request = HTTPRequest('http://example.com', headers={'foo': 'bar'})\n        self.assertEqual(request.headers, {'foo': 'bar'})\n\n    def test_headers_setter(self):\n        request = HTTPRequest('http://example.com')\n        request.headers = {'bar': 'baz'}\n        self.assertEqual(request.headers, {'bar': 'baz'})\n\n    def test_null_headers_setter(self):\n        request = HTTPRequest('http://example.com')\n        request.headers = None\n        self.assertEqual(request.headers, {})\n\n    def test_body(self):\n        request = HTTPRequest('http://example.com', body='foo')\n        self.assertEqual(request.body, utf8('foo'))\n\n    def test_body_setter(self):\n        request = HTTPRequest('http://example.com')\n        request.body = 'foo'\n        self.assertEqual(request.body, utf8('foo'))\n\n    def test_if_modified_since(self):\n        http_date = datetime.datetime.utcnow()\n        request = HTTPRequest('http://example.com', if_modified_since=http_date)\n        self.assertEqual(request.headers,\n                         {'If-Modified-Since': format_timestamp(http_date)})\n\n\nclass HTTPErrorTestCase(unittest.TestCase):\n    def test_copy(self):\n        e = HTTPError(403)\n        e2 = copy.copy(e)\n        self.assertIsNot(e, e2)\n        self.assertEqual(e.code, e2.code)\n\n    def test_plain_error(self):\n        e = HTTPError(403)\n        self.assertEqual(str(e), \"HTTP 403: Forbidden\")\n        self.assertEqual(repr(e), \"HTTP 403: Forbidden\")\n\n    def test_error_with_response(self):\n        resp = HTTPResponse(HTTPRequest('http://example.com/'), 403)\n        with self.assertRaises(HTTPError) as cm:\n            resp.rethrow()\n        e = cm.exception\n        self.assertEqual(str(e), \"HTTP 403: Forbidden\")\n        self.assertEqual(repr(e), \"HTTP 403: Forbidden\")\n",
            "header": {
                "file": {
                    "path": "tornado/test/httpclient_test.py"
                },
                "misc_line": null
            },
            "index_line": {
                "old_commit_hash": "e1496e9b",
                "new_commit_hash": "9e8e62c6",
                "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": 655,
                            "length": 6
                        },
                        "new_range": {
                            "start": 655,
                            "length": 15
                        },
                        "section": "class HTTPErrorTestCase(unittest.TestCase):"
                    },
                    "line_group": {
                        "all_lines": [
                            {
                                "content": "        self.assertIsNot(e, e2)",
                                "type": "context"
                            },
                            {
                                "content": "        self.assertEqual(e.code, e2.code)",
                                "type": "context"
                            },
                            {
                                "content": "",
                                "type": "context"
                            },
                            {
                                "content": "    def test_str(self):",
                                "type": "deleted"
                            },
                            {
                                "content": "    def test_plain_error(self):",
                                "type": "added"
                            },
                            {
                                "content": "        e = HTTPError(403)",
                                "type": "context"
                            },
                            {
                                "content": "        self.assertEqual(str(e), \"HTTP 403: Forbidden\")",
                                "type": "context"
                            },
                            {
                                "content": "        self.assertEqual(repr(e), \"HTTP 403: Forbidden\")",
                                "type": "added"
                            },
                            {
                                "content": "",
                                "type": "added"
                            },
                            {
                                "content": "    def test_error_with_response(self):",
                                "type": "added"
                            },
                            {
                                "content": "        resp = HTTPResponse(HTTPRequest('http://example.com/'), 403)",
                                "type": "added"
                            },
                            {
                                "content": "        with self.assertRaises(HTTPError) as cm:",
                                "type": "added"
                            },
                            {
                                "content": "            resp.rethrow()",
                                "type": "added"
                            },
                            {
                                "content": "        e = cm.exception",
                                "type": "added"
                            },
                            {
                                "content": "        self.assertEqual(str(e), \"HTTP 403: Forbidden\")",
                                "type": "added"
                            },
                            {
                                "content": "        self.assertEqual(repr(e), \"HTTP 403: Forbidden\")",
                                "type": "added"
                            },
                            {
                                "content": "",
                                "type": "context"
                            }
                        ]
                    },
                    "modified_entities": [
                        {
                            "file_name": "tornado/test/httpclient_test.py",
                            "type": "class",
                            "name": "HTTPErrorTestCase",
                            "content": "class HTTPErrorTestCase(unittest.TestCase):\n    def test_copy(self):\n        e = HTTPError(403)\n        e2 = copy.copy(e)\n        self.assertIsNot(e, e2)\n        self.assertEqual(e.code, e2.code)\n\n    def test_str(self):\n        e = HTTPError(403)\n        self.assertEqual(str(e), \"HTTP 403: Forbidden\")",
                            "ast_type_str": "ClassDef",
                            "start_lineno": 651,
                            "end_lineno": 660,
                            "parent": null
                        }
                    ],
                    "added_entities": [
                        {
                            "file_name": "tornado/test/httpclient_test.py",
                            "type": "method",
                            "name": "HTTPErrorTestCase.test_plain_error",
                            "content": "class HTTPErrorTestCase(unittest.TestCase):\n    def test_copy(self):\n        e = HTTPError(403)\n        e2 = copy.copy(e)\n        self.assertIsNot(e, e2)\n        self.assertEqual(e.code, e2.code)\n\n    def test_plain_error(self):\n        e = HTTPError(403)\n        self.assertEqual(str(e), \"HTTP 403: Forbidden\")\n        self.assertEqual(repr(e), \"HTTP 403: Forbidden\")\n\n    def test_error_with_response(self):\n        resp = HTTPResponse(HTTPRequest('http://example.com/'), 403)\n        with self.assertRaises(HTTPError) as cm:\n            resp.rethrow()\n        e = cm.exception\n        self.assertEqual(str(e), \"HTTP 403: Forbidden\")\n        self.assertEqual(repr(e), \"HTTP 403: Forbidden\")",
                            "ast_type_str": "ClassDef",
                            "start_lineno": 658,
                            "end_lineno": 661,
                            "parent": {
                                "file_name": "tornado/test/httpclient_test.py",
                                "type": "class",
                                "name": "HTTPErrorTestCase",
                                "content": "class HTTPErrorTestCase(unittest.TestCase):\n    def test_copy(self):\n        e = HTTPError(403)\n        e2 = copy.copy(e)\n        self.assertIsNot(e, e2)\n        self.assertEqual(e.code, e2.code)\n\n    def test_plain_error(self):\n        e = HTTPError(403)\n        self.assertEqual(str(e), \"HTTP 403: Forbidden\")\n        self.assertEqual(repr(e), \"HTTP 403: Forbidden\")\n\n    def test_error_with_response(self):\n        resp = HTTPResponse(HTTPRequest('http://example.com/'), 403)\n        with self.assertRaises(HTTPError) as cm:\n            resp.rethrow()\n        e = cm.exception\n        self.assertEqual(str(e), \"HTTP 403: Forbidden\")\n        self.assertEqual(repr(e), \"HTTP 403: Forbidden\")",
                                "ast_type_str": "ClassDef",
                                "start_lineno": 651,
                                "end_lineno": 669,
                                "parent": null
                            }
                        },
                        {
                            "file_name": "tornado/test/httpclient_test.py",
                            "type": "method",
                            "name": "HTTPErrorTestCase.test_error_with_response",
                            "content": "class HTTPErrorTestCase(unittest.TestCase):\n    def test_copy(self):\n        e = HTTPError(403)\n        e2 = copy.copy(e)\n        self.assertIsNot(e, e2)\n        self.assertEqual(e.code, e2.code)\n\n    def test_plain_error(self):\n        e = HTTPError(403)\n        self.assertEqual(str(e), \"HTTP 403: Forbidden\")\n        self.assertEqual(repr(e), \"HTTP 403: Forbidden\")\n\n    def test_error_with_response(self):\n        resp = HTTPResponse(HTTPRequest('http://example.com/'), 403)\n        with self.assertRaises(HTTPError) as cm:\n            resp.rethrow()\n        e = cm.exception\n        self.assertEqual(str(e), \"HTTP 403: Forbidden\")\n        self.assertEqual(repr(e), \"HTTP 403: Forbidden\")",
                            "ast_type_str": "ClassDef",
                            "start_lineno": 663,
                            "end_lineno": 669,
                            "parent": {
                                "file_name": "tornado/test/httpclient_test.py",
                                "type": "class",
                                "name": "HTTPErrorTestCase",
                                "content": "class HTTPErrorTestCase(unittest.TestCase):\n    def test_copy(self):\n        e = HTTPError(403)\n        e2 = copy.copy(e)\n        self.assertIsNot(e, e2)\n        self.assertEqual(e.code, e2.code)\n\n    def test_plain_error(self):\n        e = HTTPError(403)\n        self.assertEqual(str(e), \"HTTP 403: Forbidden\")\n        self.assertEqual(repr(e), \"HTTP 403: Forbidden\")\n\n    def test_error_with_response(self):\n        resp = HTTPResponse(HTTPRequest('http://example.com/'), 403)\n        with self.assertRaises(HTTPError) as cm:\n            resp.rethrow()\n        e = cm.exception\n        self.assertEqual(str(e), \"HTTP 403: Forbidden\")\n        self.assertEqual(repr(e), \"HTTP 403: Forbidden\")",
                                "ast_type_str": "ClassDef",
                                "start_lineno": 651,
                                "end_lineno": 669,
                                "parent": null
                            }
                        }
                    ],
                    "deleted_entities": [
                        {
                            "file_name": "tornado/test/httpclient_test.py",
                            "type": "method",
                            "name": "HTTPErrorTestCase.test_str",
                            "content": "class HTTPErrorTestCase(unittest.TestCase):\n    def test_copy(self):\n        e = HTTPError(403)\n        e2 = copy.copy(e)\n        self.assertIsNot(e, e2)\n        self.assertEqual(e.code, e2.code)\n\n    def test_str(self):\n        e = HTTPError(403)\n        self.assertEqual(str(e), \"HTTP 403: Forbidden\")",
                            "ast_type_str": "ClassDef",
                            "start_lineno": 658,
                            "end_lineno": 660,
                            "parent": {
                                "file_name": "tornado/test/httpclient_test.py",
                                "type": "class",
                                "name": "HTTPErrorTestCase",
                                "content": "class HTTPErrorTestCase(unittest.TestCase):\n    def test_copy(self):\n        e = HTTPError(403)\n        e2 = copy.copy(e)\n        self.assertIsNot(e, e2)\n        self.assertEqual(e.code, e2.code)\n\n    def test_str(self):\n        e = HTTPError(403)\n        self.assertEqual(str(e), \"HTTP 403: Forbidden\")",
                                "ast_type_str": "ClassDef",
                                "start_lineno": 651,
                                "end_lineno": 660,
                                "parent": null
                            }
                        }
                    ]
                }
            ]
        }
    ],
    "old_commit_hash": "22d884c5b94fed3b602bef6798cb732b4c916421^",
    "new_commit_hash": "22d884c5b94fed3b602bef6798cb732b4c916421",
    "commit_message": "Define `httpclient.HTTPError.__repr__`\n\nThis avoids stack overflow in the default `__repr__`.\n\nFixes #1624",
    "commit_date": "2016-02-13T21:15:34-05:00",
    "metadata": {}
}
{
    "repo_name": "tornado",
    "new_commit_hash": "22d884c5b94fed3b602bef6798cb732b4c916421",
    "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 copy\nimport functools\nimport sys\nimport threading\nimport datetime\nfrom io import BytesIO\n\nfrom tornado.escape import utf8\nfrom tornado import gen\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.log import gen_log\nfrom tornado import netutil\nfrom tornado.stack_context import ExceptionStackContext, NullContext\nfrom tornado.testing import AsyncHTTPTestCase, bind_unused_port, gen_test, ExpectLog\nfrom tornado.test.util import unittest, skipOnTravis\nfrom tornado.web import Application, RequestHandler, url\nfrom tornado.httputil import format_timestamp, HTTPHeaders\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 PutHandler(RequestHandler):\n    def put(self):\n        self.write(\"Put body: \")\n        self.write(self.request.body)\n\n\nclass RedirectHandler(RequestHandler):\n    def prepare(self):\n        self.write('redirects can have bodies too')\n        self.redirect(self.get_argument(\"url\"),\n                      status=int(self.get_argument(\"status\", \"302\")))\n\n\nclass ChunkHandler(RequestHandler):\n    @gen.coroutine\n    def get(self):\n        self.write(\"asdf\")\n        self.flush()\n        # Wait a bit to ensure the chunks are sent and received separately.\n        yield gen.sleep(0.01)\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\nclass PatchHandler(RequestHandler):\n\n    def patch(self):\n        \"Return the request payload - so we can check it is being kept\"\n        self.write(self.request.body)\n\n\nclass AllMethodsHandler(RequestHandler):\n    SUPPORTED_METHODS = RequestHandler.SUPPORTED_METHODS + ('OTHER',)\n\n    def method(self):\n        self.write(self.request.method)\n\n    get = post = put = delete = options = patch = other = method\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(\"/put\", PutHandler),\n            url(\"/redirect\", RedirectHandler),\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            url(\"/all_methods\", AllMethodsHandler),\n            url('/patch', PatchHandler),\n        ], gzip=True)\n\n    def test_patch_receives_payload(self):\n        body = b\"some patch data\"\n        response = self.fetch(\"/patch\", method='PATCH', body=body)\n        self.assertEqual(response.code, 200)\n        self.assertEqual(response.body, body)\n\n    @skipOnTravis\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                if b\"HTTP/1.\" not in request_data:\n                    self.skipTest(\"requires HTTP/1.x\")\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_basic_auth_explicit_mode(self):\n        self.assertEqual(self.fetch(\"/auth\", auth_username=\"Aladdin\",\n                                    auth_password=\"open sesame\",\n                                    auth_mode=\"basic\").body,\n                         b\"Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==\")\n\n    def test_unsupported_auth_mode(self):\n        # curl and simple clients handle errors a bit differently; the\n        # important thing is that they don't fall back to basic auth\n        # on an unknown mode.\n        with ExpectLog(gen_log, \"uncaught exception\", required=False):\n            with self.assertRaises((ValueError, HTTPError)):\n                response = self.fetch(\"/auth\", auth_username=\"Aladdin\",\n                                      auth_password=\"open sesame\",\n                                      auth_mode=\"asdf\")\n                response.rethrow()\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)\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/1.1 101'):\n                # Upgrading to HTTP/2\n                pass\n            elif 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.lower()] = 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, first_line)\n        self.assertRegexpMatches(first_line[0], 'HTTP/[0-9]\\\\.[0-9] 200.*\\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.lower().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', allow_ipv6=False)\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        try:\n            client.fetch(self.get_url('/user_agent'), callback=self.stop)\n            response = self.wait()\n            self.assertEqual(response.body, b'TestDefaultUserAgent')\n        finally:\n            client.close()\n\n    def test_header_types(self):\n        # Header values may be passed as character or utf8 byte strings,\n        # in a plain dictionary or an HTTPHeaders object.\n        # Keys must always be the native str type.\n        # All combinations should have the same results on the wire.\n        for value in [u\"MyUserAgent\", b\"MyUserAgent\"]:\n            for container in [dict, HTTPHeaders]:\n                headers = container()\n                headers['User-Agent'] = value\n                resp = self.fetch('/user_agent', headers=headers)\n                self.assertEqual(\n                    resp.body, b\"MyUserAgent\",\n                    \"response=%r, value=%r, container=%r\" %\n                    (resp.body, value, container))\n\n    def test_multi_line_headers(self):\n        # Multi-line http headers are rare but rfc-allowed\n        # http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2\n        sock, port = bind_unused_port()\n        with closing(sock):\n            def write_response(stream, request_data):\n                if b\"HTTP/1.\" not in request_data:\n                    self.skipTest(\"requires HTTP/1.x\")\n                stream.write(b\"\"\"\\\nHTTP/1.1 200 OK\nX-XSS-Protection: 1;\n\\tmode=block\n\n\"\"\".replace(b\"\\n\", b\"\\r\\n\"), callback=stream.close)\n\n            def accept_callback(conn, address):\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.headers['X-XSS-Protection'], \"1; mode=block\")\n            self.io_loop.remove_handler(sock.fileno())\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        with self.assertRaises(HTTPError) as context:\n            yield self.http_client.fetch(self.get_url('/notfound'))\n        self.assertEqual(context.exception.code, 404)\n        self.assertEqual(context.exception.response.code, 404)\n\n    @gen_test\n    def test_future_http_error_no_raise(self):\n        response = yield self.http_client.fetch(self.get_url('/notfound'), raise_error=False)\n        self.assertEqual(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    def test_all_methods(self):\n        for method in ['GET', 'DELETE', 'OPTIONS']:\n            response = self.fetch('/all_methods', method=method)\n            self.assertEqual(response.body, utf8(method))\n        for method in ['POST', 'PUT', 'PATCH']:\n            response = self.fetch('/all_methods', method=method, body=b'')\n            self.assertEqual(response.body, utf8(method))\n        response = self.fetch('/all_methods', method='HEAD')\n        self.assertEqual(response.body, b'')\n        response = self.fetch('/all_methods', method='OTHER',\n                              allow_nonstandard_methods=True)\n        self.assertEqual(response.body, b'OTHER')\n\n    def test_body_sanity_checks(self):\n        # These methods require a body.\n        for method in ('POST', 'PUT', 'PATCH'):\n            with self.assertRaises(ValueError) as context:\n                resp = self.fetch('/all_methods', method=method)\n                resp.rethrow()\n            self.assertIn('must not be None', str(context.exception))\n\n            resp = self.fetch('/all_methods', method=method,\n                              allow_nonstandard_methods=True)\n            self.assertEqual(resp.code, 200)\n\n        # These methods don't allow a body.\n        for method in ('GET', 'DELETE', 'OPTIONS'):\n            with self.assertRaises(ValueError) as context:\n                resp = self.fetch('/all_methods', method=method, body=b'asdf')\n                resp.rethrow()\n            self.assertIn('must be None', str(context.exception))\n\n            # In most cases this can be overridden, but curl_httpclient\n            # does not allow body with a GET at all.\n            if method != 'GET':\n                resp = self.fetch('/all_methods', method=method, body=b'asdf',\n                                  allow_nonstandard_methods=True)\n                resp.rethrow()\n                self.assertEqual(resp.code, 200)\n\n    # This test causes odd failures with the combination of\n    # curl_httpclient (at least with the version of libcurl available\n    # on ubuntu 12.04), TwistedIOLoop, and epoll.  For POST (but not PUT),\n    # curl decides the response came back too soon and closes the connection\n    # to start again.  It does this *before* telling the socket callback to\n    # unregister the FD.  Some IOLoop implementations have special kernel\n    # integration to discover this immediately.  Tornado's IOLoops\n    # ignore errors on remove_handler to accommodate this behavior, but\n    # Twisted's reactor does not.  The removeReader call fails and so\n    # do all future removeAll calls (which our tests do at cleanup).\n    #\n    # def test_post_307(self):\n    #    response = self.fetch(\"/redirect?status=307&url=/post\",\n    #                          method=\"POST\", body=b\"arg1=foo&arg2=bar\")\n    #    self.assertEqual(response.body, b\"Post arg1: foo, arg2: bar\")\n\n    def test_put_307(self):\n        response = self.fetch(\"/redirect?status=307&url=/put\",\n                              method=\"PUT\", body=b\"hello\")\n        response.rethrow()\n        self.assertEqual(response.body, b\"Put body: hello\")\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__ in ('TwistedIOLoop',\n                                                  'AsyncIOMainLoop'):\n            # TwistedIOLoop only supports the global reactor, so we can't have\n            # separate IOLoops for client and server threads.\n            # AsyncIOMainLoop doesn't work with the default policy\n            # (although it could with some tweaks to this test and a\n            # policy that created loops for non-main threads).\n            raise unittest.SkipTest(\n                'Sync HTTPClient not compatible with TwistedIOLoop or '\n                'AsyncIOMainLoop')\n        self.server_ioloop = IOLoop()\n\n        sock, self.port = bind_unused_port()\n        app = Application([('/', HelloWorldHandler)])\n        self.server = HTTPServer(app, io_loop=self.server_ioloop)\n        self.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        def stop_server():\n            self.server.stop()\n            # Delay the shutdown of the IOLoop by one iteration because\n            # the server may still have some cleanup work left when\n            # the client finishes with the response (this is noticable\n            # with http/2, which leaves a Future with an unexamined\n            # StreamClosedError on the loop).\n            self.server_ioloop.add_callback(self.server_ioloop.stop)\n        self.server_ioloop.add_callback(stop_server)\n        self.server_thread.join()\n        self.http_client.close()\n        self.server_ioloop.close(all_fds=True)\n\n    def get_url(self, path):\n        return 'http://127.0.0.1:%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\n\nclass HTTPRequestTestCase(unittest.TestCase):\n    def test_headers(self):\n        request = HTTPRequest('http://example.com', headers={'foo': 'bar'})\n        self.assertEqual(request.headers, {'foo': 'bar'})\n\n    def test_headers_setter(self):\n        request = HTTPRequest('http://example.com')\n        request.headers = {'bar': 'baz'}\n        self.assertEqual(request.headers, {'bar': 'baz'})\n\n    def test_null_headers_setter(self):\n        request = HTTPRequest('http://example.com')\n        request.headers = None\n        self.assertEqual(request.headers, {})\n\n    def test_body(self):\n        request = HTTPRequest('http://example.com', body='foo')\n        self.assertEqual(request.body, utf8('foo'))\n\n    def test_body_setter(self):\n        request = HTTPRequest('http://example.com')\n        request.body = 'foo'\n        self.assertEqual(request.body, utf8('foo'))\n\n    def test_if_modified_since(self):\n        http_date = datetime.datetime.utcnow()\n        request = HTTPRequest('http://example.com', if_modified_since=http_date)\n        self.assertEqual(request.headers,\n                         {'If-Modified-Since': format_timestamp(http_date)})\n\n\nclass HTTPErrorTestCase(unittest.TestCase):\n    def test_copy(self):\n        e = HTTPError(403)\n        e2 = copy.copy(e)\n        self.assertIsNot(e, e2)\n        self.assertEqual(e.code, e2.code)\n\n    def test_plain_error(self):\n        e = HTTPError(403)\n        self.assertEqual(str(e), \"HTTP 403: Forbidden\")\n        self.assertEqual(repr(e), \"HTTP 403: Forbidden\")\n\n    def test_error_with_response(self):\n        resp = HTTPResponse(HTTPRequest('http://example.com/'), 403)\n        with self.assertRaises(HTTPError) as cm:\n            resp.rethrow()\n        e = cm.exception\n        self.assertEqual(str(e), \"HTTP 403: Forbidden\")\n        self.assertEqual(repr(e), \"HTTP 403: Forbidden\")\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 1.03s\nPrepared 1 package in 1.16s\nInstalled 1 package in 0.79ms\n + tornado==4.4.dev1 (from file:///home/gcpuser/buckets/local_repoeval_bucket/repos/tornado_22d884c5b94fed3b602bef6798cb732b4c916421)\nResolved 29 packages in 15ms\nInstalled 29 packages in 190ms\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_22d884c5b94fed3b602bef6798cb732b4c916421\n..............................................\ncollected 46 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_all_methods\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_basic_auth\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_basic_auth_explicit_mode\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_body_encoding\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_body_sanity_checks\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_http_error_no_raise\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_header_types\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_hello_world\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_multi_line_headers\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_patch_receives_payload\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_post\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_put_307\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::HTTPClientCommonTestCase::test_unsupported_auth_mode\nPASSED r2e_tests.test_1::HTTPErrorTestCase::test_copy\nPASSED r2e_tests.test_1::HTTPErrorTestCase::test_error_with_response\nPASSED r2e_tests.test_1::HTTPErrorTestCase::test_plain_error\nPASSED r2e_tests.test_1::HTTPRequestTestCase::test_body\nPASSED r2e_tests.test_1::HTTPRequestTestCase::test_body_setter\nPASSED r2e_tests.test_1::HTTPRequestTestCase::test_headers\nPASSED r2e_tests.test_1::HTTPRequestTestCase::test_headers_setter\nPASSED r2e_tests.test_1::HTTPRequestTestCase::test_if_modified_since\nPASSED r2e_tests.test_1::HTTPRequestTestCase::test_null_headers_setter\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=================== 46 passed in 0.17s ===================\n",
    "new_commit_res_stderr": "WARNING:tornado.access:405 HEAD /all_methods (127.0.0.1) 0.29ms\nWARNING:tornado.access:404 GET /notfound (127.0.0.1) 0.28ms\nWARNING:tornado.access:404 GET /notfound (127.0.0.1) 0.27ms\nWARNING:tornado.access:404 GET /notfound (127.0.0.1) 0.74ms\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_22d884c5b94fed3b602bef6798cb732b4c916421\n..............................................\ncollected 46 items\n\n=================================== FAILURES ===================================\n__________ HTTPErrorTestCase.test_error_with_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_22d884c5b94fed3b602bef6798cb732b4c916421/r2e_tests/test_1.py\", line 669, in test_error_with_response\n    self.assertEqual(repr(e), \"HTTP 403: Forbidden\")\nAssertionError: \"HTTPError(403, 'Forbidden', HTTPResponse[267 chars]{}))\" != 'HTTP 403: Forbidden'\n- HTTPError(403, 'Forbidden', HTTPResponse(_body=None,buffer=None,code=403,effective_url='http://example.com/',error=HTTPError(...),headers=<tornado.httputil.HTTPHeaders object at 0x76c4049848b0>,reason='Forbidden',request=<tornado.httpclient.HTTPRequest object at 0x76c404984340>,request_time=None,time_info={}))\n+ HTTP 403: Forbidden\n\n__________ HTTPErrorTestCase.test_plain_error __________\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_22d884c5b94fed3b602bef6798cb732b4c916421/r2e_tests/test_1.py\", line 661, in test_plain_error\n    self.assertEqual(repr(e), \"HTTP 403: Forbidden\")\nAssertionError: 'HTTPError(403, None, None)' != 'HTTP 403: Forbidden'\n- HTTPError(403, None, None)\n+ HTTP 403: Forbidden\n\n\n==================== short test summary info ====================\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_304_with_content_length\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_all_methods\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_basic_auth\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_basic_auth_explicit_mode\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_body_encoding\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_body_sanity_checks\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_http_error_no_raise\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_header_types\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_hello_world\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_multi_line_headers\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_patch_receives_payload\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_post\nPASSED r2e_tests.test_1::HTTPClientCommonTestCase::test_put_307\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::HTTPClientCommonTestCase::test_unsupported_auth_mode\nPASSED r2e_tests.test_1::HTTPErrorTestCase::test_copy\nFAILED r2e_tests.test_1::HTTPErrorTestCase::test_error_with_response\nFAILED r2e_tests.test_1::HTTPErrorTestCase::test_plain_error\nPASSED r2e_tests.test_1::HTTPRequestTestCase::test_body\nPASSED r2e_tests.test_1::HTTPRequestTestCase::test_body_setter\nPASSED r2e_tests.test_1::HTTPRequestTestCase::test_headers\nPASSED r2e_tests.test_1::HTTPRequestTestCase::test_headers_setter\nPASSED r2e_tests.test_1::HTTPRequestTestCase::test_if_modified_since\nPASSED r2e_tests.test_1::HTTPRequestTestCase::test_null_headers_setter\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=================== 2 failed, 44 passed in 0.14s ===================\n",
    "old_commit_res_stderr": "WARNING:tornado.access:405 HEAD /all_methods (127.0.0.1) 0.17ms\nWARNING:tornado.access:404 GET /notfound (127.0.0.1) 0.26ms\nWARNING:tornado.access:404 GET /notfound (127.0.0.1) 0.26ms\nWARNING:tornado.access:404 GET /notfound (127.0.0.1) 0.29ms\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:** `repr()` of `HTTPError` Causes Stack Overflow Due to Cyclic References

**Description:**
When attempting to obtain the representation of an `HTTPError` instance, calling `repr(e)` leads to excessively verbose output or a stack overflow. This issue arises because there is a cyclic reference between the `HTTPError` instance and its associated `HTTPResponse`, causing the default `__repr__` implementation to recurse indefinitely, especially noticeable in environments like PyPy that lack robust recursion detection.

**Example Code:**
```python
try:
    response = HTTPResponse(HTTPRequest('http://example.com/'), 403)
    response.rethrow()
except HTTPError as e:
    print(repr(e))  # Causes stack overflow or overly verbose output
```

**Expected Behavior:**
Calling `repr(e)` on an `HTTPError` instance should return a concise and clear string representation similar to `str(e)`, such as `"HTTP 403: Forbidden"`, without causing a stack overflow or including unnecessary cyclic references.

**Actual Behavior:**
Instead of a simple string, `repr(e)` produces a detailed and recursive string that includes references to the `HTTPResponse` and possibly other nested objects, leading to stack overflow errors or overly long representations like:
```
HTTPError(403, 'Forbidden', HTTPResponse(..., error=HTTPError(...), ...))
```
[/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 of libcurl is
7.21.1, and the minimum version of pycurl is 7.18.2.  It is highly
recommended that your ``libcurl`` installation is built with
asynchronous DNS resolver (threaded or c-ares), otherwise you may
encounter various problems with request timeouts (for more
information, see
http://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTCONNECTTIMEOUTMS
and comments in curl_httpclient.py).

To select ``curl_httpclient``, call `AsyncHTTPClient.configure` at startup::

    AsyncHTTPClient.configure("tornado.curl_httpclient.CurlAsyncHTTPClient")
"""

from __future__ import absolute_import, division, print_function, with_statement

import functools
import time
import weakref

from tornado.concurrent import TracebackFuture
from tornado.escape import utf8, native_str
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:
            # HTTPError is raised for non-200 responses; the response
            # can be found in e.response.
            print("Error: " + str(e))
        except Exception as e:
            # Other errors are possible, such as IOError.
            print("Error: " + str(e))
        http_client.close()
    """
    def __init__(self, async_client_class=None, **kwargs):
        self._io_loop = IOLoop(make_current=False)
        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` unless
        the ``raise_error`` keyword argument is set to False.
        """
        response = self._io_loop.run_sync(functools.partial(
            self._async_client.fetch, request, **kwargs))
        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.  Unless
    ``force_instance=True`` is used, no arguments other than
    ``io_loop`` should be passed to the `AsyncHTTPClient` constructor.
    The implementation subclass as well as arguments to its
    constructor can be set with the static method `configure()`

    All `AsyncHTTPClient` implementations support a ``defaults``
    keyword argument, which can be used to set default values for
    `HTTPRequest` attributes.  For example::

        AsyncHTTPClient.configure(
            None, defaults=dict(user_agent="MyUserAgent"))
        # or with force_instance:
        client = AsyncHTTPClient(force_instance=True,
            defaults=dict(user_agent="MyUserAgent"))

    .. versionchanged:: 4.1
       The ``io_loop`` argument is deprecated.
    """
    @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 force_instance:
            instance_cache = None
        else:
            instance_cache = cls._async_clients()
        if instance_cache is not None and io_loop in instance_cache:
            return instance_cache[io_loop]
        instance = super(AsyncHTTPClient, cls).__new__(cls, io_loop=io_loop,
                                                       **kwargs)
        # Make sure the instance knows which cache to remove itself from.
        # It can't simply call _async_clients() because we may be in
        # __new__(AsyncHTTPClient) but instance.__class__ may be
        # SimpleAsyncHTTPClient.
        instance._instance_cache = instance_cache
        if instance_cache is not None:
            instance_cache[instance.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)
        self._closed = False

    def close(self):
        """Destroys this HTTP client, freeing any file descriptors used.

        This method is **not needed in normal use** due to the way
        that `AsyncHTTPClient` objects are transparently reused.
        ``close()`` is generally only necessary when either the
        `.IOLoop` is also being closed, or the ``force_instance=True``
        argument was used when creating the `AsyncHTTPClient`.

        No other methods may be called on the `AsyncHTTPClient` after
        ``close()``.

        """
        if self._closed:
            return
        self._closed = True
        if self._instance_cache is not None:
            if self._instance_cache.get(self.io_loop) is not self:
                raise RuntimeError("inconsistent AsyncHTTPClient cache")
            del self._instance_cache[self.io_loop]

    def fetch(self, request, callback=None, raise_error=True, **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`. By default, the ``Future`` will raise an
        `HTTPError` if the request returned a non-200 response code
        (other errors may also be raised if the server could not be
        contacted). Instead, if ``raise_error`` is set to False, the
        response will always be returned regardless of the 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 self._closed:
            raise RuntimeError("fetch() called on closed AsyncHTTPClient")
        if not isinstance(request, HTTPRequest):
            request = HTTPRequest(url=request, **kwargs)
        else:
            if kwargs:
                raise ValueError("kwargs can't be used if request is an HTTPRequest object")
        # 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 = TracebackFuture()
        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 raise_error and 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,
        decompress_response=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, auth_mode=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, body_producer=None,
                 expect_100_continue=False, decompress_response=None,
                 ssl_options=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 body: HTTP request body as a string (byte or unicode; if unicode
           the utf-8 encoding will be used)
        :arg body_producer: Callable used for lazy/asynchronous request bodies.
           It is called with one argument, a ``write`` function, and should
           return a `.Future`.  It should call the write function with new
           data as it becomes available.  The write function returns a
           `.Future` which can be used for flow control.
           Only one of ``body`` and ``body_producer`` may
           be specified.  ``body_producer`` is not supported on
           ``curl_httpclient``.  When using ``body_producer`` it is recommended
           to pass a ``Content-Length`` in the headers as otherwise chunked
           encoding will be used, and many servers do not support chunked
           encoding on requests.  New in Tornado 4.0
        :arg string auth_username: Username for HTTP authentication
        :arg string auth_password: Password for HTTP authentication
        :arg string auth_mode: Authentication mode; default is "basic".
           Allowed values are implementation-defined; ``curl_httpclient``
           supports "basic" and "digest"; ``simple_httpclient`` only supports
           "basic"
        :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 decompress_response: Request a compressed response from
           the server and decompress it after downloading.  Default is True.
           New in Tornado 4.0.
        :arg bool use_gzip: Deprecated alias for ``decompress_response``
           since Tornado 4.0.
        :arg string network_interface: Network interface to use for request.
           ``curl_httpclient`` only; see note below.
        :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.  See note below when used with
           ``curl_httpclient``.
        :arg string client_key: Filename for client SSL key, if any.  See
           note below when used with ``curl_httpclient``.
        :arg string client_cert: Filename for client SSL certificate, if any.
           See note below when used with ``curl_httpclient``.
        :arg ssl.SSLContext ssl_options: `ssl.SSLContext` object for use in
           ``simple_httpclient`` (unsupported by ``curl_httpclient``).
           Overrides ``validate_cert``, ``ca_certs``, ``client_key``,
           and ``client_cert``.
        :arg bool allow_ipv6: Use IPv6 when available?  Default is true.
        :arg bool expect_100_continue: If true, send the
           ``Expect: 100-continue`` header and wait for a continue response
           before sending the request body.  Only supported with
           simple_httpclient.

        .. note::

            When using ``curl_httpclient`` certain options may be
            inherited by subsequent fetches because ``pycurl`` does
            not allow them to be cleanly reset.  This applies to the
            ``ca_certs``, ``client_key``, ``client_cert``, and
            ``network_interface`` arguments.  If you use these
            options, you should pass them on every request (you don't
            have to always use the same values, but it's not possible
            to mix requests that specify these options with ones that
            use the defaults).

        .. versionadded:: 3.1
           The ``auth_mode`` argument.

        .. versionadded:: 4.0
           The ``body_producer`` and ``expect_100_continue`` arguments.

        .. versionadded:: 4.2
           The ``ssl_options`` argument.
        """
        # Note that some of these attributes go through property setters
        # defined below.
        self.headers = headers
        if if_modified_since:
            self.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.body = body
        self.body_producer = body_producer
        self.auth_username = auth_username
        self.auth_password = auth_password
        self.auth_mode = auth_mode
        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
        if decompress_response is not None:
            self.decompress_response = decompress_response
        else:
            self.decompress_response = use_gzip
        self.network_interface = network_interface
        self.streaming_callback = streaming_callback
        self.header_callback = header_callback
        self.prepare_curl_callback = 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.ssl_options = ssl_options
        self.expect_100_continue = expect_100_continue
        self.start_time = time.time()

    @property
    def headers(self):
        return self._headers

    @headers.setter
    def headers(self, value):
        if value is None:
            self._headers = httputil.HTTPHeaders()
        else:
            self._headers = value

    @property
    def body(self):
        return self._body

    @body.setter
    def body(self, value):
        self._body = utf8(value)

    @property
    def body_producer(self):
        return self._body_producer

    @body_producer.setter
    def body_producer(self, value):
        self._body_producer = stack_context.wrap(value)

    @property
    def streaming_callback(self):
        return self._streaming_callback

    @streaming_callback.setter
    def streaming_callback(self, value):
        self._streaming_callback = stack_context.wrap(value)

    @property
    def header_callback(self):
        return self._header_callback

    @header_callback.setter
    def header_callback(self, value):
        self._header_callback = stack_context.wrap(value)

    @property
    def prepare_curl_callback(self):
        return self._prepare_curl_callback

    @prepare_curl_callback.setter
    def prepare_curl_callback(self, value):
        self._prepare_curl_callback = stack_context.wrap(value)


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

    * headers: `tornado.httputil.HTTPHeaders` object

    * effective_url: final location of the resource after following any
      redirects

    * 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):
        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:
            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, message=self.reason,
                                       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
        self.message = message or httputil.responses.get(code, "Unknown")
        self.response = response
        super(HTTPError, self).__init__(code, message, response)

    def __str__(self):
        return "HTTP %d: %s" % (self.code, self.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(native_str(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:** `repr()` of `HTTPError` Causes Stack Overflow Due to Cyclic References

**Description:**
When attempting to obtain the representation of an `HTTPError` instance, calling `repr(e)` leads to excessively verbose output or a stack overflow. This issue arises because there is a cyclic reference between the `HTTPError` instance and its associated `HTTPResponse`, causing the default `__repr__` implementation to recurse indefinitely, especially noticeable in environments like PyPy that lack robust recursion detection.

**Example Code:**
```python
try:
    response = HTTPResponse(HTTPRequest('http://example.com/'), 403)
    response.rethrow()
except HTTPError as e:
    print(repr(e))  # Causes stack overflow or overly verbose output
```

**Expected Behavior:**
Calling `repr(e)` on an `HTTPError` instance should return a concise and clear string representation similar to `str(e)`, such as `"HTTP 403: Forbidden"`, without causing a stack overflow or including unnecessary cyclic references.

**Actual Behavior:**
Instead of a simple string, `repr(e)` produces a detailed and recursive string that includes references to the `HTTPResponse` and possibly other nested objects, leading to stack overflow errors or overly long representations like:
```
HTTPError(403, 'Forbidden', HTTPResponse(..., error=HTTPError(...), ...))
```
[/ISSUE]
{
    "HTTPClientCommonTestCase.test_304_with_content_length": "PASSED",
    "HTTPClientCommonTestCase.test_all_methods": "PASSED",
    "HTTPClientCommonTestCase.test_basic_auth": "PASSED",
    "HTTPClientCommonTestCase.test_basic_auth_explicit_mode": "PASSED",
    "HTTPClientCommonTestCase.test_body_encoding": "PASSED",
    "HTTPClientCommonTestCase.test_body_sanity_checks": "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_http_error_no_raise": "PASSED",
    "HTTPClientCommonTestCase.test_future_interface": "PASSED",
    "HTTPClientCommonTestCase.test_header_callback": "PASSED",
    "HTTPClientCommonTestCase.test_header_callback_stack_context": "PASSED",
    "HTTPClientCommonTestCase.test_header_types": "PASSED",
    "HTTPClientCommonTestCase.test_hello_world": "PASSED",
    "HTTPClientCommonTestCase.test_multi_line_headers": "PASSED",
    "HTTPClientCommonTestCase.test_patch_receives_payload": "PASSED",
    "HTTPClientCommonTestCase.test_post": "PASSED",
    "HTTPClientCommonTestCase.test_put_307": "PASSED",
    "HTTPClientCommonTestCase.test_reuse_request_from_response": "PASSED",
    "HTTPClientCommonTestCase.test_streaming_callback": "PASSED",
    "HTTPClientCommonTestCase.test_streaming_stack_context": "PASSED",
    "HTTPClientCommonTestCase.test_types": "PASSED",
    "HTTPClientCommonTestCase.test_unsupported_auth_mode": "PASSED",
    "HTTPErrorTestCase.test_copy": "PASSED",
    "HTTPErrorTestCase.test_error_with_response": "PASSED",
    "HTTPErrorTestCase.test_plain_error": "PASSED",
    "HTTPRequestTestCase.test_body": "PASSED",
    "HTTPRequestTestCase.test_body_setter": "PASSED",
    "HTTPRequestTestCase.test_headers": "PASSED",
    "HTTPRequestTestCase.test_headers_setter": "PASSED",
    "HTTPRequestTestCase.test_if_modified_since": "PASSED",
    "HTTPRequestTestCase.test_null_headers_setter": "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 90c78119..32ce1e0b 100644
--- a/tornado/httpclient.py
+++ b/tornado/httpclient.py
@@ -615,6 +615,12 @@ class HTTPError(Exception):
     def __str__(self):
         return "HTTP %d: %s" % (self.code, self.message)
 
+    # There is a cyclic reference between self and self.response,
+    # which breaks the default __repr__ implementation.
+    # (especially on pypy, which doesn't have the same recursion
+    # detection as cpython).
+    __repr__ = __str__
+
 
 class _RequestProxy(object):
     """Combines an object with a dictionary of defaults.

{"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\nrecommended that you use a recent version of ``libcurl`` and\n``pycurl``.  Currently the minimum supported version of libcurl is\n7.21.1, and the minimum version of pycurl is 7.18.2.  It is highly\nrecommended that your ``libcurl`` installation is built with\nasynchronous DNS resolver (threaded or c-ares), otherwise you may\nencounter various problems with request timeouts (for more\ninformation, see\nhttp://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTCONNECTTIMEOUTMS\nand comments in curl_httpclient.py).\n\nTo select ``curl_httpclient``, call `AsyncHTTPClient.configure` at startup::\n\n    AsyncHTTPClient.configure(\"tornado.curl_httpclient.CurlAsyncHTTPClient\")\n\"\"\"\n\nfrom __future__ import absolute_import, division, print_function, with_statement\n\nimport functools\nimport time\nimport weakref\n\nfrom tornado.concurrent import TracebackFuture\nfrom tornado.escape import utf8, native_str\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            # HTTPError is raised for non-200 responses; the response\n            # can be found in e.response.\n            print(\"Error: \" + str(e))\n        except Exception as e:\n            # Other errors are possible, such as IOError.\n            print(\"Error: \" + str(e))\n        http_client.close()\n    \"\"\"\n    def __init__(self, async_client_class=None, **kwargs):\n        self._io_loop = IOLoop(make_current=False)\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` unless\n        the ``raise_error`` keyword argument is set to False.\n        \"\"\"\n        response = self._io_loop.run_sync(functools.partial(\n            self._async_client.fetch, request, **kwargs))\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.  Unless\n    ``force_instance=True`` is used, no arguments other than\n    ``io_loop`` should be passed to the `AsyncHTTPClient` constructor.\n    The implementation subclass as well as arguments to its\n    constructor can be set with the static method `configure()`\n\n    All `AsyncHTTPClient` implementations support a ``defaults``\n    keyword argument, which can be used to set default values for\n    `HTTPRequest` attributes.  For example::\n\n        AsyncHTTPClient.configure(\n            None, defaults=dict(user_agent=\"MyUserAgent\"))\n        # or with force_instance:\n        client = AsyncHTTPClient(force_instance=True,\n            defaults=dict(user_agent=\"MyUserAgent\"))\n\n    .. versionchanged:: 4.1\n       The ``io_loop`` argument is deprecated.\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 force_instance:\n            instance_cache = None\n        else:\n            instance_cache = cls._async_clients()\n        if instance_cache is not None and io_loop in instance_cache:\n            return instance_cache[io_loop]\n        instance = super(AsyncHTTPClient, cls).__new__(cls, io_loop=io_loop,\n                                                       **kwargs)\n        # Make sure the instance knows which cache to remove itself from.\n        # It can't simply call _async_clients() because we may be in\n        # __new__(AsyncHTTPClient) but instance.__class__ may be\n        # SimpleAsyncHTTPClient.\n        instance._instance_cache = instance_cache\n        if instance_cache is not None:\n            instance_cache[instance.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        self._closed = False\n\n    def close(self):\n        \"\"\"Destroys this HTTP client, freeing any file descriptors used.\n\n        This method is **not needed in normal use** due to the way\n        that `AsyncHTTPClient` objects are transparently reused.\n        ``close()`` is generally only necessary when either the\n        `.IOLoop` is also being closed, or the ``force_instance=True``\n        argument was used when creating the `AsyncHTTPClient`.\n\n        No other methods may be called on the `AsyncHTTPClient` after\n        ``close()``.\n\n        \"\"\"\n        if self._closed:\n            return\n        self._closed = True\n        if self._instance_cache is not None:\n            if self._instance_cache.get(self.io_loop) is not self:\n                raise RuntimeError(\"inconsistent AsyncHTTPClient cache\")\n            del self._instance_cache[self.io_loop]\n\n    def fetch(self, request, callback=None, raise_error=True, **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`. By default, the ``Future`` will raise an\n        `HTTPError` if the request returned a non-200 response code\n        (other errors may also be raised if the server could not be\n        contacted). Instead, if ``raise_error`` is set to False, the\n        response will always be returned regardless of the response\n        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 self._closed:\n            raise RuntimeError(\"fetch() called on closed AsyncHTTPClient\")\n        if not isinstance(request, HTTPRequest):\n            request = HTTPRequest(url=request, **kwargs)\n        else:\n            if kwargs:\n                raise ValueError(\"kwargs can't be used if request is an HTTPRequest object\")\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 = TracebackFuture()\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 raise_error and 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        decompress_response=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, auth_mode=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, body_producer=None,\n                 expect_100_continue=False, decompress_response=None,\n                 ssl_options=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 body: HTTP request body as a string (byte or unicode; if unicode\n           the utf-8 encoding will be used)\n        :arg body_producer: Callable used for lazy/asynchronous request bodies.\n           It is called with one argument, a ``write`` function, and should\n           return a `.Future`.  It should call the write function with new\n           data as it becomes available.  The write function returns a\n           `.Future` which can be used for flow control.\n           Only one of ``body`` and ``body_producer`` may\n           be specified.  ``body_producer`` is not supported on\n           ``curl_httpclient``.  When using ``body_producer`` it is recommended\n           to pass a ``Content-Length`` in the headers as otherwise chunked\n           encoding will be used, and many servers do not support chunked\n           encoding on requests.  New in Tornado 4.0\n        :arg string auth_username: Username for HTTP authentication\n        :arg string auth_password: Password for HTTP authentication\n        :arg string auth_mode: Authentication mode; default is \"basic\".\n           Allowed values are implementation-defined; ``curl_httpclient``\n           supports \"basic\" and \"digest\"; ``simple_httpclient`` only supports\n           \"basic\"\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 decompress_response: Request a compressed response from\n           the server and decompress it after downloading.  Default is True.\n           New in Tornado 4.0.\n        :arg bool use_gzip: Deprecated alias for ``decompress_response``\n           since Tornado 4.0.\n        :arg string network_interface: Network interface to use for request.\n           ``curl_httpclient`` only; see note below.\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.  See note below when used with\n           ``curl_httpclient``.\n        :arg string client_key: Filename for client SSL key, if any.  See\n           note below when used with ``curl_httpclient``.\n        :arg string client_cert: Filename for client SSL certificate, if any.\n           See note below when used with ``curl_httpclient``.\n        :arg ssl.SSLContext ssl_options: `ssl.SSLContext` object for use in\n           ``simple_httpclient`` (unsupported by ``curl_httpclient``).\n           Overrides ``validate_cert``, ``ca_certs``, ``client_key``,\n           and ``client_cert``.\n        :arg bool allow_ipv6: Use IPv6 when available?  Default is true.\n        :arg bool expect_100_continue: If true, send the\n           ``Expect: 100-continue`` header and wait for a continue response\n           before sending the request body.  Only supported with\n           simple_httpclient.\n\n        .. note::\n\n            When using ``curl_httpclient`` certain options may be\n            inherited by subsequent fetches because ``pycurl`` does\n            not allow them to be cleanly reset.  This applies to the\n            ``ca_certs``, ``client_key``, ``client_cert``, and\n            ``network_interface`` arguments.  If you use these\n            options, you should pass them on every request (you don't\n            have to always use the same values, but it's not possible\n            to mix requests that specify these options with ones that\n            use the defaults).\n\n        .. versionadded:: 3.1\n           The ``auth_mode`` argument.\n\n        .. versionadded:: 4.0\n           The ``body_producer`` and ``expect_100_continue`` arguments.\n\n        .. versionadded:: 4.2\n           The ``ssl_options`` argument.\n        \"\"\"\n        # Note that some of these attributes go through property setters\n        # defined below.\n        self.headers = headers\n        if if_modified_since:\n            self.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.body = body\n        self.body_producer = body_producer\n        self.auth_username = auth_username\n        self.auth_password = auth_password\n        self.auth_mode = auth_mode\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        if decompress_response is not None:\n            self.decompress_response = decompress_response\n        else:\n            self.decompress_response = use_gzip\n        self.network_interface = network_interface\n        self.streaming_callback = streaming_callback\n        self.header_callback = header_callback\n        self.prepare_curl_callback = 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.ssl_options = ssl_options\n        self.expect_100_continue = expect_100_continue\n        self.start_time = time.time()\n\n    @property\n    def headers(self):\n        return self._headers\n\n    @headers.setter\n    def headers(self, value):\n        if value is None:\n            self._headers = httputil.HTTPHeaders()\n        else:\n            self._headers = value\n\n    @property\n    def body(self):\n        return self._body\n\n    @body.setter\n    def body(self, value):\n        self._body = utf8(value)\n\n    @property\n    def body_producer(self):\n        return self._body_producer\n\n    @body_producer.setter\n    def body_producer(self, value):\n        self._body_producer = stack_context.wrap(value)\n\n    @property\n    def streaming_callback(self):\n        return self._streaming_callback\n\n    @streaming_callback.setter\n    def streaming_callback(self, value):\n        self._streaming_callback = stack_context.wrap(value)\n\n    @property\n    def header_callback(self):\n        return self._header_callback\n\n    @header_callback.setter\n    def header_callback(self, value):\n        self._header_callback = stack_context.wrap(value)\n\n    @property\n    def prepare_curl_callback(self):\n        return self._prepare_curl_callback\n\n    @prepare_curl_callback.setter\n    def prepare_curl_callback(self, value):\n        self._prepare_curl_callback = stack_context.wrap(value)\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\n    * headers: `tornado.httputil.HTTPHeaders` object\n\n    * effective_url: final location of the resource after following any\n      redirects\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, message=self.reason,\n                                       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        self.message = message or httputil.responses.get(code, \"Unknown\")\n        self.response = response\n        super(HTTPError, self).__init__(code, message, response)\n\n    def __str__(self):\n        return \"HTTP %d: %s\" % (self.code, self.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(native_str(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:** `repr()` of `HTTPError` Causes Stack Overflow Due to Cyclic References

**Description:**
When attempting to obtain the representation of an `HTTPError` instance, calling `repr(e)` leads to excessively verbose output or a stack overflow. This issue arises because there is a cyclic reference between the `HTTPError` instance and its associated `HTTPResponse`, causing the default `__repr__` implementation to recurse indefinitely, especially noticeable in environments like PyPy that lack robust recursion detection.

**Example Code:**
```python
try:
    response = HTTPResponse(HTTPRequest('http://example.com/'), 403)
    response.rethrow()
except HTTPError as e:
    print(repr(e))  # Causes stack overflow or overly verbose output
```

**Expected Behavior:**
Calling `repr(e)` on an `HTTPError` instance should return a concise and clear string representation similar to `str(e)`, such as `"HTTP 403: Forbidden"`, without causing a stack overflow or including unnecessary cyclic references.

**Actual Behavior:**
Instead of a simple string, `repr(e)` produces a detailed and recursive string that includes references to the `HTTPResponse` and possibly other nested objects, leading to stack overflow errors or overly long representations like:
```
HTTPError(403, 'Forbidden', HTTPResponse(..., error=HTTPError(...), ...))
```
[/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 of libcurl is
7.21.1, and the minimum version of pycurl is 7.18.2.  It is highly
recommended that your ``libcurl`` installation is built with
asynchronous DNS resolver (threaded or c-ares), otherwise you may
encounter various problems with request timeouts (for more
information, see
http://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTCONNECTTIMEOUTMS
and comments in curl_httpclient.py).

To select ``curl_httpclient``, call `AsyncHTTPClient.configure` at startup::

    AsyncHTTPClient.configure("tornado.curl_httpclient.CurlAsyncHTTPClient")
"""

from __future__ import absolute_import, division, print_function, with_statement

import functools
import time
import weakref

from tornado.concurrent import TracebackFuture
from tornado.escape import utf8, native_str
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:
            # HTTPError is raised for non-200 responses; the response
            # can be found in e.response.
            print("Error: " + str(e))
        except Exception as e:
            # Other errors are possible, such as IOError.
            print("Error: " + str(e))
        http_client.close()
    """
    def __init__(self, async_client_class=None, **kwargs):
        self._io_loop = IOLoop(make_current=False)
        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` unless
        the ``raise_error`` keyword argument is set to False.
        """
        response = self._io_loop.run_sync(functools.partial(
            self._async_client.fetch, request, **kwargs))
        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.  Unless
    ``force_instance=True`` is used, no arguments other than
    ``io_loop`` should be passed to the `AsyncHTTPClient` constructor.
    The implementation subclass as well as arguments to its
    constructor can be set with the static method `configure()`

    All `AsyncHTTPClient` implementations support a ``defaults``
    keyword argument, which can be used to set default values for
    `HTTPRequest` attributes.  For example::

        AsyncHTTPClient.configure(
            None, defaults=dict(user_agent="MyUserAgent"))
        # or with force_instance:
        client = AsyncHTTPClient(force_instance=True,
            defaults=dict(user_agent="MyUserAgent"))

    .. versionchanged:: 4.1
       The ``io_loop`` argument is deprecated.
    """
    @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 force_instance:
            instance_cache = None
        else:
            instance_cache = cls._async_clients()
        if instance_cache is not None and io_loop in instance_cache:
            return instance_cache[io_loop]
        instance = super(AsyncHTTPClient, cls).__new__(cls, io_loop=io_loop,
                                                       **kwargs)
        # Make sure the instance knows which cache to remove itself from.
        # It can't simply call _async_clients() because we may be in
        # __new__(AsyncHTTPClient) but instance.__class__ may be
        # SimpleAsyncHTTPClient.
        instance._instance_cache = instance_cache
        if instance_cache is not None:
            instance_cache[instance.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)
        self._closed = False

    def close(self):
        """Destroys this HTTP client, freeing any file descriptors used.

        This method is **not needed in normal use** due to the way
        that `AsyncHTTPClient` objects are transparently reused.
        ``close()`` is generally only necessary when either the
        `.IOLoop` is also being closed, or the ``force_instance=True``
        argument was used when creating the `AsyncHTTPClient`.

        No other methods may be called on the `AsyncHTTPClient` after
        ``close()``.

        """
        if self._closed:
            return
        self._closed = True
        if self._instance_cache is not None:
            if self._instance_cache.get(self.io_loop) is not self:
                raise RuntimeError("inconsistent AsyncHTTPClient cache")
            del self._instance_cache[self.io_loop]

    def fetch(self, request, callback=None, raise_error=True, **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`. By default, the ``Future`` will raise an
        `HTTPError` if the request returned a non-200 response code
        (other errors may also be raised if the server could not be
        contacted). Instead, if ``raise_error`` is set to False, the
        response will always be returned regardless of the 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 self._closed:
            raise RuntimeError("fetch() called on closed AsyncHTTPClient")
        if not isinstance(request, HTTPRequest):
            request = HTTPRequest(url=request, **kwargs)
        else:
            if kwargs:
                raise ValueError("kwargs can't be used if request is an HTTPRequest object")
        # 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 = TracebackFuture()
        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 raise_error and 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,
        decompress_response=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, auth_mode=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, body_producer=None,
                 expect_100_continue=False, decompress_response=None,
                 ssl_options=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 body: HTTP request body as a string (byte or unicode; if unicode
           the utf-8 encoding will be used)
        :arg body_producer: Callable used for lazy/asynchronous request bodies.
           It is called with one argument, a ``write`` function, and should
           return a `.Future`.  It should call the write function with new
           data as it becomes available.  The write function returns a
           `.Future` which can be used for flow control.
           Only one of ``body`` and ``body_producer`` may
           be specified.  ``body_producer`` is not supported on
           ``curl_httpclient``.  When using ``body_producer`` it is recommended
           to pass a ``Content-Length`` in the headers as otherwise chunked
           encoding will be used, and many servers do not support chunked
           encoding on requests.  New in Tornado 4.0
        :arg string auth_username: Username for HTTP authentication
        :arg string auth_password: Password for HTTP authentication
        :arg string auth_mode: Authentication mode; default is "basic".
           Allowed values are implementation-defined; ``curl_httpclient``
           supports "basic" and "digest"; ``simple_httpclient`` only supports
           "basic"
        :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 decompress_response: Request a compressed response from
           the server and decompress it after downloading.  Default is True.
           New in Tornado 4.0.
        :arg bool use_gzip: Deprecated alias for ``decompress_response``
           since Tornado 4.0.
        :arg string network_interface: Network interface to use for request.
           ``curl_httpclient`` only; see note below.
        :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.  See note below when used with
           ``curl_httpclient``.
        :arg string client_key: Filename for client SSL key, if any.  See
           note below when used with ``curl_httpclient``.
        :arg string client_cert: Filename for client SSL certificate, if any.
           See note below when used with ``curl_httpclient``.
        :arg ssl.SSLContext ssl_options: `ssl.SSLContext` object for use in
           ``simple_httpclient`` (unsupported by ``curl_httpclient``).
           Overrides ``validate_cert``, ``ca_certs``, ``client_key``,
           and ``client_cert``.
        :arg bool allow_ipv6: Use IPv6 when available?  Default is true.
        :arg bool expect_100_continue: If true, send the
           ``Expect: 100-continue`` header and wait for a continue response
           before sending the request body.  Only supported with
           simple_httpclient.

        .. note::

            When using ``curl_httpclient`` certain options may be
            inherited by subsequent fetches because ``pycurl`` does
            not allow them to be cleanly reset.  This applies to the
            ``ca_certs``, ``client_key``, ``client_cert``, and
            ``network_interface`` arguments.  If you use these
            options, you should pass them on every request (you don't
            have to always use the same values, but it's not possible
            to mix requests that specify these options with ones that
            use the defaults).

        .. versionadded:: 3.1
           The ``auth_mode`` argument.

        .. versionadded:: 4.0
           The ``body_producer`` and ``expect_100_continue`` arguments.

        .. versionadded:: 4.2
           The ``ssl_options`` argument.
        """
        # Note that some of these attributes go through property setters
        # defined below.
        self.headers = headers
        if if_modified_since:
            self.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.body = body
        self.body_producer = body_producer
        self.auth_username = auth_username
        self.auth_password = auth_password
        self.auth_mode = auth_mode
        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
        if decompress_response is not None:
            self.decompress_response = decompress_response
        else:
            self.decompress_response = use_gzip
        self.network_interface = network_interface
        self.streaming_callback = streaming_callback
        self.header_callback = header_callback
        self.prepare_curl_callback = 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.ssl_options = ssl_options
        self.expect_100_continue = expect_100_continue
        self.start_time = time.time()

    @property
    def headers(self):
        return self._headers

    @headers.setter
    def headers(self, value):
        if value is None:
            self._headers = httputil.HTTPHeaders()
        else:
            self._headers = value

    @property
    def body(self):
        return self._body

    @body.setter
    def body(self, value):
        self._body = utf8(value)

    @property
    def body_producer(self):
        return self._body_producer

    @body_producer.setter
    def body_producer(self, value):
        self._body_producer = stack_context.wrap(value)

    @property
    def streaming_callback(self):
        return self._streaming_callback

    @streaming_callback.setter
    def streaming_callback(self, value):
        self._streaming_callback = stack_context.wrap(value)

    @property
    def header_callback(self):
        return self._header_callback

    @header_callback.setter
    def header_callback(self, value):
        self._header_callback = stack_context.wrap(value)

    @property
    def prepare_curl_callback(self):
        return self._prepare_curl_callback

    @prepare_curl_callback.setter
    def prepare_curl_callback(self, value):
        self._prepare_curl_callback = stack_context.wrap(value)


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

    * headers: `tornado.httputil.HTTPHeaders` object

    * effective_url: final location of the resource after following any
      redirects

    * 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):
        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:
            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, message=self.reason,
                                       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
        self.message = message or httputil.responses.get(code, "Unknown")
        self.response = response
        super(HTTPError, self).__init__(code, message, response)

    def __str__(self):
        return "HTTP %d: %s" % (self.code, self.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(native_str(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