Ñò
‘cEMc           @   sÀ   d  d k  Z  d  d k l Z d  d k l Z d  d k l Z d  d k l Z d  d k	 l
 Z
 d  d k l Z d  d k l Z e d	 ƒ Z d
 e f d „  ƒ  YZ d „  Z d „  Z d d „ Z d S(   iÿÿÿÿN(   t   settings(   t   http(   t   mail_managers(   t   urlquote(   t   urlresolvers(   t   md5_constructor(   t	   getLoggers   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   s  d | i  j oe xb t i D]S } | i | i  d ƒ o6 t i d | i d h d d 6| d 6ƒt i d ƒ Sq Wn | i	 ƒ  } | | i g } | } t i
 o6 | d o+ | d i d	 ƒ o d	 | d | d <n t i o¥ | d
 i d ƒ o t | d d ƒ } t | i | ƒ of t d | i | ƒ oO | d
 d | d
 <t i o/ | i d j o t d | d | d
 f ‚ q‚q†n | | j o d S| d o9 d | i ƒ  o d p d | d t | d
 ƒ f } n t | d
 ƒ } | i o | d | i  d 7} n t i | ƒ 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   ?t   QUERY_STRING(   t   METAR    t   DISALLOWED_USER_AGENTSt   searcht   loggert   warningt   pathR   t   HttpResponseForbiddent   get_hostt   PREPEND_WWWt
   startswitht   APPEND_SLASHt   endswitht   getattrt   Nonet   _is_valid_patht	   path_infot   DEBUGt   methodt   RuntimeErrort	   is_secureR   t   GETt   HttpResponsePermanentRedirect(   t   selfR   t   user_agent_regext   hostt   old_urlt   new_urlR   t   newurl(    (    sB   /usr/local/lib/python2.6/dist-packages/django/middleware/common.pyt   process_request#   sB    
 !
c         C   s°  | i  d j oò t i oä t i oÙ | i ƒ  } | i i d d ƒ } t | | ƒ } | i	 ƒ  } | o‘ t
 | ƒ oƒ | p d | j oo | i i d d ƒ } | i i d d ƒ } t d | o d p d	 | f d
 | | i	 ƒ  | | f d t ƒn | Sn t i o  | i d ƒ o | d }	 n d t | i ƒ i ƒ  }	 | i  d j oK | i  d j  o; | i i d ƒ |	 j o" | i }
 t i ƒ  } |
 | _ q¬|	 | d <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 t    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   t   getR   t   _is_internal_requestt   get_full_patht   _is_ignorable_404R   t   Truet	   USE_ETAGSt
   has_headerR   t   contentt	   hexdigestt   cookiesR   t   HttpResponseNotModified(   R(   R   t   responset   domaint   referert   is_internalR   t   uat   ipt   etagR?   (    (    sB   /usr/local/lib/python2.6/dist-packages/django/middleware/common.pyt   process_response\   s0    )	
9	(   t   __name__t
   __module__t   __doc__R.   RH   (    (    (    sB   /usr/local/lib/python2.6/dist-packages/django/middleware/common.pyR      s   	9c         C   sV   x& t  i D] } |  i | ƒ o t Sq
 Wx& t  i D] } |  i | ƒ o t Sq3 Wt S(   sV   
    Returns True if a 404 at the given URL *shouldn't* notify the site managers.
    (   R    t   IGNORABLE_404_STARTSR   R:   t   IGNORABLE_404_ENDSR   t   False(   t   urit   startt   end(    (    sB   /usr/local/lib/python2.6/dist-packages/django/middleware/common.pyR9   ~   s    
 	
 	c         C   s*   | d j	 o t i d t i |  ƒ | ƒ 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(   RB   RC   (    (    sB   /usr/local/lib/python2.6/dist-packages/django/middleware/common.pyR7   Š   s    c         C   s6   y t  i |  | ƒ t SWn t  i j
 o t SXd S(   sù   
    Returns True if the given path resolves against the default URL resolver,
    False otherwise.

    This is a convenience method to make working with "is this a match?" cases
    easier, avoiding unnecessarily indented try...except blocks.
    N(   R   t   resolveR:   t   Resolver404RN   (   R   R   (    (    sB   /usr/local/lib/python2.6/dist-packages/django/middleware/common.pyR    ‘   s
    (   RR   t   django.confR    t   djangoR   t   django.core.mailR   t   django.utils.httpR   t   django.coreR   t   django.utils.hashcompatR   t   django.utils.logR   R   t   objectR   R9   R7   R   R    (    (    (    sB   /usr/local/lib/python2.6/dist-packages/django/middleware/common.pyt   <module>   s   p		