ó
TR¹Nc           @   s¤   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 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_constructort   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 | j  k rI x7 t j D]) } | j | j  d ƒ r t j d ƒ Sq Wn  | j ƒ  } | | j g } | } t j r§ | d r§ | d j	 d ƒ r§ d | d | d <n  t j
 rM| d j d ƒ rMt | d d ƒ } t | j | ƒ rMt d | j | ƒ rM| d d | d <t j rJ| j d	 k rJt d
 | d | d f ‚ qJqMn  | | k r]d S| d rd | j ƒ  r|d pd | d t | d ƒ f } n t | d ƒ } | j rÎ| d | j  d 7} n  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   <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   searchR   t   HttpResponseForbiddent   get_hostt   patht   PREPEND_WWWt
   startswitht   APPEND_SLASHt   endswitht   getattrt   Nonet   _is_valid_patht	   path_infot   DEBUGt   methodt   RuntimeErrort	   is_secureR   t   GETt   HttpResponsePermanentRedirect(   t   selft   requestt   user_agent_regext   hostt   old_urlt   new_urlR	   t   newurl(    (    sE   /home/panlixing/Python_Projects/gaeseries/django/middleware/common.pyt   process_request   s8    
	c         C   sŒ  | j  d k ræ t j ræ | j ƒ  } | j j d d ƒ } t | | ƒ } | j ƒ  } | rß t	 | ƒ rß | sy d | k rß | j j d d ƒ } | j j d d ƒ } t
 d | rµ d p¸ d	 | f d
 | | j ƒ  | | f ƒ n  | Sn  t j rˆ| j d ƒ r| d }	 n d t | j ƒ j ƒ  }	 | j  d k r{| j  d k  r{| j j d ƒ |	 k r{| j }
 t j ƒ  } |
 | _ qˆ|	 | d <n  | S(   sC   Check for a flat page (for 404s) 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   ETags   "%s"iÈ   i,  t   HTTP_IF_NONE_MATCHN(   t   status_codeR    t   SEND_BROKEN_LINK_EMAILSR   R   t   getR   t   _is_internal_requestt   get_full_patht   _is_ignorable_404R   t	   USE_ETAGSt
   has_headerR   t   contentt	   hexdigestt   cookiesR   t   HttpResponseNotModified(   R"   R#   t   responset   domaint   referert   is_internalR   t   uat   ipt   etagR9   (    (    sE   /home/panlixing/Python_Projects/gaeseries/django/middleware/common.pyt   process_responseR   s.    	%	6	(   t   __name__t
   __module__t   __doc__R)   RB   (    (    (    sE   /home/panlixing/Python_Projects/gaeseries/django/middleware/common.pyR   
   s   	3c         C   sR   x$ t  j D] } |  j | ƒ r
 t Sq
 Wx$ t  j D] } |  j | ƒ r1 t Sq1 Wt S(   sV   
    Returns True if a 404 at the given URL *shouldn't* notify the site managers.
    (   R    t   IGNORABLE_404_STARTSR   t   Truet   IGNORABLE_404_ENDSR   t   False(   t   urit   startt   end(    (    sE   /home/panlixing/Python_Projects/gaeseries/django/middleware/common.pyR4   s   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(   R<   R=   (    (    sE   /home/panlixing/Python_Projects/gaeseries/django/middleware/common.pyR2      s    c         C   s4   y t  j |  | ƒ t SWn t  j k
 r/ t SXd 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   resolveRG   t   Resolver404RI   (   R   R	   (    (    sE   /home/panlixing/Python_Projects/gaeseries/django/middleware/common.pyR   †   s
    (   RM   t   django.confR    t   djangoR   t   django.core.mailR   t   django.utils.httpR   t   django.coreR   t   django.utils.hashcompatR   t   objectR   R4   R2   R   R   (    (    (    sE   /home/panlixing/Python_Projects/gaeseries/django/middleware/common.pyt   <module>   s   i		