3 �Pf-�@s�ddlZddlZddlmZmZmZddlmZmZdZddd�Z dd�Z Gd d �d e �Z Gd d �d e �Z Gd d�de �ZGdd�de �ZdS)�N)� formatdate� parsedate� parsedate_tz)�datetime� timedeltaz%a, %d %b %Y %H:%M:%S GMTcCs|p tj�}||S)N)r�now)�delta�date�r � /usr/lib/python3.6/heuristics.py� expire_after s r cCsttj|j���S)N)r�calendar�timegmZ timetuple)Zdtr r r �datetime_to_headersrc@s$eZdZdd�Zdd�Zdd�ZdS)� 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 )�self�responser r r �warnings zBaseHeuristic.warningcCsiS)z�Update 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)N�Warning)r�headers�updater)rrZupdated_headersZwarning_header_valuer r r �apply*s   zBaseHeuristic.applyN)�__name__� __module__� __qualname__rrrr r r r rs  rc@seZdZdZdd�ZdS)� OneDayCachezM Cache the response by providing an expires 1 day in the future. cCsRi}d|jkrNt|jd�}ttdd�t|dd��d�}t|�|d<d|d<|S) N�expiresr �)Zdays�)r �publicz cache-control)rrr rrr)rrrr rr r r r;s   zOneDayCache.update_headersN)rrr�__doc__rr r r r r6src@s(eZdZdZdd�Zdd�Zdd�ZdS) � ExpiresAfterz; Cache **all** requests for a defined time period. cKstf|�|_dS)N)rr)r�kwr r r �__init__LszExpiresAfter.__init__cCst|j�}t|�dd�S)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 d�Zdd�ZdS)� 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-i�i�i�i�i�c Cs�|j}d|kriSd|kr*|ddkr*iS|j|jkr:iSd|ksJd|krNiStjt|d��}t|d�}|dks||dkr�iStj�}td||�}|tj|�}tdt |dd ��}||kr�iS||} dtj t tj | ��iS) Nrz cache-controlr r z last-modifiedr� �ii�Q) rZstatus�cacheable_by_default_statusesr rrr�time�max�minZstrftime�TIME_FMTZgmtime) r�resprr Z last_modifiedrZ current_agerZfreshness_lifetimerr r r rks*  zLastModified.update_headerscCsdS)Nr )rr1r r r r�szLastModified.warningN)rrrr!�setr,rrr r r r r%[s  r%)N)r r-Z email.utilsrrrrrr0r r�objectrrr"r%r r r r �s "