};e$LhdZeZgdZddlZddlZddlZddlZddlZddl Z ddl Z ddl m Z ddl mZddlmZddlmZddlmZmZ dd lmZ dd lmZdd lmZdd lm Z ddl!m"Z"m#Z#ddl$m%Z%e&e'ure(Z)e*Z+ne'Z)e'Z+ejXdZ-ejXdZ.ejXdZ/dZ0dZ1ejdjgdrdZ4nddlm5Z4GddeZ6Gdde7Z8Gdde8Z9GddZ:y#e$rdd lmZej2ZYwxYw#e$r dd lmZYwxYw)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_disabledN)md5)BytesIO)dumps)sleep)Httpurlnorm)proxy_info_from_environment) ProxyInfo) urlencode) Application)DatetimeJSONEncoder) HTTPError error_for)URIs^\w+://z^\w+://s[?/:|]+c t|tr|jd}n|}tj |rDt|tr#|jd}|j d}n|j d}t|tr|j d}t|j}tjd|}tjd|}tj}|dz dz }t||kDr|d|}dj!|jd|fS#t $rYwxYw) 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-8idna, N,) isinstancebytesdecodere_url_scheme_smatchencode UnicodeError unicode_typer hexdigest re_url_schemesubre_slashrmaximum_cache_filename_lengthlenjoin)filenamefilename_matchfilemd5maximum_filename_lengthmaximum_length_before_md5_sums =/usr/lib/python3/dist-packages/lazr/restfulclient/_browser.pysafenamer1Ws  h &%__W5N%N   0(E*#??73#??62#??62(L)??7+(m%%'G  h/H||D(+H *GG$;b$@1$D! 8}44::; 88X__W-w7 885    sA=D22 D>=D>cTttjjddS)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)boolosenvirongetrr0rrs  FN OOrz"/etc/ssl/certs/ca-certificates.crt)CA_CERTSc@eZdZdZdZdddeffd ZfdZdZxZ S)rzAn 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. Nct}tt|||||t||_|j |j j |yy)N)"disable_ssl_certificate_validationca_certs)rsuperr__init__SYSTEM_CA_CERTS authorizerauthorizeSession)selfrBcachetimeout proxy_info cert_disabled __class__s r0r@zRestfulHttp.__init__sZ<=  k4)   /<$ * % ?? & OO , ,T 2 'rc d|vr|d=|j|jj||||tt|||||||||| S)z4Use the authorizer to authorize an outgoing request. authorization)rBauthorizeRequestr?r_request) rDconnhost absolute_uri request_urimethodbodyheaders redirectionscachekeyrIs r0rMzRestfulHttp._requestsm g %( ?? & OO , ,fdG [$0           rcpt|jtr|jj||Sy)+Retrieve a cached value for an HTTP header.N)rrEMultipleRepresentationCache_getCachedHeader)rDuriheaders r0rZzRestfulHttp._getCachedHeaders+ djj"= >::..sF; ;r) __name__ __module__ __qualname____doc__r(r r@rMrZ __classcell__rIs@r0rrs.%(!. 3&# Jrrc6eZdZdZdZefdZdZdZdZ dZ y) AtomicFileCachezA FileCache that can be shared by multiple processes. Based on a patch found at . z.tempctjj||_||_ tj |jy#t $r(}|jtjk7rYd}~yd}~wwxYw)aUConstruct 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) r5pathnormpath _cache_dir_get_safe_namemakedirsOSErrorerrnoEEXIST)rDrEsafees r0r@zAtomicFileCache.__init__s_''**51"  KK ( ww%,,&' sA A>A99A>c|j|}|j|jrtd|jztj j |j|S)z0Return the path on disk where ``key`` is stored.z Cache key cannot start with '%s')ri startswithTEMPFILE_PREFIX ValueErrorr5rfr*rh)rDkeysafe_keys r0 _get_key_pathzAtomicFileCache._get_key_paths^&&s+   t33 42T5I5II ww||DOOX66rc|j|} t|d} |j|jS#|jwxYw#tt f$r(}|j t jk7rYd}~yd}~wwxYw)aGet 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)rvopenreadcloseIOErrorrkrlENOENT)rDrtcache_full_pathfros r0r7zAtomicFileCache.getsr,,S1 _d+A vvx  ! ww%,,&' s- AAAAAB $BB ctj|j|j\}}t j |d}|j ||j|j|}tjdk(r4tjj|rt j|t j||y)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. )prefixdirwbwin32N)tempfilemkstemprrrhr5fdopenwriter{rvsysplatformrfexistsunlinkrename)rDrtvaluehandle path_namerr~s r0setzAtomicFileCache.sets%,,''T__   IIfd #   ,,S1 <<7 "rww~~o'F IIo & )_-rc|j|} tj|y#t$r(}|jtj k7rYd}~yd}~wwxYw)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)rvr5removerkrlr})rDrtr~ros r0deletezAtomicFileCache.delete.sM,,S1  IIo & ww%,,&' s) AAAN) r]r^r_r`rrr1r@rvr7rrr8rr0rdrds* O#+" 7,.& rrdc.eZdZdZfdZdZdZxZS)rYa>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. cPtt| ||jd|_y)z>Tell FileCache to call append_media_type when generating keys.N)r?rYr@append_media_typerequest_media_type)rDrErIs r0r@z$MultipleRepresentationCache.__init__Ms( )49 4)) #'rcT|j|dz|jz}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. -)rr1)rDrts r0rz-MultipleRepresentationCache.append_media_typeTs.  " " .)d555C}rct|\}}}}|j|}|dz}t|ts|j d}|?t |D]1} | j |s| t|djcSy)rX:rN) r r7rrr!rrqr)strip) rDr[r\scheme authorityrQrV cached_value header_startlines r0rZz,MultipleRepresentationCache._getCachedHeader_s5rs=&    "=4 !&,8:C<LILI ;' "**Z( 2::k ",9^P77>>67:O4F$FR[f[|-/-`u u s =""+"<"<=! !s$D D#D D # D10D1