Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip If-Modified-Since check when client sends If-None-Match #1154

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions bottle.py
Original file line number Diff line number Diff line change
Expand Up @@ -2908,17 +2908,20 @@ def static_file(filename, root,
clen, filename)
etag = hashlib.sha1(tob(etag)).hexdigest()

inm = getenv('HTTP_IF_NONE_MATCH')

if etag:
headers['ETag'] = etag
check = getenv('HTTP_IF_NONE_MATCH')
if check and check == etag:
if inm and inm == etag:
return HTTPResponse(status=304, **headers)

ims = getenv('HTTP_IF_MODIFIED_SINCE')
if ims:
ims = parse_date(ims.split(";")[0].strip())
if ims is not None and ims >= int(stats.st_mtime):
return HTTPResponse(status=304, **headers)
# If client has If-None-Match, ignore If-Modified-Since.
if not inm:
ims = getenv('HTTP_IF_MODIFIED_SINCE')
if ims:
ims = parse_date(ims.split(";")[0].strip())
if ims is not None and ims >= int(stats.st_mtime):
return HTTPResponse(status=304, **headers)

body = '' if request.method == 'HEAD' else open(filename, 'rb')

Expand Down
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Support for Python 2.5 was marked as deprecated since 0.12. We decided to go a s
These changes might require special care when updating.

* Signed cookies now use a stronger HMAC algorithm by default. This will result in old cookies to appear invalid after the update. Pass an explicit ``digestmod=hashlib.md5`` to :meth:`Request.get_cookie` and :meth:`Response.set_cookie` to get the old behavior.
* ``If-None-Match`` has precedence when used in combination with ``If-Modified-Since``, skip ``If-Modified-Since`` check even if ``If-None-Match`` check fails.

.. rubric:: Other Improvements
* Bottle() instances are now context managers. If used in a with-statement, the default application changes to the specific instance and the shortcuts for many instance methods can be used.
Expand Down
11 changes: 8 additions & 3 deletions test/test_sendfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ def test_ims(self):
self.assertEqual(open(__file__,'rb').read(), static_file(basename, root=root).body.read())

def test_etag(self):
""" SendFile: If-Modified-Since"""
""" SendFile: If-None-Match"""
res = static_file(basename, root=root)
self.assertTrue('ETag' in res.headers)
self.assertEqual(200, res.status_code)
etag = res.headers['ETag']

request.environ['HTTP_IF_NONE_MATCH'] = etag
res = static_file(basename, root=root)
self.assertTrue('ETag' in res.headers)
Expand All @@ -107,7 +107,12 @@ def test_etag(self):
self.assertTrue('ETag' in res.headers)
self.assertNotEqual(etag, res.headers['ETag'])
self.assertEqual(200, res.status_code)


# test ignore If-Modified-Since when If-None-Match checked failed.
request.environ['HTTP_IF_NONE_MATCH'] = '123456'
request.environ['HTTP_IF_MODIFIED_SINCE'] = time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime())
res = static_file(basename, root=root)
self.assertEqual(200, res.status_code)

def test_download(self):
""" SendFile: Download as attachment """
Expand Down