Opened 15 years ago
Closed 15 years ago
#11122 closed (invalid)
views.static.serve does not work with if-modified-since header the third time a file is requested
Reported by: | Rob van der Linde | Owned by: | nobody |
---|---|---|---|
Component: | Generic views | Version: | 1.0 |
Severity: | Keywords: | ||
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
This is a very strange bug, but I will try to explain. I have a static file that has a 2006 last-modified date on it. The first time this file is requested through django.views.static.serve, the browser will download the file fully and send a last-modified header with it, properly dating to 2006. The second time this file is requested, the browser will send an if-modified-since request and the serve function detects this and only sends a HttpResponseNotModified so the browser won't re-download the file (this is fine so far and what I would expect). The problem is when I try to request the file for a third time, it will re-download the file, although it shouldn't (this is with Firefox 3 at least). I had a look with the web developer extension in Firefox and have found that the first time the file is sent, the last-modified date is 2006, but the third time the last-modified date is 2009. In fact, it is the date of the last request... not the date of the file... this is why it appears to be re-downloading the file the third time when it shouldn't.
I was able to fix this, by changing this part in /django/views/static.py (serve function, line 192):
if not was_modified_since(request.META.get('HTTP_IF_MODIFIED_SINCE'), statobj[stat.ST_MTIME], statobj[stat.ST_SIZE]): return HttpResponseNotModified()
To:
if not was_modified_since(request.META.get('HTTP_IF_MODIFIED_SINCE'), statobj[stat.ST_MTIME], statobj[stat.ST_SIZE]): response = HttpResponseNotModified() response["Last-Modified"] = http_date(statobj[stat.ST_MTIME]) return response
By sending the last-modified date of the file again with the HttpResponseNotModified response, the third time I request the file, it won't re-download the file again in Firefox and display the proper 2006 date in web developer extension, instead of 2009, effectively fixing the problem. I don't know if this is a bug with Firefox or Django, however the small change to the code does fix it for me.
I'm actually getting mixed results now, without the code I added it's not that it seems to re-download the file always on the third time, but only every so often, most of the time it does properly cache the file. Although it doesn't do it with the extra lines of code I added, I would think this is a Firefox thing more than anything rather than a problem with Django itself, and it would probably be best to close this bug.