3 υ_-@sddlZddlZddlmZmZmZddlmZmZdZdddZ ddZ Gd d d e Z Gd d d e Z Gd dde ZGddde ZdS)N) formatdate parsedate parsedate_tz)datetime timedeltaz%a, %d %b %Y %H:%M:%S GMTcCs|p tj}||S)N)rnow)deltadater /usr/lib/python3.6/heuristics.py expire_after s r cCsttj|jS)N)rcalendartimegmZ timetuple)Zdtr r r datetime_to_headersrc@s$eZdZddZddZddZdS) BaseHeuristiccCsdS)a! Return a valid 1xx warning header value describing the cache adjustments. The response is provided too allow warnings like 113 http://tools.ietf.org/html/rfc7234#section-5.5.4 where we need to explicitly say response is over 24 hours old. z110 - "Response is Stale"r )selfresponser r r warnings zBaseHeuristic.warningcCsiS)zUpdate the response headers with any new headers. NOTE: This SHOULD always include some Warning header to signify that the response was cached by the client, not by way of the provided headers. r )rrr r r update_headers!szBaseHeuristic.update_headerscCs@|j|}|r<|jj||j|}|dk r<|jjd|i|S)NWarning)rheadersupdater)rrZupdated_headersZwarning_header_valuer r r apply*s   zBaseHeuristic.applyN)__name__ __module__ __qualname__rrrr r r r rs  rc@seZdZdZddZdS) OneDayCachezM Cache the response by providing an expires 1 day in the future. cCsRi}d|jkrNt|jd}ttddt|ddd}t||d<d|d<|S) Nexpiresr )Zdays)r publicz cache-control)rrr rrr)rrrr rr r r r;s   zOneDayCache.update_headersN)rrr__doc__rr r r r r6src@s(eZdZdZddZddZddZdS) ExpiresAfterz; Cache **all** requests for a defined time period. cKstf||_dS)N)rr)rkwr r r __init__LszExpiresAfter.__init__cCst|j}t|ddS)Nr )rz cache-control)r rr)rrrr r r rOs zExpiresAfter.update_headerscCsd}||jS)Nz:110 - Automatically cached for %s. Response might be stale)r)rrZtmplr r r rVszExpiresAfter.warningN)rrrr!r$rrr r r r r"Gsr"c @s>eZdZdZedddddddd d d d g Zd dZddZdS) LastModifieda If there is no Expires header already, fall back on Last-Modified using the heuristic from http://tools.ietf.org/html/rfc7234#section-4.2.2 to calculate a reasonable value. Firefox also does something like this per https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching_FAQ http://lxr.mozilla.org/mozilla-release/source/netwerk/protocol/http/nsHttpResponseHead.cpp#397 Unlike mozilla we limit this to 24-hr. i,i-iiiiic Cs|j}d|kriSd|kr*|ddkr*iS|j|jkr:iSd|ksJd|krNiStjt|d}t|d}|dks||dkriStj}td||}|tj|}tdt |dd }||kriS||} dtj t tj | iS) Nrz cache-controlr r z last-modifiedr iiQ) rZstatuscacheable_by_default_statusesr rrrtimemaxminZstrftimeTIME_FMTZgmtime) rresprr Z last_modifiedrZ current_agerZfreshness_lifetimerr r r rks*  zLastModified.update_headerscCsdS)Nr )rr1r r r rszLastModified.warningN)rrrr!setr,rrr r r r r%[s  r%)N)r r-Z email.utilsrrrrrr0r robjectrrr"r%r r r r s "