236
     ˆÆÅÄ‚h@ÃÂÁ ¤    ‹      µ“QkÛ0Çßû)nzØ[}	ma¬²FgH“�º”Qö [Š-f[Fº&Ë>ýäØ¥-´ŒA÷¤“ôÓýï’ø‡t•dß×3ø–]/`}ûu1O€�"Þ�%ˆi–çÑdŠ8[2`Q÷q¿ßGû³Èº³VÔÔçèÉ™‚"EŠ‰Þ¯‰à•–*ŒÀÉP­Å¼UúØ- } o”ç•Þ™Bû>&Ý`£ëã0½ÀÖ*=§Ÿ&èòTÎì´ó‡ìAGAž[uèë˜þ7Í�úhLæGé>t‚S;Y›²�ÙŽ	nš¼+b†¦°­Ç¼–íÏ¨4[²¦˜ÝÏ“Õ�À!UýiÁ%TNocö%‰——«8eb)ÍQ¾Î\æŠ‰…ô�Ufk´z¾àóûí„éÀ¤ÚÎtdlû„b°øÜjakßÉàõ"8¨Üsj@Ôßº!‹ÍX_mfË,�o†–¨>ÅSuïsƒL¬¥Ó-Ajœ.(l�µ�mî»ËÇ)Œå;SVÄÀ)¼JþKgp|3�¯”ãð[þ í§¸��       ˆÆÅÄ‚h@ÃÂÁ ¤    ‹      µ“QkÛ0Çßû)nzØ[}é`¬²JgH“�z”2ú [Š-f[Fº&K?ýäØ£´ŒB÷¤“ôÓýï’ø‡t•dwë|Ë®°þþu1O€�"ÞNÄ4K‡�óèl‚8[2`Q÷q¿ßGûid]‰Ù+jêsôäLA‘"ÅÄ	ï×Ä	ðJKFàd¨ÖbÞ*ýìÐ>�7J‡s�JïL¡}“n°Ñ�u‡qø<ÅÖ*}6N'ôù*gvÚyŽCö ‡£ Ï­:ôuLþ›fH}4&ó£t:Á©‚�¬MÙÆŒlÇ7M	Þ1CSØÖc^ËögTš-YSÌ~Ì“Õ}à�ªþ´à*§·1»LâåÅ*N™XÊFs”/3×�¹bb!=Ac•Ù­^…oøÆ<¾ž0˜TûÂ™ŽŒmŸPŸ[-lí;¼~
*÷œõ¯nÈâ¯f¬¯6³e–Î7CKTŸâ©º÷¹A&ÖÒé– 5N¶GƒƒÚÇ6÷ÝÅŸ)Œå;SVÄÀ)¼H¾¥38¾ŽÇWÊqø-¿]F�    C   ˆl–Ðz¾”Šj"Tâ‚À½q¶”Å£�þ[‘ö–q²‘¸Ý�Jð?ŸÀ„/¶çÅ_ŠI|¥�+Wé3ž¯ÄÃ ?÷     #
# Copyright 2009 Facebook
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

"""Non-blocking HTTP client implementation using pycurl."""

import collections
import functools
import logging
import pycurl
import re
import threading
import time
from io import BytesIO

from tornado import httputil
from tornado import ioloop

from tornado.escape import utf8, native_str
from tornado.httpclient import (
    HTTPRequest,
    HTTPResponse,
    HTTPError,
    AsyncHTTPClient,
    main,
)
from tornado.log import app_log

from typing import Dict, Any, Callable, Union, Optional
import typing

if typing.TYPE_CHECKING:
    from typing import Deque, Tuple  # noqa: F401

curl_log = logging.getLogger("tornado.curl_httpclient")

CR_OR_LF_RE = re.compile(b"\r|\n")


class CurlAsyncHTTPClient(AsyncHTTPClient):
    def initialize(  # type: ignore
        self, max_clients: int = 10, defaults: Optional[Dict[str, Any]] = None
    ) -> None:
        super().initialize(defaults=defaults)
        # Typeshed is incomplete for CurlMulti, so just use Any for now.
        self._multi = pycurl.CurlMulti()  # type: Any
        self._multi.setopt(pycurl.M_TIMERFUNCTION, self._set_timeout)
        self._multi.setopt(pycurl.M_SOCKETFUNCTION, self._handle_socket)
        self._curls = [self._curl_create() for i in range(max_clients)]
        self._free_list = self._curls[:]
        self._requests = (
            collections.deque()
        )  # type: Deque[Tuple[HTTPRequest, Callable[[HTTPResponse], None], float]]
        self._fds = {}  # type: Dict[int, int]
        self._timeout = None  # type: Optional[object]

        # Work around a bug in libcurl 7.29.0: Some fields in the curl
        # multi object are initialized lazily, and its destructor will
        # segfault if it is destroyed without having been used.  Add
        # and remove a dummy handle to make sure everything is
        # initialized.
        dummy_curl_handle = pycurl.Curl()
        self._multi.add_handle(dummy_curl_handle)
        self._multi.remove_handle(dummy_curl_handle)

    def close(self) -> None:
        if self._timeout is not None:
            self.io_loop.remove_timeout(self._timeout)
        for curl in self._curls:
            curl.close()
        self._multi.close()
        super().close()

        # Set below properties to None to reduce the reference count of current
        # instance, because those properties hold some methods of current
        # instance that will case circular reference.
        self._multi = None

    def fetch_impl(
        self, request: HTTPRequest, callback: Callable[[HTTPResponse], 