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 import traceback | 8 import traceback |
9 from urlparse import urlsplit | 9 from urlparse import urlsplit |
10 | 10 |
11 from branch_utility import BranchUtility | |
12 from file_system import FileNotFoundError | 11 from file_system import FileNotFoundError |
13 from servlet import Servlet, Response | 12 from servlet import Servlet, Response |
14 import svn_constants | 13 import svn_constants |
15 | 14 |
16 def _IsBinaryMimetype(mimetype): | 15 def _IsBinaryMimetype(mimetype): |
17 return any( | 16 return any( |
18 mimetype.startswith(prefix) for prefix in ['audio', 'image', 'video']) | 17 mimetype.startswith(prefix) for prefix in ['audio', 'image', 'video']) |
19 | 18 |
20 class RenderServlet(Servlet): | 19 class RenderServlet(Servlet): |
21 '''Servlet which renders templates. | 20 '''Servlet which renders templates. |
22 ''' | 21 ''' |
23 class Delegate(object): | 22 class Delegate(object): |
24 def CreateServerInstanceForChannel(self, channel): | 23 def CreateServerInstance(self): |
25 raise NotImplementedError() | 24 raise NotImplementedError(self.__class__) |
26 | 25 |
27 def __init__(self, request, delegate, default_channel='stable'): | 26 def __init__(self, request, delegate): |
28 Servlet.__init__(self, request) | 27 Servlet.__init__(self, request) |
29 self._delegate = delegate | 28 self._delegate = delegate |
30 self._default_channel = default_channel | |
31 | 29 |
32 def Get(self): | 30 def Get(self): |
33 ''' Render the page for a request. | 31 ''' Render the page for a request. |
34 ''' | 32 ''' |
35 headers = self._request.headers | 33 # TODO(kalman): a consistent path syntax (even a Path class?) so that we |
36 channel, path = BranchUtility.SplitChannelNameFromPath(self._request.path) | 34 # can stop being so conservative with stripping and adding back the '/'s. |
| 35 path = self._request.path.lstrip('/') |
37 | 36 |
38 if path.split('/')[-1] == 'redirects.json': | 37 if path.split('/')[-1] == 'redirects.json': |
39 return Response.Ok('') | 38 return Response.Ok('') |
40 | 39 |
41 if channel == self._default_channel: | 40 server_instance = self._delegate.CreateServerInstance() |
42 return Response.Redirect('/' + path) | |
43 if channel is None: | |
44 channel = self._default_channel | |
45 | |
46 server_instance = self._delegate.CreateServerInstanceForChannel(channel) | |
47 | 41 |
48 redirect = server_instance.redirector.Redirect(self._request.host, path) | 42 redirect = server_instance.redirector.Redirect(self._request.host, path) |
49 if redirect is not None: | 43 if redirect is not None: |
50 if (channel != self._default_channel and | |
51 not urlsplit(redirect).scheme in ('http', 'https')): | |
52 redirect = '/%s%s' % (channel, redirect) | |
53 return Response.Redirect(redirect) | 44 return Response.Redirect(redirect) |
54 | 45 |
55 canonical_path = server_instance.path_canonicalizer.Canonicalize(path) | 46 canonical_result = server_instance.path_canonicalizer.Canonicalize(path) |
56 redirect = canonical_path.lstrip('/') | 47 redirect = canonical_result.path.lstrip('/') |
57 if path != redirect: | 48 if path != redirect: |
58 if channel is not None: | 49 return Response.Redirect('/' + redirect, |
59 redirect = '%s/%s' % (channel, canonical_path) | 50 permanent=canonical_result.permanent) |
60 return Response.Redirect('/' + redirect) | |
61 | 51 |
62 templates = server_instance.template_data_source_factory.Create( | 52 templates = server_instance.template_data_source_factory.Create( |
63 self._request, path) | 53 self._request, path) |
64 | 54 |
65 content = None | 55 content = None |
66 content_type = None | 56 content_type = None |
67 | 57 |
68 try: | 58 try: |
69 # At this point, any valid paths ending with '/' have been redirected. | 59 # At this point, any valid paths ending with '/' have been redirected. |
70 # Therefore, the response should be a 404 Not Found. | 60 # Therefore, the response should be a 404 Not Found. |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 return Response.NotFound(content, headers=headers) | 92 return Response.NotFound(content, headers=headers) |
103 | 93 |
104 if not content: | 94 if not content: |
105 logging.error('%s had empty content' % path) | 95 logging.error('%s had empty content' % path) |
106 | 96 |
107 headers.update({ | 97 headers.update({ |
108 'content-type': content_type, | 98 'content-type': content_type, |
109 'cache-control': 'max-age=300', | 99 'cache-control': 'max-age=300', |
110 }) | 100 }) |
111 return Response.Ok(content, headers=headers) | 101 return Response.Ok(content, headers=headers) |
OLD | NEW |