Skip to content

Commit

Permalink
Always set Etag in StaticFileHandler so it won't break if the default
Browse files Browse the repository at this point in the history
Etag implementation in RequestHandler changes.
  • Loading branch information
bdarnell committed Jan 22, 2012
1 parent 76dd88f commit 3bb80eb
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions tornado/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -1518,18 +1518,16 @@ def get(self, path, include_body=True):
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"""
Expand Down

0 comments on commit 3bb80eb

Please sign in to comment.