# HG changeset patch # User Jeff Hammel # Date 1304281900 25200 # Node ID 68bea9e91bd2d7ad1c7f53fc66c7ef4d448d4530 initial import of relocator diff -r 000000000000 -r 68bea9e91bd2 README.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/README.txt Sun May 01 13:31:40 2011 -0700 @@ -0,0 +1,7 @@ +relocator +---------- + +change Location field in responses using WSGI middleware + + + diff -r 000000000000 -r 68bea9e91bd2 relocator/__init__.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/relocator/__init__.py Sun May 01 13:31:40 2011 -0700 @@ -0,0 +1,1 @@ +from relocator import Relocator diff -r 000000000000 -r 68bea9e91bd2 relocator/relocator.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/relocator/relocator.py Sun May 01 13:31:40 2011 -0700 @@ -0,0 +1,31 @@ +""" +request dispatcher +""" + +import urlparse +from webob import Request + +class Relocator(object): + + + def __init__(self, app, baseurl): + self.app = app + self.baseurl = list(urlparse.urlsplit(baseurl)) + + def __call__(self, environ, start_response): + request = Request(environ) + response = request.get_response(self.app) + if 'Location' in response.headers: + parsed = list(urlparse.urlsplit(response.headers['Location'])) + location = ['' for i in range(5)] # new location + + # overwrite scheme, netloc, and fragment + for i in (0, 1, 4): + location[i] = self.baseurl[i] + + # append path and query string + for i in (2, 3): + location[i] = self.baseurl[i] + parsed[i] + + response.headers['Location'] = urlparse.urlunsplit(location) + return response(environ, start_response) diff -r 000000000000 -r 68bea9e91bd2 relocator/sample.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/relocator/sample.py Sun May 01 13:31:40 2011 -0700 @@ -0,0 +1,19 @@ +#!/usr/bin/env python + +from relocator import Relocator +from webob import exc + +def sample_app(environ, start_response): + """sample app that does a redirect""" + response = exc.HTTPSeeOther(location='/foo/bar') + return response(environ, start_response) + +def sample_factory(baseurl='http://example.com/toolbox'): + """create a webob view and wrap it in the relocator""" + return Relocator(sample_app, baseurl) + +if __name__ == '__main__': + from wsgiref import simple_server + app = sample_factory() + server = simple_server.make_server(host='0.0.0.0', port=12345, app=app) + server.serve_forever() diff -r 000000000000 -r 68bea9e91bd2 setup.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/setup.py Sun May 01 13:31:40 2011 -0700 @@ -0,0 +1,30 @@ +from setuptools import setup, find_packages + +try: + description = file('README.txt').read() +except IOError: + description = '' + +version = "0.1" + +setup(name='relocator', + version=version, + description="change Location field in responses using WSGI middleware", + long_description=description, + classifiers=[], # Get strings from http://www.python.org/pypi?%3Aaction=list_classifiers + author='Jeff Hammel', + author_email='jhammel@mozilla.com', + url='', + license="MPL", + packages=find_packages(exclude=['ez_setup', 'examples', 'tests']), + include_package_data=True, + zip_safe=False, + install_requires=[ + # -*- Extra requirements: -*- + 'WebOb', + ], + entry_points=""" + # -*- Entry points: -*- + """, + ) +