3
š(ƒh£  ã               @   sº   d dl Z d dl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mZ d dlmZ d d	lmZ d d
lmZ d dlmZ e jdƒZG dd„ deƒZG dd„ deƒZdS )é    N)Úhttp)Úsettings)ÚPermissionDenied)Úmail_managers)Úis_valid_path)Úget_conditional_responseÚset_response_etag)ÚMiddlewareMixin)Ú
force_text)Úunquote_etag)Úurlparsezdjango.requestc               @   s6   e Zd ZdZejZdd„ Zdd„ Zdd„ Z	dd	„ Z
d
S )ÚCommonMiddlewareaó  
    "Common" middleware for taking care of some basic operations:

        - Forbids access to User-Agents in settings.DISALLOWED_USER_AGENTS

        - URL rewriting: Based on the APPEND_SLASH and PREPEND_WWW settings,
          this middleware appends missing slashes and/or prepends missing
          "www."s.

            - If APPEND_SLASH is set and the initial URL doesn't end with a
              slash, and it is not found in urlpatterns, a new URL is formed by
              appending a slash at the end. If this new URL is found in
              urlpatterns, then an HTTP-redirect is returned to this new URL;
              otherwise the initial URL is processed as usual.

          This behavior can be customized by subclassing CommonMiddleware and
          overriding the response_redirect_class attribute.

        - ETags: If the USE_ETAGS setting is set, ETags will be calculated from
          the entire page content and Not Modified responses will be returned
          appropriately.
    c             C   sª   d|j kr2x&tjD ]}|j|j d ƒrtdƒ‚qW |jƒ }tjoN|oN|jdƒ }|rbd|j|f nd}| j	|ƒr|| j
|ƒ}n|jƒ }|s”||jƒ kr¦||7 }| j|ƒS dS )z‚
        Check for denied User-Agents and rewrite the URL based on
        settings.APPEND_SLASH and settings.PREPEND_WWW
        ÚHTTP_USER_AGENTzForbidden user agentzwww.z%s://www.%sÚ N)ÚMETAr   ZDISALLOWED_USER_AGENTSÚsearchr   Úget_hostZPREPEND_WWWÚ
startswithÚschemeÚshould_redirect_with_slashÚget_full_path_with_slashÚget_full_pathÚresponse_redirect_class)ÚselfÚrequestZuser_agent_regexÚhostZmust_prependZredirect_urlÚpath© r   ú</tmp/pip-install-q3hcpn_q/Django/django/middleware/common.pyÚprocess_request,   s    

z CommonMiddleware.process_requestc             C   sD   t jr@|jƒ jdƒ r@t|ddƒ}t|j|ƒ o>td|j |ƒS dS )z˜
        Return True if settings.APPEND_SLASH is True and appending a slash to
        the request path turns an invalid path into a valid one.
        ú/ÚurlconfNz%s/F)r   ÚAPPEND_SLASHr   ÚendswithÚgetattrr   Z	path_info)r   r   r!   r   r   r   r   H   s
    z+CommonMiddleware.should_redirect_with_slashc             C   s<   |j dd}tjr8|jdkr8td|j|jƒ | dœ ƒ‚|S )	zÀ
        Return the full path of the request with a trailing slash appended.

        Raise a RuntimeError if settings.DEBUG is True and request.method is
        POST, PUT, or PATCH.
        T)Zforce_append_slashÚPOSTÚPUTÚPATCHa"  You called this URL via %(method)s, but the URL doesn't end in a slash and you have APPEND_SLASH set. Django can't redirect to the slash URL while maintaining %(method)s data. Change your form to point to %(url)s (note the trailing slash), or set APPEND_SLASH=False in your Django settings.)ÚmethodÚurl)r%   r&   r'   )r   r   ÚDEBUGr(   ÚRuntimeErrorr   )r   r   Únew_pathr   r   r   r   U   s    z)CommonMiddleware.get_full_path_with_slashc             C   s`   |j dkr$| j|ƒr$| j| j|ƒƒS tjr\|jdƒs<t|ƒ |jdƒr\t|t	|d ƒ|dS |S )zÍ
        Calculate the ETag, if needed.

        When the status code of the response is 404, it may redirect to a path
        with an appended slash if should_redirect_with_slash() returns True.
        i”  ÚETag)ÚetagÚresponse)
Ústatus_coder   r   r   r   Z	USE_ETAGSÚ
has_headerr   r   r   )r   r   r/   r   r   r   Úprocess_responsej   s    	




z!CommonMiddleware.process_responseN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ZHttpResponsePermanentRedirectr   r   r   r   r2   r   r   r   r   r      s   r   c               @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚBrokenLinkEmailsMiddlewarec             C   s¢   |j dkržtj rž|jƒ }|jƒ }t|jjddƒdd}| j||||ƒsžt|jjddƒdd}|jjddƒ}t	d	| j
||ƒr€d
nd|f d||||f dd |S )zO
        Send broken link emails for relevant 404 NOT FOUND responses.
        i”  ZHTTP_REFERERr   Úreplace)Úerrorsr   z<none>ZREMOTE_ADDRzBroken %slink on %sz	INTERNAL z=Referrer: %s
Requested URL: %s
User agent: %s
IP address: %s
T)Zfail_silently)r0   r   r*   r   r   r
   r   ÚgetÚis_ignorable_requestr   Úis_internal_request)r   r   r/   Údomainr   ÚrefererZuaÚipr   r   r   r2   ‡   s    z+BrokenLinkEmailsMiddleware.process_responsec             C   s   t tjdtj|ƒ |ƒƒS )z^
        Returns True if the referring URL is the same domain as the current request.
        z^https?://%s/)ÚboolÚreÚmatchÚescape)r   r=   r>   r   r   r   r<      s    z.BrokenLinkEmailsMiddleware.is_internal_requestc                s‚   |sdS t jr,ˆ jdƒr,|ˆ dd	… kr,dS | j||ƒ rFd|krFdS t|ƒ}|jd|gkrj|jˆ krjdS t‡ fdd„t jD ƒƒS )
z·
        Return True if the given request *shouldn't* notify the site managers
        according to project settings or in situations outlined by the inline
        comments.
        Tr    Né   ú?r   c             3   s   | ]}|j ˆ ƒV  qd S )N)r   )Ú.0Úpattern)Úurir   r   ú	<genexpr>½   s    zBBrokenLinkEmailsMiddleware.is_ignorable_request.<locals>.<genexpr>éÿÿÿÿ)	r   r"   r#   r<   r   Únetlocr   ÚanyZIGNORABLE_404_URLS)r   r   rH   r=   r>   Zparsed_refererr   )rH   r   r;   ¤   s     z/BrokenLinkEmailsMiddleware.is_ignorable_requestN)r3   r4   r5   r2   r<   r;   r   r   r   r   r7   …   s   r7   )ÚloggingrA   Zdjangor   Zdjango.confr   Zdjango.core.exceptionsr   Zdjango.core.mailr   Zdjango.urlsr   Zdjango.utils.cacher   r   Zdjango.utils.deprecationr	   Zdjango.utils.encodingr
   Zdjango.utils.httpr   Z#django.utils.six.moves.urllib.parser   Ú	getLoggerÚloggerr   r7   r   r   r   r   Ú<module>   s   
s