3
ûâdeæ  ã               @   sŽ   d dl Z d dlZd dlmZmZmZ d dlmZmZ dZddd„Z	dd„ Z
G d	d
„ d
eƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZdS )é    N)Ú
formatdateÚ	parsedateÚparsedate_tz)ÚdatetimeÚ	timedeltaz%a, %d %b %Y %H:%M:%S GMTc             C   s   |p
t jƒ }||  S )N)r   Úutcnow)ÚdeltaÚdate© r
   úB/tmp/pip-build-red6nood/pip/pip/_vendor/cachecontrol/heuristics.pyÚexpire_after   s    r   c             C   s   t tj| jƒ ƒƒS )N)r   ÚcalendarÚtimegmÚ	timetuple)Údtr
   r
   r   Údatetime_to_header   s    r   c               @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚBaseHeuristicc             C   s   dS )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.
        z110 - "Response is Stale"r
   )ÚselfÚresponser
   r
   r   Úwarning   s    	zBaseHeuristic.warningc             C   s   i S )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
   )r   r   r
   r
   r   Úupdate_headers!   s    zBaseHeuristic.update_headersc             C   s@   | j |ƒ}|r<|jj|ƒ | j|ƒ}|d k	r<|jjd|iƒ |S )NÚWarning)r   ÚheadersÚupdater   )r   r   Zupdated_headersZwarning_header_valuer
   r
   r   Úapply*   s    

zBaseHeuristic.applyN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r
   r
   r
   r   r      s   	r   c               @   s   e Zd ZdZdd„ ZdS )ÚOneDayCachezM
    Cache the response by providing an expires 1 day in the
    future.
    c             C   sR   i }d|j krNt|j d ƒ}ttdd�t|d d… Ž d�}t|ƒ|d< d|d< |S )	NÚexpiresr	   é   )Údaysé   )r	   Úpubliczcache-control)r   r   r   r   r   r   )r   r   r   r	   r   r
   r
   r   r   <   s    
zOneDayCache.update_headersN)r   r   r   Ú__doc__r   r
   r
   r
   r   r   6   s   r   c               @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚExpiresAfterz;
    Cache **all** requests for a defined time period.
    c             K   s   t f |Ž| _d S )N)r   r   )r   Úkwr
   r
   r   Ú__init__L   s    zExpiresAfter.__init__c             C   s   t | jƒ}t|ƒddœS )Nr#   )r   zcache-control)r   r   r   )r   r   r   r
   r
   r   r   O   s    
zExpiresAfter.update_headersc             C   s   d}|| j  S )Nz:110 - Automatically cached for %s. Response might be stale)r   )r   r   Útmplr
   r
   r   r   S   s    zExpiresAfter.warningN)r   r   r   r$   r'   r   r   r
   r
   r
   r   r%   G   s   r%   c               @   s:   e Zd ZdZdddddddd	d
ddhZdd„ Zdd„ ZdS )Ú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       
      C   sâ   |j }d|kri S d|kr*|d dkr*i S |j| jkr:i S d|ksJd|krNi S tjt|d ƒƒ}t|d ƒ}|d ks||d kr€i S tjƒ }td|| ƒ}|tj|ƒ }tdt	|d d
ƒƒ}||krÄi S || }	dtj
ttj|	ƒƒiS )Nr   zcache-controlr#   r	   zlast-modifiedr   é
   é   i  i€Q )r   ÚstatusÚcacheable_by_default_statusesr   r   r   r   ÚtimeÚmaxÚminÚstrftimeÚTIME_FMTÚgmtime)
r   Úrespr   r	   Zlast_modifiedÚnowÚcurrent_ager   Úfreshness_lifetimer   r
   r
   r   r   h   s*    zLastModified.update_headersc             C   s   d S )Nr
   )r   r8   r
   r
   r   r   †   s    zLastModified.warningN)r   r   r   r$   r1   r   r   r
   r
   r
   r   r)   X   s   r)   )N)r   r2   Úemail.utilsr   r   r   r   r   r6   r   r   Úobjectr   r   r%   r)   r
   r
   r
   r   Ú<module>   s   
"