Skip to content

Commit

Permalink
Merge pull request #203 from edmorley/fix-etag-handling
Browse files Browse the repository at this point in the history
Fix HTTP 304 handling when both ETag and last modified specified
  • Loading branch information
evansd committed Nov 12, 2018
2 parents 1b5a7f3 + f623081 commit e045411
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
12 changes: 12 additions & 0 deletions tests/test_whitenoise.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ def test_etag_doesnt_match(self):
response = self.server.get(self.files.js_url, headers={'If-None-Match': etag})
self.assertEqual(response.status_code, 200)

def test_etag_overrules_modified_since(self):
"""
Browsers send both headers so it's important that the ETag takes precedence
over the last modified time, so that deploy-rollbacks are handled correctly.
"""
headers = {
'If-None-Match': '"594bd1d1-36"',
'If-Modified-Since': 'Fri, 11 Apr 2100 11:47:06 GMT',
}
response = self.server.get(self.files.js_url, headers=headers)
self.assertEqual(response.status_code, 200)

def test_max_age(self):
response = self.server.get(self.files.js_url)
self.assertEqual(response.headers['Cache-Control'], 'max-age=1000, public')
Expand Down
5 changes: 3 additions & 2 deletions whitenoise/responders.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,9 @@ def get_alternatives(base_headers, files):
return alternatives

def is_not_modified(self, request_headers):
if self.etag == request_headers.get('HTTP_IF_NONE_MATCH'):
return True
previous_etag = request_headers.get('HTTP_IF_NONE_MATCH')
if previous_etag is not None:
return previous_etag == self.etag
try:
last_requested = request_headers['HTTP_IF_MODIFIED_SINCE']
except KeyError:
Expand Down

0 comments on commit e045411

Please sign in to comment.