3
 [ð]9/  ã               @   sð   d dl m Z  d dl mZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z	 dd	lm
Z
 dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ G dd„ deƒZG dd„ deƒZdS )é    )Údatetime)Ú	timedeltaé   )Ústring_types)ÚCallbackDict)Údump_age)Údump_header)Údump_options_header)Ú	http_date)Ú	parse_age)Ú
parse_date)Úparse_options_header)Úparse_set_header)Úcached_property)Úenviron_property)Úget_content_type)Úheader_property)Úget_content_lengthc               @   s˜   e Zd ZdZeddd�Zedd„ ƒZeddd�Zed	d
d�Z	eddd�Z
eddedd�Zeddedd�Zdd„ Zedd„ ƒZedd„ ƒZedd„ ƒZdS )ÚCommonRequestDescriptorsMixinzÜA mixin for :class:`BaseRequest` subclasses.  Request objects that
    mix this class in will automatically get descriptors for a couple of
    HTTP headers with automatic type conversion.

    .. versionadded:: 0.5
    ÚCONTENT_TYPEzçThe Content-Type entity-header field indicates the media
        type of the entity-body sent to the recipient or, in the case of
        the HEAD method, the media type that would have been sent had
        the request been a GET.)Údocc             C   s
   t | jƒS )zìThe Content-Length entity-header field indicates the size of the
        entity-body in bytes or, in the case of the HEAD method, the size of
        the entity-body that would have been sent had the request been a
        GET.
        )r   Úenviron)Úself© r   úH/tmp/pip-build-w1as9q70/Werkzeug/werkzeug/wrappers/common_descriptors.pyÚcontent_length%   s    z,CommonRequestDescriptorsMixin.content_lengthZHTTP_CONTENT_ENCODINGa�  The Content-Encoding entity-header field is used as a
        modifier to the media-type. When present, its value indicates
        what additional content codings have been applied to the
        entity-body, and thus what decoding mechanisms must be applied
        in order to obtain the media-type referenced by the Content-Type
        header field.

        .. versionadded:: 0.9ZHTTP_CONTENT_MD5a‹  The Content-MD5 entity-header field, as defined in
        RFC 1864, is an MD5 digest of the entity-body for the purpose of
        providing an end-to-end message integrity check (MIC) of the
        entity-body. (Note: a MIC is good for detecting accidental
        modification of the entity-body in transit, but is not proof
        against malicious attacks.)

        .. versionadded:: 0.9ZHTTP_REFERERzúThe Referer[sic] request-header field allows the client
        to specify, for the server's benefit, the address (URI) of the
        resource from which the Request-URI was obtained (the
        "referrer", although the header field is misspelled).Z	HTTP_DATENz¢The Date general-header field represents the date and
        time at which the message was originated, having the same
        semantics as orig-date in RFC 822.ZHTTP_MAX_FORWARDSzÙThe Max-Forwards request-header field provides a
        mechanism with the TRACE and OPTIONS methods to limit the number
        of proxies or gateways that can forward the request to the next
        inbound server.c             C   s"   t | dƒst| jjddƒƒ| _d S )NÚ_parsed_content_typer   Ú )Úhasattrr   r   Úgetr   )r   r   r   r   Ú_parse_content_type]   s    
z1CommonRequestDescriptorsMixin._parse_content_typec             C   s   | j ƒ  | jd jƒ S )zñLike :attr:`content_type`, but without parameters (eg, without
        charset, type etc.) and always lowercase.  For example if the content
        type is ``text/HTML; charset=utf-8`` the mimetype would be
        ``'text/html'``.
        r   )r    r   Úlower)r   r   r   r   Úmimetypec   s    z&CommonRequestDescriptorsMixin.mimetypec             C   s   | j ƒ  | jd S )z¨The mimetype parameters as dict.  For example if the content
        type is ``text/html; charset=utf-8`` the params would be
        ``{'charset': 'utf-8'}``.
        é   )r    r   )r   r   r   r   Úmimetype_paramsm   s    z-CommonRequestDescriptorsMixin.mimetype_paramsc             C   s   t | jjddƒƒS )aj  The Pragma general-header field is used to include
        implementation-specific directives that might apply to any recipient
        along the request/response chain.  All pragma directives specify
        optional behavior from the viewpoint of the protocol; however, some
        systems MAY require that behavior be consistent with the directives.
        ZHTTP_PRAGMAr   )r   r   r   )r   r   r   r   Úpragmav   s    z$CommonRequestDescriptorsMixin.pragma)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úcontent_typer   r   Úcontent_encodingÚcontent_md5Zreferrerr   ÚdateÚintZmax_forwardsr    Úpropertyr"   r$   r%   r   r   r   r   r      s8   	
	r   c               @   s  e Zd ZdZedd„ ƒZejdd„ ƒZedd„ ƒZeddd	�Z	ed
de
edd	�Zeddd	�Zeddeedd	�Zeddd	�Zeddd	�Zeddd	�Zeddeedd	�Zeddeedd	�Zeddeedd	�Zedd„ ƒZejdd„ ƒZd(d d!„Zed"d#d	�Zed$d%d	�Zed&d'd	�Z[dS ))ÚCommonResponseDescriptorsMixinzÃA mixin for :class:`BaseResponse` subclasses.  Response objects that
    mix this class in will automatically get descriptors for a couple of
    HTTP headers with automatic type conversion.
    c             C   s&   | j jdƒ}|r"|jdƒd jƒ S dS )z0The mimetype (content type without charset etc.)zcontent-typeú;r   N)Úheadersr   ÚsplitÚstrip)r   Úctr   r   r   r"   ‡   s    z'CommonResponseDescriptorsMixin.mimetypec             C   s   t || jƒ| jd< d S )NzContent-Type)r   Úcharsetr2   )r   Úvaluer   r   r   r"   Ž   s    c                s,   ‡ fdd„}t ˆ jjddƒƒd }t||ƒS )zÆThe mimetype parameters as dict. For example if the
        content type is ``text/html; charset=utf-8`` the params would be
        ``{'charset': 'utf-8'}``.

        .. versionadded:: 0.5
        c                s   t ˆ j| ƒˆ jd< d S )NzContent-Type)r	   r"   r2   )Úd)r   r   r   Ú	on_update›   s    zACommonResponseDescriptorsMixin.mimetype_params.<locals>.on_updatezcontent-typer   r#   )r   r2   r   r   )r   r9   r8   r   )r   r   r$   ’   s    	z.CommonResponseDescriptorsMixin.mimetype_paramsÚLocationzÈThe Location response-header field is used to redirect
        the recipient to a location other than the Request-URI for
        completion of the request or identification of a new
        resource.)r   ZAgeNa  The Age response-header field conveys the sender's
        estimate of the amount of time since the response (or its
        revalidation) was generated at the origin server.

        Age values are non-negative decimal integers, representing time
        in seconds.zContent-TypezçThe Content-Type entity-header field indicates the media
        type of the entity-body sent to the recipient or, in the case of
        the HEAD method, the media type that would have been sent had
        the request been a GET.zContent-Lengtha  The Content-Length entity-header field indicates the size
        of the entity-body, in decimal number of OCTETs, sent to the
        recipient or, in the case of the HEAD method, the size of the
        entity-body that would have been sent had the request been a
        GET.zContent-LocationzîThe Content-Location entity-header field MAY be used to
        supply the resource location for the entity enclosed in the
        message when that entity is accessible from a location separate
        from the requested resource's URI.zContent-Encodingab  The Content-Encoding entity-header field is used as a
        modifier to the media-type. When present, its value indicates
        what additional content codings have been applied to the
        entity-body, and thus what decoding mechanisms must be applied
        in order to obtain the media-type referenced by the Content-Type
        header field.zContent-MD5al  The Content-MD5 entity-header field, as defined in
        RFC 1864, is an MD5 digest of the entity-body for the purpose of
        providing an end-to-end message integrity check (MIC) of the
        entity-body. (Note: a MIC is good for detecting accidental
        modification of the entity-body in transit, but is not proof
        against malicious attacks.)ÚDatez¢The Date general-header field represents the date and
        time at which the message was originated, having the same
        semantics as orig-date in RFC 822.ZExpiresz®The Expires entity-header field gives the date/time after
        which the response is considered stale. A stale cache entry may
        not normally be returned by a cache.zLast-Modifiedz”The Last-Modified entity-header field indicates the date
        and time at which the origin server believes the variant was
        last modified.c             C   s>   | j jdƒ}|dkrdS |jƒ r6tjƒ tt|ƒd� S t|ƒS )a   The Retry-After response-header field can be used with a
        503 (Service Unavailable) response to indicate how long the
        service is expected to be unavailable to the requesting client.

        Time in seconds until expiration or date.
        zretry-afterN)Úseconds)r2   r   Úisdigitr   Úutcnowr   r.   r   )r   r7   r   r   r   Úretry_afterû   s    z*CommonResponseDescriptorsMixin.retry_afterc             C   sH   |d krd| j kr| j d= d S t|tƒr2t|ƒ}nt|ƒ}|| j d< d S )Nzretry-afterzRetry-After)r2   Ú
isinstancer   r
   Ústr)r   r7   r   r   r   r?   
  s    


c                s&   ‡ fdd„}‡ fdd„}t |||d�S )Nc                s    ‡‡ fdd„}t ˆ jjˆƒ|ƒS )Nc                s0   |  rˆ ˆj krˆj ˆ = n| r,| jƒ ˆj ˆ < d S )N)r2   Z	to_header)Z
header_set)Únamer   r   r   r9     s    
zMCommonResponseDescriptorsMixin._set_property.<locals>.fget.<locals>.on_update)r   r2   r   )r   r9   )rB   )r   r   Úfget  s    z:CommonResponseDescriptorsMixin._set_property.<locals>.fgetc                s6   |s| j ˆ = n$t|tƒr$|| j ˆ < nt|ƒ| j ˆ < d S )N)r2   r@   r   r   )r   r7   )rB   r   r   Úfset   s
    

z:CommonResponseDescriptorsMixin._set_property.<locals>.fset)r   )r/   )rB   r   rC   rD   r   )rB   r   Ú_set_property  s    	z,CommonResponseDescriptorsMixin._set_propertyZVaryzóThe Vary field value indicates the set of request-header
        fields that fully determines, while the response is fresh,
        whether a cache is permitted to use the response to reply to a
        subsequent request without revalidation.zContent-LanguagezíThe Content-Language entity-header field describes the
        natural language(s) of the intended audience for the enclosed
        entity. Note that this might not be equivalent to all the
        languages used within the entity-body.ZAllowaR  The Allow entity-header field lists the set of methods
        supported by the resource identified by the Request-URI. The
        purpose of this field is strictly to inform the recipient of
        valid methods associated with the resource. An Allow header
        field MUST be present in a 405 (Method Not Allowed)
        response.)N)r&   r'   r(   r)   r/   r"   Úsetterr$   r   Úlocationr   r   Zager*   r.   rA   r   Úcontent_locationr+   r,   r   r
   r-   ÚexpiresZlast_modifiedr?   rE   ÚvaryZcontent_languageZallowr   r   r   r   r0   �   s|   
r0   N)r   r   Ú_compatr   Zdatastructuresr   Úhttpr   r   r	   r
   r   r   r   r   Úutilsr   r   r   r   Zwsgir   Úobjectr   r0   r   r   r   r   Ú<module>   s$   l