diff options
author | 张酉夫 <zhangyoufu@gmail.com> | 2019-12-19 11:59:12 +0800 |
---|---|---|
committer | Marcel Hellkamp <marc@gsites.de> | 2019-12-19 13:22:28 +0100 |
commit | 9f81b8087ba1486a475c27208a3fd678a526f6fc (patch) | |
tree | b66a4c8b1587c0e85d9e7bc8cd239603ffbd1d84 | |
parent | 8e898e408a9d7460151c328b5c09079e91e0d6c9 (diff) | |
download | bottle-9f81b8087ba1486a475c27208a3fd678a526f6fc.tar.gz |
fix date formatting for non-English locales
-rwxr-xr-x | bottle.py | 28 | ||||
-rwxr-xr-x | test/test_sendfile.py | 18 |
2 files changed, 27 insertions, 19 deletions
@@ -69,8 +69,8 @@ if __name__ == '__main__': ############################################################################### -import base64, cgi, email.utils, functools, hmac, imp, itertools, mimetypes,\ - os, re, tempfile, threading, time, warnings, weakref, hashlib +import base64, calendar, cgi, email.utils, functools, hmac, imp, itertools,\ + mimetypes, os, re, tempfile, threading, time, warnings, weakref, hashlib from types import FunctionType from datetime import date as datedate, datetime, timedelta @@ -1887,11 +1887,7 @@ class BaseResponse(object): if isinstance(value, timedelta): value = value.seconds + value.days * 24 * 3600 if key == 'expires': - if isinstance(value, (datedate, datetime)): - value = value.timetuple() - elif isinstance(value, (int, float)): - value = time.gmtime(value) - value = time.strftime("%a, %d %b %Y %H:%M:%S GMT", value) + value = http_date(value) if key in ('same_site', 'samesite'): # 'samesite' variant added in 0.13 key, value = 'samesite', (value or "none").lower() if value not in ('lax', 'strict', 'none'): @@ -2965,13 +2961,19 @@ def debug(mode=True): def http_date(value): - if isinstance(value, (datedate, datetime)): + if isinstance(value, basestring): + return value + if isinstance(value, datetime): + # aware datetime.datetime is converted to UTC time + # naive datetime.datetime is treated as UTC time value = value.utctimetuple() - elif isinstance(value, (int, float)): - value = time.gmtime(value) - if not isinstance(value, basestring): - value = time.strftime("%a, %d %b %Y %H:%M:%S GMT", value) - return value + elif isinstance(value, datedate): + # datetime.date is naive, and is treated as UTC time + value = value.timetuple() + if not isinstance(value, (int, float)): + # convert struct_time in UTC to UNIX timestamp + value = calendar.timegm(value) + return email.utils.formatdate(value, usegmt=True) def parse_date(ims): diff --git a/test/test_sendfile.py b/test/test_sendfile.py index 6872e4d..622d992 100755 --- a/test/test_sendfile.py +++ b/test/test_sendfile.py @@ -13,23 +13,29 @@ basename2 = os.path.basename(bottle.__file__) root2 = os.path.dirname(bottle.__file__) +weekday_full = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] +weekday_abbr = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] +month_abbr = [None, 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] + class TestDateParser(unittest.TestCase): def test_rfc1123(self): """DateParser: RFC 1123 format""" ts = time.time() - rs = time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(ts)) + rs = bottle.http_date(ts) self.assertEqual(int(ts), int(parse_date(rs))) def test_rfc850(self): """DateParser: RFC 850 format""" ts = time.time() - rs = time.strftime("%A, %d-%b-%y %H:%M:%S GMT", time.gmtime(ts)) + t = time.gmtime(ts) + rs = time.strftime("%%s, %d-%%s-%y %H:%M:%S GMT", t) % (weekday_full[t.tm_wday], month_abbr[t.tm_mon]) self.assertEqual(int(ts), int(parse_date(rs))) def test_asctime(self): """DateParser: asctime format""" ts = time.time() - rs = time.strftime("%a %b %d %H:%M:%S %Y", time.gmtime(ts)) + t = time.gmtime(ts) + rs = time.strftime("%%s %%s %d %H:%M:%S %Y", t) % (weekday_abbr[t.tm_wday], month_abbr[t.tm_mon]) self.assertEqual(int(ts), int(parse_date(rs))) def test_bad(self): @@ -81,12 +87,12 @@ class TestSendFile(unittest.TestCase): def test_ims(self): """ SendFile: If-Modified-Since""" - request.environ['HTTP_IF_MODIFIED_SINCE'] = time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime()) + request.environ['HTTP_IF_MODIFIED_SINCE'] = bottle.http_date(time.time()) res = static_file(basename, root=root) self.assertEqual(304, res.status_code) self.assertEqual(int(os.stat(__file__).st_mtime), parse_date(res.headers['Last-Modified'])) self.assertAlmostEqual(int(time.time()), parse_date(res.headers['Date'])) - request.environ['HTTP_IF_MODIFIED_SINCE'] = time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(100)) + 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_etag(self): @@ -116,7 +122,7 @@ class TestSendFile(unittest.TestCase): f = static_file(basename, root=root, download=True) self.assertEqual('attachment; filename="%s"' % basename, f.headers['Content-Disposition']) - request.environ['HTTP_IF_MODIFIED_SINCE'] = time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(100)) + request.environ['HTTP_IF_MODIFIED_SINCE'] = bottle.http_date(100) f = static_file(basename, root=root) self.assertEqual(open(__file__,'rb').read(), f.body.read()) |