From: Ben Darnell Date: Sun, 22 Jan 2012 22:19:19 +0000 (-0800) Subject: Always set Etag in StaticFileHandler so it won't break if the default X-Git-Tag: v2.2.0~13 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3bb80eb83fc230361e8d29ba75fb4d6fb8b45987;p=thirdparty%2Ftornado.git Always set Etag in StaticFileHandler so it won't break if the default Etag implementation in RequestHandler changes. --- diff --git a/tornado/web.py b/tornado/web.py index 07d576c07..ea57e925e 100644 --- a/tornado/web.py +++ b/tornado/web.py @@ -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"""