Debian Codegraph @

django/middleware/common.py

raw
1 import re
2 from urllib.parse import urlsplit
3
4 from django.conf import settings
5 from django.core.exceptions import PermissionDenied
6 from django.core.mail import mail_managers
7 from django.http import HttpResponsePermanentRedirect
8 from django.urls import is_valid_path
9 from django.utils.deprecation import MiddlewareMixin
10 from django.utils.http import escape_leading_slashes
11
12
13 class CommonMiddleware(MiddlewareMixin):
14 """
15 "Common" middleware for taking care of some basic operations:
16
17 - Forbid access to User-Agents in settings.DISALLOWED_USER_AGENTS
18
19 - URL rewriting: Based on the APPEND_SLASH and PREPEND_WWW settings,
20 append missing slashes and/or prepends missing "www."s.
21
22 - If APPEND_SLASH is set and the initial URL doesn't end with a
23 slash, and it is not found in urlpatterns, form a new URL by
24 appending a slash at the end. If this new URL is found in
25 urlpatterns, return an HTTP redirect to this new URL; otherwise
26 process the initial URL as usual.
27
28 This behavior can be customized by subclassing CommonMiddleware and
29 overriding the response_redirect_class attribute.
30 """
31
32 response_redirect_class = HttpResponsePermanentRedirect
33
34 def process_request(self, request):
35 """
36 Check for denied User-Agents and rewrite the URL based on
37 settings.APPEND_SLASH and settings.PREPEND_WWW
38 """
39
40 # Check for denied User-Agents
41 user_agent = request.META.get("HTTP_USER_AGENT")
42 if user_agent is not None:
43 for user_agent_regex in settings.DISALLOWED_USER_AGENTS:
44 if user_agent_regex.search(user_agent):
45 raise PermissionDenied("Forbidden user agent")
46
47 # Check for a redirect based on settings.PREPEND_WWW
48 host = request.get_host()
49
50 if settings.PREPEND_WWW and host and not host.startswith("www."):
51 # Check if we also need to append a slash so we can do it all
52 # with a single redirect. (This check may be somewhat expensive,
53 # so we only do it if we already know we're sending a redirect,
54 # or in process_response if we get a 404.)
55 if self.should_redirect_with_slash(request):
56 path = self.get_full_path_with_slash(request)
57 else:
58 path = request.get_full_path()
59
60 return self.response_redirect_class(f"{request.scheme}://www.{host}{path}")
61
62 def should_redirect_with_slash(self, request):
63 """
64 Return True if settings.APPEND_SLASH is True and appending a slash to
65 the request path turns an invalid path into a valid one.
66 """
67 if settings.APPEND_SLASH and not request.path_info.endswith("/"):
68 urlconf = getattr(request, "urlconf", None)
69 if not is_valid_path(request.path_info, urlconf):
70 match = is_valid_path("%s/" % request.path_info, urlconf)
71 if match:
72 view = match.func
73 return getattr(view, "should_append_slash", True)
74 return False
75
76 def get_full_path_with_slash(self, request):
77 """
78 Return the full path of the request with a trailing slash appended.
79
80 Raise a RuntimeError if settings.DEBUG is True and request.method is
81 DELETE, POST, PUT, or PATCH.
82 """
83 new_path = request.get_full_path(force_append_slash=True)
84 # Prevent construction of scheme relative urls.
85 new_path = escape_leading_slashes(new_path)
86 if settings.DEBUG and request.method in ("DELETE", "POST", "PUT", "PATCH"):
87 raise RuntimeError(
88 "You called this URL via %(method)s, but the URL doesn't end "
89 "in a slash and you have APPEND_SLASH set. Django can't "
90 "redirect to the slash URL while maintaining %(method)s data. "
91 "Change your form to point to %(url)s (note the trailing "
92 "slash), or set APPEND_SLASH=False in your Django settings."
93 % {
94 "method": request.method,
95 "url": request.get_host() + new_path,
96 }
97 )
98 return new_path
99
100 def process_response(self, request, response):
101 """
102 When the status code of the response is 404, it may redirect to a path
103 with an appended slash if should_redirect_with_slash() returns True.
104 """
105 # If the given URL is "Not Found", then check if we should redirect to
106 # a path with a slash appended.
107 if response.status_code == 404 and self.should_redirect_with_slash(request):
108 return self.response_redirect_class(self.get_full_path_with_slash(request))
109
110 # Add the Content-Length header to non-streaming responses if not
111 # already set.
112 if not response.streaming and not response.has_header("Content-Length"):
113 response.headers["Content-Length"] = str(len(response.content))
114
115 return response
116
117
118 class BrokenLinkEmailsMiddleware(MiddlewareMixin):
119 def process_response(self, request, response):
120 """Send broken link emails for relevant 404 NOT FOUND responses."""
121 if response.status_code == 404 and not settings.DEBUG:
122 domain = request.get_host()
123 path = request.get_full_path()
124 referer = request.META.get("HTTP_REFERER", "")
125
126 if not self.is_ignorable_request(request, path, domain, referer):
127 ua = request.META.get("HTTP_USER_AGENT", "<none>")
128 ip = request.META.get("REMOTE_ADDR", "<none>")
129 mail_managers(
130 "Broken %slink on %s"
131 % (
132 (
133 "INTERNAL "
134 if self.is_internal_request(domain, referer)
135 else ""
136 ),
137 domain,
138 ),
139 "Referrer: %s\nRequested URL: %s\nUser agent: %s\n"
140 "IP address: %s\n" % (referer, path, ua, ip),
141 fail_silently=True,
142 )
143 return response
144
145 def is_internal_request(self, domain, referer):
146 """
147 Return True if the referring URL is the same domain as the current
148 request.
149 """
150 # Different subdomains are treated as different domains.
151 return bool(re.match("^https?://%s/" % re.escape(domain), referer))
152
153 def is_ignorable_request(self, request, uri, domain, referer):
154 """
155 Return True if the given request *shouldn't* notify the site managers
156 according to project settings or in situations outlined by the inline
157 comments.
158 """
159 # The referer is empty.
160 if not referer:
161 return True
162
163 # APPEND_SLASH is enabled and the referer is equal to the current URL
164 # without a trailing slash indicating an internal redirect.
165 if settings.APPEND_SLASH and uri.endswith("/") and referer == uri[:-1]:
166 return True
167
168 # A '?' in referer is identified as a search engine source.
169 if not self.is_internal_request(domain, referer) and "?" in referer:
170 return True
171
172 # The referer is equal to the current URL, ignoring the scheme (assumed
173 # to be a poorly implemented bot).
174 parsed_referer = urlsplit(referer)
175 if parsed_referer.netloc in ["", domain] and parsed_referer.path == uri:
176 return True
177
178 return any(pattern.search(uri) for pattern in settings.IGNORABLE_404_URLS)