Commits

Atsushi Odagiri  committed 9646f0e

first

  • Participants

Comments (0)

Files changed (3)

+[egg_info]
+tag_build = dev
+tag_svn_revision = true
+from setuptools import setup, find_packages
+import sys, os
+
+version = '0.0'
+
+setup(name='wsgiheaders',
+      version=version,
+      description="",
+      long_description="""\
+""",
+      classifiers=[], # Get strings from http://pypi.python.org/pypi?%3Aaction=list_classifiers
+      keywords='',
+      author='',
+      author_email='',
+      url='',
+      license='',
+      packages=find_packages(exclude=['ez_setup', 'examples', 'tests']),
+      include_package_data=True,
+      zip_safe=False,
+      install_requires=[
+          # -*- Extra requirements: -*-
+      ],
+      entry_points="""
+      # -*- Entry points: -*-
+      """,
+      )

File wsgiheaders/__init__.py

+#
+"""
+>>> import re
+>>> p = re.compile(r'.*\.html')
+>>> @replaceheader([(p, [('Content-type', 'text/html')])])
+... def app(environ, start_response):
+...     start_response('200 OK',
+...                    [('Content-type', 'text/plain')])
+...     return ['Hello, world!']
+>>> import webtest
+>>> app = webtest.TestApp(app)
+>>> res = app.get('/a.txt')
+>>> res.content_type
+'text/plain'
+>>> res = app.get('/a.html')
+>>> res.content_type
+'text/html'
+>>> p = re.compile(r'.*')
+>>> @addheader([(p, [('X-XRDS', 'http://localhost/services.xrds')])])
+... def app(environ, start_response):
+...     start_response('200 OK',
+...                    [('Content-type', 'text/plain')])
+...     return ['Hello, world!']
+>>> app = webtest.TestApp(app)
+>>> res = app.get('/')
+>>> res.headers['X-XRDS']
+'http://localhost/services.xrds'
+"""
+
+from webob import Request, Response
+
+def addheader(headerConditions):
+    def decorator(app):
+        def wrap(environ, start_response):
+            req = Request(environ)
+            res = req.get_response(app)
+            for pred, headers in headerConditions:
+                if pred.match(req.url):
+                    for name, value in headers:
+                        res.headers.add(name, value)
+            return res(environ, start_response)
+        return wrap
+    return decorator
+
+def replaceheader(headerConditions):
+    def decorator(app):
+        def wrap(environ, start_response):
+            req = Request(environ)
+            res = req.get_response(app)
+            for pred, headers in headerConditions:
+                if pred.match(req.url):
+                    for name, value in headers:
+                        res.headers[name] = value
+            return res(environ, start_response)
+        return wrap
+    return decorator