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

« back to all changes in this revision

Viewing changes to MoinMoin/support/flup/server/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
# Taken from <http://www.python.org/dev/peps/pep-0333/>
 
2
# which was placed in the public domain.
 
3
 
 
4
import os, sys
 
5
 
 
6
 
 
7
__all__ = ['WSGIServer']
 
8
 
 
9
 
 
10
class WSGIServer(object):
 
11
 
 
12
    def __init__(self, application):
 
13
        self.application = application
 
14
 
 
15
    def run(self):
 
16
 
 
17
        environ = dict(os.environ.items())
 
18
        environ['wsgi.input']        = sys.stdin
 
19
        environ['wsgi.errors']       = sys.stderr
 
20
        environ['wsgi.version']      = (1,0)
 
21
        environ['wsgi.multithread']  = False
 
22
        environ['wsgi.multiprocess'] = True
 
23
        environ['wsgi.run_once']     = True
 
24
 
 
25
        if environ.get('HTTPS','off') in ('on','1'):
 
26
            environ['wsgi.url_scheme'] = 'https'
 
27
        else:
 
28
            environ['wsgi.url_scheme'] = 'http'
 
29
 
 
30
        headers_set = []
 
31
        headers_sent = []
 
32
 
 
33
        def write(data):
 
34
            if not headers_set:
 
35
                 raise AssertionError("write() before start_response()")
 
36
 
 
37
            elif not headers_sent:
 
38
                 # Before the first output, send the stored headers
 
39
                 status, response_headers = headers_sent[:] = headers_set
 
40
                 sys.stdout.write('Status: %s\r\n' % status)
 
41
                 for header in response_headers:
 
42
                     sys.stdout.write('%s: %s\r\n' % header)
 
43
                 sys.stdout.write('\r\n')
 
44
 
 
45
            sys.stdout.write(data)
 
46
            sys.stdout.flush()
 
47
 
 
48
        def start_response(status,response_headers,exc_info=None):
 
49
            if exc_info:
 
50
                try:
 
51
                    if headers_sent:
 
52
                        # Re-raise original exception if headers sent
 
53
                        raise exc_info[0], exc_info[1], exc_info[2]
 
54
                finally:
 
55
                    exc_info = None     # avoid dangling circular ref
 
56
            elif headers_set:
 
57
                raise AssertionError("Headers already set!")
 
58
 
 
59
            headers_set[:] = [status,response_headers]
 
60
            return write
 
61
 
 
62
        result = self.application(environ, start_response)
 
63
        try:
 
64
            for data in result:
 
65
                if data:    # don't send headers until body appears
 
66
                    write(data)
 
67
            if not headers_sent:
 
68
                write('')   # send headers now if body was empty
 
69
        finally:
 
70
            if hasattr(result,'close'):
 
71
                result.close()