|
0
|
1 |
import re |
|
|
2 |
|
|
|
3 |
from django.conf import settings |
|
|
4 |
from django import http |
|
|
5 |
from django.core.mail import mail_managers |
|
|
6 |
from django.utils.http import urlquote |
|
|
7 |
from django.core import urlresolvers |
|
|
8 |
from django.utils.hashcompat import md5_constructor |
|
|
9 |
|
|
|
10 |
class CommonMiddleware(object): |
|
|
11 |
""" |
|
|
12 |
"Common" middleware for taking care of some basic operations: |
|
|
13 |
|
|
|
14 |
- Forbids access to User-Agents in settings.DISALLOWED_USER_AGENTS |
|
|
15 |
|
|
|
16 |
- URL rewriting: Based on the APPEND_SLASH and PREPEND_WWW settings, |
|
|
17 |
this middleware appends missing slashes and/or prepends missing |
|
|
18 |
"www."s. |
|
|
19 |
|
|
|
20 |
- If APPEND_SLASH is set and the initial URL doesn't end with a |
|
|
21 |
slash, and it is not found in urlpatterns, a new URL is formed by |
|
|
22 |
appending a slash at the end. If this new URL is found in |
|
|
23 |
urlpatterns, then an HTTP-redirect is returned to this new URL; |
|
|
24 |
otherwise the initial URL is processed as usual. |
|
|
25 |
|
|
|
26 |
- ETags: If the USE_ETAGS setting is set, ETags will be calculated from |
|
|
27 |
the entire page content and Not Modified responses will be returned |
|
|
28 |
appropriately. |
|
|
29 |
""" |
|
|
30 |
|
|
|
31 |
def process_request(self, request): |
|
|
32 |
""" |
|
|
33 |
Check for denied User-Agents and rewrite the URL based on |
|
|
34 |
settings.APPEND_SLASH and settings.PREPEND_WWW |
|
|
35 |
""" |
|
|
36 |
|
|
|
37 |
# Check for denied User-Agents |
|
|
38 |
if 'HTTP_USER_AGENT' in request.META: |
|
|
39 |
for user_agent_regex in settings.DISALLOWED_USER_AGENTS: |
|
|
40 |
if user_agent_regex.search(request.META['HTTP_USER_AGENT']): |
|
|
41 |
return http.HttpResponseForbidden('<h1>Forbidden</h1>') |
|
|
42 |
|
|
|
43 |
# Check for a redirect based on settings.APPEND_SLASH |
|
|
44 |
# and settings.PREPEND_WWW |
|
|
45 |
host = request.get_host() |
|
|
46 |
old_url = [host, request.path] |
|
|
47 |
new_url = old_url[:] |
|
|
48 |
|
|
|
49 |
if (settings.PREPEND_WWW and old_url[0] and |
|
|
50 |
not old_url[0].startswith('www.')): |
|
|
51 |
new_url[0] = 'www.' + old_url[0] |
|
|
52 |
|
|
|
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 |
|
|
55 |
if settings.APPEND_SLASH and (not old_url[1].endswith('/')): |
|
29
|
56 |
urlconf = getattr(request, 'urlconf', None) |
|
|
57 |
if (not _is_valid_path(request.path_info, urlconf) and |
|
|
58 |
_is_valid_path("%s/" % request.path_info, urlconf)): |
|
0
|
59 |
new_url[1] = new_url[1] + '/' |
|
|
60 |
if settings.DEBUG and request.method == 'POST': |
|
|
61 |
raise RuntimeError, ("" |
|
|
62 |
"You called this URL via POST, but the URL doesn't end " |
|
|
63 |
"in a slash and you have APPEND_SLASH set. Django can't " |
|
|
64 |
"redirect to the slash URL while maintaining POST data. " |
|
|
65 |
"Change your form to point to %s%s (note the trailing " |
|
|
66 |
"slash), or set APPEND_SLASH=False in your Django " |
|
|
67 |
"settings.") % (new_url[0], new_url[1]) |
|
|
68 |
|
|
|
69 |
if new_url == old_url: |
|
|
70 |
# No redirects required. |
|
|
71 |
return |
|
|
72 |
if new_url[0]: |
|
|
73 |
newurl = "%s://%s%s" % ( |
|
|
74 |
request.is_secure() and 'https' or 'http', |
|
|
75 |
new_url[0], urlquote(new_url[1])) |
|
|
76 |
else: |
|
|
77 |
newurl = urlquote(new_url[1]) |
|
|
78 |
if request.GET: |
|
|
79 |
newurl += '?' + request.META['QUERY_STRING'] |
|
|
80 |
return http.HttpResponsePermanentRedirect(newurl) |
|
|
81 |
|
|
|
82 |
def process_response(self, request, response): |
|
|
83 |
"Check for a flat page (for 404s) and calculate the Etag, if needed." |
|
|
84 |
if response.status_code == 404: |
|
|
85 |
if settings.SEND_BROKEN_LINK_EMAILS: |
|
|
86 |
# If the referrer was from an internal link or a non-search-engine site, |
|
|
87 |
# send a note to the managers. |
|
|
88 |
domain = request.get_host() |
|
|
89 |
referer = request.META.get('HTTP_REFERER', None) |
|
|
90 |
is_internal = _is_internal_request(domain, referer) |
|
|
91 |
path = request.get_full_path() |
|
|
92 |
if referer and not _is_ignorable_404(path) and (is_internal or '?' not in referer): |
|
|
93 |
ua = request.META.get('HTTP_USER_AGENT', '<none>') |
|
|
94 |
ip = request.META.get('REMOTE_ADDR', '<none>') |
|
|
95 |
mail_managers("Broken %slink on %s" % ((is_internal and 'INTERNAL ' or ''), domain), |
|
|
96 |
"Referrer: %s\nRequested URL: %s\nUser agent: %s\nIP address: %s\n" \ |
|
|
97 |
% (referer, request.get_full_path(), ua, ip)) |
|
|
98 |
return response |
|
|
99 |
|
|
|
100 |
# Use ETags, if requested. |
|
|
101 |
if settings.USE_ETAGS: |
|
|
102 |
if response.has_header('ETag'): |
|
|
103 |
etag = response['ETag'] |
|
|
104 |
else: |
|
|
105 |
etag = '"%s"' % md5_constructor(response.content).hexdigest() |
|
|
106 |
if response.status_code >= 200 and response.status_code < 300 and request.META.get('HTTP_IF_NONE_MATCH') == etag: |
|
|
107 |
cookies = response.cookies |
|
|
108 |
response = http.HttpResponseNotModified() |
|
|
109 |
response.cookies = cookies |
|
|
110 |
else: |
|
|
111 |
response['ETag'] = etag |
|
|
112 |
|
|
|
113 |
return response |
|
|
114 |
|
|
|
115 |
def _is_ignorable_404(uri): |
|
|
116 |
""" |
|
|
117 |
Returns True if a 404 at the given URL *shouldn't* notify the site managers. |
|
|
118 |
""" |
|
|
119 |
for start in settings.IGNORABLE_404_STARTS: |
|
|
120 |
if uri.startswith(start): |
|
|
121 |
return True |
|
|
122 |
for end in settings.IGNORABLE_404_ENDS: |
|
|
123 |
if uri.endswith(end): |
|
|
124 |
return True |
|
|
125 |
return False |
|
|
126 |
|
|
|
127 |
def _is_internal_request(domain, referer): |
|
|
128 |
""" |
|
|
129 |
Returns true if the referring URL is the same domain as the current request. |
|
|
130 |
""" |
|
|
131 |
# Different subdomains are treated as different domains. |
|
|
132 |
return referer is not None and re.match("^https?://%s/" % re.escape(domain), referer) |
|
|
133 |
|
|
29
|
134 |
def _is_valid_path(path, urlconf=None): |
|
0
|
135 |
""" |
|
|
136 |
Returns True if the given path resolves against the default URL resolver, |
|
|
137 |
False otherwise. |
|
|
138 |
|
|
|
139 |
This is a convenience method to make working with "is this a match?" cases |
|
|
140 |
easier, avoiding unnecessarily indented try...except blocks. |
|
|
141 |
""" |
|
|
142 |
try: |
|
29
|
143 |
urlresolvers.resolve(path, urlconf) |
|
0
|
144 |
return True |
|
|
145 |
except urlresolvers.Resolver404: |
|
|
146 |
return False |
|
|
147 |
|