~ubuntu-branches/ubuntu/oneiric/moin/oneiric-security

« back to all changes in this revision

Viewing changes to MoinMoin/web/_fallback_cgi.py

  • Committer: Bazaar Package Importer
  • Author(s): Jamie Strandboge
  • Date: 2010-03-30 12:55:34 UTC
  • mfrom: (0.1.17 sid)
  • Revision ID: james.westby@ubuntu.com-20100330125534-4c2ufc1rok24447l
Tags: 1.9.2-2ubuntu1
* Merge from Debian testing (LP: #521834). Based on work by Stefan Ebner.
  Remaining changes:
 - Remove python-xml from Suggests field, the package isn't anymore in
   sys.path.
 - Demote fckeditor from Recommends to Suggests; the code was previously
   embedded in moin, but it was also disabled, so there's no reason for us
   to pull this in by default currently. Note: This isn't necessary anymore
   but needs a MIR for fckeditor, so postpone dropping this change until
   lucid+1
* debian/rules:
  - Replace hardcoded python2.5 with python* and hardcore python2.6 for ln
* debian/control.in: drop versioned depends on cdbs

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: iso-8859-1 -*-
 
2
"""
 
3
    MoinMoin - Fallback CGI WSGI interface
 
4
 
 
5
    Provide a simple WSGI-to-CGI adapter in case flup is not available.
 
6
    It is taken from http://www.python.org/dev/peps/pep-0333/.
 
7
 
 
8
    @copyright: 2003-2006 Phillip J. Eby <pje at telecommunity.com>
 
9
    @license: Public Domain
 
10
"""
 
11
import os, sys
 
12
 
 
13
 
 
14
__all__ = ['WSGIServer']
 
15
 
 
16
 
 
17
class WSGIServer(object):
 
18
 
 
19
    def __init__(self, application):
 
20
        self.application = application
 
21
 
 
22
    def run(self):
 
23
 
 
24
        environ = dict(os.environ.items())
 
25
        environ['wsgi.input']        = sys.stdin
 
26
        environ['wsgi.errors']       = sys.stderr
 
27
        environ['wsgi.version']      = (1, 0)
 
28
        environ['wsgi.multithread']  = False
 
29
        environ['wsgi.multiprocess'] = True
 
30
        environ['wsgi.run_once']     = True
 
31
 
 
32
        if environ.get('HTTPS', 'off') in ('on', '1'):
 
33
            environ['wsgi.url_scheme'] = 'https'
 
34
        else:
 
35
            environ['wsgi.url_scheme'] = 'http'
 
36
 
 
37
        headers_set = []
 
38
        headers_sent = []
 
39
 
 
40
        def write(data):
 
41
            if not headers_set:
 
42
                raise AssertionError("write() before start_response()")
 
43
 
 
44
            elif not headers_sent:
 
45
                # Before the first output, send the stored headers
 
46
                status, response_headers = headers_sent[:] = headers_set
 
47
                sys.stdout.write('Status: %s\r\n' % status)
 
48
                for header in response_headers:
 
49
                    sys.stdout.write('%s: %s\r\n' % header)
 
50
                sys.stdout.write('\r\n')
 
51
 
 
52
            sys.stdout.write(data)
 
53
            sys.stdout.flush()
 
54
 
 
55
        def start_response(status, response_headers, exc_info=None):
 
56
            if exc_info:
 
57
                try:
 
58
                    if headers_sent:
 
59
                        # Re-raise original exception if headers sent
 
60
                        raise exc_info[0], exc_info[1], exc_info[2]
 
61
                finally:
 
62
                    exc_info = None     # avoid dangling circular ref
 
63
            elif headers_set:
 
64
                raise AssertionError("Headers already set!")
 
65
 
 
66
            headers_set[:] = [status, response_headers]
 
67
            return write
 
68
 
 
69
        result = self.application(environ, start_response)
 
70
        try:
 
71
            for data in result:
 
72
                if data:    # don't send headers until body appears
 
73
                    write(data)
 
74
            if not headers_sent:
 
75
                write('')   # send headers now if body was empty
 
76
        finally:
 
77
            if hasattr(result, 'close'):
 
78
                result.close()