U e@sddlmZddlZddlZddlZddlZddlZddlZddlZddl Z ddl m Z ddl m Z ddl mZddlmZejrddlmZdd lmZdd lmZdd lmZdd lmZdd lmZddlmZmZm Z ddl!m"Z"ddl#m$Z$zddl%Z%e%j&Z'Wn.e(e)fk r6dZ%Gddde*Z'YnXddl+m,Z,ddl+m-Z-ddl+m.Z.ddl/m0Z0ddl1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7ddl8m9Z9m:Z:m;Z;mZ>ddlm?Z@ddlmAZAmBZBmCZCmDZDmEZEddlFmGZGmHZHddlImJZJeKZKeLZLeMeNZOddd ZPeQd!d"dZReSd#ZTeUed$ZVGd%d&d&e Z Gd'd(d(e ZWGd)d*d*ejXZYd+d,d-d.d.d/d/d0d0d0d0d0d1d2d0d0d3d4d*d5d6d7ZZdLd8d9d4d:d;dd0d?d@dAdBZ\d9dCdDdEZ]GdFdGdGZ^e%se^ZWeWZ_dMdHd0d9dIdJdKZ`dS)N) annotationsN)HTTPConnection) HTTPException)ResponseNotReadytimeout)Literal HTTPResponse)_TYPE_PEER_CERT_RET_DICT) SSLTransport)HTTPHeaderDict)assert_header_parsing)_DEFAULT_TIMEOUT _TYPE_TIMEOUTTimeout)to_str) wait_for_readc@s eZdZdS) BaseSSLErrorN)__name__ __module__ __qualname__rr@/opt/hc_python/lib/python3.8/site-packages/urllib3/connection.pyr$sr) _TYPE_BODY) ProxyConfig)_ResponseOptions __version__)ConnectTimeoutErrorHeaderParsingErrorNameResolutionErrorNewConnectionError ProxyErrorSystemTimeWarning) SKIP_HEADERSKIPPABLE_HEADERS connectionssl_)body_to_chunks)assert_fingerprint)create_urllib3_context is_ipaddressresolve_cert_reqsresolve_ssl_versionssl_wrap_socket)CertificateErrormatch_hostname)UrlPihttphttpsiz[^-!#$%&'*+.^_`|~0-9a-zA-Z]auditc s eZdZUdZedZded<ejej dfgZ ded<dZ d ed <d Z d ed <ded<ded<ded<d ed<ded<ded<ded<ded<dQe d de d d ddddddd d!d"d#d$ fd%d&Zedd'd(d)Zejdd#d*d+d)Zd,d'd-d.ZdRddd/dd#d0fd1d2 Zd#d'd3d4Zed d'd5d6Zed d'd7d8Zed d'd9d:Zed d'd;d<Zd#d'fd=d> ZdSddd d d#d?fd@dA Zddd#dBfdCdD ZdTddEdEdEdFdddGd/d d d d d#dH dIdJZdUdddGd/d#dKdLdMZdNd'fdOdP ZZS)Vra Based on :class:`http.client.HTTPConnection` but provides an extra constructor backwards-compatibility layer between older and newer Pythons. Additional keyword parameters are used to configure attributes of the connection. Accepted parameters include: - ``source_address``: Set the source address for the current connection. - ``socket_options``: Set specific options on the underlying socket. If not specified, then defaults are loaded from ``HTTPConnection.default_socket_options`` which includes disabling Nagle's algorithm (sets TCP_NODELAY to 1) unless the connection is behind a proxy. For example, if you wish to enable TCP Keep Alive in addition to the defaults, you might pass: .. code-block:: python HTTPConnection.default_socket_options + [ (socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1), ] Or you may want to disable the defaults by passing an empty list (e.g., ``[]``). r6ztyping.ClassVar[int] default_portr z0typing.ClassVar[connection._TYPE_SOCKET_OPTIONS]default_socket_optionsFbool is_verifiedNz bool | Noneproxy_is_verifiedint blocksizetuple[str, int] | Nonesource_addressz&connection._TYPE_SOCKET_OPTIONS | Nonesocket_options_has_connected_to_proxyz_ResponseOptions | None_response_options str | None _tunnel_host int | None _tunnel_port_tunnel_scheme@)rrBr@rCproxy proxy_configstrr&None | connection._TYPE_SOCKET_OPTIONS Url | NoneProxyConfig | NoneNone) hostportrrBr@rCrLrMreturnc sPtj||t|||d||_||_||_d|_d|_d|_ d|_ d|_ dS)N)rSrTrrBr@F) super__init__rresolve_default_timeoutrCrLrMrDrErGrIrJ) selfrSrTrrBr@rCrLrM __class__rrrWs zHTTPConnection.__init__rUcCs |jdS)a Getter method to remove any trailing dots that indicate the hostname is an FQDN. In general, SSL certificates don't include the trailing dot indicating a fully-qualified domain name, and thus, they don't validate properly when checked against a domain name that includes the dot. In addition, some servers may not expect to receive the trailing dot when provided. However, the hostname with trailing dot is critical to DNS resolution; doing a lookup with the trailing dot will properly only resolve the appropriate FQDN, whereas a lookup without a trailing dot will search the system's search domain list. Thus, it's important to keep the original host around for use only in those cases where it's appropriate (i.e., when doing DNS lookup to establish the actual TCP connection across which we're going to send HTTP requests). .) _dns_hostrstriprYrrrrSszHTTPConnection.host)valuerUcCs ||_dS)z Setter for the `host` property. We assume that only urllib3 uses the _dns_host attribute; httplib itself only uses `host`, and it seems reasonable that other libraries follow suit. N)r^)rYrarrrrSs socket.socketc Csz$tj|j|jf|j|j|jd}Wntjk rX}zt |j |||W5d}~XYnrt k r}z"t |d|j d|jd|W5d}~XYn4t k r}zt|d||W5d}~XYnXtrtd||j |j|S)zoEstablish a socket connection and set nodelay settings on it. :return: New socket connection. )rBrCNzConnection to z timed out. (connect timeout=)z&Failed to establish a new connection: zhttp.client.connect)r(create_connectionr^rTrrBrCsocketgaierrorr"rS SocketTimeoutr OSErrorr#_HAS_SYS_AUDITsysr9)rYsockerrr _new_conns4   zHTTPConnection._new_connztyping.Mapping[str, str] | None)rSrTheadersschemerUcs4|dkrtd|dtj|||d||_dS)Nr5z$Invalid proxy scheme for tunneling: z", must be either 'http' or 'https')rTrn) ValueErrorrV set_tunnelrJ)rYrSrTrnrorZrrrqs  zHTTPConnection.set_tunnelcCs:||_|jrd|_|t|j|_|jr6d|_dS)NTF)rmrkrGrD_tunnelr<rLr>r`rrrconnects  zHTTPConnection.connectcCs |jdkSN)rkr`rrr is_closedszHTTPConnection.is_closedcCs|jdkrdSt|jdd S)NFgr)rkrr`rrr is_connecteds zHTTPConnection.is_connectedcCs|jSrt)rDr`rrrhas_connected_to_proxysz%HTTPConnection.has_connected_to_proxycCst|jo|jdkS)zT Return True if a forwarding proxy is configured, else return False N)r<rLrGr`rrrproxy_is_forwarding sz"HTTPConnection.proxy_is_forwardingcsFzt W5d|_d|_d|_d|_d|_d|_d|_d|_XdS)NF) rkr=r>rDrErGrIrJrVcloser`rZrrryszHTTPConnection.close)methodurl skip_hostskip_accept_encodingrUcs<t|}|r(td|d|dtj||||dS)z+Method cannot contain non-token characters z (found at least rc)r|r})_CONTAINS_CONTROL_CHAR_REsearchrpgrouprV putrequest)rYrzr{r|r}matchrZrrr"s zHTTPConnection.putrequest)headervaluesrUcsbtdd|Ds&tj|f|n8t|tkr^dddttD}td|ddS) r~css |]}t|to|tkVqdSrt) isinstancerNr&).0vrrr 8sz+HTTPConnection.putheader..z', 'cSsg|]}t|qSr)rNtitle)rrrrr <sz,HTTPConnection.putheader..z(urllib3.util.SKIP_HEADER only supports ''N) anyrV putheaderrlowerr'joinsortedrp)rYrrZskippable_headersrZrrr6s zHTTPConnection.putheaderT)chunkedpreload_contentdecode_contentenforce_content_lengthz_TYPE_BODY | None) rzr{bodyrnrrrrrUcCs|jdk r|j|jt|||||d|_|dkr8i}tdd|D} d| k} d| k} |j||| | dt|||jd} | j } | j }|rd| kr| d d nRd | krd }nDd| krd }n6d }|dkr| dk rd }| d d n| dt |d| kr | dt |D]\}}| ||q|| dk r| D]N}|sPqBt|t rf|d}|r|dt||fn ||qB|r|ddS)N)request_method request_urlrrrcss|]}t|VqdSrt)rr)rkrrrrgsz)HTTPConnection.request..zaccept-encodingrS)r}r|)rzr@ztransfer-encodingzTransfer-Encodingrzcontent-lengthFTzContent-Lengthz user-agentz User-Agentzutf-8s%x %b s0 )rk settimeoutrrrE frozensetrr*r@chunkscontent_lengthrrN_get_default_user_agentitems endheadersrencodesendlen)rYrzr{rrnrrrr header_keysr}r|Z chunks_and_clrrrrachunkrrrrequestDsh      zHTTPConnection.request)rzr{rrnrUcCs(tjdtdd|j||||dddS)z Alternative to the common request method, which sends the body with chunked encoding and not as one block zHTTPConnection.request_chunked() is deprecated and will be removed in urllib3 v2.1.0. Instead use HTTPConnection.request(..., chunked=True).category stacklevelT)rrnrN)warningswarnDeprecationWarningr)rYrzr{rrnrrrrequest_chunkeds zHTTPConnection.request_chunkedr c s|jdkrt|j}d|_|j|jddlm}t}zt |j Wn@t t fk r}zt jdt||j|ddW5d}~XYnXt|j }||||j|j|j|j|j||j|j|jd }|S)a Get the response from the server. If the HTTPConnection is in the correct state, returns an instance of HTTPResponse or of whatever object is returned by the response_class variable. If a request has not been sent or if a previous response has not be handled, ResponseNotReady is raised. If the HTTP response indicates that the connection should be closed, then it will be closed before the response is returned. When the connection is closed, the underlying socket is closed. Nr r z$Failed to parse headers (url=%s): %sT)exc_info) rrnstatusversionreasonrroriginal_responserrr)rErrkrrresponser rV getresponsermsgr! TypeErrorlogwarning_url_from_connectionrrrrrrrrrr)rYZ resp_optionsr httplib_responsehpernrrZrrrs>     zHTTPConnection.getresponse)N)NNr6)FF)NN)NN) rrr__doc__port_by_schemer:__annotations__re IPPROTO_TCP TCP_NODELAYr;r=r>rrWpropertyrSsetterrmrqrsrurvrwrxryrrrrr __classcell__rrrZrrSst    & ""ercs.eZdZUdZedZdZded<dZded<dZ ded<dZ d ed <dZ ded <dZ d ed <dZ d ed<dZded<d*eddejddddddddddddddddddd dddddddddddddd d d ddddddfddZd+ddddddddd dd d d!Zdd"d#d$Zdd%d&d'd(d)ZZS),HTTPSConnectionz Many of the parameters to this constructor are passed to the underlying SSL socket by means of :py:func:`urllib3.util.ssl_wrap_socket`. r7Nzint | str | None cert_reqsrFca_certs ca_cert_dirNone | str | bytes ca_cert_data ssl_versionrHssl_minimum_versionssl_maximum_versionr+rK)rrBr@rCrLrMrassert_hostnamer+server_hostname ssl_contextrrrrrr cert_filekey_file key_passwordrNrrAr?rOrPrQNone | str | Literal[False]ssl.SSLContext | NonerR)rSrTrrBr@rCrLrMrrr+rrrrrrrrrrrrUc stj||||||||d||_||_||_| |_| |_| |_| |_||_ ||_ ||_ |oft j ||_|oxt j ||_||_| dkr|jdk r|jj} ntd} | |_dS)N)rTrrBr@rCrLrM)rVrWrrrrrrr+rrrospath expanduserrrr verify_moder.r)rYrSrTrrBr@rCrLrMrrr+rrrrrrrrrrrrZrrrWs8   zHTTPSConnection.__init__) rrrrrrr+rrrUc Cstjdtdd|dkr4|jdk r,|jj}ntd}||_||_||_||_ ||_ ||_ |oft j ||_|oxt j ||_| |_dS)zX This method should only be called once, before the connection is used. zHTTPSConnection.set_cert() is deprecated and will be removed in urllib3 v2.1.0. Instead provide the parameters to the HTTPSConnection constructor.rrN)rrrrrr.rrrrrr+rrrrrr) rYrrrrrrr+rrrrrset_cert=s$   zHTTPSConnection.set_certr\cCs8||_}|j}d}|jdk rj|jdkrF||j||_}d}n|jdkrVd|_d|_||j}|j dk rz|j }t j t k}|rtdt dt|d}t||j|j|j|j|j|j|j|j|j|j||j||j|jd}|j |_|j!rd|_"n|j"|_"t#|j$|_|jr4|jdkr4|j"|_dS) NFr7Tr6zSystem time is way off (before z5). This will probably lead to SSL verification errorsr])rkrrrrrrrrrrrr tls_in_tlsrr+)%rmrkrSrGrJ_connect_tls_proxyr>rDrrrdatetimedatetoday RECENT_DATErrr%r_#_ssl_wrap_socket_and_match_hostnamerrrrrrrrrrrrr+rerxr=r<rL)rYrkrr is_time_offZserver_hostname_rm_dotsock_and_verifiedrrrrsfs\       zHTTPSConnection.connectrbz ssl.SSLSocket)hostnamerkrUcCs\tt|j}|j}t||j|j|j|j |j |j |j |||j |jddddd}|j|_|jS)zY Establish a TLS connection to the proxy using the provided SSL context. NF)rrrrrrrrrrr+rrrr)typingcastrrMrrrrrrrrrrr+r=r>re)rYrrkrMrrrrrrs,z"HTTPSConnection._connect_tls_proxy)N) NNNNNNNNN)rrrrrr:rrrrrrrrr+rrr;rWrrsrrrrrZrrsZ         B>")Nrc@s"eZdZUdZded<ded<dS)_WrappedAndVerifiedSocketz[ Wrapped socket and whether the connection is verified after the TLS handshake zssl.SSLSocket | SSLTransportrer<r=N)rrrrrrrrrrs rF)rrbzNone | str | intrHrFrrrr<)rkrrrrrrrrrrrr+rrrrUc Csd}|dkr*d}tt|||t|d}n|}t||_| sT| sT| dksTtjsTtjsZd|_|s|| s|| s||r|t|dr|| | dk r| d}d|kr|d| d}t |r|} t |||||| | | ||d }z| rt|jdd | nX|jtjkrB|jsB| dk rB|}|rd}nt|d dp.d}t|| p<| |t||jtjkpZt| d WStk r|YnXdS) a2Logic for constructing an SSLContext from all TLS parameters, passing that down into ssl_wrap_socket, and then doing certificate verification either via hostname or fingerprint. This function exists to guarantee that both proxies and targets have the same behavior when connecting via TLS. FNT)rrrrload_default_certs[]%) rkkeyfilecertfilerrrrrrr) binary_formhostname_checks_common_name)rer=)r,r/r.rr) IS_PYOPENSSLHAS_NEVER_CHECK_COMMON_NAMEcheck_hostnamehasattrrstriprfindr-r0_assert_fingerprint getpeercertssl CERT_NONEgetattr_match_hostnamer CERT_REQUIREDr< BaseExceptionry)rkrrrrrrrrrrrr+rrrdefault_ssl_contextcontext normalizedssl_sockcertrrrrrs       rz_TYPE_PEER_CERT_RET_DICT | NonerNrR)rasserted_hostnamerrUc Csd|d}t|r|}zt|||Wn8tk r^}ztd||||_W5d}~XYnXdS)Nrz@Certificate did not match expected hostname: %s. Certificate: %s)rr-r2r1rr _peer_cert)rrrstripped_hostnamerlrrrrYs r Exceptionr$)err proxy_schemerUcCs`dtdt|}d|kp0d|kp0d|k}d}td|rJ|dkrJ|nd |}||_|S) N z[^a-z]zwrong version numberzunknown protocolzrecord layer failurez. Your proxy appears to only use HTTP and not HTTPS, try changing your proxy URL to be HTTP. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#https-proxy-error-http-proxyzUnable to connect to proxyr7r~)rresplitrNrr$ __cause__)rrZerror_normalizedZis_likely_http_proxyZhttp_proxy_warningZnew_errrrr_wrap_proxy_errorssr r\cCs dtS)Nzpython-urllib3/rrrrrrsrc@seZdZdZdS)DummyConnectionz-Used to detect a failed ConnectionCls import.N)rrrrrrrrr sr z HTTPConnection | HTTPSConnection)connrrUcCs(t|trdnd}t||j|j|djS)zUReturns the URL from a given connection. This is mainly used for testing and logging.r7r6)rorSrTr)rrr3rSrTr{)rrrorrrrsr)F)N)a __future__rrloggingrr rerjrr http.clientr_HTTPConnectionrrrrg TYPE_CHECKINGrrr Z util.ssl_r Zutil.ssltransportr _collectionsrZ util.responserZ util.timeoutrrrZ util.utilrZ util.waitrrSSLErrorr ImportErrorAttributeErrorrZ_base_connectionrrr_versionr exceptionsr r!r"r#r$r%utilr&r'r(r)Z util.requestr*r+rr,r-r.r/r0Zutil.ssl_match_hostnamer1r2Zutil.urlr3ConnectionErrorBrokenPipeError getLoggerrrrrrcompilerrrir NamedTuplerrrr rr VerifiedHTTPSConnectionrrrrrs                           c2