| 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 logging |
| 7 import mimetypes | 7 import mimetypes |
| 8 import traceback | 8 import traceback |
| 9 import os | 9 import os |
| 10 | 10 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 from reference_resolver import ReferenceResolver | 28 from reference_resolver import ReferenceResolver |
| 29 from samples_data_source import SamplesDataSource | 29 from samples_data_source import SamplesDataSource |
| 30 from sidenav_data_source import SidenavDataSource | 30 from sidenav_data_source import SidenavDataSource |
| 31 from subversion_file_system import SubversionFileSystem | 31 from subversion_file_system import SubversionFileSystem |
| 32 import svn_constants | 32 import svn_constants |
| 33 from template_data_source import TemplateDataSource | 33 from template_data_source import TemplateDataSource |
| 34 from third_party.json_schema_compiler.memoize import memoize | 34 from third_party.json_schema_compiler.memoize import memoize |
| 35 from third_party.json_schema_compiler.model import UnixName | 35 from third_party.json_schema_compiler.model import UnixName |
| 36 import url_constants | 36 import url_constants |
| 37 | 37 |
| 38 def _IsBinaryMimetype(mimetype): | |
| 39 return any(mimetype.startswith(prefix) | |
| 40 for prefix in ['audio', 'image', 'video']) | |
| 41 | |
| 42 def _IsSamplesDisabled(): | 38 def _IsSamplesDisabled(): |
| 43 return IsDevServer() | 39 return IsDevServer() |
| 44 | 40 |
| 45 class ServerInstance(object): | 41 class ServerInstance(object): |
| 46 # Lazily create so we don't create github file systems unnecessarily in | 42 # Lazily create so we don't create github file systems unnecessarily in |
| 47 # tests. | 43 # tests. |
| 48 branch_utility = None | 44 branch_utility = None |
| 49 github_file_system = None | 45 github_file_system = None |
| 50 | 46 |
| 51 @staticmethod | 47 @staticmethod |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 | 202 |
| 207 self.example_zipper = ExampleZipper( | 203 self.example_zipper = ExampleZipper( |
| 208 self.compiled_fs_factory, | 204 self.compiled_fs_factory, |
| 209 svn_constants.DOCS_PATH) | 205 svn_constants.DOCS_PATH) |
| 210 | 206 |
| 211 self.path_canonicalizer = PathCanonicalizer( | 207 self.path_canonicalizer = PathCanonicalizer( |
| 212 channel, | 208 channel, |
| 213 self.compiled_fs_factory) | 209 self.compiled_fs_factory) |
| 214 | 210 |
| 215 self.content_cache = self.compiled_fs_factory.GetOrCreateIdentity() | 211 self.content_cache = self.compiled_fs_factory.GetOrCreateIdentity() |
| 216 | |
| 217 def _FetchStaticResource(self, path, response): | |
| 218 """Fetch a resource in the 'static' directory. | |
| 219 """ | |
| 220 mimetype = mimetypes.guess_type(path)[0] or 'text/plain' | |
| 221 result = self.content_cache.GetFromFile( | |
| 222 svn_constants.DOCS_PATH + '/' + path, | |
| 223 binary=_IsBinaryMimetype(mimetype)) | |
| 224 response.headers['content-type'] = mimetype | |
| 225 return result | |
| 226 | |
| 227 def Get(self, path, request, response): | |
| 228 templates = self.template_data_source_factory.Create(request, path) | |
| 229 | |
| 230 content = None | |
| 231 try: | |
| 232 if fnmatch(path, 'extensions/examples/*.zip'): | |
| 233 content = self.example_zipper.Create( | |
| 234 path[len('extensions/'):-len('.zip')]) | |
| 235 response.headers['content-type'] = 'application/zip' | |
| 236 elif path.startswith('extensions/examples/'): | |
| 237 mimetype = mimetypes.guess_type(path)[0] or 'text/plain' | |
| 238 content = self.content_cache.GetFromFile( | |
| 239 '%s/%s' % (svn_constants.DOCS_PATH, path[len('extensions/'):]), | |
| 240 binary=_IsBinaryMimetype(mimetype)) | |
| 241 response.headers['content-type'] = 'text/plain' | |
| 242 elif path.startswith('static/'): | |
| 243 content = self._FetchStaticResource(path, response) | |
| 244 elif path.endswith('.html'): | |
| 245 content = templates.Render(path) | |
| 246 except FileNotFoundError as e: | |
| 247 logging.warning(traceback.format_exc()) | |
| 248 | |
| 249 response.headers['x-frame-options'] = 'sameorigin' | |
| 250 if content is None: | |
| 251 response.set_status(404); | |
| 252 response.out.write(templates.Render('404')) | |
| 253 else: | |
| 254 if not content: | |
| 255 logging.error('%s had empty content' % path) | |
| 256 response.headers['cache-control'] = 'max-age=300' | |
| 257 response.out.write(content) | |
| OLD | NEW |