Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/django/middleware/http.py: 29%
18 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
1from django.utils.cache import cc_delim_re, get_conditional_response, set_response_etag
2from django.utils.deprecation import MiddlewareMixin
3from django.utils.http import parse_http_date_safe
6class ConditionalGetMiddleware(MiddlewareMixin):
7 """
8 Handle conditional GET operations. If the response has an ETag or
9 Last-Modified header and the request has If-None-Match or If-Modified-Since,
10 replace the response with HttpNotModified. Add an ETag header if needed.
11 """
13 def process_response(self, request, response):
14 # It's too late to prevent an unsafe request with a 412 response, and
15 # for a HEAD request, the response body is always empty so computing
16 # an accurate ETag isn't possible.
17 if request.method != "GET":
18 return response
20 if self.needs_etag(response) and not response.has_header("ETag"):
21 set_response_etag(response)
23 etag = response.get("ETag")
24 last_modified = response.get("Last-Modified")
25 last_modified = last_modified and parse_http_date_safe(last_modified)
27 if etag or last_modified:
28 return get_conditional_response(
29 request,
30 etag=etag,
31 last_modified=last_modified,
32 response=response,
33 )
35 return response
37 def needs_etag(self, response):
38 """Return True if an ETag header should be added to response."""
39 cache_control_headers = cc_delim_re.split(response.get("Cache-Control", ""))
40 return all(header.lower() != "no-store" for header in cache_control_headers)