Mercurial > hg > relocator
comparison relocator/relocator.py @ 0:68bea9e91bd2
initial import of relocator
| author | Jeff Hammel <jhammel@mozilla.com> |
|---|---|
| date | Sun, 01 May 2011 13:31:40 -0700 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:68bea9e91bd2 |
|---|---|
| 1 """ | |
| 2 request dispatcher | |
| 3 """ | |
| 4 | |
| 5 import urlparse | |
| 6 from webob import Request | |
| 7 | |
| 8 class Relocator(object): | |
| 9 | |
| 10 | |
| 11 def __init__(self, app, baseurl): | |
| 12 self.app = app | |
| 13 self.baseurl = list(urlparse.urlsplit(baseurl)) | |
| 14 | |
| 15 def __call__(self, environ, start_response): | |
| 16 request = Request(environ) | |
| 17 response = request.get_response(self.app) | |
| 18 if 'Location' in response.headers: | |
| 19 parsed = list(urlparse.urlsplit(response.headers['Location'])) | |
| 20 location = ['' for i in range(5)] # new location | |
| 21 | |
| 22 # overwrite scheme, netloc, and fragment | |
| 23 for i in (0, 1, 4): | |
| 24 location[i] = self.baseurl[i] | |
| 25 | |
| 26 # append path and query string | |
| 27 for i in (2, 3): | |
| 28 location[i] = self.baseurl[i] + parsed[i] | |
| 29 | |
| 30 response.headers['Location'] = urlparse.urlunsplit(location) | |
| 31 return response(environ, start_response) |
