OLD | NEW |
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import hashlib | 5 import hashlib |
6 import logging | 6 import logging |
7 import posixpath | 7 import posixpath |
8 import traceback | 8 import traceback |
9 | 9 |
10 from branch_utility import BranchUtility | 10 from branch_utility import BranchUtility |
11 from environment import IsPreviewServer | 11 from environment import IsPreviewServer |
12 from file_system import FileNotFoundError | 12 from file_system import FileNotFoundError |
13 from redirector import Redirector | 13 from redirector import Redirector |
14 from servlet import Servlet, Response | 14 from servlet import Servlet, Response |
15 from special_paths import SITE_VERIFICATION_FILE | 15 from special_paths import SITE_VERIFICATION_FILE |
16 from third_party.handlebar import Handlebar | 16 from third_party.motemplate import Motemplate |
17 | 17 |
18 | 18 |
19 def _MakeHeaders(content_type, etag=None): | 19 def _MakeHeaders(content_type, etag=None): |
20 headers = { | 20 headers = { |
21 # See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.1. | 21 # See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.1. |
22 'Cache-Control': 'public, max-age=0, no-cache', | 22 'Cache-Control': 'public, max-age=0, no-cache', |
23 'Content-Type': content_type, | 23 'Content-Type': content_type, |
24 'X-Frame-Options': 'sameorigin', | 24 'X-Frame-Options': 'sameorigin', |
25 } | 25 } |
26 if etag is not None: | 26 if etag is not None: |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 # Directory request hasn't been redirected by now. Default behaviour is | 104 # Directory request hasn't been redirected by now. Default behaviour is |
105 # to redirect as though it were a file. | 105 # to redirect as though it were a file. |
106 return Response.Redirect('/' + request_path.rstrip('/'), | 106 return Response.Redirect('/' + request_path.rstrip('/'), |
107 permanent=False) | 107 permanent=False) |
108 | 108 |
109 content_and_type = content_provider.GetContentAndType(path).Get() | 109 content_and_type = content_provider.GetContentAndType(path).Get() |
110 if not content_and_type.content: | 110 if not content_and_type.content: |
111 logging.error('%s had empty content' % path) | 111 logging.error('%s had empty content' % path) |
112 | 112 |
113 content = content_and_type.content | 113 content = content_and_type.content |
114 if isinstance(content, Handlebar): | 114 if isinstance(content, Motemplate): |
115 template_content, template_warnings = ( | 115 template_content, template_warnings = ( |
116 server_instance.template_renderer.Render(content, self._request)) | 116 server_instance.template_renderer.Render(content, self._request)) |
117 # HACK: the site verification file (google2ed...) doesn't have a title. | 117 # HACK: the site verification file (google2ed...) doesn't have a title. |
118 content, doc_warnings = server_instance.document_renderer.Render( | 118 content, doc_warnings = server_instance.document_renderer.Render( |
119 template_content, | 119 template_content, |
120 path, | 120 path, |
121 render_title=path != SITE_VERIFICATION_FILE) | 121 render_title=path != SITE_VERIFICATION_FILE) |
122 warnings = template_warnings + doc_warnings | 122 warnings = template_warnings + doc_warnings |
123 if warnings: | 123 if warnings: |
124 sep = '\n - ' | 124 sep = '\n - ' |
(...skipping 17 matching lines...) Expand all Loading... |
142 if etag is None: | 142 if etag is None: |
143 # Note: we're using md5 as a convenient and fast-enough way to identify | 143 # Note: we're using md5 as a convenient and fast-enough way to identify |
144 # content. It's not intended to be cryptographic in any way, and this | 144 # content. It's not intended to be cryptographic in any way, and this |
145 # is *not* what etags is for. That's what SSL is for, this is unrelated. | 145 # is *not* what etags is for. That's what SSL is for, this is unrelated. |
146 etag = '"%s"' % hashlib.md5(content).hexdigest() | 146 etag = '"%s"' % hashlib.md5(content).hexdigest() |
147 | 147 |
148 headers = _MakeHeaders(content_type, etag=etag) | 148 headers = _MakeHeaders(content_type, etag=etag) |
149 if etag == self._request.headers.get('If-None-Match'): | 149 if etag == self._request.headers.get('If-None-Match'): |
150 return Response.NotModified('Not Modified', headers=headers) | 150 return Response.NotModified('Not Modified', headers=headers) |
151 return Response.Ok(content, headers=headers) | 151 return Response.Ok(content, headers=headers) |
OLD | NEW |