
]'v_*                 @   sl  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 y$ d  d l m Z d  d l m Z Wn2 e k
 r d  d l m Z d  d l m Z Yn Xy d  d l Z e j Z Wn4 e e f k
 r"d Z Gd d   d e  Z Yn Xy
 e Z Wn( e k
 rWGd d	   d	 e  Z Yn Xd
 d l m Z m Z m Z m Z d
 d l  m! Z! d
 d l" m# Z# m$ Z$ m% Z% m& Z& d
 d l' m( Z( d d d d i Z) e j* d d
 d
  Z+ Gd d   d e,  Z- e j. d  Z/ Gd d   d e e,  Z Gd d   d e  Z0 Gd d   d e0  Z1 e rbe0 Z2 e1 Z0 n e- Z0 d 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)ConnectionErrorN)r   r	   r
   r   r   r   r   r       s   r      )NewConnectionErrorConnectTimeoutErrorSubjectAltNameWarningSystemTimeWarning)match_hostname)resolve_cert_reqsresolve_ssl_versionssl_wrap_socketassert_fingerprint)
connectionhttpP   httpsi  i  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   >   s   r   z[^-!#$%&'*+.^_`|~0-9a-zA-Z]c               @   sz   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   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   sy   t  j r | j d d   | j d  |  _ t j d k  rJ | j d d   | j d |  j  |  _ t	 j
 |  | |  d  S)Nstrictsource_address      socket_options)r    r!   )sixZPY3popgetr   sysversion_infodefault_socket_optionsr"   _HTTPConnection__init__)selfargskwr   r   r   r*   i   s    	zHTTPConnection.__init__c             C   s   i  } |  j  r |  j  | d <|  j r2 |  j | d <y( t j |  j |  j f |  j |  } Wnh t k
 r t |  d |  j |  j f   Yn8 t	 k
 r } z t
 |  d |   WYd d } ~ Xn 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_connectionhostportr   SocketTimeoutr   SocketErrorr   )r+   Zextra_kwconner   r   r   	_new_conn~   s     		"#zHTTPConnection._new_connc             C   s2   | |  _  t |  d d   r. |  j   d |  _ d  S)N_tunnel_hostr   )sockgetattr_tunnel	auto_open)r+   r2   r   r   r   _prepare_conn   s    	
zHTTPConnection._prepare_connc             C   s   |  j    } |  j |  d  S)N)r4   r:   )r+   r2   r   r   r   connect   s    zHTTPConnection.connectc             O   sJ   t  j |  } | r1 t d | | j   f   t j |  | | | |  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+   methodZurlr,   kwargsmatchr   r   r   r@      s    zHTTPConnection.putrequestN)r   r	   r
   r   port_by_schemedefault_portsocketZIPPROTO_TCPZTCP_NODELAYr(   is_verifiedr*   r4   r:   r;   r@   r   r   r   r   r   E   s   
r   c               @   sD   e  Z d  Z e d Z d d d d e j d d  Z d d   Z d S)HTTPSConnectionr   Nc          	   K   sA   t  j |  | | d | d | | | |  _ | |  _ d |  _ d  S)Nr   r   r   )r   r*   key_file	cert_fileZ	_protocol)r+   r.   r/   rI   rJ   r   r   r-   r   r   r   r*      s
    
		zHTTPSConnection.__init__c             C   s;   |  j    } |  j |  t j | |  j |  j  |  _ d  S)N)r4   r:   sslZwrap_socketrI   rJ   r6   )r+   r2   r   r   r   r;      s    zHTTPSConnection.connect)	r   r	   r
   rD   rE   rF   Z_GLOBAL_DEFAULT_TIMEOUTr*   r;   r   r   r   r   rH      s   
	rH   c            	   @   sa   e  Z d  Z d Z d Z d Z d Z d Z d Z d d d d d d d d d  Z	 d d   Z
 d S)VerifiedHTTPSConnectionz[
    Based on httplib.HTTPSConnection but wraps the socket with
    SSL certification.
    Nc             C   s   | s | r | d  k r d } | |  _  | |  _ | |  _ | |  _ | |  _ | o` t j j |  |  _ | o{ t j j |  |  _	 d  S)NCERT_REQUIRED)
rI   rJ   	cert_reqsassert_hostnamer   ospath
expanduserca_certsca_cert_dir)r+   rI   rJ   rN   rS   rO   r   rT   r   r   r   set_cert   s    					z VerifiedHTTPSConnection.set_certc             C   s  |  j    } t |  j  } t |  j  } |  j } t |  d d   rj | |  _ |  j   d |  _	 |  j
 } t j j   t k  } | r t j d j t  t  t | |  j |  j d | d |  j d |  j d | d | |  _ |  j rt |  j j d	 d
  |  j  n | t j k r|  j d k	 r|  j j   } | j d f   s`t j d j |  t  |  j pl| } | j d  } t  | |  | t j! k p|  j d  k	 |  _" d  S)Nr5   r   zWSystem time is way off (before {0}). This will probably lead to SSL verification errorsrN   rS   rT   Zserver_hostnamessl_versionZbinary_formTFZ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.)z[])#r4   r   rN   r   rV   r.   r7   r6   r8   r9   r5   datetimedateZtodayRECENT_DATEwarningswarnformatr   r   rI   rJ   rS   rT   r   ZgetpeercertrK   Z	CERT_NONErO   r%   r   stripr   rM   rG   )r+   r2   Zresolved_cert_reqsZresolved_ssl_versionZhostnameZis_time_offZcertZasserted_hostnamer   r   r   r;      sJ    		
						zVerifiedHTTPSConnection.connect)r   r	   r
   r   rN   rS   rT   rV   r   rU   r;   r   r   r   r   rL      s   rL   )3Z
__future__r   rerW   rP   r&   rF   r   r1   r   r0   rZ   r#   Zhttp.clientr   r)   r   ImportErrorZhttplibrK   ZSSLErrorr   AttributeErrorBaseExceptionr   	NameError	Exception
exceptionsr   r   r   r   Zpackages.ssl_match_hostnamer   Z	util.ssl_r   r   r   r   utilr   rD   rX   rY   objectr   compiler<   rH   rL   ZUnverifiedHTTPSConnectionr   r   r   r   <module>   sP   
""o_	