U fT@sddlmZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddlm ZddlmZddlmZddlmZe jrddlmZdd lmZdd lmZdd lmZdd lmZdd lm Z ddl!m"Z"m#Z#m$Z$ddl%m&Z&ddl'm(Z(zddl)Z)e)j*Z+Wn.e,e-fk rFdZ)Gddde.Z+YnXddl/m0Z0ddl/m1Z1ddl/m2Z2ddl3m4Z4ddl5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;ddlZ>m?Z?m@Z@ddlAmBZBddlmCZDddlmEZEmFZFmGZGmHZHmIZIddlJmKZKmLZLddlMmNZNeOZOePZPeQeRZSddd ZTeUd!d"dZVeWd#ZXeYe d$ZZGd%d&d&eZ Gd'd(d(e Z[Gd)d*d*e j\Z]d+d,d-d.d.d/d/d0d0d0d0d0d1d2d0d0d3d4d*d5d6d7Z^dLd8d9d4d:d;dd0d?d@dAdBZ`d9dCdDdEZaGdFdGdGZbe)sebZ[e[ZcdMdHd0d9dIdJdKZddS)N) annotationsN)HTTPConnection) HTTPException)ResponseNotReadytimeout HTTPResponse)_TYPE_PEER_CERT_RET_DICT) SSLTransport)HTTPHeaderDict)probe)assert_header_parsing)_DEFAULT_TIMEOUT _TYPE_TIMEOUTTimeout)to_str) wait_for_readc@s eZdZdS) BaseSSLErrorN)__name__ __module__ __qualname__rrC/opt/alt/python38/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<dTe d de d d ddddddd d!d"d#d$ fd%d&Zedd'd(d)Zejdd#d*d+d)Zd,d'd-d.ZdUddd/dd#d0fd1d2 Zejd3kr.d#d'd4d5Zd#d'd6d7Zed d'd8d9Zed d'd:d;Zed d'dd?Zd#d'fd@dA ZdVddd d d#dBfdCdD Zddd#dEfdFdG ZdWddHdHdHdIdddJd/d d d d d#dK dLdMZdXdddJd/d#dNdOdPZ dQd'fdRdS Z!Z"S)Yra 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_portrz0typing.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__rZresolve_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_hostrstriprXrrrrSszHTTPConnection.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])rXr`rrrrSs 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)rXsockerrr _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')rTrm) ValueErrorrV set_tunnelrJ)rXrSrTrmrnrYrrrps  zHTTPConnection.set_tunnel) c Cs&tjj}d|jd|jf}|g}|jD]$\}}||d|ddq.|d| d |~|j |j |j d}z|\}}} |tjjkr|td |d | |j|d } t| |krtjd | sq| d krq|jdkrtd| qW5|XdS)NsCONNECT %s:%d HTTP/1.0 asciiz: z zlatin-1 )methodzTunnel connection failed:  rz header line)ru rvrzheader:)r6client_MAXLINErGencoderI_tunnel_headersitemsappendsendjoinresponse_classrj_methodclose _read_status HTTPStatusOKrgstripfpreadlinelen LineTooLong debuglevelprintdecode) rXr{connectrmheaderr`responseversioncodemessagelinerrr_tunnels6      zHTTPConnection._tunnelcCs:||_|jrd|_|t|j|_|jr6d|_dS)NTF)rlrjrGrDrr<rLr>r_rrrrs  zHTTPConnection.connectcCs |jdkSN)rjr_rrr is_closed'szHTTPConnection.is_closedcCs|jdkrdSt|jdd S)NFgr)rjrr_rrr is_connected+s zHTTPConnection.is_connectedcCs|jSr)rDr_rrrhas_connected_to_proxy1sz%HTTPConnection.has_connected_to_proxycCst|jo|jdkS)zT Return True if a forwarding proxy is configured, else return False N)r<rLrGr_rrrproxy_is_forwarding5sz"HTTPConnection.proxy_is_forwardingcsFzt W5d|_d|_d|_d|_d|_d|_d|_d|_XdS)NF) rjr=r>rDrErGrIrJrVrr_rYrrr<szHTTPConnection.close)rwurl skip_hostskip_accept_encodingrUcs<t|}|r(td|d|dtj||||dS)z+Method cannot contain non-token characters z (found at least rb)rr)_CONTAINS_CONTROL_CHAR_REsearchrogrouprV putrequest)rXrwrrrmatchrYrrrKs zHTTPConnection.putrequest)rvaluesrUcsbtdd|Ds&tj|f|n8t|tkr^dddttD}td|ddS) rcss |]}t|to|tkVqdSr) isinstancerNr&).0vrrr asz+HTTPConnection.putheader..z', 'cSsg|]}t|qSr)rNtitle)rrrrr esz,HTTPConnection.putheader..z(urllib3.util.SKIP_HEADER only supports ''N) anyrV putheaderrlowerr'rsortedro)rXrrZskippable_headersrYrrr_s zHTTPConnection.putheaderT)chunkedpreload_contentdecode_contentenforce_content_lengthz_TYPE_BODY | None) rwrbodyrmrrrrrUcCs|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|VqdSr)rr)rkrrrrsz)HTTPConnection.request..zaccept-encodingrS)rr)rwr@ztransfer-encodingzTransfer-Encodingrzcontent-lengthFTzContent-Lengthz user-agentz User-Agentzutf-8s%x %b s0 )rj settimeoutrrrE frozensetrr*r@chunkscontent_lengthrrN_get_default_user_agentr~ endheadersrr|rr)rXrwrrrmrrrr header_keysrrZ chunks_and_clrrrr`chunkrrrrequestmsh      zHTTPConnection.request)rwrrrmrUcCs(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)rrmrN)warningswarnDeprecationWarningr)rXrwrrrmrrrrequest_chunkeds zHTTPConnection.request_chunkedr cs|jdkrt|j}d|_|j|jddlm}t}zt |j Wn@t t fk r}zt jdt||j|ddW5d}~XYnXt|j }||||j|jt|dd|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. Nrr z$Failed to parse headers (url=%s): %sT)exc_info _http_vsn_strzHTTP/?) rrmstatusrversion_stringreasonrroriginal_responserrr)rErrjrrrr rV getresponsermsgr! TypeErrorlogwarning_url_from_connectionrr r~rrgetattrrrrrr)rXZ resp_optionsr httplib_responsehpermrrYrrrs@     zHTTPConnection.getresponse)N)NNr6)FF)NN)NN)#rrr__doc__port_by_schemer:__annotations__rd IPPROTO_TCP TCP_NODELAYr;r=r>rrWpropertyrSsetterrlrpri version_inforrrrrrrrrrrr __classcell__rrrYrrTsx    & " &"ercs:eZdZUdZedZdZded<dZded<dZ ded<dZ d ed <dZ ded <dZ d ed <dZ d ed<dZded<dZded<d,eddejddddddddddddddddddd dddddddddddddd d d ddddddfdd Zd-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+z(typing.Callable[(Ellipsis, None)] | None_connect_callbackrK)rrBr@rCrLrMrassert_hostnamer+server_hostname ssl_contextrrrrrr cert_filekey_file key_passwordrNrrAr?rOrPrQ"None | str | typing.Literal[False]ssl.SSLContext | NonerR)rSrTrrBr@rCrLrMrrr+rrrrrrrrrrrrUc stj||||||||d||_||_||_| |_| |_| |_| |_||_ ||_ ||_ |oft j ||_|oxt j ||_||_| dkr|jdk r|jj} ntd} | |_d|_dS)N)rTrrBr@rCrLrM)rVrWrrrrrrr+rrrospath expanduserrrr verify_moder.rr)rXrSrTrrBr@rCrLrMrrr+rrrrrrrrrrrrYrrrW,s:   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) rXrrrrrrr+rrrrrset_certis$   zHTTPSConnection.set_certr[c Cs$|jdk r"|jdk r"|j}|j}n |j}|j}dtjkrHtj||d}nd}|jdk rj|jdt |dz| |_ }|j}d}|jdk r|j dkr||j||_ }d}n|j dkrd|_d|_||j}|jdk r|j}tjtk}|rtd td t|d }t||j|j|j|j|j |j!|j"|j#|j$|j%||j&||j'|j(d } | j)|_ WnRt*k r|jdk r|jd t |d|dkrtj+||ddYnX|dkr| j),dk} tj+||| d|j-rd|_.n| j.|_.t/|j0|_|jr |jdkr | j.|_dS)NZh2)rSrTFzbefore connect) thread_idtarget_supports_http2r7Tr6zSystem time is way off (before z5). This will probably lead to SSL verification errorsr\)rjrrrrrrrrrrrr tls_in_tlsrr+zafter connect failure)rSrTsupports_http2)1rGrIrSrTr)ALPN_PROTOCOLS http2_probeZacquire_and_getr threading get_identrlrjrJ_connect_tls_proxyr>rDrrdatetimedatetoday RECENT_DATErrr%r^#_ssl_wrap_socket_and_match_hostnamerrrrrrrrrrrrr+rd BaseExceptionZset_and_releaseselected_alpn_protocolrr=r<rL) rXZprobe_http2_hostZprobe_http2_portrrjrr is_time_offZserver_hostname_rm_dotsock_and_verifiedrrrrrs              zHTTPSConnection.connectraz ssl.SSLSocket)hostnamerjrUcCs\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>rd)rXr rjrMrr rrrrs,z"HTTPSConnection._connect_tls_proxy)N) NNNNNNNNN)rrrrrr:rrrrrrrrr+rrrr;rWrrrrrrrYrrs^          B?") rc@s"eZdZUdZded<ded<dS)_WrappedAndVerifiedSocketz[ Wrapped socket and whether the connection is verified after the TLS handshake zssl.SSLSocket | SSLTransportrdr<r=N)rrrrrrrrrr9s rF)rrazNone | str | intrHrFrrrr<)rjrrrrrrrrrrrr+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[]%) rjkeyfilecertfilerrrrrrr) binary_formhostname_checks_common_name)rdr=)r,r/r.rr) IS_PYOPENSSLHAS_NEVER_CHECK_COMMON_NAMEcheck_hostnamehasattrrrrfindr-r0_assert_fingerprint getpeercertssl CERT_NONEr_match_hostnamer CERT_REQUIREDr<rr)rjrrrrrrrrrrrr+rrrdefault_ssl_contextcontext normalizedssl_sockcertrrrrrCs       rz_TYPE_PEER_CERT_RET_DICT | NonerNrR)r&asserted_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)r&r'rstripped_hostnamerkrrrr s r  Exceptionr$)err proxy_schemerUcCs`dtdt|}d|kp0d|kp0d|k}d}td|rJ|dkrJ|nd |}||_|S) Nrxz[^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__)r+r,Zerror_normalizedZis_likely_http_proxyZhttp_proxy_warningZnew_errrrr_wrap_proxy_errorsr0r[cCs dtS)Nzpython-urllib3/rrrrrrsrc@seZdZdZdS)DummyConnectionz-Used to detect a failed ConnectionCls import.N)rrrrrrrrr1sr1z 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)rnrSrTr)rrr3rSrTr)r2rrnrrrrsr)F)N)e __future__rr http.clientr6loggingrr-rdrirr rr_HTTPConnectionrrrrf TYPE_CHECKINGrr Z util.ssl_r Zutil.ssltransportr _collectionsr Zhttp2rrZ 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 getLoggerrrrrrcompilerrrhr NamedTuplerrr r0rr1VerifiedHTTPSConnectionrrrrrs                          I!2