Skip to content

Commit 3bb80eb

Browse files
committed
Always set Etag in StaticFileHandler so it won't break if the default
Etag implementation in RequestHandler changes.
1 parent 76dd88f commit 3bb80eb

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

tornado/web.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,18 +1518,16 @@ def get(self, path, include_body=True):
15181518
self.set_status(304)
15191519
return
15201520

1521-
if not include_body:
1522-
self.set_header("Content-Length", os.path.getsize(abspath))
1523-
with open(abspath, "rb") as file:
1524-
hasher = hashlib.sha1()
1525-
hasher.update(file.read())
1526-
self.set_header("Etag", '"%s"' % hasher.hexdigest())
1527-
return
1528-
file = open(abspath, "rb")
1529-
try:
1530-
self.write(file.read())
1531-
finally:
1532-
file.close()
1521+
with open(abspath, "rb") as file:
1522+
data = file.read()
1523+
hasher = hashlib.sha1()
1524+
hasher.update(data)
1525+
self.set_header("Etag", '"%s"' % hasher.hexdigest())
1526+
if include_body:
1527+
self.write(data)
1528+
else:
1529+
assert self.request.method == "HEAD"
1530+
self.set_header("Content-Length", len(data))
15331531

15341532
def set_extra_headers(self, path):
15351533
"""For subclass to add extra headers to the response"""

0 commit comments

Comments
 (0)