B
    ª0H[ˆ!  ã               @   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 d dlmZmZmZ d d	lmZmZ G d
d„ deƒZG dd„ deƒZdS )é    N)Úurlparse)Úsettings)ÚPermissionDenied)Úmail_managers)ÚHttpResponsePermanentRedirect)Úis_valid_path)Úcc_delim_reÚget_conditional_responseÚset_response_etag)ÚMiddlewareMixinÚRemovedInDjango21Warningc               @   s<   e Zd ZdZeZdd„ 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:

        - Forbid access to User-Agents in settings.DISALLOWED_USER_AGENTS

        - URL rewriting: Based on the APPEND_SLASH and PREPEND_WWW settings,
          append 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, form a new URL by
              appending a slash at the end. If this new URL is found in
              urlpatterns, return an HTTP redirect to this new URL; otherwise
              process the initial URL 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. USE_ETAGS is deprecated in favor of
          ConditionalGetMiddleware.
    c             C   sª   d|j kr2x&tjD ]}| |j d ¡rtdƒ‚qW | ¡ }tjoN|oN| d¡ }|rbd|j|f nd}|  	|¡r||  
|¡}n| ¡ }|s”|| ¡ kr¦||7 }|  |¡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   ú\C:\Users\HIRONO~1\AppData\Local\Temp\pip-install-6bm3nxem\django\django\middleware\common.pyÚprocess_request*   s    

z CommonMiddleware.process_requestc             C   s@   t jr<|j d¡s<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_SLASHZ	path_infoÚendswithÚgetattrr   )r   r   r!   r   r   r   r   F   s
    z+CommonMiddleware.should_redirect_with_slashc             C   s<   |j dd�}tjr8|jdkr8td|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   ÚDEBUGr(   ÚRuntimeErrorr   )r   r   Únew_pathr   r   r   r   S   s    z)CommonMiddleware.get_full_path_with_slashc             C   s”   |j dkr$|  |¡r$|  |  |¡¡S tjrn|  |¡rnt dt	¡ | 
d¡sRt|ƒ | 
d¡rnt||d |d�S |js�| 
d¡s�tt|jƒƒ|d< |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”  z°The USE_ETAGS setting is deprecated in favor of ConditionalGetMiddleware which sets the ETag regardless of the setting. CommonMiddleware won't do ETag processing in Django 2.1.ÚETag)ÚetagÚresponsezContent-Length)Ústatus_coder   r   r   r   Z	USE_ETAGSÚ
needs_etagÚwarningsÚwarnr   Ú
has_headerr
   r	   Z	streamingÚstrÚlenÚcontent)r   r   r/   r   r   r   Úprocess_responseh   s"    	



z!CommonMiddleware.process_responsec             C   s$   t  | dd¡¡}tdd„ |D ƒƒS )z:Return True if an ETag header should be added to response.zCache-Controlr   c             s   s   | ]}|  ¡ d kV  qdS )zno-storeN)Úlower)Ú.0Úheaderr   r   r   ú	<genexpr>�   s    z.CommonMiddleware.needs_etag.<locals>.<genexpr>)r   ÚsplitÚgetÚall)r   r/   Zcache_control_headersr   r   r   r1   �   s    zCommonMiddleware.needs_etagN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r8   r1   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sŒ| ¡ }| ¡ }|j dd¡}|  ||||¡sŒ|j dd¡}|j dd¡}td|  	||¡rndnd|f d	||||f d
d� |S )z=Send broken link emails for relevant 404 NOT FOUND responses.i”  ZHTTP_REFERERr   r   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>   Úis_ignorable_requestr   Úis_internal_request)r   r   r/   Údomainr   ÚrefererZuaÚipr   r   r   r8   •   s    z+BrokenLinkEmailsMiddleware.process_responsec             C   s   t t dt |¡ |¡ƒS )ze
        Return True if the referring URL is the same domain as the current
        request.
        z^https?://%s/)ÚboolÚreÚmatchÚescape)r   rG   rH   r   r   r   rF   ©   s    z.BrokenLinkEmailsMiddleware.is_internal_requestc                s€   |sdS t jr,ˆ  d¡r,|ˆ dd… kr,dS |  ||¡sDd|krDdS t|ƒ}|jd|gkrh|jˆ krhd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   | ]}|  ˆ ¡V  qd S )N)r   )r:   Úpattern)Úurir   r   r<   Ê   s    zBBrokenLinkEmailsMiddleware.is_ignorable_request.<locals>.<genexpr>)	r   r"   r#   rF   r   Únetlocr   ÚanyZIGNORABLE_404_URLS)r   r   rQ   rG   rH   Zparsed_refererr   )rQ   r   rE   ±   s     z/BrokenLinkEmailsMiddleware.is_ignorable_requestN)r@   rA   rB   r8   rF   rE   r   r   r   r   rD   “   s   rD   )rK   r2   Úurllib.parser   Zdjango.confr   Zdjango.core.exceptionsr   Zdjango.core.mailr   Zdjango.httpr   Zdjango.urlsr   Zdjango.utils.cacher   r	   r
   Zdjango.utils.deprecationr   r   r   rD   r   r   r   r   Ú<module>   s    