
_2                 @   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 d d l m Z d d l m Z Gd d   d e  Z Gd d   d e  Z d S)    )datetime)	timedelta   )string_types)CallbackDict)dump_age)dump_csp_header)dump_header)dump_options_header)	http_date)	parse_age)parse_csp_header)
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)CommonRequestDescriptorsMixinzA 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_TYPEdoczThe 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.c             C   s   t  |  j  S)zThe 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-5gj8f0j9/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_REFERERzThe 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_DATENzThe 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_FORWARDSzThe 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   s1   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)zLike :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   mimetypee   s    
z&CommonRequestDescriptorsMixin.mimetypec             C   s   |  j    |  j d S)zThe 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_paramso   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   pragmax   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 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),CommonResponseDescriptorsMixinzA 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   s3   |  j  j d  } | r/ | j d  d j   Sd 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   charsetr4   )r   valuer   r   r   r$      s    c                s>     f d d   } t    j j d d   d } t | |  S)zThe 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$   r4   )d)r   r   r   	on_update   s    zACommonResponseDescriptorsMixin.mimetype_params.<locals>.on_updatezcontent-typer   r%   )r   r4   r!   r   )r   r;   r:   r   )r   r   r&      s    	z.CommonResponseDescriptorsMixin.mimetype_paramsLocationr   zThe 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.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-TypezThe 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-LocationzThe 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.)zContent-Security-PolicyzThe Content-Security-Policy header adds an additional layer of
        security to help detect and mitigate certain types of attacks.z#Content-Security-Policy-Report-OnlyzThe Content-Security-Policy-Report-Only header adds a csp policy
        that is not enforced but is reported thereby helping detect
        certain types of attacks.DatezThe 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ExpireszThe 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-ModifiedzThe Last-Modified entity-header field indicates the date
        and time at which the origin server believes the variant was
        last modified.c             C   sU   |  j  j d  } | d k r" d S| j   rK t j   t d t |   St |  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)r4   r!   isdigitr   utcnowr   r0   r   )r   r9   r   r   r   retry_after  s    z*CommonResponseDescriptorsMixin.retry_afterc             C   sd   | d  k r) d |  j  k r% |  j  d =d  St | t  rG t |  } n t |  } | |  j  d <d  S)Nzretry-afterzRetry-After)r4   
isinstancer   r   str)r   r9   r   r   r   rA     s    
c                s7     f d d   }   f d d   } t  | | d | S)Nc                s.      f d d   } t    j j   |  S)Nc                s@   |  r#    j  k r#  j    =n |  r< |  j    j    <d  S)N)r4   Z	to_header)Z
header_set)namer   r   r   r;   +  s    zMCommonResponseDescriptorsMixin._set_property.<locals>.fget.<locals>.on_update)r   r4   r!   )r   r;   )rD   )r   r   fget*  s    z:CommonResponseDescriptorsMixin._set_property.<locals>.fgetc                sI   | s |  j    =n2 t | t  r2 | |  j    <n t |  |  j    <d  S)N)r4   rB   r   r	   )r   r9   )rD   r   r   fset3  s
    z:CommonResponseDescriptorsMixin._set_property.<locals>.fsetr   )r1   )rD   r   rE   rF   r   )rD   r   _set_property)  s    	z,CommonResponseDescriptorsMixin._set_propertyZVaryzThe 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-LanguagezThe 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.)"r(   r)   r*   r+   r1   r$   setterr&   r   locationr   r   Zager,   r0   rC   r   content_locationr-   r.   r   r   Zcontent_security_policyZ#content_security_policy_report_onlyr   r   r/   expiresZlast_modifiedrA   rG   varyZcontent_languageZallowr   r   r   r   r2      s   															r2   N)r   r   _compatr   Zdatastructuresr   httpr   r   r	   r
   r   r   r   r   r   r   utilsr   r   r   r   Zwsgir   objectr   r2   r   r   r   r   <module>   s(   l