
) Xa-                 @   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 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
 r[Gd
 d   d e  Z Yn Xd 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( d d l) m* Z* d d l+ m, Z, e j- e.  Z/ i d d 6d d 6Z0 e j1 d d d  Z2 Gd d   d e3  Z4 Gd d   d e e3  Z Gd d   d e  Z5 Gd d   d e5  Z6 d d   Z7 e re5 Z8 e6 Z5 n e4 Z5 d S)     )absolute_importN)errortimeout   )six)HTTPConnection)HTTPExceptionc               @   s   e  Z d  Z d S)BaseSSLErrorN)__name__
__module____qualname__ r   r   T/home/ubuntu/projects/ifolica/build/requests/requests/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CertificateError)resolve_cert_reqsresolve_ssl_versionssl_wrap_socketassert_fingerprint)
connection)HTTPHeaderDictP   httpi  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   B   s   r   c               @   s   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 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   s   t  j r | j d d   n  | j d  |  _ t j d k  rP | j d d   n  | j d |  j  |  _ t	 j
 |  | |  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-   k   s    	zHTTPConnection.__init__c             C   s   i  } |  j  r |  j  | d <n  |  j r8 |  j | d <n  y( t j |  j |  j f |  j |  } Wn{ t k
 r } z$ t |  d |  j |  j f   WYd d } ~ Xn8 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)Nz(Failed to establish a new connection: %s)r"   r%   r   create_connectionhostportr   SocketTimeoutr   SocketErrorr   )r.   Zextra_kwconner   r   r   	_new_conn   s     		"(#zHTTPConnection._new_connc             C   s5   | |  _  t |  d d   r1 |  j   d |  _ n  d  S)N_tunnel_hostr   )sockgetattr_tunnel	auto_open)r.   r6   r   r   r   _prepare_conn   s    	
zHTTPConnection._prepare_connc             C   s   |  j    } |  j |  d  S)N)r8   r>   )r.   r6   r   r   r   connect   s    zHTTPConnection.connectNc             C   s~  t  | d k	 r | n i   } d | k } |  j | | d | x* | j   D] \ } } |  j | |  qM Wd | k r |  j d d  n  |  j   | d k	 rmt j t j f } t | |  r | f } n  x | D] }	 |	 s q n  t |	 t j  s|	 j	 d  }	 n  t
 t |	   d d  }
 |  j |
 j	 d	   |  j d
  |  j |	  |  j d
  q Wn  |  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-encodingskip_accept_encodingz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headersr@   headervalueZstringish_typeschunkZlen_strr   r   r   request_chunked   s.    
zHTTPConnection.request_chunked)r
   r   r   r    port_by_schemedefault_portsocketIPPROTO_TCPTCP_NODELAYr+   is_verifiedr-   r8   r>   r?   rU   r   r   r   r   r   G   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.   r2   r3   r]   r^   r!   r   r0   r   r   r   r-      s
    
		zHTTPSConnection.__init__c             C   s;   |  j    } |  j |  t j | |  j |  j  |  _ d  S)N)r8   r>   sslwrap_socketr]   r^   r:   )r.   r6   r   r   r   r?      s    zHTTPSConnection.connect)	r
   r   r   rV   rW   rX   _GLOBAL_DEFAULT_TIMEOUTr-   r?   r   r   r   r   r\      s   
	r\   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 } n  | |  _  | |  _ | |  _ | |  _ | |  _ | oc t j j |  |  _ | o~ t j j |  |  _	 d  S)NCERT_REQUIRED)
r]   r^   	cert_reqsassert_hostnamer   ospath
expanduserca_certsca_cert_dir)r.   r]   r^   rd   ri   re   r   rj   r   r   r   set_cert   s    						z VerifiedHTTPSConnection.set_certc             C   s  |  j    } t |  j  } t |  j  } |  j } t |  d d   rm | |  _ |  j   d |  _	 |  j
 } n  t j j   t k  } | r t j d j t  t  n  t | |  j |  j d | d |  j d |  j d | d | |  _ |  j rt |  j j d	 d
  |  j  nt | t j k r|  j d k	 r|  j j   } | j d f   sit j d j |  t  n  t | |  j p{|  n  | 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 errorsrd   ri   rj   server_hostnamessl_versionbinary_formTF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.))"r8   r   rd   r   rm   r2   r;   r:   r<   r=   r9   datetimedatetodayRECENT_DATEwarningswarnformatr   r   r]   r^   ri   rj   r   getpeercertr_   	CERT_NONEre   r(   r   _match_hostnamerc   r[   )r.   r6   resolved_cert_reqsresolved_ssl_versionhostnameZis_time_offcertr   r   r   r?      sF    		
	
			
zVerifiedHTTPSConnection.connect)r
   r   r   r    rd   ri   rj   rm   r   rk   r?   r   r   r   r   rb      s   rb   c             C   s\   y t  |  |  WnD t k
 rW } z$ t j d | |   |  | _   WYd  d  } ~ Xn Xd  S)Nz@Certificate did not match expected hostname: %s. Certificate: %s)r   r   logr   Z
_peer_cert)r}   Zasserted_hostnamer7   r   r   r   ry   7  s    
	ry   )9
__future__r   rp   loggingrf   r)   rX   r   r5   r   r4   rt   packagesr   http.clientr   r,   r   ImportErrorhttplibr_   SSLErrorr	   AttributeErrorBaseExceptionr   	NameError	Exception
exceptionsr   r   r   r   Zpackages.ssl_match_hostnamer   r   Z	util.ssl_r   r   r   r   utilr   _collectionsr   	getLoggerr
   r~   rV   rq   rs   objectr   r\   rb   ry   ZUnverifiedHTTPSConnectionr   r   r   r   <module>   sV   
""
V	