o dfN @sddlmZddlZddlZddlZddlZddlZddlZddlmZ ddlm Z ddl m Z ddlmZddlmZdd lmZz ddlZejZWneefy`dZGd d d eZYnwzeZWneyvGd d d eZYnwzeZWneyGdddeZYnwddlmZddl m!Z!ddl"m#Z#m$Z$m%Z%m&Z&ddl'm(Z(m)Z)m*Z*ddl+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1ddl2m3Z3m4Z4e5e6Z7dddZ8e9dddZ:e;dZdd Z?d!d"Z@Gd#d$d$e=ZAeseAZ>e>ZBdS)%)absolute_importN)error)timeout)six)HTTPConnection) HTTPException)create_proxy_ssl_contextc@ eZdZdS) BaseSSLErrorN__name__ __module__ __qualname__rrm/private/var/folders/v1/_jykv66s6qd26_69j1njbrl80000gr/T/pip-target-p1gutpg6/lib/python/urllib3/connection.pyr r c@r )ConnectionErrorNr rrrrr!rrc@r )BrokenPipeErrorNr rrrrr*rr)HTTPHeaderDict __version__)ConnectTimeoutErrorNewConnectionErrorSubjectAltNameWarningSystemTimeWarning) SKIP_HEADERSKIPPABLE_HEADERS connection)assert_fingerprintcreate_urllib3_context is_ipaddressresolve_cert_reqsresolve_ssl_versionssl_wrap_socket)CertificateErrormatch_hostnamePi)httphttpsiz[^-!#$%&'*+.^_`|~0-9a-zA-Z]cseZdZdZedZejejdfgZ dZ dZ ddZ e dd Zejd d Zd d Zd dZddZddZddZddZdfdd ZdddZZS)ra5 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: - ``strict``: See the documentation on :class:`urllib3.connectionpool.HTTPConnectionPool` - ``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., ``[]``). r(rFNcOsftjs |dd|d|_|d|j|_|dd|_|dd|_t j |g|Ri|dS)Nstrictsource_addresssocket_optionsproxy proxy_config) rPY2popgetr+default_socket_optionsr,r-r._HTTPConnection__init__)selfargskwrrrr4ss  zHTTPConnection.__init__cCs |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_hostrstripr5rrrhosts zHTTPConnection.hostcCs ||_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)r9)r5valuerrrr<s c Csi}|jr |j|d<|jr|j|d<ztj|j|jf|jfi|}W|Sty6t|d|j |jft yH}zt |d|d}~ww)zoEstablish a socket connection and set nodelay settings on it. :return: New socket connection. r+r,z0Connection to %s timed out. (connect timeout=%s)z(Failed to establish a new connection: %sN) r+r,rcreate_connectionr9portr SocketTimeoutrr< SocketErrorr)r5extra_kwconnerrr _new_conns2    zHTTPConnection._new_conncCs t|ddS)N _tunnel_host)getattrr;rrr_is_using_tunnels zHTTPConnection._is_using_tunnelcCs$||_|r|d|_dSdS)Nr)sockrH_tunnel auto_openr5rCrrr _prepare_conns  zHTTPConnection._prepare_conncCs|}||dSN)rErMrLrrrconnectszHTTPConnection.connectcOs>t|}|rtd||ftj|||g|Ri|S) zAMethod cannot contain non-token characters %r (found at least %r))_CONTAINS_CONTROL_CHAR_REsearch ValueErrorgroupr3 putrequest)r5methodurlr6kwargsmatchrrrrUs  zHTTPConnection.putrequestcGs`tdd|Dstj||g|RdSt|tvr.tddt t j t tfdS)rPcss"|] }t|to |tkVqdSrN) isinstancestrr).0vrrr s z+HTTPConnection.putheader..z+urllib3.util.SKIP_HEADER only supports '%s'z', 'N) anyr3 putheaderr ensure_strlowerrrSjoinmapr[titlesorted)r5headervaluesrrrr`szHTTPConnection.putheadercsN|duri}n|}ddd|Dvrt|d<tt|j||||ddS)N user-agentcss|] }t|VqdSrNrrarbr\krrrr^sz)HTTPConnection.request.. User-Agent)bodyheaders)copy_get_default_user_agentsuperrrequest)r5rVrWrnro __class__rrrss  zHTTPConnection.requestcCs$|pi}tdd|D}d|v}d|v}|j||||dd|vr)|dt|D] \}} ||| q-d|vrB|d d ||d urtjtf} t || rX|f}|D]0} | s_qZt | tsi| d } t t | d d } t | } | d7} | | 7} | d7} || qZ|dd S)z Alternative to the common request method, which sends the body with chunked encoding and not as one block cSsg|] }t|qSrrjrkrrr sz2HTTPConnection.request_chunked..zaccept-encodingr<)skip_accept_encoding skip_hostrirmztransfer-encodingzTransfer-EncodingchunkedNutf8s s0 )setrUr`rqitems endheadersr string_typesbytesrZencodehexlen bytearraysend)r5rVrWrnro header_keysrwrxrgr=stringish_typeschunklen_strto_sendrrrrequest_chunkeds<       zHTTPConnection.request_chunked)NN)r rr__doc__port_by_scheme default_portsocket IPPROTO_TCP TCP_NODELAYr2 is_verifiedproxy_is_verifiedr4propertyr<setterrErHrMrOrUr`rsr __classcell__rrrtrrLs&      rc@s|eZdZdZedZdZdZdZdZ dZ dZ dZ ddddde jddfddZ         d ddZd d Zd d ZdS)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)NFc KsDtj|||f||d| ||_||_||_||_| |_d|_dS)N)r*rr))rr4key_file cert_file key_password ssl_contextserver_hostname _protocol) r5r<r?rrrr*rrrr7rrrr4)s zHTTPSConnection.__init__c Csv|dur|jdur|jj}ntd}||_||_||_||_||_||_|o+t j ||_ |o4t j ||_ | |_dS)zX This method should only be called once, before the connection is used. N)r verify_moder"rr cert_reqsrassert_hostnamerospath expanduserca_certs ca_cert_dir ca_cert_data) r5rrrrrrrrrrrrset_certCs   zHTTPSConnection.set_certc Cs||_}|j}d}|r(|jr||||_}d}|d|_|j}|}|j dur2|j }t j t k}|rDtdt td}|jdurZd}tt|jt|jd|_|j}t|j|_|jsw|jsw|jsw|rwt|drw|t||j|j |j!|j|j|j|||d |_|r|jdurt|jdr|j"d vrtd |j|j"ft#|j$rt$|jj%dd |j$n-|jt&j'krt(|d ds|j)dur|j%}|*d dstd|t+t,||j)p||jt&j-kp|j$du|_.dS)NFTrzWSystem time is way off (before {0}). This will probably lead to SSL verification errors) ssl_versionrload_default_certs) rIkeyfilecertfilerrrrrr tls_in_tlsversion>zTLSv1.1TLSv1zNegotiating TLSv1/TLSv1.1 by default is deprecated and will be disabled in urllib3 v2.0.0. Connecting to '%s' with '%s' can be enabled by explicitly opting-in with 'ssl_version') binary_formcheck_hostnamesubjectAltNamerCertificate for {0} has no `subjectAltName`, falling back to check for a `commonName` for now. This feature is being removed by major browsers and deprecated by RFC 2818. (See https://github.com/urllib3/urllib3/issues/497 for details.))/rErIr<rHtls_in_tls_required_connect_tls_proxyrJrKrFrdatetimedatetoday RECENT_DATEwarningswarnformatrrr r#rr"rrrrrhasattrrr$rrrrDeprecationWarningr getpeercertssl CERT_NONErGrr1r_match_hostname CERT_REQUIREDr) r5rChostnamerr is_time_offdefault_ssl_contextcontextcertrrrrOds          zHTTPSConnection.connectcCs|j}|j}|rt|||dSt|j|j|j|j|j}t||j|j|j||d}|j t j krMt |ddsM| }|ddsHtd|tt|||j t jk|_|S)zY Establish a TLS connection to the proxy using the provided SSL context. )rIrr)rIrrrrrrFrrr)r.rr$r rrrrrrrrrGrr1rrrrrrr)r5rrCr.rrrrrrrsH    z"HTTPSConnection._connect_tls_proxy) NNNNNNNNN)r rrrrrrrrrrrrr_GLOBAL_DEFAULT_TIMEOUTr4rrOrrrrrrs>  ! vrc CsV|d}t|r |}zt||WdSty*}z td||||_d}~ww)Nzu[]z@Certificate did not match expected hostname: %s. Certificate: %s)stripr!r&r%logwarning _peer_cert)rasserted_hostnamestripped_hostnamerDrrrrs rcCsdtS)Nzpython-urllib3/%srrrrrrq)srqc@seZdZdZdS)DummyConnectionz-Used to detect a failed ConnectionCls import.N)r rrrrrrrr-sr)C __future__rrloggingrrerrrrArr@packagesrZpackages.six.moves.http_clientrr3rZ util.proxyr rSSLErrorr ImportErrorAttributeError BaseExceptionr NameError Exceptionr _collectionsr_versionr exceptionsrrrrutilrrrZ util.ssl_rr r!r"r#r$Zutil.ssl_match_hostnamer%r& getLoggerr rrrrcompilerQobjectrrrqrVerifiedHTTPSConnectionrrrrsd                N{