ó
Òbc           @@  s1  d  d l  m Z d  d l Z e j d k r> d d l m Z n d d l 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 m Z Wn! e k
 rÁ d  d	 l m Z n Xd d
 l m Z d d l m Z m Z d d l m Z d g Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   i    (   t   absolute_importNi   i   (   t   httplib(   t   debug(   t	   Semaphore(   t   time(   t   urlparse(   t   Config(   t   ParameterErrort   S3SSLCertificateError(   t   getBucketFromHostnamet   ConnMant   http_connectionc           B@  s}   e  Z d Z e Z e d  „  ƒ Z e d „  ƒ Z e d „  ƒ Z	 e d „  ƒ Z
 d „  Z d „  Z e d d „ ƒ Z d „  Z RS(	   c         C@  sc   t  ƒ  } d  } y t j d |  ƒ } Wn t k
 r8 n X| r_ | j r_ t | _ t d ƒ n  | S(   Nt   cafileu+   Disabling SSL certificate hostname checking(	   R   t   Nonet   sslt   create_default_contextt   AttributeErrort   check_ssl_hostnamet   Falset   check_hostnameR   (   R   t   cfgt   context(    (    sJ   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/ConnMan.pyt   _ssl_verified_context(   s    		c         C@  sG   t  d ƒ d  } y t j d |  d t j ƒ } Wn t k
 rB n X| S(   Nu"   Disabling SSL certificate checkingR   t	   cert_reqs(   R   R   R   t   _create_unverified_contextt	   CERT_NONER   (   R   R   (    (    sJ   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/ConnMan.pyt   _ssl_unverified_context6   s    
c      	   C@  s^   d  } y@ | r t j n t j } t j d | d | d |  d | ƒ } Wn t k
 rY n X| S(   NR   t   keyfilet   certfileR   (   R   R   t   CERT_REQUIREDR   R   R   (   R   R   t   check_server_certR   R   R   (    (    sJ   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/ConnMan.pyt   _ssl_client_auth_contextA   s    c          C@  sæ   t  j r t  j St ƒ  }  |  j } | d k r7 d  } n  |  j pC d  } |  j pR d  } t d | ƒ t d | ƒ t d | ƒ | d  k	 r¦ t  j	 | | |  j
 | ƒ } n* |  j
 rÁ t  j | ƒ } n t  j | ƒ } | t  _ t t  _ | S(   Nt    u   Using ca_certs_file %su   Using ssl_client_cert_file %su   Using ssl_client_key_file %s(   R   t   context_setR   R   t   ca_certs_fileR   t   ssl_client_cert_filet   ssl_client_key_fileR   R   t   check_ssl_certificateR   R   t   True(   R   R   R   R   R   (    (    sJ   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/ConnMan.pyt   _ssl_contextN   s&    							c         C@  s  t  d ƒ | j d d ƒ } | j ƒ  } t d t j ƒ j } xÓ | D]Ë \ } } | d k rE | j ƒ  } | j d ƒ r– | j d ƒ r– | j d ƒ s´ | j d ƒ r¸ | j d ƒ r¸ t	 S| | i d d	 6t j
 j ƒ  d
 6k r| j | i d d	 6t j
 j ƒ  d
 6ƒ rt	 SqE qE Wt S(   sú  
        Wildcard matching for *.s3.amazonaws.com and similar per region.

        Per http://docs.aws.amazon.com/AmazonS3/latest/dev/BucketRestrictions.html:
        "We recommend that all bucket names comply with DNS naming conventions."

        Per http://docs.aws.amazon.com/AmazonS3/latest/dev/VirtualHosting.html:
        "When using virtual hosted-style buckets with SSL, the SSL
        wild card certificate only matches buckets that do not contain
        periods. To work around this, use HTTP or write your own
        certificate verification logic."

        Therefore, we need a custom validation routine that allows
        mybucket.example.com.s3.amazonaws.com to be considered a valid
        hostname for the *.s3.amazonaws.com wildcard cert, and for the
        region-specific *.s3-[region].amazonaws.com wildcard cert.

        We also forgive non-S3 wildcard certificates should the
        hostname match, to allow compatibility with other S3
        API-compatible storage providers.
        u6   checking SSL subjectAltName as forgiving wildcard certt   subjectAltNames   https://t   DNSs   *.s3s   .amazonaws.coms   .amazonaws.com.cnt   *t   buckett   locationR    (    (   R   t   gett   lowerR   R   t   host_buckett   hostnamet
   startswitht   endswithR&   t   bucket_locationR   (   t   selft   certR0   t   sant   cleaned_host_bucket_configt   keyt   value(    (    sJ   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/ConnMan.pyt   forgive_wildcard_certi   s"    
!	!c         C@  sƒ   |  j  j j ƒ  } y t j | |  j ƒ WnS t k
 r= d  St k
 rN d  St k
 r~ } |  j	 | |  j ƒ s | ‚ q n Xd  S(   N(
   t   ct   sockt   getpeercertR   t   match_hostnameR0   R   t
   ValueErrort   S3CertificateErrorR:   (   R4   R5   t   e(    (    sJ   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/ConnMan.pyR>   ‘   s    c         C@  s  y™ t  j ƒ  } t |  ƒ \ } } | rX d | k rX t d ƒ t } | rp t | _ qp n | rj | j } n t } t j |  | d | d | ƒ} t d ƒ Wng t	 k
 ry& t j |  | d | ƒ} t d ƒ Wqt	 k
 rþ t j |  | ƒ } t d ƒ qXn X| S(   Nt   .uH   Bucket name contains "." character, disabling initial SSL hostname checkR   R   u=   httplib.HTTPSConnection() has both context and check_hostnameu*   httplib.HTTPSConnection() has only contextu@   httplib.HTTPSConnection() has neither context nor check_hostname(
   R   R'   R	   R   R   R   R&   R   t   HTTPSConnectiont	   TypeError(   R0   t   portR   t   bucket_namet   successR   t   conn(    (    sJ   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/ConnMan.pyt   _https_connectionŸ   s*    
c         C@  s¿  | |  _  | |  _ d |  _ t d | ƒ } | j |  _ | j |  _ | j rƒ | j d k rƒ | j j d ƒ |  _ t d |  j ƒ n	 d  |  _ | j
 s| rÏ t j |  j |  j ƒ |  _ t d |  j |  j ƒ q¯t j |  j |  j ƒ |  _ t d |  j |  j ƒ n¬ | r~t j | j
 | j ƒ |  _ t d | j
 | j ƒ |  j rL|  j pOd } |  j j |  j | ƒ t d	 |  j | ƒ n1 t j | j
 | j ƒ |  _ t d
 | j
 | j ƒ t ƒ  |  _ d  S(   Ni    s   https://t   /u   endpoint path set to %su#   non-proxied HTTPSConnection(%s, %s)u"   non-proxied HTTPConnection(%s, %s)u   proxied HTTPSConnection(%s, %s)i»  u   tunnel to %s, %su   proxied HTTPConnection(%s, %s)(   R   t   idt   counterR   R0   RE   t   patht   rstripR   R   t
   proxy_hostR   RI   R;   R   t   HTTPConnectiont
   proxy_portt
   set_tunnelR   t   last_used_time(   R4   RK   R0   R   R   t   parsed_hostnameRE   (    (    sJ   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/ConnMan.pyt   __init__Ä   s2    					N(   t   __name__t
   __module__R   R   R   R!   t   staticmethodR   R   R   R'   R:   R>   RI   RU   (    (    (    sJ   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/ConnMan.pyR   $   s   	(	$c           B@  s_   e  Z e j Z e j Z e ƒ  Z i  Z d  Z e	 d d „ ƒ Z e	 d „  ƒ Z e	 d „  ƒ Z RS(   i   c         C@  sÌ  t  ƒ  } | d  k r! | j } n  d  } | j d k rs | rZ t j d k  rZ t d ƒ ‚ n  d | j | j f } n d | r‚ d p… d |  f } t j	 j
 ƒ  | t j k r» g  t j | <n  x t j | rJt j | j ƒ  } t ƒ  } | | j | j k  r*| | j k r*t d | j | j f ƒ Pn  t d ƒ t j | ƒ d  } q¾ Wt j	 j ƒ  | s¹t d	 | ƒ t | |  | | ƒ } | j j ƒ  | j r¹| j r¹| j r¹| j ƒ  q¹n  | j d
 7_ | S(   NR    i  s6   use_https=True can't be used with proxy on Python <2.7s   proxy://%s:%ss   http%s://%st   ss)   ConnMan.get(): re-using connection: %s#%ds)   ConnMan.get(): closing expired connections*   ConnMan.get(): creating new connection: %si   (   R   R   t	   use_httpsRO   t   syst
   hexversionR   RQ   R
   t   conn_pool_semt   acquiret	   conn_poolt   popR   RS   t   connection_max_ageR   RK   RL   t   closet   releaseR   R;   t   connectR   R%   R   R>   (   R0   R   R   RH   t   conn_idt   cur_time(    (    sJ   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/ConnMan.pyR-   ü   s@    		

c         C@  sâ   |  j  j d ƒ r- t j |  ƒ t d ƒ d  S|  j t j k rZ t j |  ƒ t d ƒ d  St ƒ  } | j s‡ t j |  ƒ t d ƒ d  St	 ƒ  |  _
 t j j ƒ  t j |  j  j |  ƒ t j j ƒ  t d |  j  |  j f ƒ d  S(   Ns   proxy://sF   ConnMan.put(): closing proxy connection (keep-alive not yet supported)s+   ConnMan.put(): closing over-used connections?   ConnMan.put(): closing connection (connection pooling disabled)s2   ConnMan.put(): connection put back to pool (%s#%d)(   RK   R1   R
   Rb   R   RL   t   conn_max_counterR   t   connection_poolingR   RS   R]   R^   R_   t   appendRc   (   RH   R   (    (    sJ   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/ConnMan.pyt   put"  s&    

		
c         C@  s   |  r |  j  j ƒ  n  d  S(   N(   R;   Rb   (   RH   (    (    sJ   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/ConnMan.pyRb   >  s    N(   RV   RW   R   t   _CS_REQ_SENTt   CONTINUER   R]   R_   Rg   RX   R   R-   Rj   Rb   (    (    (    sJ   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/ConnMan.pyR
   õ   s   			%(   i   i    (   t
   __future__R    R[   t   version_infot   Custom_httplib3xR   t   Custom_httplib27R   t   loggingR   t	   threadingR   R   R   t   ImportErrort   urllib.parseR   t
   ExceptionsR   R   t   UtilsR	   t   __all__t   objectR   R
   (    (    (    sJ   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/ConnMan.pyt   <module>	   s$   	Ñ