OLD | NEW |
(Empty) | |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is govered by a BSD-style |
| 3 # license that can be found in the LICENSE file or at |
| 4 # https://developers.google.com/open-source/licenses/bsd |
| 5 |
| 6 """This file sets up all the urls for monorail pages.""" |
| 7 |
| 8 |
| 9 import httplib |
| 10 import logging |
| 11 |
| 12 import webapp2 |
| 13 |
| 14 |
| 15 def MakeRedirect(redirect_to_this_uri, permanent=True): |
| 16 """Return a new request handler class that redirects to the given URL.""" |
| 17 |
| 18 class Redirect(webapp2.RequestHandler): |
| 19 """Redirect is a response handler that issues a redirect to another URI.""" |
| 20 |
| 21 def get(self, **_kw): |
| 22 """Send the 301/302 response code and write the Location: redirect.""" |
| 23 self.response.location = redirect_to_this_uri |
| 24 self.response.headers.add('Strict-Transport-Security', |
| 25 'max-age=31536000; includeSubDomains') |
| 26 self.response.status = ( |
| 27 httplib.MOVED_PERMANENTLY if permanent else httplib.FOUND) |
| 28 |
| 29 return Redirect |
| 30 |
| 31 |
| 32 def MakeRedirectInScope(uri_in_scope, scope, permanent=True): |
| 33 """Redirect to a URI within a given scope, e.g., per project or user. |
| 34 |
| 35 Args: |
| 36 uri_in_scope: a uri within a project or user starting with a slash. |
| 37 scope: a string indicating the uri-space scope: |
| 38 p for project pages |
| 39 u for user pages |
| 40 g for group pages |
| 41 permanent: True for a HTTP 301 permanently moved response code, |
| 42 otherwise a HTTP 302 temporarily moved response will be used. |
| 43 |
| 44 Example: |
| 45 self._SetupProjectPage( |
| 46 redirect.MakeRedirectInScope('/newpage', 'p'), '/oldpage') |
| 47 |
| 48 Returns: |
| 49 A class that can be used with webapp2. |
| 50 """ |
| 51 assert uri_in_scope.startswith('/') |
| 52 |
| 53 class RedirectInScope(webapp2.RequestHandler): |
| 54 """A handler that redirects to another URI in the same scope.""" |
| 55 |
| 56 def get(self, **_kw): |
| 57 """Send the 301/302 response code and write the Location: redirect.""" |
| 58 split_path = self.request.path.lstrip('/').split('/') |
| 59 if len(split_path) > 1: |
| 60 project_or_user = split_path[1] |
| 61 url = '//%s/%s/%s%s' % ( |
| 62 self.request.host, scope, project_or_user, uri_in_scope) |
| 63 self.response.location = url |
| 64 else: |
| 65 self.response.location = '/' |
| 66 |
| 67 self.response.headers.add('Strict-Transport-Security', |
| 68 'max-age=31536000; includeSubDomains') |
| 69 self.response.status = ( |
| 70 httplib.MOVED_PERMANENTLY if permanent else httplib.FOUND) |
| 71 |
| 72 return RedirectInScope |
OLD | NEW |