Removed outdated handling of length parameter to If-Modified-Since header.

The length parameter is not described in RFC-7232 and it's against
HTTP/1.0 and HTTP/1.1 specifications. It was an old and unofficial
extension set by some ancient versions of IE.
This commit is contained in:
Mariusz Felisiak 2022-03-15 13:07:44 +01:00 committed by GitHub
parent a88fab1bca
commit be80aa55ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 13 deletions

View File

@ -4,7 +4,6 @@ during development, and SHOULD NOT be used in a production setting.
""" """
import mimetypes import mimetypes
import posixpath import posixpath
import re
from pathlib import Path from pathlib import Path
from django.http import FileResponse, Http404, HttpResponse, HttpResponseNotModified from django.http import FileResponse, Http404, HttpResponse, HttpResponseNotModified
@ -42,7 +41,7 @@ def serve(request, path, document_root=None, show_indexes=False):
# Respect the If-Modified-Since header. # Respect the If-Modified-Since header.
statobj = fullpath.stat() statobj = fullpath.stat()
if not was_modified_since( if not was_modified_since(
request.META.get("HTTP_IF_MODIFIED_SINCE"), statobj.st_mtime, statobj.st_size request.META.get("HTTP_IF_MODIFIED_SINCE"), statobj.st_mtime
): ):
return HttpResponseNotModified() return HttpResponseNotModified()
content_type, encoding = mimetypes.guess_type(str(fullpath)) content_type, encoding = mimetypes.guess_type(str(fullpath))
@ -111,7 +110,7 @@ def directory_index(path, fullpath):
return HttpResponse(t.render(c)) return HttpResponse(t.render(c))
def was_modified_since(header=None, mtime=0, size=0): def was_modified_since(header=None, mtime=0):
""" """
Was something modified since the user last downloaded it? Was something modified since the user last downloaded it?
@ -121,20 +120,11 @@ def was_modified_since(header=None, mtime=0, size=0):
mtime mtime
This is the modification time of the item we're talking about. This is the modification time of the item we're talking about.
size
This is the size of the item we're talking about.
""" """
try: try:
if header is None: if header is None:
raise ValueError raise ValueError
matches = re.match(r"^([^;]+)(; length=([0-9]+))?$", header, re.IGNORECASE) header_mtime = parse_http_date(header)
if matches is None:
raise ValueError
header_mtime = parse_http_date(matches[1])
header_len = matches[3]
if header_len and int(header_len) != size:
raise ValueError
if int(mtime) > header_mtime: if int(mtime) > header_mtime:
raise ValueError raise ValueError
except (ValueError, OverflowError): except (ValueError, OverflowError):

View File

@ -430,6 +430,9 @@ Miscellaneous
* The ``exc_info`` argument of the undocumented * The ``exc_info`` argument of the undocumented
``django.utils.log.log_response()`` function is replaced by ``exception``. ``django.utils.log.log_response()`` function is replaced by ``exception``.
* The ``size`` argument of the undocumented
``django.views.static.was_modified_since()`` function is removed.
.. _deprecated-features-4.1: .. _deprecated-features-4.1:
Features deprecated in 4.1 Features deprecated in 4.1