| 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 mimetypes | 6 import mimetypes |
| 7 import os | 7 import os |
| 8 | 8 |
| 9 STATIC_DIR_PREFIX = 'docs/server2' | 9 STATIC_DIR_PREFIX = 'docs/server2' |
| 10 DOCS_PREFIX = 'docs' | 10 DOCS_PREFIX = 'docs' |
| 11 | 11 |
| 12 class ServerInstance(object): | 12 class ServerInstance(object): |
| 13 """This class is used to hold a data source and fetcher for an instance of a | 13 """This class is used to hold a data source and fetcher for an instance of a |
| 14 server. Each new branch will get its own ServerInstance. | 14 server. Each new branch will get its own ServerInstance. |
| 15 """ | 15 """ |
| 16 def __init__(self, template_data_source, example_zipper, cache_builder): | 16 def __init__(self, template_data_source, example_zipper, cache_builder): |
| 17 self._template_data_source = template_data_source | 17 self._template_data_source = template_data_source |
| 18 self._example_zipper = example_zipper | 18 self._example_zipper = example_zipper |
| 19 self._cache = cache_builder.build(lambda x: x) | 19 self._cache = cache_builder.build(lambda x: x) |
| 20 mimetypes.init() | 20 mimetypes.init() |
| 21 | 21 |
| 22 def _FetchStaticResource(self, path, request_handler): | 22 def _FetchStaticResource(self, path, request_handler): |
| 23 """Fetch a resource in the 'static' directory. | 23 """Fetch a resource in the 'static' directory. |
| 24 """ | 24 """ |
| 25 try: | 25 try: |
| 26 result = self._cache.getFromFile(STATIC_DIR_PREFIX + '/' + path) | 26 result = self._cache.GetFromFile(STATIC_DIR_PREFIX + '/' + path) |
| 27 base, ext = os.path.splitext(path) | 27 base, ext = os.path.splitext(path) |
| 28 request_handler.response.headers['content-type'] = ( | 28 request_handler.response.headers['content-type'] = ( |
| 29 mimetypes.types_map[ext]) | 29 mimetypes.types_map[ext]) |
| 30 return result | 30 return result |
| 31 except: | 31 except Exception: |
| 32 return '' | 32 return '' |
| 33 | 33 |
| 34 def Get(self, path, request_handler): | 34 def Get(self, path, request_handler): |
| 35 if fnmatch(path, 'examples/*.zip'): | 35 if fnmatch(path, 'examples/*.zip'): |
| 36 content = self._example_zipper.create(path[:-4]) | 36 content = self._example_zipper.Create(path[:-4]) |
| 37 request_handler.response.headers['content-type'] = ( | 37 request_handler.response.headers['content-type'] = ( |
| 38 mimetypes.types_map['.zip']) | 38 mimetypes.types_map['.zip']) |
| 39 elif path.startswith('examples/'): | 39 elif path.startswith('examples/'): |
| 40 content = self._cache.getFromFile(DOCS_PREFIX + '/' + path) | 40 content = self._cache.GetFromFile(DOCS_PREFIX + '/' + path) |
| 41 request_handler.response.headers['content-type'] = 'text/plain' | 41 request_handler.response.headers['content-type'] = 'text/plain' |
| 42 elif path.startswith('static/'): | 42 elif path.startswith('static/'): |
| 43 content = self._FetchStaticResource(path, request_handler) | 43 content = self._FetchStaticResource(path, request_handler) |
| 44 else: | 44 else: |
| 45 content = self._template_data_source.Render(path) | 45 content = self._template_data_source.Render(path) |
| 46 if content: | 46 if content: |
| 47 request_handler.response.out.write(content) | 47 request_handler.response.out.write(content) |
| 48 else: | 48 else: |
| 49 request_handler.response.set_status(404); | 49 request_handler.response.set_status(404); |
| 50 request_handler.response.out.write( | 50 request_handler.response.out.write( |
| 51 self._template_data_source.Render('404')) | 51 self._template_data_source.Render('404')) |
| OLD | NEW |