| 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 from fnmatch import fnmatch | 5 from fnmatch import fnmatch |
| 6 import logging | 6 import logging |
| 7 import mimetypes | 7 import mimetypes |
| 8 from urlparse import urlsplit | 8 from urlparse import urlsplit |
| 9 | 9 |
| 10 from data_source_registry import CreateDataSources | 10 from data_source_registry import CreateDataSources |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 redirect = server_instance.redirector.Redirect(self._request.host, path) | 42 redirect = server_instance.redirector.Redirect(self._request.host, path) |
| 43 if redirect is not None: | 43 if redirect is not None: |
| 44 return Response.Redirect(redirect) | 44 return Response.Redirect(redirect) |
| 45 | 45 |
| 46 canonical_result = server_instance.path_canonicalizer.Canonicalize(path) | 46 canonical_result = server_instance.path_canonicalizer.Canonicalize(path) |
| 47 redirect = canonical_result.path.lstrip('/') | 47 redirect = canonical_result.path.lstrip('/') |
| 48 if path != redirect: | 48 if path != redirect: |
| 49 return Response.Redirect('/' + redirect, | 49 return Response.Redirect('/' + redirect, |
| 50 permanent=canonical_result.permanent) | 50 permanent=canonical_result.permanent) |
| 51 | 51 |
| 52 trunk_fs = server_instance.host_file_system_provider.GetTrunk() |
| 52 templates = server_instance.template_data_source_factory.Create( | 53 templates = server_instance.template_data_source_factory.Create( |
| 53 self._request, | 54 self._request, |
| 54 CreateDataSources(server_instance, self._request)) | 55 CreateDataSources(server_instance, self._request)) |
| 55 | 56 |
| 56 content = None | 57 content = None |
| 57 content_type = None | 58 content_type = None |
| 58 | 59 |
| 59 try: | 60 try: |
| 60 # At this point, any valid paths ending with '/' have been redirected. | 61 # At this point, any valid paths ending with '/' have been redirected. |
| 61 # Therefore, the response should be a 404 Not Found. | 62 # Therefore, the response should be a 404 Not Found. |
| 62 if path.endswith('/'): | 63 if path.endswith('/'): |
| 63 pass | 64 pass |
| 64 elif fnmatch(path, 'extensions/examples/*.zip'): | 65 elif fnmatch(path, 'extensions/examples/*.zip'): |
| 65 content = server_instance.example_zipper.Create( | 66 content = server_instance.example_zipper.Create( |
| 66 path[len('extensions/'):-len('.zip')]) | 67 path[len('extensions/'):-len('.zip')]) |
| 67 content_type = 'application/zip' | 68 content_type = 'application/zip' |
| 68 elif path.startswith('extensions/examples/'): | 69 elif path.startswith('extensions/examples/'): |
| 69 mimetype = mimetypes.guess_type(path)[0] or 'text/plain' | 70 mimetype = mimetypes.guess_type(path)[0] or 'text/plain' |
| 70 content = server_instance.host_file_system.ReadSingle( | 71 content = trunk_fs.ReadSingle( |
| 71 '%s/%s' % (svn_constants.DOCS_PATH, path[len('extensions/'):]), | 72 '%s/%s' % (svn_constants.DOCS_PATH, path[len('extensions/'):]), |
| 72 binary=_IsBinaryMimetype(mimetype)) | 73 binary=_IsBinaryMimetype(mimetype)) |
| 73 content_type = mimetype | 74 content_type = mimetype |
| 74 elif path.startswith('static/'): | 75 elif path.startswith('static/'): |
| 75 mimetype = mimetypes.guess_type(path)[0] or 'text/plain' | 76 mimetype = mimetypes.guess_type(path)[0] or 'text/plain' |
| 76 content = server_instance.host_file_system.ReadSingle( | 77 content = trunk_fs.ReadSingle( |
| 77 ('%s/%s' % (svn_constants.DOCS_PATH, path)), | 78 ('%s/%s' % (svn_constants.DOCS_PATH, path)), |
| 78 binary=_IsBinaryMimetype(mimetype)) | 79 binary=_IsBinaryMimetype(mimetype)) |
| 79 content_type = mimetype | 80 content_type = mimetype |
| 80 elif path.endswith('.html'): | 81 elif path.endswith('.html'): |
| 81 content = templates.Render(path) | 82 content = templates.Render(path) |
| 82 content_type = 'text/html' | 83 content_type = 'text/html' |
| 83 else: | 84 else: |
| 84 content = None | 85 content = None |
| 85 except FileNotFoundError: | 86 except FileNotFoundError: |
| 86 content = None | 87 content = None |
| 87 | 88 |
| 88 headers = {'x-frame-options': 'sameorigin'} | 89 headers = {'x-frame-options': 'sameorigin'} |
| 89 if content is None: | 90 if content is None: |
| 90 content = (templates.Render('%s/404' % path.split('/', 1)[0]) or | 91 content = (templates.Render('%s/404' % path.split('/', 1)[0]) or |
| 91 templates.Render('extensions/404')) | 92 templates.Render('extensions/404')) |
| 92 return Response.NotFound(content, headers=headers) | 93 return Response.NotFound(content, headers=headers) |
| 93 | 94 |
| 94 if not content: | 95 if not content: |
| 95 logging.error('%s had empty content' % path) | 96 logging.error('%s had empty content' % path) |
| 96 | 97 |
| 97 headers.update({ | 98 headers.update({ |
| 98 'content-type': content_type, | 99 'content-type': content_type, |
| 99 'cache-control': 'max-age=300', | 100 'cache-control': 'max-age=300', |
| 100 }) | 101 }) |
| 101 return Response.Ok(content, headers=headers) | 102 return Response.Ok(content, headers=headers) |
| OLD | NEW |