3 »nNj 8ã@sÈddlmZddlZddlZddlZddlZddlZddlmZm Z ddl Z ddl m Z ddlmZddlmZyddlZejZWn,eefk r®dZGdd „d eƒZYnXyeZWn$ek rÜGd d „d eƒZYnXdd lmZmZmZmZdd l m!Z!m"Z"ddl#m$Z$m%Z%m&Z&m'Z'm(Z(ddl)m*Z*ddl+m,Z,ej-e.ƒZ/dddœZ0ej1dddƒZ2ej3dƒZ4Gdd„de5ƒZ6Gdd„dee5ƒZGdd„deƒZ7Gdd„de7ƒZ8d d!„Z9e�rÀe7Z:e8Z7ne6Z7dS)"é)Úabsolute_importN)ÚerrorÚtimeouté)Úsix)ÚHTTPConnection)Ú HTTPExceptionc@s eZdZdS)Ú BaseSSLErrorN)Ú__name__Ú __module__Ú __qualname__©r r ú /usr/lib/python3.6/connection.pyr sr c@s eZdZdS)ÚConnectionErrorN)r r r r r r rrsr)ÚNewConnectionErrorÚConnectTimeoutErrorÚSubjectAltNameWarningÚSystemTimeWarning)Úmatch_hostnameÚCertificateError)Úresolve_cert_reqsÚresolve_ssl_versionÚassert_fingerprintÚcreate_urllib3_contextÚssl_wrap_socket)Ú connection)ÚHTTPHeaderDictéPi»)ÚhttpÚhttpsiéé éz[^-!#$%&'*+.^_`|~0-9a-zA-Z]c@seZdZdZdS)ÚDummyConnectionz-Used to detect a failed ConnectionCls import.N)r r r Ú__doc__r r r rr"Dsr"c@sxeZdZdZedZejejdfgZ dZ dd„Z e dd„ƒZ e jd d„ƒZ d d „Zd d „Zdd„Zdd„Zddd„ZdS)ra$ Based on httplib.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:: 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., ``[]``). rrFcOsDtjr|jddƒ|jdƒ|_|jd|jƒ|_tj|f|ž|ŽdS)NÚstrictÚsource_addressÚsocket_options) rZPY3ÚpopÚgetr%Údefault_socket_optionsr&Ú_HTTPConnectionÚ__init__)ÚselfÚargsÚkwr r rr+js   zHTTPConnection.__init__cCs |jjdƒS)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_hostÚrstrip)r,r r rÚhostwszHTTPConnection.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)r0)r,Úvaluer r rr2ŠscCs®i}|jr|j|d<|jr$|j|d<ytj|j|jf|jf|Ž}Wnftk rz}zt|d|j |jfƒ‚WYdd}~Xn0t k r¨}zt |d|ƒ‚WYdd}~XnX|S)zp Establish a socket connection and set nodelay settings on it. :return: New socket connection. r%r&z0Connection to %s timed out. (connect timeout=%s)Nz(Failed to establish a new connection: %s) r%r&rZcreate_connectionr0ÚportrÚ SocketTimeoutrr2Ú SocketErrorr)r,Zextra_kwÚconnÚer r rÚ _new_conn”s    zHTTPConnection._new_conncCs||_|jr|jƒd|_dS)Nr)ÚsockÚ _tunnel_hostÚ_tunnelÚ auto_open)r,r7r r rÚ _prepare_conn¯szHTTPConnection._prepare_conncCs|jƒ}|j|ƒdS)N)r9r>)r,r7r r rÚconnect·szHTTPConnection.connectcOs8tj|ƒ}|r"td||jƒfƒ‚tj|||f|ž|ŽS)zSend a request to the serverzAMethod cannot contain non-token characters %r (found at least %r))Ú_CONTAINS_CONTROL_CHAR_REÚsearchÚ ValueErrorÚgroupr*Ú putrequest)r,ÚmethodÚurlr-ÚkwargsÚmatchr r rrD»s  zHTTPConnection.putrequestNc Cst|dk r|niƒ}d|k}d|k}|j||||d�x |jƒD]\}}|j||ƒq@Wd|krl|jddƒ|jƒ|dk �rtjtf} t|| ƒrš|f}xh|D]`} | sªq t| tƒs¾| j dƒ} t t | ƒƒd d…} |j | j d ƒƒ|j d ƒ|j | ƒ|j d ƒq W|j d ƒdS) zƒ Alternative to the common request method, which sends the body with chunked encoding and not as one block Nzaccept-encodingr2)Úskip_accept_encodingÚ skip_hostztransfer-encodingzTransfer-EncodingZchunkedÚutf8ézutf-8s s0 ) rrDÚitemsZ putheaderZ endheadersrZ string_typesÚbytesÚ isinstanceÚencodeÚhexÚlenÚsend) r,rErFZbodyZheadersrIrJÚheaderr3Zstringish_typesÚchunkZlen_strr r rÚrequest_chunkedÆs8         zHTTPConnection.request_chunked)NN)r r r r#Úport_by_schemeÚ default_portÚsocketZ IPPROTO_TCPZ TCP_NODELAYr)Ú is_verifiedr+Úpropertyr2Úsetterr9r>r?rDrVr r r rrIs   rc@s:eZdZedZdZddddejddfdd„Zdd„Z dS)ÚHTTPSConnectionrNc Ks>tj|||f||dœ| —Ž||_||_||_||_d|_dS)N)r$rr)rr+Úkey_fileÚ cert_fileÚ ssl_contextÚserver_hostnameZ _protocol) r,r2r4r^r_r$rr`rar.r r rr+òszHTTPSConnection.__init__cCsR|jƒ}|j|ƒ|jdkr2ttdƒtdƒd�|_t||j|j|j|j d�|_ dS)N)Ú ssl_versionÚ cert_reqs)r:ÚkeyfileÚcertfiler`ra) r9r>r`rrrrr^r_rar:)r,r7r r rr?s  zHTTPSConnection.connect) r r r rWrXrbrYZ_GLOBAL_DEFAULT_TIMEOUTr+r?r r r rr]ís r]c@s6eZdZdZdZdZdZdZdZddd„Z dd„Z dS)ÚVerifiedHTTPSConnectionz[ Based on httplib.HTTPSConnection but wraps the socket with SSL certification. NcCsn|dkr(|s|rd}n|jdk r(|jj}||_||_||_||_||_|oTtjj |ƒ|_ |oftjj |ƒ|_ dS)zX This method should only be called once, before the connection is used. NÚ CERT_REQUIRED) r`Ú verify_moder^r_rcÚassert_hostnamerÚosÚpathÚ expanduserÚca_certsÚ ca_cert_dir)r,r^r_rcrmrirrnr r rÚset_cert s  z VerifiedHTTPSConnection.set_certc CsZ|jƒ}|j}|jr.||_|jƒd|_|j}|}|jdk rB|j}tjj ƒt k}|rft j dj t ƒtƒ|jdkrŠtt|jƒt|jƒd�|_|j}t|jƒ|_t||j|j|j|j||d�|_|jrÚt|jjdd�|jƒnb|jtjkoüt|ddƒ oü|j dk �r<|jjƒ}|j!d fƒ�s*t j d j |ƒt"ƒt#||j �p8|ƒ|jtj$k�pR|jdk |_%dS) NrzWSystem time is way off (before {0}). This will probably lead to SSL verification errors)rbrc)r:rdrermrnrar`T)Z binary_formZcheck_hostnameFZsubjectAltNamezêCertificate 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/shazow/urllib3/issues/497 for details.))&r9r2r;r:r<r=raÚdatetimeÚdateZtodayÚ RECENT_DATEÚwarningsÚwarnÚformatrr`rrrbrrcrhrr^r_rmrnrZ getpeercertÚsslZ CERT_NONEÚgetattrrir(rÚ_match_hostnamergrZ)r,r7ZhostnameraZ is_time_offÚcontextÚcertr r rr?9sZ        zVerifiedHTTPSConnection.connect)NNNNNNN) r r r r#rcrmrnrbrror?r r r rrfs rfcCsLyt||ƒWn8tk rF}ztjd||ƒ||_‚WYdd}~XnXdS)Nz@Certificate did not match expected hostname: %s. Certificate: %s)rrÚlogrZ _peer_cert)rzZasserted_hostnamer8r r rrx‚srx);Z __future__rÚrerpZloggingrjrYrr6rr5rsZpackagesrZpackages.six.moves.http_clientrr*rrvZSSLErrorr Ú ImportErrorÚAttributeErrorÚ BaseExceptionrÚ NameErrorÚ ExceptionÚ exceptionsrrrrZpackages.ssl_match_hostnamerrZ util.ssl_rrrrrÚutilrÚ _collectionsrZ getLoggerr r{rWrqrrÚcompiler@Úobjectr"r]rfrxZUnverifiedHTTPSConnectionr r r rÚsP         %(m