ó
Ñ7ec           @   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 d  d l m Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   iÿÿÿÿN(   t   http(   t   settings(   t   PermissionDenied(   t   mail_managers(   t   is_valid_path(   t   cc_delim_ret   get_conditional_responset   set_response_etag(   t   MiddlewareMixint   RemovedInDjango21Warning(   t
   force_text(   t   escape_leading_slashes(   t   urlparset   CommonMiddlewarec           B   sD   e  Z d  Z e j Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 RS(   s;  
    "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(   s‚   
        Check for denied User-Agents and rewrite the URL based on
        settings.APPEND_SLASH and settings.PREPEND_WWW
        t   HTTP_USER_AGENTs   Forbidden user agents   www.s   %s://www.%st    N(   t   METAR   t   DISALLOWED_USER_AGENTSt   searchR   t   get_hostt   PREPEND_WWWt
   startswitht   schemet   should_redirect_with_slasht   get_full_path_with_slasht   get_full_patht   response_redirect_class(   t   selft   requestt   user_agent_regext   hostt   must_prependt   redirect_urlt   path(    (    sB   /usr/local/lib/python2.7/dist-packages/django/middleware/common.pyt   process_request-   s    
c         C   sY   t  j rU | j j d ƒ rU t | d d ƒ } t | j | ƒ oT t d | j | ƒ St S(   s˜   
        Return True if settings.APPEND_SLASH is True and appending a slash to
        the request path turns an invalid path into a valid one.
        t   /t   urlconfs   %s/N(   R   t   APPEND_SLASHt	   path_infot   endswitht   getattrt   NoneR   t   False(   R   R   R$   (    (    sB   /usr/local/lib/python2.7/dist-packages/django/middleware/common.pyR   I   s
    c         C   sh   | j  d t ƒ } t | ƒ } t j rd | j d k rd t d i | j d 6| j ƒ  | d 6ƒ ‚ n  | S(	   sÀ   
        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   force_append_slasht   POSTt   PUTt   PATCHs"  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.t   methodt   url(   R,   R-   R.   (   R   t   TrueR   R   t   DEBUGR/   t   RuntimeErrorR   (   R   R   t   new_path(    (    sB   /usr/local/lib/python2.7/dist-packages/django/middleware/common.pyR   V   s    
c         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(   sÍ   
        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”  s°   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.t   ETagt   etagt   responses   Content-Length(   t   status_codeR   R   R   R   t	   USE_ETAGSt
   needs_etagt   warningst   warnR	   t
   has_headerR   R   t	   streamingt   strt   lent   content(   R   R   R7   (    (    sB   /usr/local/lib/python2.7/dist-packages/django/middleware/common.pyt   process_responsem   s"    	

c         C   s/   t  j | j d d ƒ ƒ } t d „  | Dƒ ƒ S(   sL   
        Return True if an ETag header should be added to response.
        s   Cache-ControlR   c         s   s!   |  ] } | j  ƒ  d  k Vq d S(   s   no-storeN(   t   lower(   t   .0t   header(    (    sB   /usr/local/lib/python2.7/dist-packages/django/middleware/common.pys	   <genexpr>—   s    (   R   t   splitt   gett   all(   R   R7   t   cache_control_headers(    (    sB   /usr/local/lib/python2.7/dist-packages/django/middleware/common.pyR:   ’   s    (   t   __name__t
   __module__t   __doc__R    t   HttpResponsePermanentRedirectR   R"   R   R   RB   R:   (    (    (    sB   /usr/local/lib/python2.7/dist-packages/django/middleware/common.pyR      s   					%t   BrokenLinkEmailsMiddlewarec           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c         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 t ƒqë n  | S(   sO   
        Send broken link emails for relevant 404 NOT FOUND responses.
        i”  t   HTTP_REFERERR   t   errorst   replaceR   s   <none>t   REMOTE_ADDRs   Broken %slink on %ss	   INTERNAL s=   Referrer: %s
Requested URL: %s
User agent: %s
IP address: %s
t   fail_silently(   R8   R   R2   R   R   R
   R   RG   t   is_ignorable_requestR   t   is_internal_requestR1   (   R   R   R7   t   domainR!   t   referert   uat   ip(    (    sB   /usr/local/lib/python2.7/dist-packages/django/middleware/common.pyRB   œ   s    !!c         C   s#   t  t j d t j | ƒ | ƒ ƒ S(   s^   
        Returns True if the referring URL is the same domain as the current request.
        s   ^https?://%s/(   t   boolt   ret   matcht   escape(   R   RV   RW   (    (    sB   /usr/local/lib/python2.7/dist-packages/django/middleware/common.pyRU   ²   s    c            sª   | s
 t  St j r6 ˆ  j d ƒ r6 | ˆ  d  k r6 t  S|  j | | ƒ rY d | k rY t  St | ƒ } | j d | g k r | j ˆ  k r t  St ‡  f d †  t j	 Dƒ ƒ S(   s·   
        Return True if the given request *shouldn't* notify the site managers
        according to project settings or in situations outlined by the inline
        comments.
        R#   iÿÿÿÿt   ?R   c         3   s   |  ] } | j  ˆ  ƒ Vq d  S(   N(   R   (   RD   t   pattern(   t   uri(    sB   /usr/local/lib/python2.7/dist-packages/django/middleware/common.pys	   <genexpr>Ò   s    (
   R1   R   R%   R'   RU   R   t   netlocR!   t   anyt   IGNORABLE_404_URLS(   R   R   R`   RV   RW   t   parsed_referer(    (   R`   sB   /usr/local/lib/python2.7/dist-packages/django/middleware/common.pyRT   ¹   s    ($(   RJ   RK   RB   RU   RT   (    (    (    sB   /usr/local/lib/python2.7/dist-packages/django/middleware/common.pyRN   š   s   		(   R[   R;   t   djangoR    t   django.confR   t   django.core.exceptionsR   t   django.core.mailR   t   django.urlsR   t   django.utils.cacheR   R   R   t   django.utils.deprecationR   R	   t   django.utils.encodingR
   t   django.utils.httpR   t#   django.utils.six.moves.urllib.parseR   R   RN   (    (    (    sB   /usr/local/lib/python2.7/dist-packages/django/middleware/common.pyt   <module>   s   ˆ