web/lib/django/middleware/common.py
changeset 29 cc9b7e14412b
parent 0 0d40e90630ef
equal deleted inserted replaced
28:b758351d191f 29:cc9b7e14412b
    51             new_url[0] = 'www.' + old_url[0]
    51             new_url[0] = 'www.' + old_url[0]
    52 
    52 
    53         # Append a slash if APPEND_SLASH is set and the URL doesn't have a
    53         # Append a slash if APPEND_SLASH is set and the URL doesn't have a
    54         # trailing slash and there is no pattern for the current path
    54         # trailing slash and there is no pattern for the current path
    55         if settings.APPEND_SLASH and (not old_url[1].endswith('/')):
    55         if settings.APPEND_SLASH and (not old_url[1].endswith('/')):
    56             if (not _is_valid_path(request.path_info) and
    56             urlconf = getattr(request, 'urlconf', None)
    57                     _is_valid_path("%s/" % request.path_info)):
    57             if (not _is_valid_path(request.path_info, urlconf) and
       
    58                     _is_valid_path("%s/" % request.path_info, urlconf)):
    58                 new_url[1] = new_url[1] + '/'
    59                 new_url[1] = new_url[1] + '/'
    59                 if settings.DEBUG and request.method == 'POST':
    60                 if settings.DEBUG and request.method == 'POST':
    60                     raise RuntimeError, (""
    61                     raise RuntimeError, (""
    61                     "You called this URL via POST, but the URL doesn't end "
    62                     "You called this URL via POST, but the URL doesn't end "
    62                     "in a slash and you have APPEND_SLASH set. Django can't "
    63                     "in a slash and you have APPEND_SLASH set. Django can't "
   128     Returns true if the referring URL is the same domain as the current request.
   129     Returns true if the referring URL is the same domain as the current request.
   129     """
   130     """
   130     # Different subdomains are treated as different domains.
   131     # Different subdomains are treated as different domains.
   131     return referer is not None and re.match("^https?://%s/" % re.escape(domain), referer)
   132     return referer is not None and re.match("^https?://%s/" % re.escape(domain), referer)
   132 
   133 
   133 def _is_valid_path(path):
   134 def _is_valid_path(path, urlconf=None):
   134     """
   135     """
   135     Returns True if the given path resolves against the default URL resolver,
   136     Returns True if the given path resolves against the default URL resolver,
   136     False otherwise.
   137     False otherwise.
   137 
   138 
   138     This is a convenience method to make working with "is this a match?" cases
   139     This is a convenience method to make working with "is this a match?" cases
   139     easier, avoiding unnecessarily indented try...except blocks.
   140     easier, avoiding unnecessarily indented try...except blocks.
   140     """
   141     """
   141     try:
   142     try:
   142         urlresolvers.resolve(path)
   143         urlresolvers.resolve(path, urlconf)
   143         return True
   144         return True
   144     except urlresolvers.Resolver404:
   145     except urlresolvers.Resolver404:
   145         return False
   146         return False
   146 
   147