From ac45401f2a71a32447afcdc64e123a396a549020 Mon Sep 17 00:00:00 2001 From: Birk Nilson Date: Fri, 9 Mar 2012 18:25:52 +0100 Subject: [PATCH] Introduce StaticFileHandler.(get_content|set_headers) which reduces the complexity of the HTTP GET handler of static resources. --- tornado/web.py | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/tornado/web.py b/tornado/web.py index a516372d5..0c4b0076c 100644 --- a/tornado/web.py +++ b/tornado/web.py @@ -1507,6 +1507,27 @@ class StaticFileHandler(RequestHandler): if not os.path.isfile(abspath): raise HTTPError(403, "%s is not a file", path) + if not self.set_headers(abspath, path) + return + + body = self.get_content(abspath) + if not body: + return + + hasher = hashlib.sha1() + hasher.update(body) + self.set_header("Etag", '"%s"' % hasher.hexdigest()) + if include_body: + self.write(body) + else: + assert self.request.method == "HEAD" + self.set_header("Content-Length", len(body)) + + def set_headers(self, abspath, path): + """Set the response headers in order to ensure that client browsers + will cache the requested resource and not proceed to retrieve its content + in the case of a 304 response. + """ stat_result = os.stat(abspath) modified = datetime.datetime.fromtimestamp(stat_result[stat.ST_MTIME]) @@ -1535,18 +1556,17 @@ class StaticFileHandler(RequestHandler): if_since = datetime.datetime.fromtimestamp(time.mktime(date_tuple)) if if_since >= modified: self.set_status(304) - return + return False + + return True + def get_content(self, abspath): + """Retrieve the content of the requested resource which is located + at the given ``abspath``. + """ 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)) + return file.read() + return None def set_extra_headers(self, path): """For subclass to add extra headers to the response""" -- 2.47.2