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 from urlparse import urlparse | 7 from urlparse import urlparse |
8 | 8 |
9 from appengine_url_fetcher import AppEngineUrlFetcher | 9 from appengine_url_fetcher import AppEngineUrlFetcher |
10 from appengine_wrappers import IsDevServer | 10 from appengine_wrappers import IsDevServer |
(...skipping 10 matching lines...) Expand all Loading... |
21 from server_instance import ServerInstance | 21 from server_instance import ServerInstance |
22 from servlet import Request, Response, Servlet | 22 from servlet import Request, Response, Servlet |
23 import svn_constants | 23 import svn_constants |
24 import url_constants | 24 import url_constants |
25 | 25 |
26 class _PatchServletDelegate(RenderServlet.Delegate): | 26 class _PatchServletDelegate(RenderServlet.Delegate): |
27 def __init__(self, issue, delegate): | 27 def __init__(self, issue, delegate): |
28 self._issue = issue | 28 self._issue = issue |
29 self._delegate = delegate | 29 self._delegate = delegate |
30 | 30 |
31 def CreateServerInstanceForChannel(self, channel): | 31 def CreateServerInstance(self): |
32 base_object_store_creator = ObjectStoreCreator(channel, | 32 object_store_creator = ObjectStoreCreator(start_empty=False) |
33 start_empty=False) | 33 branch_utility = self._delegate.CreateBranchUtility(object_store_creator) |
34 branch_utility = self._delegate.CreateBranchUtility( | |
35 base_object_store_creator) | |
36 host_file_system_creator = self._delegate.CreateHostFileSystemCreator( | 34 host_file_system_creator = self._delegate.CreateHostFileSystemCreator( |
37 base_object_store_creator) | 35 object_store_creator) |
38 # TODO(fj): Use OfflineFileSystem here once all json/idl files in api/ | 36 # TODO(fj): Use OfflineFileSystem here once all json/idl files in api/ |
39 # are pulled into data store by cron jobs. | 37 # are pulled into data store by cron jobs. |
40 base_file_system = CachingFileSystem( | 38 base_file_system = CachingFileSystem(host_file_system_creator.Create(), |
41 host_file_system_creator.Create( | 39 object_store_creator) |
42 branch_utility.GetChannelInfo(channel).branch), | 40 base_compiled_fs_factory = CompiledFileSystem.Factory(base_file_system, |
43 base_object_store_creator) | 41 object_store_creator) |
44 base_compiled_fs_factory = CompiledFileSystem.Factory( | |
45 base_file_system, base_object_store_creator) | |
46 | 42 |
47 object_store_creator = ObjectStoreCreator('trunk@%s' % self._issue, | |
48 start_empty=False) | |
49 rietveld_patcher = CachingRietveldPatcher( | 43 rietveld_patcher = CachingRietveldPatcher( |
50 RietveldPatcher(svn_constants.EXTENSIONS_PATH, | 44 RietveldPatcher(svn_constants.EXTENSIONS_PATH, |
51 self._issue, | 45 self._issue, |
52 AppEngineUrlFetcher(url_constants.CODEREVIEW_SERVER)), | 46 AppEngineUrlFetcher(url_constants.CODEREVIEW_SERVER)), |
53 object_store_creator) | 47 object_store_creator) |
54 patched_file_system = PatchedFileSystem(base_file_system, | 48 patched_file_system = PatchedFileSystem(base_file_system, |
55 rietveld_patcher) | 49 rietveld_patcher) |
56 patched_compiled_fs_factory = CompiledFileSystem.Factory( | 50 patched_compiled_fs_factory = CompiledFileSystem.Factory( |
57 patched_file_system, object_store_creator) | 51 patched_file_system, object_store_creator) |
58 | 52 |
59 compiled_fs_factory = ChainedCompiledFileSystem.Factory( | 53 compiled_fs_factory = ChainedCompiledFileSystem.Factory( |
60 [(patched_compiled_fs_factory, patched_file_system), | 54 [(patched_compiled_fs_factory, patched_file_system), |
61 (base_compiled_fs_factory, base_file_system)]) | 55 (base_compiled_fs_factory, base_file_system)]) |
62 | 56 |
63 return ServerInstance(channel, | 57 return ServerInstance(object_store_creator, |
64 object_store_creator, | |
65 patched_file_system, | 58 patched_file_system, |
66 self._delegate.CreateAppSamplesFileSystem( | 59 self._delegate.CreateAppSamplesFileSystem( |
67 base_object_store_creator), | 60 object_store_creator), |
68 '/_patch/%s' % self._issue, | 61 '/_patch/%s' % self._issue, |
69 compiled_fs_factory, | 62 compiled_fs_factory, |
70 branch_utility, | 63 branch_utility, |
71 host_file_system_creator) | 64 host_file_system_creator) |
72 | 65 |
73 class PatchServlet(Servlet): | 66 class PatchServlet(Servlet): |
74 '''Servlet which renders patched docs. | 67 '''Servlet which renders patched docs. |
75 ''' | 68 ''' |
76 def __init__(self, request, delegate=None): | 69 def __init__(self, request, delegate=None): |
77 self._request = request | 70 self._request = request |
78 self._delegate = delegate or InstanceServlet.Delegate() | 71 self._delegate = delegate or InstanceServlet.Delegate() |
79 | 72 |
80 def Get(self): | 73 def Get(self): |
81 if (not IsDevServer() and | 74 if (not IsDevServer() and |
82 not fnmatch(urlparse(self._request.host).netloc, '*.appspot.com')): | 75 not fnmatch(urlparse(self._request.host).netloc, '*.appspot.com')): |
83 # Only allow patches on appspot URLs; it doesn't matter if appspot.com is | 76 # Only allow patches on appspot URLs; it doesn't matter if appspot.com is |
84 # XSS'ed, but it matters for chrome.com. | 77 # XSS'ed, but it matters for chrome.com. |
85 redirect_host = 'https://chrome-apps-doc.appspot.com' | 78 redirect_host = 'https://chrome-apps-doc.appspot.com' |
86 logging.info('Redirecting from XSS-able host %s to %s' % ( | 79 logging.info('Redirecting from XSS-able host %s to %s' % ( |
87 self._request.host, redirect_host)) | 80 self._request.host, redirect_host)) |
88 return Response.Redirect( | 81 return Response.Redirect( |
89 '%s/_patch/%s' % (redirect_host, self._request.path)) | 82 '%s/_patch/%s' % (redirect_host, self._request.path)) |
90 | 83 |
91 path_with_issue = self._request.path.lstrip('/') | 84 path_with_issue = self._request.path.lstrip('/') |
92 if '/' in path_with_issue: | 85 if '/' in path_with_issue: |
93 issue, real_path = path_with_issue.split('/', 1) | 86 issue, path_without_issue = path_with_issue.split('/', 1) |
94 else: | 87 else: |
95 return Response.NotFound('Malformed URL. It should look like ' + | 88 return Response.NotFound('Malformed URL. It should look like ' + |
96 'https://developer.chrome.com/_patch/12345/extensions/...') | 89 'https://developer.chrome.com/_patch/12345/extensions/...') |
97 | 90 |
98 fake_path = '/trunk/%s' % real_path | |
99 | |
100 try: | 91 try: |
101 response = RenderServlet( | 92 response = RenderServlet( |
102 Request(fake_path, self._request.host, self._request.headers), | 93 Request(path_without_issue, |
| 94 self._request.host, |
| 95 self._request.headers), |
103 _PatchServletDelegate(issue, self._delegate)).Get() | 96 _PatchServletDelegate(issue, self._delegate)).Get() |
104 # Disable cache for patched content. | 97 # Disable cache for patched content. |
105 response.headers.pop('cache-control', None) | 98 response.headers.pop('cache-control', None) |
106 except RietveldPatcherError as e: | 99 except RietveldPatcherError as e: |
107 response = Response.NotFound(e.message, {'Content-Type': 'text/plain'}) | 100 response = Response.NotFound(e.message, {'Content-Type': 'text/plain'}) |
108 | 101 |
109 redirect_url, permanent = response.GetRedirect() | 102 redirect_url, permanent = response.GetRedirect() |
110 if redirect_url is not None: | 103 if redirect_url is not None: |
111 if redirect_url.startswith('/trunk/'): | |
112 redirect_url = redirect_url.split('/trunk', 1)[1] | |
113 response = Response.Redirect('/_patch/%s%s' % (issue, redirect_url), | 104 response = Response.Redirect('/_patch/%s%s' % (issue, redirect_url), |
114 permanent) | 105 permanent) |
115 return response | 106 return response |
OLD | NEW |