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