1
# -*- coding: iso-8859-1 -*-
3
MoinMoin - Fallback CGI WSGI interface
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/.
8
@copyright: 2003-2006 Phillip J. Eby <pje at telecommunity.com>
9
@license: Public Domain
14
__all__ = ['WSGIServer']
17
class WSGIServer(object):
19
def __init__(self, application):
20
self.application = application
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
32
if environ.get('HTTPS', 'off') in ('on', '1'):
33
environ['wsgi.url_scheme'] = 'https'
35
environ['wsgi.url_scheme'] = 'http'
42
raise AssertionError("write() before start_response()")
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')
52
sys.stdout.write(data)
55
def start_response(status, response_headers, exc_info=None):
59
# Re-raise original exception if headers sent
60
raise exc_info[0], exc_info[1], exc_info[2]
62
exc_info = None # avoid dangling circular ref
64
raise AssertionError("Headers already set!")
66
headers_set[:] = [status, response_headers]
69
result = self.application(environ, start_response)
72
if data: # don't send headers until body appears
75
write('') # send headers now if body was empty
77
if hasattr(result, 'close'):