CYh(ddlmZddlZddlZddlZddlZddlZddlZddlZddl Z ddl m Z ddl m Z ddl mZddlmZej"rddlmZdd lmZdd lmZdd lmZdd lmZdd lmZddlmZm Z m!Z!ddl"m#Z#ddl$m%Z% ddl&Z&e&jNZ(ddl,m-Z-ddl,m.Z.ddl,m/Z/ddl0m1Z1ddl2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8ddl9m:Z:m;Z;mm?Z?ddlm@ZAddlmBZBmCZCmDZDmEZEmFZFddlGmHZHmIZIddlJmKZKeLZLeMZMejeOZPddd ZQejd!ddZSejd"ZUeVed#ZWGd$d%e Z Gd&d'e ZXGd(d)ejZZd*d+ d3d,Z[ d4 d5d-Z\d6d.Z]d7d/Z^Gd0d1Z_e&se_ZXeXZ` d8 d9d2Zay#e)e*f$rdZ&Gdde+Z(YAwxYw):) 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 eZdZy) BaseSSLErrorN)__name__ __module__ __qualname__4/usr/lib/python3/dist-packages/urllib3/connection.pyrr$s rr) _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]auditcfeZdZUdZedZded<ejejdfgZ ded<dZ d ed <d Z d ed <ded<ded<ded<d ed<ded<ded<ded<ded< d/e d de d d d d0fdZed1dZej"d2d Zd3d!Z d4 d5fd" Zd6d#Zed7d$Zed7d%Zed7d&Zd6fd' Z d8 d9fd( Zd:fd) Z d;dd*d*d*d+ dfd. ZxZS)?ra 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., ``[]``). r9ztyping.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@)rrErCrFproxy proxy_configct |||tj|||||_||_||_d|_d|_d|_ d|_ d|_ y)N)hostportrrErCF) super__init__rresolve_default_timeoutrFrOrPrGrHrJrLrM) selfrRrSrrErCrFrOrP __class__s rrUzHTTPConnection.__init__sr 33G<)  - (',$!%(,(,*.rc8|jjdS)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_hostrstriprWs rrRzHTTPConnection.hosts"~~$$S))rc||_y)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[)rWvalues rrRzHTTPConnection.hosts rc   tj|j|jf|j|j |j }tr,t!j"d||j|j|S#tj$r}t|j|||d}~wt$r-}t|d|jd|jd|d}~wt$r}t|d||d}~wwxYw)zoEstablish a socket connection and set nodelay settings on it. :return: New socket connection. )rErFNzConnection to z timed out. (connect timeout=)z&Failed to establish a new connection: zhttp.client.connect)r+create_connectionr[rSrrErFsocketgaierrorr%rR SocketTimeoutr#OSErrorr&_HAS_SYS_AUDITsysr;)rWsockes r _new_connzHTTPConnection._new_conns  //+ #22#22 D(  II+T499dii H # A%diiq9q @ %  +HVWX   $>qcB  s0A BDB.. D:(C"" D.C>>Dc^|dvrtd|dt| |||||_y)Nr8z$Invalid proxy scheme for tunneling: z", must be either 'http' or 'https')rSheaders) ValueErrorrT set_tunnelrM)rWrRrSrmschemerXs rrozHTTPConnection.set_tunnelsF * *6vj@bc  4dG<$rc|j|_|jrd|_|j t |j |_y)NT)rkrirJrG_tunnelr?rOr]s rconnectzHTTPConnection.connects?NN$   +/D ( LLN (,DJJ'7$rc|jduSN)rir]s r is_closedzHTTPConnection.is_closedsyyD  rcL|jyt|jd S)NFgr)rirr]s r is_connectedzHTTPConnection.is_connecteds# 99  C888rc|jSru)rGr]s rhas_connected_to_proxyz%HTTPConnection.has_connected_to_proxy s+++rc t|d|_d|_d|_d|_d|_d|_d|_d|_ y#d|_d|_d|_d|_d|_d|_d|_d|_ wxYw)NF) rTcloserir@rArGrHrJrLrM)rWrXs rr|zHTTPConnection.closes ' GMODI$D %)D "+0D (%)D " $D  $D "&D DI$D %)D "+0D (%)D " $D  $D "&D s A :Bctj|}|r td|d|jdt|||||S)z+Method cannot contain non-token characters z (found at least ra) skip_hostskip_accept_encoding)_CONTAINS_CONTROL_CHAR_REsearchrngrouprT putrequest)rWmethodurlrrmatchrXs rrzHTTPConnection.putrequestsg*008 =fZGXY^YdYdYfXiijk w! C9CW"  rc0td|Dst||g|yt|j t vrNdj tt Dcgc]}tj|c}}td|dycc}w)r~c3RK|]}t|txr |tk(!ywru) isinstancestrr)).0vs r z+HTTPConnection.putheader..3s$Kq:a%:!{*::Ks%'z', 'z(urllib3.util.SKIP_HEADER only supports ''N) anyrT putheaderrlowerr*joinsortedrtitlern)rWheadervaluesskippable_headersrXs rrzHTTPConnection.putheader1sKFKK G f .v . FLLN #+< < & 178I1JKv6"K! :;L:MQO  =Ks BT)chunkedpreload_contentdecode_contentenforce_content_lengthc|j%|jj|jt||||||_|i}t d|D} d| v} d| v} |j ||| | t|||j} | j} | j}|rd| vrW|jdd nDd | vrd }n=d| vrd }n6d }|| 0d }|jdd n|jd t|d| vr|jdt|jD]\}}|j|||j| ^| D]Y}|st!|tr|j#d}|r |j%dt'||fzI|j%|[|r|j%dyy)N)request_method request_urlrrrc3NK|]}t|jywru)rr)rks rrz)HTTPConnection.request..bsCaqwwy 1Cs#%zaccept-encodingrR)rr)rrCztransfer-encodingzTransfer-Encodingrzcontent-lengthFTzContent-Lengthz user-agentz User-Agentzutf-8s%x %b s0 )ri settimeoutrr rH frozensetrr-rCchunkscontent_lengthrr_get_default_user_agentitems endheadersrencodesendlen)rWrrbodyrmrrrr header_keysrr chunks_and_clrrrr_chunks rrequestzHTTPConnection.request?s 99 II  ."2!+)#9 "  ?GC7CC 0K?k)   C.Bi  'tFdnnU %%&55 "+52I>  ;.$ 3 !))"&':IFNN#3S5HI { * NN<)@)B C$]]_ *MFE NN65 ) *    %eS)!LL1EIIoUU0CCDIIe$ %  IIl # rchtjdtd|j||||dy)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)rWrrrrms rrequest_chunkedzHTTPConnection.request_chunkeds4   X'  VStWd Krc |j t|j}d|_|jj|jddlm}t|!} t|jt#|jj%}||||j&|j(|j*|j,|j.||j0|j2|j  }|S#ttf$r7}tjdt||j |dYd}~d}~wwxYw)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) rrmstatusversionreasonrroriginal_responserrr)rHrrirrresponser rT getresponsermsgr$ TypeErrorlogwarning_url_from_connectionrrrrrrrrrr)rW resp_optionsr httplib_responsehpermrrXs rrzHTTPConnection.getresponses4  ! ! )"$ $-- !% T\\* +!7.0  !"2"6"6 7!!1!5!5!;!;!=>!#**$,,#**(88'66.#/#F#F'66$00  /#I.  KK6$T<+C+CD    s%DE-EEru)rRrrSrKrrrErDrCrBrF&None | connection._TYPE_SOCKET_OPTIONSrO Url | NonerPProxyConfig | NonereturnNonerr)r_rrr)r socket.socket)NNr9) rRrrSrKrmtyping.Mapping[str, str] | Nonerprrrrr)rr?)FF) rrrrrr?rr?rr)rrrrrr)NN)rrrrr_TYPE_BODY | Nonermrrr?rr?rr?rr?rr) rrrrrrrmrrr)rr )rrr__doc__port_by_schemer=__annotations__rc IPPROTO_TCP TCP_NODELAYr>r@rArrUpropertyrRsetterrkrorsrvrxrzr|rrrrr __classcell__rXs@rrrSs0*8)?L&?   V//3PL K&*{)N**::!!..  / "215.D +////  / / //,//)/ /F**$ [[D 37 % % %1 %  %  % 8!!99 ,, '& %*      #    ( $#'37 _$ $#'+_$_$_$ _$ 1 _$_$_$_$!%_$ _$J#'37 LLL L 1 L  L&5 55rrceZdZUdZedZdZded<dZded<dZ ded<dZ d ed <dZ ded <dZ d ed <dZ d ed<dZded< deddej"ddddddddddddddddd dfdZ d ddZddZddZxZS)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`. r:Nint | str | None cert_reqsrIca_certs ca_cert_dirNone | str | bytes ca_cert_data ssl_versionrKssl_minimum_versionssl_maximum_versionr.rN)rrErCrFrOrPrassert_hostnamer.server_hostname ssl_contextrrrrrr cert_filekey_file key_passwordc t|||||||||||_||_||_| |_| |_| |_| |_||_ ||_ ||_ |xrtjj||_|xrtjj||_||_| 5|j |j j$} | |_yt'd} | |_y)N)rSrrErCrFrOrP)rTrUrrrrrrr.rrrospath expanduserrrr verify_moder1r)rWrRrSrrErCrFrOrPrrr.rrrrrrrrrrrrXs rrUzHTTPSConnection.__init__s6  ))%  ! "(&.."4&#6 #6  ARWW%7%7%A &J277+=+=k+J(  + ,,88 #.d3 "rc tjdtd|.|j|jj}n t d}||_||_||_||_ ||_ ||_ |xrtjj||_|xrtjj||_| |_y)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)rrrrrr1rrrrrr.rrrrrr) rWrrrrrrr.rrs rset_certzHTTPSConnection.set_cert8s   +(    + ,,88 -d3   ""(."4 ARWW%7%7%A &J277+=+=k+J(rc|jx|_}|j}d}|jW|jdk(r%|j |j|x|_}d}d|_|j|j}|j |j}tjjtk}|r"tjdtdtt!did|d|j"d|j$d |j&d |j(d |j*d |j,d |j.d|j0d|j2d|j4d|d|j6d|d|j8d|j:}|j<|_|j>|_tA|jB|_y)NFr:TzSystem time is way off (before z5). This will probably lead to SSL verification errorsrirrrrrrrrrrrr tls_in_tlsrr.r)"rkrirRrJrM_connect_tls_proxyrGrrrdatetimedatetoday RECENT_DATErrr(#_ssl_wrap_socket_and_match_hostnamerrrrrrrrrrrrr.rcr@r?rO)rWrirr is_time_offsock_and_verifieds rrszHTTPSConnection.connectas>>++ D#yy     (""g-#'#:#:499d#KK D! ,0D ( LLN"//O    +"22Omm))+k9  MM5k]C66"  @  nn (( !% 8 8  !% 8 8  ]]  (( ** nn ]] ** , (( " !00 $66! $&,, ,88 (,DJJ'7$rctjt|j}|j}t |f|j |j|j|j|j|j|j|||j|jddddd}|j|_|j"S)zY Establish a TLS connection to the proxy using the provided SSL context. NF)rrrrrrrrrrr.rrrr)typingcastrrPrrrrrrrrrrr.r@rArc)rWhostnamerirPrrs rrz"HTTPSConnection._connect_tls_proxys {{;0A0AB ".. ?  nn(( $ 8 8 $ 8 8]]((**$#(88+>># &"3!>!> '''rru).rRrrSrKrrrErDrCrBrFrrOrrPrrrrNone | str | Literal[False]r.rIrrIrssl.SSLContext | NonerrIrrIrrrrKrrKrrrrIrrIrrIrr) NNNNNNNNN)rrIrrIrrrrIrrIrrr.rIrrIrrrrr)rrrirrz ssl.SSLSocket)rrrrrr=rrrrrrrrr.rrr>rUrrsrrrs@rrrs2 "'*L"&I&Hj"K"'+L$+$(K!(&**&**%) )  :# "215.<.S.S +/&*7;)-&*-1#"&+/*.*.(, $##'3:#:#:#  :# / :#:#,:#:#):#$:#5:#':#$:# +!:#"#:#$ %:#&)':#(():#*(+:#,&-:#./:#01:#2!3:#4 5:#| $ $&*#'#7;)-"&+/')')')$ ') ! ')  ')5')'') '))') ')R98v(rrc&eZdZUdZded<ded<y)_WrappedAndVerifiedSocketz[ Wrapped socket and whether the connection is verified after the TLS handshake zssl.SSLSocket | SSLTransportrcr?r@N)rrrrrrrrrrs )(rrF)rc dd}|$d}tt|||t|}n|}t||_| s&| s$| dus tj stj sd|_|s"| s | s|rt|dr|j| 6| jd}d|vr|d|jd}t|r|} t|||||| | | || } | rt|jd | nd|jt j"k7rG|js;| dur7|j}|rd}nt%|d dxsd}t'|| xs| |t)||jt j*k(xs t-|  S#t.$r|j1wxYw) 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[]%) rikeyfilecertfilerrrrrrr) binary_formhostname_checks_common_name)rcr@)r/r2r1rr, IS_PYOPENSSLHAS_NEVER_CHECK_COMMON_NAMEcheck_hostnamehasattrrstriprfindr0r3_assert_fingerprint getpeercertssl CERT_NONEgetattr_match_hostnamer CERT_REQUIREDr? BaseExceptionr|)rirrrrrrrrrrrr.rrrdefault_ssl_contextcontext normalizedssl_sockcertrs rrrs0 "(+K8 3 3' 2  +I6G   e #   //!&   G1 2""$ "$**40 * #$;j&6&6s&;>3C DE"22 2 !1 1  (  $!5,':Q WY Z \ G G NrcdtS)Nzpython-urllib3/r!rrrrrts [M **rceZdZdZy)DummyConnectionz-Used to detect a failed ConnectionCls import.N)rrrrrrrr4r4xs7rr4ct|trdnd}t||j|j|j S)zUReturns the URL from a given connection. This is mainly used for testing and logging.r:r9)rprRrSr)rrr6rRrSr)connrrps rrrs3 #49WvF f4994994 H L LLr)"rirrNone | str | intrr7rrKrrKrrIrrIrrIrrIrrIrrrrr.rIrrIrrrr?rr)F)r!z_TYPE_PEER_CERT_RET_DICT | Noner$rrr?rr)r+ Exceptionr,rIrr'rru)r6z HTTPConnection | HTTPSConnectionrrIrr)b __future__rrloggingrr(rcrhrr http.clientr_HTTPConnectionrrrre TYPE_CHECKINGtyping_extensionsr rr util.ssl_r util.ssltransportr _collectionsr util.responser util.timeoutrrr util.utilr util.waitrrSSLErrorr ImportErrorAttributeErrorr_base_connectionrrr _versionr" exceptionsr#r$r%r&r'r(utilr)r*r+r, util.requestr-r.rr/r0r1r2r3util.ssl_match_hostnamer4r5util.urlr6ConnectionErrorBrokenPipeError getLoggerrrrrrcompilerrrgr NamedTuplerrrr1rr4VerifiedHTTPSConnectionrrrrrVs" 96(+ )&3/(0BB$ <