Skip to content
Browse files

Check If-None-Match in StaticFileHandler, move etag quoting out of co…

…mpute_etag()

Add test
  • Loading branch information...
1 parent 46f520a commit fbb1be0936ed53b2f7f8e58d1c9aaa05cdc73727 @alekstorm committed
Showing with 16 additions and 3 deletions.
  1. +6 −0 tornado/test/web_test.py
  2. +10 −3 tornado/web.py
View
6 tornado/test/web_test.py
@@ -734,6 +734,12 @@ def test_static_304(self):
self.assertTrue('Content-Length' not in response2.headers)
self.assertTrue('Last-Modified' not in response2.headers)
+ response3 = self.fetch("/static/robots.txt", headers={
+ 'If-None-Match': response1.headers['Etag']})
+ self.assertEqual(response3.code, 304)
+ self.assertTrue('Content-Length' not in response3.headers)
+ self.assertTrue('Etag' in response3.headers)
+
class CustomStaticFileTest(AsyncHTTPTestCase, LogTrapTestCase):
def get_app(self):
View
13 tornado/web.py
@@ -678,7 +678,7 @@ def finish(self, chunk=None):
"Etag" not in self._headers):
etag = self.compute_etag()
if etag is not None:
- self.set_header("Etag", etag)
+ self.set_header("Etag", '"%s"' % etag)
inm = self.request.headers.get("If-None-Match")
if inm and inm.find(etag) != -1:
self._write_buffer = []
@@ -990,7 +990,7 @@ def compute_etag(self):
hasher = hashlib.sha1()
for part in self._write_buffer:
hasher.update(part)
- return '"%s"' % hasher.hexdigest()
+ return hasher.hexdigest()
def _stack_context_handle_exception(self, type, value, traceback):
try:
@@ -1555,7 +1555,14 @@ def get(self, path, include_body=True):
stat_result = os.stat(abspath)[stat.ST_MTIME]
modified = datetime.datetime.fromtimestamp(stat_result)
self.set_header("Last-Modified", modified)
- self.set_header("Etag", '"%s"' % self.compute_etag_for_path(abspath))
+ etag = self.compute_etag_for_path(abspath)
+ if etag is not None:
+ quoted_etag = '"%s"' % etag
+ self.set_header("Etag", quoted_etag)
+ inm = self.request.headers.get("If-None-Match")
+ if inm and inm.find(quoted_etag) != -1:
+ self.set_status(304)
+ return
mime_type, encoding = mimetypes.guess_type(abspath)
if mime_type:

0 comments on commit fbb1be0

Please sign in to comment.
Something went wrong with that request. Please try again.