Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

http.py 1.6KB

1 year ago
12345678910111213141516171819202122232425262728293031323334353637383940
  1. from django.utils.cache import cc_delim_re, get_conditional_response, set_response_etag
  2. from django.utils.deprecation import MiddlewareMixin
  3. from django.utils.http import parse_http_date_safe
  4. class ConditionalGetMiddleware(MiddlewareMixin):
  5. """
  6. Handle conditional GET operations. If the response has an ETag or
  7. Last-Modified header and the request has If-None-Match or If-Modified-Since,
  8. replace the response with HttpNotModified. Add an ETag header if needed.
  9. """
  10. def process_response(self, request, response):
  11. # It's too late to prevent an unsafe request with a 412 response, and
  12. # for a HEAD request, the response body is always empty so computing
  13. # an accurate ETag isn't possible.
  14. if request.method != "GET":
  15. return response
  16. if self.needs_etag(response) and not response.has_header("ETag"):
  17. set_response_etag(response)
  18. etag = response.get("ETag")
  19. last_modified = response.get("Last-Modified")
  20. last_modified = last_modified and parse_http_date_safe(last_modified)
  21. if etag or last_modified:
  22. return get_conditional_response(
  23. request,
  24. etag=etag,
  25. last_modified=last_modified,
  26. response=response,
  27. )
  28. return response
  29. def needs_etag(self, response):
  30. """Return True if an ETag header should be added to response."""
  31. cache_control_headers = cc_delim_re.split(response.get("Cache-Control", ""))
  32. return all(header.lower() != "no-store" for header in cache_control_headers)