Commits

Ian Bicking  committed a653405

Grabbing the code from WSGIFilter

  • Participants

Comments (0)

Files changed (4)

+syntax: glob
+
+*.egg-info
+build
+dist

File debugheaders/__init__.py

+"""
+WSGI proxy application that applies a deliverance theme while
+passing the request to another HTTP server
+"""
+
+from cStringIO import StringIO
+import sys
+from paste.request import construct_url
+
+class DebugHeaders(object):
+
+    """Middleware that shows all headers.
+    """
+
+    translate_keys = {
+        'CONTENT_LENGTH': 'HTTP_CONTENT_LENGTH',
+        'CONTENT_TYPE': 'HTTP_CONTENT_TYPE',
+        }
+
+    def __init__(self, app, show_body=False, show_response_body=False,
+                 output=sys.stdout):
+        self.app = app
+        self.show_body = show_body
+        self.show_response_body = show_response_body
+        self.output = output or sys.stdout
+
+    def __call__(self, environ, start_response):
+        output = self.output
+        if output == 'wsgi.errors':
+            output = environ['wsgi.errors']
+        output.write(
+            'Incoming headers: (%s %s SCRIPT_NAME=%r)\n' %
+            (environ['REQUEST_METHOD'], construct_url(environ), environ.get('SCRIPT_NAME')))
+        for name, value in sorted(environ.items()):
+            name = self.translate_keys.get(name, name)
+            if not name.startswith('HTTP_'):
+                continue
+            name = name[5:].replace('_', '-').title()
+            output.write('  %s: %s\n' % (name, value))
+        if self.show_body:
+            self.show_request_body(environ, output)
+
+        def repl_start_response(status, headers, exc_info=None):
+            output.write('Outgoing headers: (%s)\n' % status)
+            for name, value in headers:
+                output.write('  %s: %s\n' % (name.title(), value))
+            return start_response(status, headers, exc_info)
+        if self.show_response_body:
+            out = []
+
+            def capture_start_response(status, headers, exc_info=None):
+                repl_start_response(status, headers, exc_info)
+                return out.append
+            for chunk in self.app(environ, capture_start_response):
+                out.append(chunk)
+            output.write('\nResponse body:\n')
+            self.show_output(''.join(out), output)
+        else:
+            return self.app(environ, repl_start_response)
+
+    def show_request_body(self, environ, output):
+        length = int(environ.get('CONTENT_LENGTH') or '0')
+        body = environ['wsgi.input'].read(length)
+        environ['wsgi.input'] = StringIO(body)
+        self.show_output(body, output)
+
+    def show_output(self, data, output):
+        if data:
+            for line in data.splitlines():
+                # This way we won't print out control characters:
+                output.write(line.encode('string_escape')+'\n')
+            output.write('-'*70+'\n')
+
+
+def make_debug_headers(app, global_conf, show_body=False,
+                       stderr=False):
+    """
+    Show all the headers that come to the application.
+
+    These are printed to sys.stdout, or sys.stderr if stderr=True.  If
+    show_body is true, then the body of all requests is also
+    displayed.
+    """
+    from paste.deploy.converters import asbool
+    if asbool(stderr):
+        output = sys.stderr
+    else:
+        output = sys.stdout
+    return DebugHeaders(app, show_body=asbool(show_body),
+                        output=output)

File docs/license.txt

+Copyright (c) 2007 Ian Bicking and Contributors
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+from setuptools import setup, find_packages
+
+version = '0.1'
+
+setup(name='DebugHeaders',
+      version=version,
+      description="Debug headers (and bodies) of WSGI requests and responses",
+      long_description="""\
+This WSGI middleware will display the headers of the incoming request
+and the outgoing response.  You can also optionally display the
+request or response body.
+
+Use it like this::
+
+    from debugheaders import DebugHeaders
+
+    def application(environ, start_response):
+        blah blah blah
+
+    application = DebugHeaders(application,
+                               show_body=True, show_response_body=True,
+                               output='wsgi.errors')
+
+`show_body` shows the request body, `show_response_body` does what you
+would expect, and this information is written to the stream given by
+`output` (defaults to stdout, can be any object with a ``.write()``
+method, and `'wsgi.errors'` is special and means to write to
+``environ['wsgi.errors']``).
+""",
+      classifiers=[
+          "Development Status :: 4 - Beta",
+          "Environment :: Web Environment",
+          "Framework :: Paste",
+          "Intended Audience :: Developers",
+          "License :: OSI Approved :: MIT License",
+          "Topic :: Internet :: WWW/HTTP :: WSGI",
+          "Topic :: Internet :: WWW/HTTP :: WSGI :: Middleware",
+          ],
+      keywords='wsgi debugging',
+      author='Ian Bicking',
+      author_email='ianb@colorstudy.com',
+      #url='http://pythonpaste.org/debugheaders/',
+      license='MIT',
+      packages=find_packages(exclude=['ez_setup', 'examples', 'tests']),
+      install_requires=[
+          "Paste",
+      ],
+      entry_points="""
+      [paste.filter_app_factory]
+      main = debugheaders:make_debug_headers
+      """,
+      )