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 re(Z)e*Z+ne'Z)e'Z+e,dZ-e,dZ.e,dZ/dd Z0dd Z1ej23drd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 )!zBrowser 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 )	zReturn 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 )zWhether 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   zAn 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 )AtomicFileCachezA 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 )zSet ``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 )zDelete ``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 )zAppend 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 )zInitialize, 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  s2   







	
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^   



/.\.