OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 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 from fnmatch import fnmatch | 5 from fnmatch import fnmatch |
| 6 import logging |
6 import mimetypes | 7 import mimetypes |
7 import os | 8 import os |
8 | 9 |
9 from api_data_source import APIDataSource | 10 from api_data_source import APIDataSource |
10 from api_list_data_source import APIListDataSource | 11 from api_list_data_source import APIListDataSource |
11 from appengine_blobstore import AppEngineBlobstore | 12 from appengine_blobstore import AppEngineBlobstore |
12 from appengine_url_fetcher import AppEngineUrlFetcher | 13 from appengine_url_fetcher import AppEngineUrlFetcher |
13 from branch_utility import BranchUtility | 14 from branch_utility import BranchUtility |
14 from caching_file_system import CachingFileSystem | 15 from caching_file_system import CachingFileSystem |
15 from compiled_file_system import CompiledFileSystem | 16 from compiled_file_system import CompiledFileSystem |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 self.api_list_data_source_factory, | 178 self.api_list_data_source_factory, |
178 self.intro_data_source_factory, | 179 self.intro_data_source_factory, |
179 self.samples_data_source_factory, | 180 self.samples_data_source_factory, |
180 self.sidenav_data_source_factory, | 181 self.sidenav_data_source_factory, |
181 self.compiled_fs_factory, | 182 self.compiled_fs_factory, |
182 self.ref_resolver_factory, | 183 self.ref_resolver_factory, |
183 svn_constants.PUBLIC_TEMPLATE_PATH, | 184 svn_constants.PUBLIC_TEMPLATE_PATH, |
184 svn_constants.PRIVATE_TEMPLATE_PATH) | 185 svn_constants.PRIVATE_TEMPLATE_PATH) |
185 | 186 |
186 self.example_zipper = ExampleZipper( | 187 self.example_zipper = ExampleZipper( |
187 self.svn_file_system, | |
188 self.compiled_fs_factory, | 188 self.compiled_fs_factory, |
189 svn_constants.DOCS_PATH) | 189 svn_constants.DOCS_PATH) |
190 | 190 |
191 self.path_canonicalizer = PathCanonicalizer( | 191 self.path_canonicalizer = PathCanonicalizer( |
192 channel, | 192 channel, |
193 self.compiled_fs_factory) | 193 self.compiled_fs_factory) |
194 | 194 |
195 self.content_cache = self.compiled_fs_factory.GetOrCreateIdentity() | 195 self.content_cache = self.compiled_fs_factory.GetOrCreateIdentity() |
196 | 196 |
197 def _FetchStaticResource(self, path, response): | 197 def _FetchStaticResource(self, path, response): |
198 """Fetch a resource in the 'static' directory. | 198 """Fetch a resource in the 'static' directory. |
199 """ | 199 """ |
200 mimetype = mimetypes.guess_type(path)[0] or 'text/plain' | 200 mimetype = mimetypes.guess_type(path)[0] or 'text/plain' |
201 try: | 201 result = self.content_cache.GetFromFile( |
202 result = self.content_cache.GetFromFile( | 202 svn_constants.DOCS_PATH + '/' + path, |
203 svn_constants.DOCS_PATH + '/' + path, | 203 binary=_IsBinaryMimetype(mimetype)) |
204 binary=_IsBinaryMimetype(mimetype)) | |
205 except FileNotFoundError: | |
206 return None | |
207 response.headers['content-type'] = mimetype | 204 response.headers['content-type'] = mimetype |
208 return result | 205 return result |
209 | 206 |
210 def Get(self, path, request, response): | 207 def Get(self, path, request, response): |
211 templates = self.template_data_source_factory.Create(request, path) | 208 templates = self.template_data_source_factory.Create(request, path) |
212 | 209 |
213 if path.rsplit('/', 1)[-1] in ('favicon.ico', 'robots.txt'): | |
214 response.set_status(404) | |
215 response.out.write(templates.Render('404')) | |
216 return | |
217 | |
218 content = None | 210 content = None |
219 if fnmatch(path, 'extensions/examples/*.zip'): | 211 try: |
220 try: | 212 if fnmatch(path, 'extensions/examples/*.zip'): |
221 content = self.example_zipper.Create( | 213 content = self.example_zipper.Create( |
222 path[len('extensions/'):-len('.zip')]) | 214 path[len('extensions/'):-len('.zip')]) |
223 response.headers['content-type'] = 'application/zip' | 215 response.headers['content-type'] = 'application/zip' |
224 except FileNotFoundError: | 216 elif path.startswith('extensions/examples/'): |
225 content = None | 217 mimetype = mimetypes.guess_type(path)[0] or 'text/plain' |
226 elif path.startswith('extensions/examples/'): | |
227 mimetype = mimetypes.guess_type(path)[0] or 'text/plain' | |
228 try: | |
229 content = self.content_cache.GetFromFile( | 218 content = self.content_cache.GetFromFile( |
230 '%s/%s' % (svn_constants.DOCS_PATH, path[len('extensions/'):]), | 219 '%s/%s' % (svn_constants.DOCS_PATH, path[len('extensions/'):]), |
231 binary=_IsBinaryMimetype(mimetype)) | 220 binary=_IsBinaryMimetype(mimetype)) |
232 response.headers['content-type'] = 'text/plain' | 221 response.headers['content-type'] = 'text/plain' |
233 except FileNotFoundError: | 222 elif path.startswith('static/'): |
234 content = None | 223 content = self._FetchStaticResource(path, response) |
235 elif path.startswith('static/'): | 224 elif path.endswith('.html'): |
236 content = self._FetchStaticResource(path, response) | 225 content = templates.Render(path) |
237 elif path.endswith('.html'): | 226 except FileNotFoundError as e: |
238 content = templates.Render(path) | 227 logging.warning(e) |
239 | 228 |
240 response.headers['x-frame-options'] = 'sameorigin' | 229 response.headers['x-frame-options'] = 'sameorigin' |
241 if content: | 230 if content is None: |
| 231 response.set_status(404); |
| 232 response.out.write(templates.Render('404')) |
| 233 else: |
| 234 if not content: |
| 235 logging.error('%s had empty content' % path) |
242 response.headers['cache-control'] = 'max-age=300' | 236 response.headers['cache-control'] = 'max-age=300' |
243 response.out.write(content) | 237 response.out.write(content) |
244 else: | |
245 response.set_status(404); | |
246 response.out.write(templates.Render('404')) | |
OLD | NEW |