1 # hgweb/wsgicgi.py - CGI->WSGI translator
3 # Copyright 2006 Eric Hopper <hopper@omnifarious.org>
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2, incorporated herein by reference.
8 # This was originally copied from the public domain code at
9 # http://www.python.org/dev/peps/pep-0333/#the-server-gateway-side
12 from mercurial import util
14 def launch(application):
15 util.set_binary(sys.stdin)
16 util.set_binary(sys.stdout)
18 environ = dict(os.environ.iteritems())
19 environ.setdefault('PATH_INFO', '')
20 if '.cgi' in environ['PATH_INFO']:
21 environ['PATH_INFO'] = environ['PATH_INFO'].split('.cgi', 1)[1]
23 environ['wsgi.input'] = sys.stdin
24 environ['wsgi.errors'] = sys.stderr
25 environ['wsgi.version'] = (1, 0)
26 environ['wsgi.multithread'] = False
27 environ['wsgi.multiprocess'] = True
28 environ['wsgi.run_once'] = True
30 if environ.get('HTTPS','off').lower() in ('on','1','yes'):
31 environ['wsgi.url_scheme'] = 'https'
33 environ['wsgi.url_scheme'] = 'http'
41 raise AssertionError("write() before start_response()")
43 elif not headers_sent:
44 # Before the first output, send the stored headers
45 status, response_headers = headers_sent[:] = headers_set
46 out.write('Status: %s\r\n' % status)
47 for header in response_headers:
48 out.write('%s: %s\r\n' % header)
54 def start_response(status, response_headers, exc_info=None):
58 # Re-raise original exception if headers sent
59 raise exc_info[0](exc_info[1], exc_info[2])
61 exc_info = None # avoid dangling circular ref
63 raise AssertionError("Headers already set!")
65 headers_set[:] = [status, response_headers]
68 content = application(environ, start_response)