]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Always set Etag in StaticFileHandler so it won't break if the default
authorBen Darnell <ben@bendarnell.com>
Sun, 22 Jan 2012 22:19:19 +0000 (14:19 -0800)
committerBen Darnell <ben@bendarnell.com>
Sun, 22 Jan 2012 22:19:19 +0000 (14:19 -0800)
Etag implementation in RequestHandler changes.

tornado/web.py

index 07d576c07841562814bdc92d37ab8a4311cc0a75..ea57e925e60ac7df4ccafb01e1a36d553f7ad080 100644 (file)
@@ -1518,18 +1518,16 @@ class StaticFileHandler(RequestHandler):
                 self.set_status(304)
                 return
 
-        if not include_body:
-            self.set_header("Content-Length", os.path.getsize(abspath))
-            with open(abspath, "rb") as file:
-                hasher = hashlib.sha1()
-                hasher.update(file.read())
-                self.set_header("Etag", '"%s"' % hasher.hexdigest())
-            return
-        file = open(abspath, "rb")
-        try:
-            self.write(file.read())
-        finally:
-            file.close()
+        with open(abspath, "rb") as file:
+            data = file.read()
+            hasher = hashlib.sha1()
+            hasher.update(data)
+            self.set_header("Etag", '"%s"' % hasher.hexdigest())
+            if include_body:
+                self.write(data)
+            else:
+                assert self.request.method == "HEAD"
+                self.set_header("Content-Length", len(data))
 
     def set_extra_headers(self, path):
         """For subclass to add extra headers to the response"""