
TQc           @   s   d  d l  Z  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 e j d  Z d	 e f d
     YZ d   Z d   Z d S(   iN(   t   settings(   t   http(   t   mail_managers(   t   urlquote(   t   six(   t   urlresolverss   django.requestt   CommonMiddlewarec           B   s    e  Z d  Z d   Z d   Z RS(   sh  
    "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.

        - 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   sV  d | j  k rp x^ t j D]P } | j | j  d  r t j d | j d i d d 6| d 6t j d  Sq Wn  | j	   } | | j g } | } t j
 r | d r | d j d	  r d	 | d | d <n  t j r}| d
 j d  r}t | d d  } t j | j |  r}t j d | j |  r}| d
 d | d
 <t j rz| j d k rzt d | d | d
 f   qzq}n  | | k rd S| d rd | j   rd pd | d t | d
  f } n t | d
  } | j  j d d  rIt j r| d | j  d 7} qIy | d | j  d j   7} WqIt k
 rEqIXn  t j |  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 agent): %st   extrai  t   status_codet   requests   <h1>Forbidden</h1>i    s   www.i   t   /t   urlconfs   %s/t   POSTs  You called this URL via POST, 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 POST data. Change your form to point to %s%s (note the trailing slash), or set APPEND_SLASH=False in your Django settings.Ns	   %s://%s%st   httpsR   t   QUERY_STRINGt    t   ?(   t   METAR    t   DISALLOWED_USER_AGENTSt   searcht   loggert   warningt   pathR   t   HttpResponseForbiddent   get_hostt   PREPEND_WWWt
   startswitht   APPEND_SLASHt   endswitht   getattrt   NoneR   t   is_valid_patht	   path_infot   DEBUGt   methodt   RuntimeErrort	   is_secureR   t   getR   t   PY3t   decodet   UnicodeDecodeErrort   HttpResponsePermanentRedirect(   t   selfR
   t   user_agent_regext   hostt   old_urlt   new_urlR   t   newurl(    (    s1   ../Django//lib/python/django/middleware/common.pyt   process_request%   sJ    !
	c         C   s  | j  d k r t j r t j r | j   } | j j d d  } t | |  } | j	   } | r t
 |  r | s d | k r | j j d d  } | j j d d  } t d | r d p d	 | f d
 | | j	   | | f d t n  | Sn  t j r| j d  r| d }	 n. | j r-d }	 n d t j | j  j   }	 |	 d k	 rd | j  k ood k  n r| j j d  |	 k r| j }
 t j   } |
 | _ q|	 | d <qn  | S(   s:   Send broken link emails and calculate the Etag, if needed.i  t   HTTP_REFERERR   R   s   <none>t   REMOTE_ADDRs   Broken %slink on %ss	   INTERNAL R   s=   Referrer: %s
Requested URL: %s
User agent: %s
IP address: %s
t   fail_silentlyt   ETags   "%s"i   i,  t   HTTP_IF_NONE_MATCHN(   R	   R    t   SEND_BROKEN_LINK_EMAILSR"   R   R   R&   R   t   _is_internal_requestt   get_full_patht   _is_ignorable_404R   t   Truet	   USE_ETAGSt
   has_headert	   streamingt   hashlibt   md5t   contentt	   hexdigestt   cookiesR   t   HttpResponseNotModified(   R+   R
   t   responset   domaint   referert   is_internalR   t   uat   ipt   etagRC   (    (    s1   ../Django//lib/python/django/middleware/common.pyt   process_responseh   s8    %
				(   t   __name__t
   __module__t   __doc__R1   RL   (    (    (    s1   ../Django//lib/python/django/middleware/common.pyR      s   	Cc            s   t  t d d  rX d d l } | j d t  x' t j D] }   j |  r8 t Sq8 Wn  t  t d d	  r d d l } | j d t  x' t j D] }   j	 |  r t Sq Wn  t
   f d   t j D  S(
   sV   
    Returns True if a 404 at the given URL *shouldn't* notify the site managers.
    t   IGNORABLE_404_STARTSiNsT   The IGNORABLE_404_STARTS setting has been deprecated in favor of IGNORABLE_404_URLS.t   IGNORABLE_404_ENDSsR   The IGNORABLE_404_ENDS setting has been deprecated in favor of IGNORABLE_404_URLS.c         3   s   |  ] } | j     Vq d  S(   N(   R   (   t   .0t   pattern(   t   uri(    s1   ../Django//lib/python/django/middleware/common.pys	   <genexpr>   s    (    (    (   R   R    t   warningst   warnt   DeprecationWarningRP   R   R;   RQ   R   t   anyt   IGNORABLE_404_URLS(   RT   RU   t   startt   end(    (   RT   s1   ../Django//lib/python/django/middleware/common.pyR:      s    		c         C   s)   | d k	 o( t j d t j |   |  S(   sV   
    Returns true if the referring URL is the same domain as the current request.
    s   ^https?://%s/N(   R   t   ret   matcht   escape(   RF   RG   (    (    s1   ../Django//lib/python/django/middleware/common.pyR8      s    (   R?   t   loggingR\   t   django.confR    t   djangoR   t   django.core.mailR   t   django.utils.httpR   t   django.utilsR   t   django.coreR   t	   getLoggerR   t   objectR   R:   R8   (    (    (    s1   ../Django//lib/python/django/middleware/common.pyt   <module>   s   ~	