From eb1982ea22937a042f5a6e98c5709f779a8fd66c Mon Sep 17 00:00:00 2001 From: Braedon Vickers Date: Mon, 11 Oct 2021 13:53:49 +0800 Subject: Fix handling of empty If-Modified-Since header for static files Previously, if the If-Modified-Since header was an empty string it wouldn't be parsed into an int (or None), but would still be compared to the int mtime, causing a TypeError. --- bottle.py | 4 ++-- test/test_sendfile.py | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/bottle.py b/bottle.py index d94721f..48aefbb 100755 --- a/bottle.py +++ b/bottle.py @@ -2928,8 +2928,8 @@ def static_file(filename, root, 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 ims is not None and ims >= int(stats.st_mtime): + return HTTPResponse(status=304, **headers) body = '' if request.method == 'HEAD' else open(filename, 'rb') diff --git a/test/test_sendfile.py b/test/test_sendfile.py index d6693c2..ef8eef5 100755 --- a/test/test_sendfile.py +++ b/test/test_sendfile.py @@ -95,6 +95,11 @@ class TestSendFile(unittest.TestCase): request.environ['HTTP_IF_MODIFIED_SINCE'] = bottle.http_date(100) self.assertEqual(open(__file__,'rb').read(), static_file(basename, root=root).body.read()) + def test_ims_empty(self): + """ SendFile: Empty If-Modified-Since""" + request.environ['HTTP_IF_MODIFIED_SINCE'] = '' + self.assertEqual(open(__file__, 'rb').read(), static_file(basename, root=root).body.read()) + def test_etag(self): """ SendFile: If-Modified-Since""" res = static_file(basename, root=root) -- cgit v1.2.1