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 appengine_wrappers import webapp | 5 from branch_utility import BranchUtility |
6 from cron_servlet import CronServlet | 6 from cron_servlet import CronServlet |
7 from render_servlet import RenderServlet | 7 from instance_servlet import InstanceServlet |
8 from servlet import Request | 8 from servlet import Servlet, Request, Response |
9 | 9 |
10 _SERVLETS = { | 10 _SERVLETS = { |
11 'cron': CronServlet, | 11 'cron': CronServlet, |
12 } | 12 } |
| 13 _DEFAULT_SERVLET = InstanceServlet.GetConstructor() |
13 | 14 |
14 class Handler(webapp.RequestHandler): | 15 class Handler(Servlet): |
15 def __init__(self, request, response): | 16 def Get(self): |
16 super(Handler, self).__init__(request, response) | 17 path = self._request.path |
17 | |
18 def _RedirectSpecialCases(self, path): | |
19 if not path or path == 'index.html': | |
20 self.redirect('http://developer.google.com/chrome') | |
21 return True | |
22 | |
23 if path == 'apps.html': | |
24 self.redirect('/apps/about_apps.html') | |
25 return True | |
26 | |
27 return False | |
28 | |
29 def _RedirectFromCodeDotGoogleDotCom(self, path): | |
30 if (not self.request.url.startswith(('http://code.google.com', | |
31 'https://code.google.com'))): | |
32 return False | |
33 | |
34 new_url = 'http://developer.chrome.com/' | |
35 | |
36 # switch to https if necessary | |
37 if (self.request.url.startswith('https')): | |
38 new_url = new_url.replace('http', 'https', 1) | |
39 | |
40 path = path.split('/') | |
41 if len(path) > 0 and path[0] == 'chrome': | |
42 path.pop(0) | |
43 for channel in BranchUtility.GetAllBranchNames(): | |
44 if channel in path: | |
45 position = path.index(channel) | |
46 path.pop(position) | |
47 path.insert(0, channel) | |
48 new_url += '/'.join(path) | |
49 self.redirect(new_url) | |
50 return True | |
51 | |
52 def get(self): | |
53 path, request, response = (self.request.path.lstrip('/'), | |
54 self.request, | |
55 self.response) | |
56 | 18 |
57 if path in ['favicon.ico', 'robots.txt']: | 19 if path in ['favicon.ico', 'robots.txt']: |
58 response.set_status(404) | 20 return Response.NotFound('') |
59 return | |
60 | 21 |
61 if self._RedirectSpecialCases(path): | 22 redirect = self._RedirectSpecialCases() |
62 return | 23 if redirect is None: |
63 if self._RedirectFromCodeDotGoogleDotCom(path): | 24 redirect = self._RedirectFromCodeDotGoogleDotCom() |
64 return | 25 if redirect is not None: |
| 26 return redirect |
65 | 27 |
66 if path.startswith('_'): | 28 if path.startswith('_'): |
67 servlet_path = path[1:] | 29 servlet_path = path[1:] |
68 if servlet_path.find('/') == -1: | 30 if servlet_path.find('/') == -1: |
69 servlet_path += '/' | 31 servlet_path += '/' |
70 servlet_name, servlet_path = servlet_path.split('/', 1) | 32 servlet_name, servlet_path = servlet_path.split('/', 1) |
71 servlet = _SERVLETS.get(servlet_name) | 33 servlet = _SERVLETS.get(servlet_name) |
72 if servlet is None: | 34 if servlet is None: |
73 response.out.write('"%s" servlet not found' % servlet_path) | 35 return Response.NotFound('"%s" servlet not found' % servlet_path) |
74 response.set_status(404) | |
75 return | |
76 else: | 36 else: |
77 servlet_path = path | 37 servlet_path = path |
78 servlet = RenderServlet | 38 servlet = _DEFAULT_SERVLET |
79 | 39 |
80 servlet_response = servlet(Request(servlet_path, request.headers)).Get() | 40 return servlet(Request(servlet_path, |
| 41 self._request.host, |
| 42 self._request.headers)).Get() |
81 | 43 |
82 response.out.write(servlet_response.content.ToString()) | 44 def _RedirectSpecialCases(self): |
83 response.headers.update(servlet_response.headers) | 45 path = self._request.path |
84 response.status = servlet_response.status | 46 if not path or path == 'index.html': |
| 47 return Response.Redirect('http://developer.google.com/chrome') |
| 48 if path == 'apps.html': |
| 49 return Response.Redirect('/apps/about_apps.html') |
| 50 return None |
| 51 |
| 52 def _RedirectFromCodeDotGoogleDotCom(self): |
| 53 host, path = (self._request.host, self._request.path) |
| 54 |
| 55 if not host in ('http://code.google.com', 'https://code.google.com'): |
| 56 return None |
| 57 |
| 58 new_host = 'http://developer.chrome.com' |
| 59 |
| 60 # switch to https if necessary |
| 61 if host.startswith('https'): |
| 62 new_host = new_host.replace('http', 'https', 1) |
| 63 |
| 64 new_path = path.split('/') |
| 65 if len(new_path) > 0 and new_path[0] == 'chrome': |
| 66 new_path.pop(0) |
| 67 for channel in BranchUtility.GetAllChannelNames(): |
| 68 if channel in new_path: |
| 69 position = new_path.index(channel) |
| 70 new_path.pop(position) |
| 71 new_path.insert(0, channel) |
| 72 return Response.Redirect('/'.join([new_host] + new_path)) |
OLD | NEW |