o
    ‹`J  ã                   @   sÊ  d Z eZg d¢ZddlZddlZddlmZ ddlm	Z	 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 ddlmZmZ zdd	lmZ W n ey^   dd
lmZ ejZY nw zddlmZ W n eyv   ddlmZ Y nw ddlmZ ddlm Z  ddl!m"Z"m#Z# ddl$m%Z% e&e'u rše(Z)e*Z+ne'Z)e'Z+e ,d¡Z-e ,d¡Z.e ,d¡Z/dd„ Z0dd„ Z1ej2 3d¡r¾dZ4nddlm5Z4 G dd„ deƒZ6G dd„ de7ƒZ8G dd„ de8ƒZ9G dd „ d ƒZ:dS )!zïBrowser object to make requests of lazr.restful web services.

The `Browser` class does some massage of HTTP requests and responses,
and handles custom caches. It is not part of the public
lazr.restfulclient API. (But maybe it should be?)
)ÚBrowserÚRestfulHttpÚ#ssl_certificate_validation_disabledé    N)Úmd5)ÚBytesIO)Údumps)Úsleep)ÚHttpÚurlnorm)Úproxy_info_from_environment)Ú	ProxyInfo)Ú	urlencode)ÚApplication)ÚURI)Ú	error_forÚ	HTTPError)ÚDatetimeJSONEncoders   ^\w+://z^\w+://s   [?/:|]+c                 C   sÜ   z)t | tƒr|  d¡}n| }t |¡r(t | tƒr#|  d¡} |  d¡} n|  d¡} W n	 ty2   Y nw t | tƒr=|  d¡} t| ƒ 	¡ }t
 d| ¡} t d| ¡} tj}|d d }t| ƒ|krd| d|… } d |  d¡|f¡S )	z•Return a filename suitable for the cache.

    Strips dangerous and common characters to create a filename we
    can use to store the cache in.
    úutf-8Úidnaó    ó   ,é    é   Nú,)Ú
isinstanceÚbytesÚdecodeÚre_url_scheme_sÚmatchÚencodeÚUnicodeErrorÚunicode_typer   Ú	hexdigestÚre_url_schemeÚsubÚre_slashr   Úmaximum_cache_filename_lengthÚlenÚjoin)ÚfilenameÚfilename_matchÚfilemd5Úmaximum_filename_lengthÚmaximum_length_before_md5_sum© r.   ú=/usr/lib/python3/dist-packages/lazr/restfulclient/_browser.pyÚsafenameU   s.   




€ÿ

r0   c                   C   s   t tj dd¡ƒS )zûWhether the user has disabled SSL certificate connection.

    Some testing servers have broken certificates.  Rather than raising an
    error, we allow an environment variable,
    ``LP_DISABLE_SSL_CERTIFICATE_VALIDATION`` to disable the check.
    Ú%LP_DISABLE_SSL_CERTIFICATE_VALIDATIONF)ÚboolÚosÚenvironÚgetr.   r.   r.   r/   r   „   s   ÿr   z"/etc/ssl/certs/ca-certificates.crt)ÚCA_CERTSc                       sB   e Zd ZdZdZdddef‡ fdd„	Z‡ fdd„Zdd	„ Z‡  Z	S )
r   zúAn Http subclass with some custom behavior.

    This Http client uses the TE header instead of the Accept-Encoding
    header to ask for compressed representations. It also knows how to
    react when its cache is a MultipleRepresentationCache.
    é�   Nc                    sD   t ƒ }tt| ƒj||||td� || _| jd ur | j | ¡ d S d S )N)Ú"disable_ssl_certificate_validationÚca_certs)r   Úsuperr   Ú__init__ÚSYSTEM_CA_CERTSÚ
authorizerÚauthorizeSession)Úselfr=   ÚcacheÚtimeoutÚ
proxy_infoÚcert_disabled©Ú	__class__r.   r/   r;   Ÿ   s   
ý
ÿzRestfulHttp.__init__c
           
         sJ   d|v r|d= | j dur| j  ||||¡ tt| ƒ |||||||||	¡	S )z4Use the authorizer to authorize an outgoing request.ÚauthorizationN)r=   ÚauthorizeRequestr:   r   Ú_request)
r?   ÚconnÚhostÚabsolute_uriÚrequest_uriÚmethodÚbodyÚheadersÚredirectionsÚcachekeyrD   r.   r/   rH   ª   s   
ÿ
þzRestfulHttp._requestc                 C   s   t | jtƒr| j ||¡S dS )ú+Retrieve a cached value for an HTTP header.N)r   r@   ÚMultipleRepresentationCacheÚ_getCachedHeader)r?   ÚuriÚheaderr.   r.   r/   rT   ¼   s   zRestfulHttp._getCachedHeader)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r&   r   r;   rH   rT   Ú__classcell__r.   r.   rD   r/   r   •   s    ÿr   c                   @   s@   e Zd ZdZdZefdd„Zdd„ Zdd„ Zd	d
„ Z	dd„ Z
dS )ÚAtomicFileCachez—A FileCache that can be shared by multiple processes.

    Based on a patch found at
    <http://code.google.com/p/httplib2/issues/detail?id=125>.
    z.tempc              
   C   s\   t j |¡| _|| _z	t  | j¡ W dS  ty- } z|jtjkr"‚ W Y d}~dS d}~ww )aU  Construct an ``AtomicFileCache``.

        :param cache: The directory to use as a cache.
        :param safe: A function that takes a key and returns a name that's
            safe to use as a filename.  The key must never return a string
            that begins with ``TEMPFILE_PREFIX``.  By default uses
            ``safename``.
        N)	r3   ÚpathÚnormpathÚ
_cache_dirÚ_get_safe_nameÚmakedirsÚOSErrorÚerrnoÚEEXIST)r?   r@   ÚsafeÚer.   r.   r/   r;   Ì   s   	ÿ€ÿzAtomicFileCache.__init__c                 C   s4   |   |¡}| | j¡rtd| j ƒ‚tj | j|¡S )z0Return the path on disk where ``key`` is stored.z Cache key cannot start with '%s')r`   Ú
startswithÚTEMPFILE_PREFIXÚ
ValueErrorr3   r]   r(   r_   )r?   ÚkeyÚsafe_keyr.   r.   r/   Ú_get_key_pathÝ   s   
ÿzAtomicFileCache._get_key_pathc              
   C   sn   |   |¡}zt|dƒ}z
| ¡ W | ¡  W S | ¡  w  ttfy6 } z|jtjkr+‚ W Y d}~dS d}~ww )a¤  Get the value of ``key`` if set.

        This behaves slightly differently to ``FileCache`` in that if
        ``set()`` fails to store a key, this ``get()`` will behave as if that
        key were never set whereas ``FileCache`` returns the empty string.

        :param key: The key to retrieve.  Must be either bytes or unicode
            text.
        :return: The value of ``key`` if set, None otherwise.
        ÚrbN)rl   ÚopenÚreadÚcloseÚIOErrorrb   rc   ÚENOENT)r?   rj   Úcache_full_pathÚfrf   r.   r.   r/   r5   è   s   

ÿ€ÿzAtomicFileCache.getc                 C   sn   t j| j| jd�\}}t |d¡}| |¡ | ¡  |  |¡}t	j
dkr/tj |¡r/t |¡ t ||¡ dS )z±Set ``key`` to ``value``.

        :param key: The key to set.  Must be either bytes or unicode text.
        :param value: The value to set ``key`` to.  Must be bytes.
        )ÚprefixÚdirÚwbÚwin32N)ÚtempfileÚmkstemprh   r_   r3   ÚfdopenÚwriterp   rl   ÚsysÚplatformr]   ÚexistsÚunlinkÚrename)r?   rj   ÚvalueÚhandleÚ	path_namert   rs   r.   r.   r/   Úsetþ   s   
ÿ


zAtomicFileCache.setc              
   C   sP   |   |¡}zt |¡ W dS  ty' } z|jtjkr‚ W Y d}~dS d}~ww )z·Delete ``key`` from the cache.

        If ``key`` has not already been set then has no effect.

        :param key: The key to delete.  Must be either bytes or unicode text.
        N)rl   r3   Úremoverb   rc   rr   )r?   rj   rs   rf   r.   r.   r/   Údelete  s   
ÿ€ÿzAtomicFileCache.deleteN)rW   rX   rY   rZ   rh   r0   r;   rl   r5   r…   r‡   r.   r.   r.   r/   r\   Ã   s    r\   c                       s0   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Z‡  ZS )rS   a>  A cache that can hold different representations of the same resource.

    If a resource has two representations with two media types,
    FileCache will only store the most recently fetched
    representation. This cache can keep track of multiple
    representations of the same resource.

    This class works on the assumption that outside calling code sets
    an instance's request_media_type attribute to the value of the
    'Accept' header before initiating the request.

    This class is very much not thread-safe, but FileCache isn't
    thread-safe anyway.
    c                    s   t t| ƒ || j¡ d| _dS )z>Tell FileCache to call append_media_type when generating keys.N)r:   rS   r;   Úappend_media_typeÚrequest_media_type)r?   r@   rD   r.   r/   r;   .  s   
ÿ
z$MultipleRepresentationCache.__init__c                 C   s    | j dur|d | j  }t|ƒS )z×Append the request media type to the cache key.

        This ensures that representations of the same resource will be
        cached separately, so long as they're served as different
        media types.
        Nú-)r‰   r0   )r?   rj   r.   r.   r/   rˆ   4  s   
z-MultipleRepresentationCache.append_media_typec           
      C   sr   t |ƒ\}}}}|  |¡}|d }t|tƒs| d¡}|dur7t|ƒD ]}	|	 |¡r6|	t|ƒd…  ¡   S q#dS )rR   ú:r   N)	r
   r5   r   r   r   r   rg   r'   Ústrip)
r?   rU   rV   ÚschemeÚ	authorityrL   rQ   Úcached_valueÚheader_startÚliner.   r.   r/   rT   ?  s   



ÿz,MultipleRepresentationCache._getCachedHeader)rW   rX   rY   rZ   r;   rˆ   rT   r[   r.   r.   rD   r/   rS     s
    rS   c                   @   sz   e Zd ZdZeƒ ZdZddddefdd„Zdd„ Z		dd
d„Z	ddd„Z
dd„ Zdd„ Zddd„Zdd„ Zddd„ZdS )r   z6A class for making calls to lazr.restful web services.é   Nc                 C   sR   |du rt  ¡ }t tj|¡ t|tƒrt|ƒ}| 	||||¡| _
|| _|| _dS )zåInitialize, possibly creating a cache.

        If no cache is provided, a temporary directory will be used as
        a cache. The temporary directory will be automatically removed
        when the Python process exits.
        N)ry   ÚmkdtempÚatexitÚregisterÚshutilÚrmtreer   Ú	str_typesrS   ÚhttpFactoryÚ_connectionÚ
user_agentÚmax_retries)r?   Úservice_rootÚcredentialsr@   rA   rB   r›   rœ   r.   r.   r/   r;   S  s   
ÿ
zBrowser.__init__c           	      C   sl   t d| jd ƒD ])}| jj||||d�\}}|jdv r-|| jk r-td|d  ƒ}t|ƒ q ||fS ||fS )Nr   r   ©rM   rN   rO   )iö  i÷  é   )Úrangerœ   rš   ÚrequestÚstatusÚintr   )	r?   ÚurlrM   rN   rO   Úretry_countÚresponseÚcontentÚ	sleep_forr.   r.   r/   Ú_request_and_retrye  s   
ÿ


zBrowser._request_and_retryÚGETúapplication/jsonc           
      C   sÒ   |dkrt dƒ‚d|i}| jdur| j|d< t| jjtƒr"|| jj_|dur+| |¡ | jt	|ƒ|||d�\}}|j
dkrZ|dkrSd	|v sId
|v rN|| jfS t||ƒ‚d|_
||fS t||ƒ}	|	dure|	‚||fS )z'Create an authenticated request object.ztag:launchpad.net:2008:redactedzUYou tried to access a resource that you don't have the server-side permission to see.ÚAcceptNz
User-AgentrŸ   i0  r   zIf-None-MatchzIf-Modified-SinceéÈ   )ri   r›   r   rš   r@   rS   r‰   Úupdaterª   Ústrr£   ÚNOT_MODIFIEDr   r   )
r?   r¥   ÚdatarM   Ú
media_typeÚextra_headersrO   r§   r¨   Úerrorr.   r.   r/   rH   t  s0   




ÿ


	
zBrowser._requestFc                 C   sH   t |ttfƒr
|}n	| d¡}| ¡ }| j||d�\}}|r"||fS |S )z2GET a representation of the given resource or URI.r5   ©r´   )r   r˜   r   Ú
get_methodÚbuild_request_urlrH   )r?   Úresource_or_urirO   Úreturn_responser¥   rM   r§   r¨   r.   r.   r/   r5   °  s   
zBrowser.getc                 C   s<   d}| j ||d�\}}t|ƒ}t|tƒs| d¡}t||ƒS )z?GET a WADL representation of the resource at the requested url.zapplication/vnd.sun.wadl+xml)r³   r   )rH   r°   r   r   r   r   )r?   r¥   Ú	wadl_typer§   r¨   r.   r.   r/   Úget_wadl_application¼  s   


zBrowser.get_wadl_applicationc                 K   s   ||d< t |ƒ}|  ||d¡S )z"POST a request to the web service.zws.opÚPOST)r   rH   )r?   r¥   Úmethod_nameÚkwsr²   r.   r.   r/   ÚpostÅ  s   zBrowser.postc                 C   s,   d|i}|dur|  |¡ | j||d|d�S )z(PUT the given representation to the URL.úContent-TypeNÚPUTr¶   )r¯   rH   )r?   r¥   Úrepresentationr³   rO   r´   r.   r.   r/   ÚputË  s   
ÿzBrowser.putc                 C   s   | j |dd� dS )z%DELETE the resource at the given URL.ÚDELETE)rM   N)rH   )r?   r¥   r.   r.   r/   r‡   Ó  s   zBrowser.deletec                 C   s^   ddi}|dur|  |¡ | j t|ƒd¡}|dur"| jjs"||d< | j|t|td�d|d�S )	z8PATCH the object at url with the updated representation.rÁ   r¬   NÚetagzIf-Match)ÚclsÚPATCHr¶   )r¯   rš   rT   r°   Úignore_etagrH   r   r   )r?   r¥   rÃ   rO   r´   Úcached_etagr.   r.   r/   ÚpatchØ  s   
þzBrowser.patch)Nr«   r¬   N)NF)N)rW   rX   rY   rZ   Úobjectr±   ÚMAX_RETRIESr;   rª   rH   r5   r¼   rÀ   rÄ   r‡   rË   r.   r.   r.   r/   r   M  s"    
ÿ
ÿ
<	
r   );rZ   ÚtypeÚ__metaclass__Ú__all__r”   rc   Úhashlibr   Úior   Újsonr   r3   Úrer–   r}   ry   Útimer   Úhttplib2r	   r
   r   ÚImportErrorr   Úfrom_environmentÚurllib.parser   ÚurllibÚwadllib.applicationr   Úlazr.urir   Úlazr.restfulclient.errorsr   r   Úlazr.restfulclient._jsonr   r   r°   Úunicoder!   Ú
basestringr˜   Úcompiler#   r   r%   r0   r   r]   r   r<   r6   r   rÌ   r\   rS   r   r.   r.   r.   r/   Ú<module>   s^   
þÿ


/.\.