3
h`2                 @   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mZ ddlmZ ddlmZ y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 y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j-e.Z/dddZ0ej1dddZ2G dd de3Z4G dd dee3ZG dd deZ5G dd de5Z6dd Z7ere5Z8e6Z5ne4Z5dS )    )absolute_importN)errortimeout   )six)HTTPConnection)HTTPExceptionc               @   s   e Zd ZdS )BaseSSLErrorN)__name__
__module____qualname__ r   r   =/tmp/pip-build-ikjamrr1/pip/pip/_vendor/urllib3/connection.pyr	      s   r	   c               @   s   e Zd ZdS )ConnectionErrorN)r
   r   r   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  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               @   sV   e Zd ZdZed ZejejdfgZ	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.

        .. note:: This is ignored for Python 2.6. It is only applied for 2.7 and 3.x

      - ``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   sZ   t jr|jdd  |jd| _tjdk r4|jdd  |jd| j| _t	j
| f|| d S )Nstrictsource_address      socket_options)r$   r%   )r   PY3popgetr#   sysversion_infodefault_socket_optionsr&   _HTTPConnection__init__)selfargskwr   r   r   r.   j   s    
zHTTPConnection.__init__c             C   s   i }| j r| j |d< | jr$| j|d< ytj| j| jf| jf|}W nf tk
rz } zt| d| j| jf W Y dd}~X n0 t	k
r } zt
| d| W Y dd}~X 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)Nz(Failed to establish a new connection: %s)r#   r&   r   create_connectionhostportr   SocketTimeoutr   SocketErrorr   )r/   extra_kwconner   r   r   	_new_conn   s     

 zHTTPConnection._new_connc             C   s$   || _ t| dd r | j  d| _d S )N_tunnel_hostr   )sockgetattr_tunnel	auto_open)r/   r8   r   r   r   _prepare_conn   s    zHTTPConnection._prepare_connc             C   s   | j  }| j| d S )N)r:   r@   )r/   r8   r   r   r   connect   s    zHTTPConnection.connectNc             C   s  t |dk	r|ni }d|k}d|k}| j||||d x |j D ]\}}| j|| q@W d|krl| jdd | j  |dk	rtjtjf }	t||	r|f}xj|D ]b}
|
sqt|
tjs|
j	d}
t
t|
d	d }| j|j	d
 | jd | j|
 | jd qW | jd dS )z
        Alternative to the common request method, which sends the
        body with chunked encoding and not as one block
        Nzaccept-encodingr3   )skip_accept_encoding	skip_hostztransfer-encodingzTransfer-Encodingchunkedutf8r$   zutf-8s   
s   0

)r   
putrequestitems	putheader
endheadersr   string_typesbinary_type
isinstanceencodehexlensend)r/   methodurlbodyheadersrB   rC   headervaluestringish_typeschunklen_strr   r   r   request_chunked   s8    





zHTTPConnection.request_chunked)NN)r
   r   r   r!   port_by_schemedefault_portsocketIPPROTO_TCPTCP_NODELAYr,   is_verifiedr.   r:   r@   rA   rZ   r   r   r   r   r   F   s   r   c               @   s8   e Zd Zed ZdZddddejdfddZdd Z	dS )HTTPSConnectionr   Nc       	      K   s8   t j| ||f||d| || _|| _|| _d| _d S )N)r"   r   r   )r   r.   key_file	cert_filessl_context	_protocol)	r/   r3   r4   rb   rc   r"   r   rd   r1   r   r   r   r.      s    zHTTPSConnection.__init__c             C   sN   | j  }| j| | jd kr2ttd td d| _t|| j| j| jd| _	d S )N)ssl_version	cert_reqs)r<   keyfilecertfilerd   )
r:   r@   rd   r   r   r   r   rb   rc   r<   )r/   r8   r   r   r   rA      s    

zHTTPSConnection.connect)
r
   r   r   r[   r\   rf   r]   _GLOBAL_DEFAULT_TIMEOUTr.   rA   r   r   r   r   ra      s   ra   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   sn   |dkr(|s|rd}n| j dk	r(| j j}|| _|| _|| _|| _|| _|oTtjj	|| _
|oftjj	|| _dS )zX
        This method should only be called once, before the connection is used.
        NCERT_REQUIRED)rd   verify_moderb   rc   rg   assert_hostnamer   ospath
expanduserca_certsca_cert_dir)r/   rb   rc   rg   rr   rn   r   rs   r   r   r   set_cert  s    
z VerifiedHTTPSConnection.set_certc          	   C   sL  | j  }| j}t| dd r4|| _| j  d| _| j}tjj	 t
k }|rXtjdjt
t | jd kr|tt| jt| jd| _| j}t| j|_t|| j| j| j| j||d| _| jrt| jjdd| j nb|jtjkot|dd	 o| jd	k	r.| jj }|j d
f stjdj|t! t"|| jp*| |jtj#kpD| jd k	| _$d S )Nr;   r   zWSystem time is way off (before {0}). This will probably lead to SSL verification errors)rf   rg   )r<   rh   ri   rr   rs   server_hostnamerd   T)binary_formcheck_hostnameFsubjectAltNamezCertificate 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.))%r:   r3   r=   r<   r>   r?   r;   datetimedatetodayRECENT_DATEwarningswarnformatr   rd   r   r   rf   r   rg   rm   r   rb   rc   rr   rs   r   getpeercertssl	CERT_NONErn   r)   r   _match_hostnamerl   r`   )r/   r8   hostnameis_time_offcontextcertr   r   r   rA     sT    



zVerifiedHTTPSConnection.connect)NNNNNNN)r
   r   r   r!   rg   rr   rs   rf   r   rt   rA   r   r   r   r   rk      s      
rk   c             C   sL   yt | | W n8 tk
rF } ztjd||  | |_ W Y d d }~X nX d S )Nz@Certificate did not match expected hostname: %s. Certificate: %s)r   r   logr   
_peer_cert)r   asserted_hostnamer9   r   r   r   r   b  s    r   )9
__future__r   ry   loggingro   r*   r]   r   r6   r   r5   r}   packagesr   Zpackages.six.moves.http_clientr   r-   r   r   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   	getLoggerr
   r   r[   rz   r|   objectr    ra   rk   r   UnverifiedHTTPSConnectionr   r   r   r   <module>   sN   
	
 &l