B
    uf                 @  s   d dl mZ d dlZd dlZd dlmZmZmZ d dlmZm	Z	m
Z
 d dlmZmZmZ erhd dlmZ dZddd	d
dddZd
ddddZG dd dZG dd deZG dd deZG dd deZdS )    )annotationsN)datetime	timedeltatimezone)
formatdate	parsedateparsedate_tz)TYPE_CHECKINGAnyMapping)HTTPResponsez%a, %d %b %Y %H:%M:%S GMTr   zdatetime | Noner   )deltadatereturnc             C  s   |pt tj}||  S )N)r   nowr   utc)r   r    r   Y/home/ankuromar296_gmail_com/myenv/lib/python3.7/site-packages/cachecontrol/heuristics.pyexpire_after   s    r   str)dtr   c             C  s   t t|  S )N)r   calendartimegm	timetuple)r   r   r   r   datetime_to_header   s    r   c               @  s<   e Zd ZdddddZdddddZdddd	d
ZdS )BaseHeuristicr   z
str | None)responser   c             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   )selfr   r   r   r   warning   s    	zBaseHeuristic.warningzdict[str, str]c             C  s   i S )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   )r   r   r   r   r   update_headers'   s    zBaseHeuristic.update_headersc             C  s@   |  |}|r<|j| | |}|d k	r<|jd|i |S )NWarning)r   headersupdater   )r   r   Zupdated_headersZwarning_header_valuer   r   r   apply0   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dddZdS )OneDayCachezM
    Cache the response by providing an expires 1 day in the
    future.
    r   zdict[str, str])r   r   c             C  sZ   i }d|j krVt|j d }ttddt|d d dtjid}t||d< d|d	< |S )
Nexpiresr      )days   tzinfo)r   publiczcache-control)r!   r   r   r   r   r   r   r   )r   r   r!   r   r(   r   r   r   r   B   s    
&zOneDayCache.update_headersN)r$   r%   r&   __doc__r   r   r   r   r   r'   <   s   r'   c               @  s@   e Zd ZdZdddddZddd	d
dZddd	ddZdS )ExpiresAfterz;
    Cache **all** requests for a defined time period.
    r
   None)kwr   c             K  s   t f || _d S )N)r   r   )r   r1   r   r   r   __init__R   s    zExpiresAfter.__init__r   zdict[str, str])r   r   c             C  s   t | j}t|ddS )Nr-   )r(   zcache-control)r   r   r   )r   r   r(   r   r   r   r   U   s    
zExpiresAfter.update_headersz
str | Nonec             C  s   d}|| j  S )Nz:110 - Automatically cached for %s. Response might be stale)r   )r   r   tmplr   r   r   r   Y   s    zExpiresAfter.warningN)r$   r%   r&   r.   r2   r   r   r   r   r   r   r/   M   s   r/   c               @  sJ   e Zd ZdZdddddddd	d
ddhZdddddZddd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  r   zdict[str, str])respr   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|d }|d k	sftt|d d }t|d }|d kri S t }t	d|| }|t| }t	dt
|d d	}	|	|kri S ||	 }
dttt|
iS )
Nr(   zcache-controlr-   r   zlast-modifiedr+   r   
   iQ )r!   statuscacheable_by_default_statusesr   AssertionErrorr   r   r   timemaxminstrftimeTIME_FMTgmtime)r   r9   r!   Z
time_tupler   Zlast_modifiedr   current_ager   freshness_lifetimer(   r   r   r   r   y   s.    zLastModified.update_headersz
str | Nonec             C  s   d S )Nr   )r   r9   r   r   r   r      s    zLastModified.warningN)r$   r%   r&   r.   r<   r   r   r   r   r   r   r4   ^   s    r4   )N)
__future__r   r   r>   r   r   r   email.utilsr   r   r   typingr	   r
   r   urllib3r   rB   r   r   r   r'   r/   r4   r   r   r   r   <module>   s   !