changeset 0:68bea9e91bd2

initial import of relocator
author Jeff Hammel <jhammel@mozilla.com>
date Sun, 01 May 2011 13:31:40 -0700
parents
children a12647395076
files README.txt relocator/__init__.py relocator/relocator.py relocator/sample.py setup.py
diffstat 5 files changed, 88 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /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
+
+
+
--- /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
--- /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)
--- /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()
--- /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: -*-
+      """,
+      )
+