U
    .c8                  	   @   s  d dl mZ d dlZd dlZd dlZd dlZd dlZd dlmZm	Z
 d dlZd dlZd dlmZ d dlmZ zd dlZejZW n, eefk
r   dZG dd deZY nX zeZW n$ ek
r   G dd	 d	eZY nX d
dlmZmZmZmZ d
dlm Z m!Z! d
dl"m#Z#m$Z$m%Z%m&Z&m'Z' d
dl(m)Z) d
dl*m+Z+ e,e-Z.dddZ/e0dd
d
Z1e2dZ3G dd de4Z5G dd dee4ZG dd deZ6G dd de6Z7dd Z8ere6Z9e7Z6ne5Z6dS )    )absolute_importN)errortimeout)HTTPConnection)HTTPExceptionc                   @   s   e Zd ZdS )BaseSSLErrorN__name__
__module____qualname__ r   r   4/usr/lib/python3/dist-packages/urllib3/connection.pyr      s   r   c                   @   s   e Zd ZdS )ConnectionErrorNr   r   r   r   r   r      s   r      )NewConnectionErrorConnectTimeoutErrorSubjectAltNameWarningSystemTimeWarning)match_hostnameCertificateError)resolve_cert_reqsresolve_ssl_versionassert_fingerprintcreate_urllib3_contextssl_wrap_socket)
connection)HTTPHeaderDictP   i  )httphttpsi  z[^-!#$%&'*+.^_`|~0-9a-zA-Z]c                   @   s   e Zd ZdZdS )DummyConnectionz-Used to detect a failed ConnectionCls import.N)r	   r
   r   __doc__r   r   r   r   r    A   s   r    c                   @   sx   e Zd ZdZed ZejejdfgZ	dZ
dd Zedd Zejd	d Zd
d Zdd Zdd Zdd ZdddZdS )r   a$  
    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., ``[]``).
    r   r   Fc                 O   sD   t js|dd  |d| _|d| j| _tj| f|| d S )Nstrictsource_addresssocket_options)	sixZPY2popgetr#   default_socket_optionsr$   _HTTPConnection__init__)selfargskwr   r   r   r*   h   s
    zHTTPConnection.__init__c                 C   s   | j d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   hostu   s    zHTTPConnection.hostc                 C   s
   || _ dS )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/   )r+   valuer   r   r   r1      s    c              
   C   s   i }| j r| j |d< | jr$| j|d< ztj| j| jf| jf|}W nX tk
rl   t| d| j	| jf Y n0 t
k
r } zt| d| W 5 d}~X Y nX |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)z(Failed to establish a new connection: %sN)r#   r$   r   Zcreate_connectionr/   portr   SocketTimeoutr   r1   SocketErrorr   )r+   Zextra_kwconner   r   r   	_new_conn   s2    


 
 zHTTPConnection._new_connc                 C   s$   || _ t| dd r |   d| _d S )N_tunnel_hostr   )sockgetattr_tunnel	auto_openr+   r6   r   r   r   _prepare_conn   s    zHTTPConnection._prepare_connc                 C   s   |   }| | d S )N)r8   r?   r>   r   r   r   connect   s    zHTTPConnection.connectc                 O   s8   t |}|r"td|| f tj| ||f||S )zSend 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   r   rE      s    

zHTTPConnection.putrequestNc                 C   s  t |dk	r|ni }d|k}d|k}| j||||d | D ]\}}| || q>d|krh| dd |   |dk	rtjtf }	t||	r|f}|D ]`}
|
sqt|
ts|
	d}
t
t|
d	d }| |	d
 | d | |
 | d q| d dS )z
        Alternative to the common request method, which sends the
        body with chunked encoding and not as one block
        Nzaccept-encodingr1   )skip_accept_encoding	skip_hostztransfer-encodingzTransfer-EncodingZchunkedutf8   zutf-8s   
s   0

)r   rE   itemsZ	putheaderZ
endheadersr%   Zstring_typesbytes
isinstanceencodehexlensend)r+   rF   rG   ZbodyZheadersrJ   rK   headerr2   Zstringish_typeschunkZlen_strr   r   r   request_chunked   s:       




zHTTPConnection.request_chunked)NN)r	   r
   r   r!   port_by_schemedefault_portsocketZIPPROTO_TCPZTCP_NODELAYr(   is_verifiedr*   propertyr1   setterr8   r?   r@   rE   rW   r   r   r   r   r   G   s   

		r   c                   @   s4   e Zd Zed ZdZdddddejddfddZdS )HTTPSConnectionr   Nc
                 K   sD   t j| ||f||d|
 || _|| _|| _|| _|	| _d| _d S )N)r"   r   r   )r   r*   key_file	cert_filekey_passwordssl_contextserver_hostnameZ	_protocol)r+   r1   r3   r_   r`   ra   r"   r   rb   rc   r-   r   r   r   r*      s    zHTTPSConnection.__init__)	r	   r
   r   rX   rY   ssl_versionrZ   Z_GLOBAL_DEFAULT_TIMEOUTr*   r   r   r   r   r^      s   r^   c                   @   s6   e Zd ZdZdZdZdZdZdZdddZ	dd Z
dS )VerifiedHTTPSConnectionz[
    Based on httplib.HTTPSConnection but wraps the socket with
    SSL certification.
    Nc	           	      C   sp   |dkr$| j dk	r| j j}ntd}|| _|| _|| _|| _|| _|| _|oVt	j
|| _|oht	j
|| _dS )zX
        This method should only be called once, before the connection is used.
        N)rb   verify_moder   r_   r`   	cert_reqsra   assert_hostnamer   ospath
expanduserca_certsca_cert_dir)	r+   r_   r`   rg   ra   rl   rh   r   rm   r   r   r   set_cert  s    

z VerifiedHTTPSConnection.set_certc              
   C   s  |   }| j}t| dd r4|| _|   d| _| j}|}| jd k	rH| j}tj	
 tk }|rltdtt d}| jd krd}tt| jt| jd| _| j}t| j|_| js| js|rt|dr|  t|| j| j| j| j| j||d| _| jrt| jj dd	| j nd|jt!j"krtt|d
dst| j#dk	rt| j  }|$ddsbtd|t% t&|| j#pp| |jt!j'kp| jd k	| _(d S )Nr9   r   zWSystem time is way off (before {0}). This will probably lead to SSL verification errorsFT)rd   rg   load_default_certs)r:   ZkeyfileZcertfilera   rl   rm   rc   rb   )Zbinary_formZcheck_hostnameZsubjectAltNamer   zCertificate 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.)))r8   r1   r;   r:   r<   r=   r9   rc   datetimedateZtodayRECENT_DATEwarningswarnformatr   rb   r   r   rd   r   rg   rf   rl   rm   hasattrro   r   r_   r`   ra   r   ZgetpeercertsslZ	CERT_NONErh   r'   r   _match_hostnameZCERT_REQUIREDr[   )r+   r6   Zhostnamerc   Zis_time_offZdefault_ssl_contextcontextcertr   r   r   r@   8  s    


 


	zVerifiedHTTPSConnection.connect)NNNNNNNN)r	   r
   r   r!   rg   rl   rm   rd   r   rn   r@   r   r   r   r   re     s            
re   c              
   C   sL   zt | | W n8 tk
rF } ztd||  | |_ W 5 d }~X Y nX d S )Nz@Certificate did not match expected hostname: %s. Certificate: %s)r   r   logZwarningZ
_peer_cert)rz   Zasserted_hostnamer7   r   r   r   rx     s    rx   ):Z
__future__r   rerp   Zloggingri   rZ   r   r5   r   r4   rs   r%   Zsix.moves.http_clientr   r)   r   rw   ZSSLErrorr   ImportErrorAttributeErrorBaseExceptionr   	NameError	Exception
exceptionsr   r   r   r   Zpackages.ssl_match_hostnamer   r   Z	util.ssl_r   r   r   r   r   utilr   _collectionsr   Z	getLoggerr	   r{   rX   rq   rr   compilerA   objectr    r^   re   rx   ZUnverifiedHTTPSConnectionr   r   r   r   <module>   sP   
	


 '  