srhkddlmZddlZddlZddlZddlZddlZddlZddlZddl m Z ddl m Z ddlmZddlmZddlmZdd lmZmZmZmZmZmZmZmZdd lmZdd lmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+dd l,m-Z-dd l.m/Z/ddl0m1Z1ddl2m3Z3m4Z4ddl5m6Z6ddl7m8Z8ddl9m:Z:m;Z;mZ>m?Z?ddl=m@ZAddl=mBZBddlCmDZDejErddlFZFddlGmHZHddlmIZImJZJejKeLZMejNeZdddd1d1eddddd1d1f dSdGZdS)THTTPConnectionPoola Thread-safe connection pool for one host. :param host: Host used for this HTTP Connection (e.g. "localhost"), passed into :class:`http.client.HTTPConnection`. :param port: Port used for this HTTP Connection (None is equivalent to 80), passed into :class:`http.client.HTTPConnection`. :param timeout: Socket timeout in seconds for each individual connection. This can be a float or integer, which sets the timeout for the HTTP request, or an instance of :class:`urllib3.util.Timeout` which gives you more fine-grained control over request timeouts. After the constructor has been parsed, this is always a `urllib3.util.Timeout` object. :param maxsize: Number of connections to save that can be reused. More than 1 is useful in multithreaded situations. If ``block`` is set to False, more connections will be created but they will not be saved once they've been used. :param block: If set to True, no more than ``maxsize`` connections will be used at a time. When no free connections are available, the call will block until a connection has been released. This is a useful side effect for particular multithreaded situations where one does not want to use more than maxsize connections per host to prevent flooding. :param headers: Headers to include with all requests, unless other headers are given explicitly. :param retries: Retry configuration to use by default with requests in this pool. :param _proxy: Parsed proxy URL, should not be used directly, instead, see :class:`urllib3.ProxyManager` :param _proxy_headers: A dictionary with proxy headers, should not be used directly, instead, see :class:`urllib3.ProxyManager` :param \**conn_kw: Additional parameters are used to create fresh :class:`urllib3.connection.HTTPConnection`, :class:`urllib3.connection.HTTPSConnection` instances. httpz4type[BaseHTTPConnection] | type[BaseHTTPSConnection] ConnectionClsNrFr7r8r9r:r_TYPE_TIMEOUT | Nonemaxsizeintblockboolheaderstyping.Mapping[str, str] | NoneretriesRetry | bool | int | None_proxy Url | None_proxy_headers _proxy_configProxyConfig | Noneconn_kw typing.Anyc  t|||tj||t|tst j|}| t j}||_||_ | ||_ ||_ ||_ | pi|_| |_t!|D]} |j dd|_d|_| |_|j r9|jdg|j |jd<|j|jd<|j } t-j|t0| dS)Nrsocket_optionsproxy proxy_config)r4rDr isinstancer* from_floatr&DEFAULTrrprdpoolrlr{ proxy_headersr|rangeputnum_connections num_requestsrw setdefaultweakreffinalize_close_pool_connections)rBr7r9rrjrlrnrprrrtrurw_rs rCrDzHTTPConnectionPool.__init__sQ dD111g...'7++ 2(11G ?mG  8< g8N8N   +1r)w  A IMM$     ! : = L # #$4b 9 9 9$(JDL !+/+Pool reached maximum size and no more connections are allowed.zLConnection pool is full, discarding connection: %s. Connection pool size: %s) rrrrbFullrXrlrrwarningr7qsizers rC _put_connzHTTPConnectionPool._put_conn&s 9   d% 000!   :   !JJLLL: 'X   bIIOO%% &   JJLLLLL  s' B*A4B*)B*cdS)U Called right before a request is made, after the socket is created. NrNrs rC_validate_connz!HTTPConnectionPool._validate_connQr\rEcdSrMrNrs rC_prepare_proxyz!HTTPConnectionPool._prepare_proxyVs rE _TYPE_TIMEOUTr*c|tur|jSt|tr|St j|S)z:Helper that always returns a :class:`urllib3.util.Timeout`)r(rcloner}r*r~)rBrs rC _get_timeoutzHTTPConnectionPool._get_timeoutZsU & & &<%%'' ' gw ' ' /==?? "%g.. .rEerr&BaseSSLError | OSError | SocketTimeouturl timeout_valuect|trt||d|d|t|dr$|jt vrt||d|d|dSdS)zAIs the error actually a timeout? Will raise a ReadTimeout or passRead timed out. (read timeout=rGerrnoN)r} SocketTimeoutrhasattrr_blocking_errnos)rBrrrs rC_raise_timeoutz!HTTPConnectionPool._raise_timeoutfs c= ) ) "cLMLLL  3  SY2B%B%B"cLMLLL   %B%BrETmethodbody_TYPE_BODY | None Retry | Nonechunked response_connpreload_contentdecode_contentenforce_content_lengthr!c *|xjdz c_||} | tj| j|_ ||n7#ttf$r#}| |||jd}~wwxYwn#ttttttf$r}|}t!|ttfrt|}t!|ttttfr*|r(|jr!|jst'||jj}|d}~wwxYw ||||||| | | nM#t,$rYnAt$r5}|jt.jkr|jt.jkrYd}~nd}~wwxYw| j}|js"|dkrt9||d|d||_ |}n2#ttf$r}| |||d}~wwxYw||_| |_||_ tB"d|j|j#|j$|||j%|j&|j' |S) a8 Perform a request on a given urllib connection object taken from our pool. :param conn: a connection from one of our connection pools :param method: HTTP request method (such as GET, POST, PUT, etc.) :param url: The URL to perform the request on. :param body: Data to send in the request body, either :class:`str`, :class:`bytes`, an iterable of :class:`str`/:class:`bytes`, or a file-like object. :param headers: Dictionary of custom headers to send, such as User-Agent, If-None-Match, etc. If None, pool headers are used. If provided, these headers completely replace any pool-specific headers. :param retries: Configure the number of retries to allow before raising a :class:`~urllib3.exceptions.MaxRetryError` exception. Pass ``None`` to retry until you receive a response. Pass a :class:`~urllib3.util.retry.Retry` object for fine-grained control over different types of retries. Pass an integer number to retry connection errors that many times, but no other types of errors. Pass zero to never retry. If ``False``, then retries are disabled and any exception is raised immediately. Also, instead of raising a MaxRetryError on redirects, the redirect response will be returned. :type retries: :class:`~urllib3.util.retry.Retry`, False, or an int. :param timeout: If specified, overrides the default timeout for this one request. It may be a float (in seconds) or an instance of :class:`urllib3.util.Timeout`. :param chunked: If True, urllib3 will send the body using chunked transfer encoding. Otherwise, urllib3 will send the body using the standard content-length form. Defaults to False. :param response_conn: Set this to ``None`` if you will handle releasing the connection or set the connection to have the response release it. :param preload_content: If True, the response's body will be preloaded during construction. :param decode_content: If True, will attempt to decode the body based on the 'content-encoding' header. :param enforce_content_length: Enforce content length checking. Body returned by server must match value of Content-Length header, if present. Otherwise, raise error. rrrrN)rrnrrrrrrrGz%s://%s:%s "%s %s %s" %s %s)(rr start_connectr*resolve_default_timeoutrrrrr rOSErrorrr r'rr}r{has_connected_to_proxyrr6requestr r EPROTOTYPE ECONNRESET read_timeout is_closedr getresponserp _connection_poolrrr7r9version_stringstatuslength_remaining)rBrrrrrnrprrrrrr timeout_objenew_erresponses rC _make_requestz HTTPConnectionPool._make_requestys!\ Q''00 !!###6{7RSS   ##D))))!<0   ##s$,#OOO *            !E!l,<=>> $  !3\8L D D J D/3/J D*%1BCCK# *  LL /-'=         D    w%***qw%:J/J/J  #/ ~ ( q  &#O OOO(DL ''))HHg&      A3l  K K K   #, ) K I I    # O  % smA0/B(0B$BB$$B(()EBEEE99 G G+F>>G7H H;H66H;cT|jdS|jdc}|_t|dSr[)rr)rBold_pools rCrXzHTTPConnectionPool.close.s7 9  F"i$) )))))rEc:|drdSt|^}}}}}|pd}|t||}|jr|st j|}n!|js|t j|krd}|||f|j|j|jfkS)zj Check if the given ``url`` is a member of the same host as this connection pool. /TrgNr>) startswithr.r-r9rrr6r7)rBrr6rr7r9s rC is_same_hostzHTTPConnectionPool.is_same_host:s >>#   4%.cNN!4!6  "4777D 9 T !%f--DD t~'9&'A'AAADd# TY 'JJJrEredirectassert_same_host pool_timeout release_conn bool | Nonebody_pos_TYPE_BODY_POSITION | None response_kwc  t|}|j}||j}t|tst j|||j}| | } |r&||st|||| drtt|}nt|j }d}| }t|j|j|}|s.|}||jd}d}t'|| } ||}|| }|j|_|jg|re|jr^ ||nG#t4t6t8f$r-}|||jj |jd}~wwxYw| s|nd}|j|||f|||| ||| |d|}d}nL#t>$rd}d}t@tBt6tDt4tFtHtJf$r}d}|}t|t4tHfrtG|}t|t6tLt@tFtBfr+|r)|jr"|j'stQ||jj}n,t|t6tBfrtEd |}|)||||tUj+d  }|,|}Yd}~nd}~wwxYw|s|r|-d}d}|r|.|n9#|s|r|-d}d}|r|.|wwxYw|s9t^0d ||||j1|||||||f|| | | | | |d |S|o|2}|r|j3dkr%d}d}ti|5} |)||||}n.#tl$r!|j7r|8|cYSwxYw|8|9|t^:d|||j1||||f||||| | | | | |d |Stw|j<d}|=||j3|r |)||||}n.#tl$r!|j>r|8|cYSwxYw|8|,|t^:d||j1||||f||||| | | | | |d |S|S)a Get a connection from the pool and perform an HTTP request. This is the lowest level call for making a request, so you'll need to specify all the raw details. .. note:: More commonly, it's appropriate to use a convenience method such as :meth:`request`. .. note:: `release_conn` will only behave as expected if `preload_content=False` because we want to make `preload_content=False` the default behaviour someday soon without breaking backwards compatibility. :param method: HTTP request method (such as GET, POST, PUT, etc.) :param url: The URL to perform the request on. :param body: Data to send in the request body, either :class:`str`, :class:`bytes`, an iterable of :class:`str`/:class:`bytes`, or a file-like object. :param headers: Dictionary of custom headers to send, such as User-Agent, If-None-Match, etc. If None, pool headers are used. If provided, these headers completely replace any pool-specific headers. :param retries: Configure the number of retries to allow before raising a :class:`~urllib3.exceptions.MaxRetryError` exception. If ``None`` (default) will retry 3 times, see ``Retry.DEFAULT``. Pass a :class:`~urllib3.util.retry.Retry` object for fine-grained control over different types of retries. Pass an integer number to retry connection errors that many times, but no other types of errors. Pass zero to never retry. If ``False``, then retries are disabled and any exception is raised immediately. Also, instead of raising a MaxRetryError on redirects, the redirect response will be returned. :type retries: :class:`~urllib3.util.retry.Retry`, False, or an int. :param redirect: If True, automatically handle redirects (status codes 301, 302, 303, 307, 308). Each redirect counts as a retry. Disabling retries will disable redirect, too. :param assert_same_host: If ``True``, will make sure that the host of the pool requests is consistent else will raise HostChangedError. When ``False``, you can use the pool on an HTTP proxy and request foreign hosts. :param timeout: If specified, overrides the default timeout for this one request. It may be a float (in seconds) or an instance of :class:`urllib3.util.Timeout`. :param pool_timeout: If set and the pool is set to block=True, then this method will block for ``pool_timeout`` seconds and raise EmptyPoolError if no connection is available within the time period. :param bool preload_content: If True, the response's body will be preloaded into memory. :param bool decode_content: If True, will attempt to decode the body based on the 'content-encoding' header. :param release_conn: If False, then the urlopen call will not release the connection back into the pool once a response is received (but will release if you read the entire contents of the response such as when `preload_content=True`). This is useful if you're not preloading the response's content immediately. You will need to call ``r.release_conn()`` on the response ``r`` to return the connection back into the pool. If None, it takes the value of ``preload_content`` which defaults to ``True``. :param bool chunked: If True, urllib3 will send the body using chunked transfer encoding. Otherwise, urllib3 will send the body using the standard content-length form. Defaults to False. :param int body_pos: Position to seek to in file-like body in the event of a retry or redirect. Typically this won't need to be set because urllib3 will auto-populate the value when needed. N)rdefaultrFrr)rrrnrrprrrTzConnection aborted.)errorr _stacktracez1Retrying (%r) after connection broken by '%r': %s)rrrrrrri/GET)rrzRedirecting %s -> %s) rprrrrrrrrrz Retry-Afterz Retry: %s)?r.r6rnr}r&from_intrprrrr/r,rr#r{r|copyupdaterr%rrrrrrr rrrrrr rrrr'rrrr incrementsysexc_infosleeprXrrrurlopenget_redirect_locationrr _prepare_for_method_changerraise_on_redirect drain_connsleep_for_retryrrmris_retryraise_on_status)rBrrrrnrprrrrrrrrrr parsed_urldestination_schemerrelease_this_connhttp_tunnel_requiredr clean_exitrrrrrredirect_locationhas_retry_afters rCrzHTTPConnectionPool.urlopenPswbs^^ '. ?lG'5)) WnWxVVVG  *L  7D$5$5c$:$: 7"4g66 6 >>#   ),,--CC((C)> J)+=  $ /llnnG NN4- . . . %T844e %++G44K>>,>77D&6DLz%*>%4>%''----$g}=''4:>( )5>DD$M*t) $+ /-    H JJ   J %            $ $ $ J E!l,<=>> $  & !    D D!J D04/J D*%1BCCEG]#;<< D%&;UCC''5#,..QRBS(G MMOOOCCCCCCI$ N )  JJLLLD$(!  %t$$$ )  JJLLLD$(!  %t$$$$  %   KKCWcSV    4<  ))! /- &%I)G)G)I)I # #%%(11LLNN !++FC(RV+WW    ,'')))     ! ! !  # #H - - - II,c3D E E E4<!   !!1))! /- &x/33MBBCC   FHO_ E E  !++FC(RV+WW    *'')))     ! ! ! MM( # # # IIk3 ' ' '4<   !!1))! /- $s{'AG//FG/G (GG  $G/-M//AL83C;L3.M/3L88M//6N%,Q(Q10Q1T33(UU)r7r8r9r:rrirjrkrlrmrnrorprqrrrsrtrorurvrwrx)r;r1rM)rrr;r1)rrr;r<rr1r;r<)rrr;r*)rrrr8rrir;r<)rr1rr8rr8rrrnrorprrrrrmrrrrmrrmrrmr;r!r])rr8r;rm) rr8rr8rrrnrorprqrrmrrmrrrr:rrrrmrrrrmrrmrrxr;r!)rIr^r_r`r6rrhrar(rDrrrrrrrrrXrrrNrErCrfrfxs11fFJXMXXXX  (837-1!:>,0;>;>;>;>;>z($($($($($(L))))V         / / / /0#'37 $!137 $#'+sssssj * * * *KKKK4#'37-1!%!1#'$(/3 $#ppppppprErfc|eZdZUdZdZeZded<dedddddddddddddddddfd4fd, Z d5d/Z d6d1Z d7fd3 Z xZ S)8HTTPSConnectionPoola Same as :class:`.HTTPConnectionPool`, but HTTPS. :class:`.HTTPSConnection` uses one of ``assert_fingerprint``, ``assert_hostname`` and ``host`` in this order to verify connections. If ``assert_hostname`` is False, no verification is done. The ``key_file``, ``cert_file``, ``cert_reqs``, ``ca_certs``, ``ca_cert_dir``, ``ssl_version``, ``key_password`` are only used if :mod:`ssl` is available and are fed into :meth:`urllib3.util.ssl_wrap_socket` to upgrade the connection socket into an SSL socket. httpsztype[BaseHTTPSConnection]rhNrFr7r8r9r:rrirjrkrlrmrnrorprqrrrsrtkey_filer5 cert_file cert_reqsint | str | None key_passwordca_certs ssl_versionssl_minimum_versionssl.TLSVersion | Nonessl_maximum_versionassert_hostname"str | typing.Literal[False] | Noneassert_fingerprint ca_cert_dirrwrxr;r<c tj||||||||| f i|| |_| |_| |_| |_||_||_||_||_ ||_ ||_ ||_ dSrM) superrDrrrrrrrrr r r )rBr7r9rrjrlrnrprrrtrrrrrrrr r r rrw __class__s rCrDzHTTPSConnectionPool.__init__s0            ! ""(  &&#6 #6 ."4rErrc|jr|jjdkrd}nd}|||j|j|j|dS)z5Establishes a tunnel connection through HTTP CONNECT.rrg)r6r7r9rnN)r{r6 set_tunnelrAr9rconnect)rBr tunnel_schemes rCrz"HTTPSConnectionPool._prepare_proxysi : #$*+w66#MM"M  "&    rEr2c|xjdz c_td|j|j|jpd|jr|jt urtd|j}|j}|j$|jj|jj}|jj}|jd|||j j |j |j |j |j|j|j|j|j|j|j|jd|jS)zL Return a fresh :class:`urllib3.connection.HTTPConnection`. rz)Starting new HTTPS connection (%d): %s:%s443zCCan't connect to HTTPS URL because the SSL module is not available.N)r7r9rrrrrrrr r rrr rN)rrrr7r9rhr ImportErrorr{rrrrrrrrr r rrr rw)rB actual_host actual_ports rCrzHTTPSConnectionPool._new_conns! ! 7  I I     ! T%7?%J%JU  9 i : !djo&A*/K*/K!t! L0n]*n]( 0#6( $ 8 $ 8  l   rEr1ct||jr||js,|js't jd|jdtdSdSdS)rz0Unverified HTTPS request is being made to host 'z'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#tls-warningsN) rrrr is_verifiedproxy_is_verifiedwarningswarnr7r)rBrrs rCrz"HTTPSConnectionPool._validate_conn=s t$$$ >  LLNNN (>  M$ty$$$ '         rE),r7r8r9r:rrirjrkrlrmrnrorprqrrrsrtrorr5rr5rrrr5rr5rrrr r r r r r r5rr5rwrxr;r<)rrr;r<)r;r2r)rIr^r_r`r6rrhrar(rDrrr __classcell__)rs@rCrrs  F/>M>>>>  (837-1!:># $&*#'#(,5959>B)-"&+/5/5/5/5/5/5/5b    ' ' ' ' RrErrr8kwrxr;c t|^}}}}}|pd}|ptj|d}|dkrt|fd|i|St |fd|i|S)a Given a url, return an :class:`.ConnectionPool` instance of its host. This is a shortcut for not having to parse out the scheme, host, and port of the url before creating an :class:`.ConnectionPool` instance. :param url: Absolute URL string that must include the scheme. Port is optional. :param \**kw: Passes additional parameters to the constructor of the appropriate :class:`.ConnectionPool`. Useful for specifying things like timeout, maxsize, headers, etc. Example:: >>> conn = connection_from_url('http://google.com/') >>> r = conn.request('GET', '/') rgPrr9)r.rrrrf)rr!r6rr7r9s rCconnection_from_urlr$Ts|(!*#FAtTA  vF  1>%fb11D "499d9b999!$88T8R888rEr7r<r6r5cdSrMrNr7r6s rCr-r-qs=@SrEcdSrMrNr&s rCr-r-us;>3rEct||}|r4|dr|dr |dd}|S)z? Normalize hosts for comparisons and use with sockets. []r)r?rendswithr&s rCr-r-ysU $ ' 'D $$s););AbDz KrEr(HTTPConnectionPool | HTTPSConnectionPoolpathcPt|j|j|j|jS)zZReturns the URL from a given connection pool. This is mainly used for testing and logging.)r6r7r9r.)r+r6r7r9r)rr.s rC_url_from_poolr0s$ dk   M M M QQrEqueue.LifoQueue[typing.Any]c |d}|r|-#tj$rYdSwxYw)z2Drains a queue of connections and closes each one.TFrN)rrXrbr)rrs rCrrs_  88%8((D    ;     s.0AA)rr8r!rxr;rf)r7r<r6r5r;r<)r7r8r6r5r;r8)r7r5r6r5r;r5rM)rr-r.r5r;r8)rr1r;r<)[ __future__rrloggingrbrtypingrrsocketrrtypesr_base_connectionr _collectionsr _request_methodsr connectionr r r rrrrrr exceptionsrrrrrrrrrrrrr rr!util.connectionr" util.proxyr# util.requestr$r% util.retryr&util.ssl_match_hostnamer' util.timeoutr(r)r*util.urlr+r,r-r?r. util.utilr/ TYPE_CHECKINGssltyping_extensionsr0r1r2 getLoggerrIrUnionfloatrr4EAGAIN EWOULDBLOCKrrfrr$overloadr0rrNrErCrNsG""""""  ++++++((((((((((((,,,,,,                    988888'&&&&&222222777777@@@@@@@@555555BBBBBBBBBB))))))))777777 JJJJ&&&&&&IIIIIIIIg!! We]D@A . . . . . . . . dL%"34H H H H H H H H VNNNNN,NNNb9999:@@@@>>>>&HLRRRRR      rE