Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2603)

Unified Diff: chrome/common/extensions/docs/server2/render_servlet.py

Issue 218363002: Docs: Use ETags. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: use http 1.1 to make preview server respect it Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/common/extensions/docs/server2/render_servlet.py
diff --git a/chrome/common/extensions/docs/server2/render_servlet.py b/chrome/common/extensions/docs/server2/render_servlet.py
index c714b5de031f8ca7e8b0cac2a10e01e2266ae6f1..cfda81c8f4f3361c868ba9e18649187c01960d02 100644
--- a/chrome/common/extensions/docs/server2/render_servlet.py
+++ b/chrome/common/extensions/docs/server2/render_servlet.py
@@ -2,6 +2,7 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
+import hashlib
import logging
import posixpath
import traceback
@@ -15,12 +16,16 @@ from special_paths import SITE_VERIFICATION_FILE
from third_party.handlebar import Handlebar
-def _MakeHeaders(content_type):
- return {
- 'X-Frame-Options': 'sameorigin',
+def _MakeHeaders(content_type, etag=None):
+ headers = {
+ 'Cache-Control': 'public, max-age=0, no-cache',
Jeffrey Yasskin 2014/04/02 19:54:50 Maybe link to http://www.w3.org/Protocols/rfc2616/
not at google - send to devlin 2014/04/02 22:52:53 I'll link to the spec.
'Content-Type': content_type,
- 'Cache-Control': 'max-age=300',
+ 'ETag': etag,
Jeffrey Yasskin 2014/04/02 19:54:50 Remove this line, I think. you don't want to send
not at google - send to devlin 2014/04/02 22:52:53 Oops. forgot to remove it from this list.
+ 'X-Frame-Options': 'sameorigin',
}
+ if etag is not None:
+ headers['ETag'] = etag
+ return headers
class RenderServlet(Servlet):
@@ -122,10 +127,27 @@ class RenderServlet(Servlet):
if warnings:
sep = '\n - '
logging.warning('Rendering %s:%s%s' % (path, sep, sep.join(warnings)))
+ # Content was dynamic. The new etag is the md5 hash of the content.
Jeffrey Yasskin 2014/04/02 19:54:50 Maybe comment here that we're relying on SSL for s
not at google - send to devlin 2014/04/02 22:52:53 good point.
+ etag = None
+ elif content_and_type.version is not None:
+ # Content was static. The new etag is the version of the content.
+ etag = '"%s"' % content_and_type.version
+ else:
+ # Sometimes non-dynamic content does not have a version, for example
+ # .zip files. The new etag is the md5 hash of the content.
+ etag = None
content_type = content_and_type.content_type
if isinstance(content, unicode):
content = content.encode('utf-8')
content_type += '; charset=utf-8'
- return Response.Ok(content, headers=_MakeHeaders(content_type))
+ if etag is None:
+ m = hashlib.md5()
Jeffrey Yasskin 2014/04/02 19:54:50 You can just use hashlib.md5(content).hexdigest().
not at google - send to devlin 2014/04/02 22:52:53 Done.
+ m.update(content)
+ etag = '"%s"' % m.hexdigest()
+
+ headers = _MakeHeaders(content_type, etag=etag)
+ if etag == self._request.headers.get('If-None-Match'):
+ return Response.NotModified('Not Modified', headers=headers)
Jeffrey Yasskin 2014/04/02 19:54:50 Since we've already done the work to generate the
not at google - send to devlin 2014/04/02 22:52:53 We might as well. The only reason not to do it is
+ return Response.Ok(content, headers=headers)

Powered by Google App Engine
This is Rietveld 408576698