
^Q\<"                 @   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 m Z d  d l m Z m Z d  d	 l m Z d  d
 l m Z Gd d   d e  Z Gd d   d e  Z d S)    N)http)settings)PermissionDenied)mail_managers)is_valid_path)cc_delim_reget_conditional_responseset_response_etag)MiddlewareMixinRemovedInDjango21Warning)
force_text)urlparsec               @   s[   e  Z d  Z d Z e j 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:

        - 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. USE_ETAGS is deprecated in favor of
          ConditionalGetMiddleware.
    c             C   s   d | j  k rK x9 t j D]+ } | j | j  d  r t d   q q Wn  | j   } t j os | os | j d  } | r d | j | f n d } |  j	 |  r |  j
 |  } n | j   } | s | | j   k r | | 7} |  j |  Sd 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   >/var/www/dbchiro/venv/build/Django/django/middleware/common.pyprocess_request,   s    
z CommonMiddleware.process_requestc             C   sY   t  j rU | j j d  rU t | d d  } t | j |  oT t d | j |  Sd 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   H   s
    z+CommonMiddleware.should_redirect_with_slashc             C   s\   | j  d d  } t j rX | j d	 k rX t d i | j d 6| j   | d 6  n  | 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.
        Zforce_append_slashT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)zPOSTzPUTzPATCH)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 r7 |  j |  r7 |  j |  j |   Sn  t j r |  j |  r t j d t	  | j
 d  s{ t |  n  | j
 d  r t | d | d d | Sn  | j r | j
 d  r t t | j   | d <n  | 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  zThe 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.ZETagZ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_responsej   s"    	

z!CommonMiddleware.process_responsec             C   s2   t  j | j d d   } t d d   | D  S)zL
        Return True if an ETag header should be added to response.
        zCache-Controlr   c             s   s!   |  ] } | j    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   r0      s    zCommonMiddleware.needs_etagN)__name__
__module____qualname____doc__r   ZHttpResponsePermanentRedirectr   r    r   r   r7   r0   r   r   r   r   r      s   	%r   c               @   s4   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 q n  | S)zO
        Send broken link emails for relevant 404 NOT FOUND responses.
        i  HTTP_REFERERr   errorsreplacer   z<none>REMOTE_ADDRzBroken %slink on %sz	INTERNAL z=Referrer: %s
Requested URL: %s
User agent: %s
IP address: %s
Zfail_silentlyT)r/   r   r+   r   r   r   r   r=   is_ignorable_requestr   is_internal_request)r   r   r.   domainr   refereruaipr   r   r   r7      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   rJ   rK   r   r   r   rI      s    z.BrokenLinkEmailsMiddleware.is_internal_requestc                s   | s
 d St  j r<   j d  r< |   d d	  k r< d S|  j | |  r_ d | k r_ d St |  } | j d | g k r | j   k r d St   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   )r9   pattern)urir   r   r;      s    zBBrokenLinkEmailsMiddleware.is_ignorable_request.<locals>.<genexpr>)	r   r#   r$   rI   r   netlocr   anyZIGNORABLE_404_URLS)r   r   rU   rJ   rK   Zparsed_refererr   )rU   r   rH      s    .$z/BrokenLinkEmailsMiddleware.is_ignorable_requestN)r?   r@   rA   r7   rI   rH   r   r   r   r   rC      s   rC   )rO   r1   Zdjangor   Zdjango.confr   Zdjango.core.exceptionsr   Zdjango.core.mailr   Zdjango.urlsr   Zdjango.utils.cacher   r   r	   Zdjango.utils.deprecationr
   r   Zdjango.utils.encodingr   Z#django.utils.six.moves.urllib.parser   r   rC   r   r   r   r   <module>   s   