3 Pf2@sdZddlZddlZddlZddlZddlmZddlmZddl m Z ddl m Z ej eZejdZd d ZGd d d eZdS) z7 The httplib2 algorithms ported for use with requests. N) parsedate_tz)CaseInsensitiveDict) DictCache) Serializerz9^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?cCs0tj|j}|d|d|d|d|dfS)zParses a URI using the regex given in Appendix B of RFC 3986. (scheme, authority, path, query, fragment) = parse_uri(uri) r)URImatchgroups)urir r /usr/lib/python3.6/controller.py parse_urisrc@s\eZdZdZdddZeddZedd Zd d Zd d Z ddZ dddZ ddZ dS)CacheControllerz9An interface to see if request should cached or not. NTcCs"|pt|_||_|pt|_dS)N)rcache cache_etagsr serializer)selfrrrrrr__init__!s zCacheController.__init__c Cslt|\}}}}}| s| r*td||j}|j}|sBd}|rTdj||gpV|}|d||}|S)z4Normalize the URL to create a safe key for the cachez(Only absolute URIs are allowed. uri = %s/?z://)r Exceptionlowerjoin) clsrschemeZ authoritypathZqueryZfragmentZ request_uriZ defrag_urirrr_urlnorm&s  zCacheController._urlnormcCs |j|S)N)r )rrrrr cache_url:szCacheController.cache_urlcCsVi}d}d|krd}||krR||jd}dd|D}dd|D}t||}|S)zz Parse the cache control headers returning a dictionary with values for the different directives. z cache-controlz Cache-Control,cSs4g|],}d|jdkrtdd|jddDqS)r=cSsg|]}|jjqSr)stripr).0xrrr LszBCacheController.parse_cache_control...)findtuplesplit)r%partrrrr'Lsz7CacheController.parse_cache_control..cSs*g|]"}d|jdkr|jjdfqS)rr#r()r)r$r)r%namerrrr'Ps)r+dict)rheadersZretvalZ cc_headerpartsZparts_with_argsZ parts_wo_argsrrrparse_cache_control>s z#CacheController.parse_cache_controlcCs|j|j}tjd||j|j}d|kr:tjddSd|kr\|ddkr\tjddS|jj|}|dkr~tjd dS|jj ||}|stj d dS|j d krd }tj||St |j}| sd |krd|krtjd|jj |tjddStj}tjt|d } td|| } tjd| |j|} d} d| krt| djrtt| d} tjd| nDd|krt|d} | dk rtj| | }td|} tjd| d|kryt|d} tjd| Wntk rd} YnXd|krDyt|d}Wntk r.d}YnX| |7} tjd| | | krjtjdtjd| | |Sd|krtjd|jj |dS)ze Return a cached response if it exists in the cache, otherwise return False. zLooking up "%s" in the cachezno-cachez-Request header has "no-cache", cache bypassedFzmax-agerz1Request header has "max_age" as 0, cache bypassedNzNo cache entry availablez1Cache entry deserialization failed, entry ignoredi-zVReturning cached "301 Moved Permanently" response (ignoring date and etag information)dateetagz(Purging cached response: no date or etagz!Ignoring cached response: no datezCurrent age based on date: %iz#Freshness lifetime from max-age: %iexpiresz#Freshness lifetime from expires: %iz+Freshness lifetime from request max-age: %iz min-freshz'Adjusted current age from min-fresh: %iz2The response is "fresh", returning cached responsez%i > %iz4The cached response is "stale" with no etag, purging)r!urlloggerdebugr1r/rgetrloadsZwarningstatusrdeletetimecalendarZtimegmrmaxisdigitint ValueError)rrequestr!ccZ cache_datarespmsgr/Znowr2Z current_ageZresp_ccZfreshness_lifetimer4Z expire_timeZ min_freshrrrcached_requestVs                               zCacheController.cached_requestcCs`|j|j}|jj||jj|}i}|r\t|j}d|krH|d|d<d|kr\|d|d<|S)Nr3ZETagz If-None-Matchz last-modifiedz Last-ModifiedzIf-Modified-Since)r!r5rr9rr8rr/)rrBr!rDZ new_headersr/rrrconditional_headerss    z#CacheController.conditional_headersc Csddddg}|j|kr*tjd|j|dSt|j}|dk rhd|krh|djrht|dt|krhdS|j|j}|j|}|j |j }tjd|d } |j d rd } tjd |j d rd } tjd | r|j j |rtjd|j j ||jod|kr(tjd|j j||jj|||dn|jdkrXtjd|j j||jj||nd|kr|r|j dr|djrt|ddkrtjd|j j||jj|||dn:d|kr|drtjd|j j||jj|||ddS)zc Algorithm for caching requests. This assumes a requests Response object. i,i-zStatus code %s not in %sNzcontent-lengthz&Updating cache with response from "%s"Fzno-storeTzResponse header has "no-store"zRequest header has "no-store"z0Purging existing cache entry to honor "no-store"r3zCaching due to etag)bodyzCaching permanant redirectr2zmax-agerz'Caching b/c date exists and max-age > 0r4zCaching b/c of expires header)r:r6r7rr/r?r@lenr1r!r5r8rr;rsetrdumps) rrBresponserJZcacheable_status_codesZresponse_headersZcc_reqrCr!Zno_storerrrcache_responsesd                       zCacheController.cache_responsecsv|j|j}|jj||jj|}|s*|Sdg|jjtfdd|jj Dd|_ |jj ||jj |||S)zOn a 304 we will get a new set of headers that we want to update our cached value with, assuming we have one. This should only ever be called when we've sent an ETag and gotten a 304 as the response. zcontent-lengthc3s&|]\}}|jkr||fVqdS)N)r)r%kv)excluded_headersrr Tsz9CacheController.update_cached_response..rH) r!r5rr9rr8r/updater.itemsr:rLrM)rrBrNr!Zcached_responser)rRrupdate_cached_response6s  z&CacheController.update_cached_response)NTN)N) __name__ __module__ __qualname____doc__r classmethodr r!r1rFrGrOrVrrrrrs   y Wr)rZZloggingrer=r<Z email.utilsrZpip._vendor.requests.structuresrrrZ serializerZ getLoggerrWr6compiler robjectrrrrrs