Show More
@@ -1,67 +1,70 b'' | |||
|
1 | 1 | # hgweb/wsgicgi.py - CGI->WSGI translator |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2006 Eric Hopper <hopper@omnifarious.org> |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms |
|
6 | 6 | # of the GNU General Public License, incorporated herein by reference. |
|
7 | 7 | # |
|
8 | 8 | # This was originally copied from the public domain code at |
|
9 | 9 | # http://www.python.org/dev/peps/pep-0333/#the-server-gateway-side |
|
10 | 10 | |
|
11 | 11 | import os, sys |
|
12 | 12 | from mercurial import util |
|
13 | 13 | |
|
14 | 14 | def launch(application): |
|
15 | 15 | util.set_binary(sys.stdin) |
|
16 | 16 | util.set_binary(sys.stdout) |
|
17 | 17 | |
|
18 | 18 | environ = dict(os.environ.items()) |
|
19 | 19 | environ.setdefault('PATH_INFO', '') |
|
20 | if '.cgi' in environ['PATH_INFO']: | |
|
21 | environ['PATH_INFO'] = environ['PATH_INFO'].split('.cgi', 1)[1] | |
|
22 | ||
|
20 | 23 | environ['wsgi.input'] = sys.stdin |
|
21 | 24 | environ['wsgi.errors'] = sys.stderr |
|
22 | 25 | environ['wsgi.version'] = (1, 0) |
|
23 | 26 | environ['wsgi.multithread'] = False |
|
24 | 27 | environ['wsgi.multiprocess'] = True |
|
25 | 28 | environ['wsgi.run_once'] = True |
|
26 | 29 | |
|
27 | 30 | if environ.get('HTTPS','off').lower() in ('on','1','yes'): |
|
28 | 31 | environ['wsgi.url_scheme'] = 'https' |
|
29 | 32 | else: |
|
30 | 33 | environ['wsgi.url_scheme'] = 'http' |
|
31 | 34 | |
|
32 | 35 | headers_set = [] |
|
33 | 36 | headers_sent = [] |
|
34 | 37 | out = sys.stdout |
|
35 | 38 | |
|
36 | 39 | def write(data): |
|
37 | 40 | if not headers_set: |
|
38 | 41 | raise AssertionError("write() before start_response()") |
|
39 | 42 | |
|
40 | 43 | elif not headers_sent: |
|
41 | 44 | # Before the first output, send the stored headers |
|
42 | 45 | status, response_headers = headers_sent[:] = headers_set |
|
43 | 46 | out.write('Status: %s\r\n' % status) |
|
44 | 47 | for header in response_headers: |
|
45 | 48 | out.write('%s: %s\r\n' % header) |
|
46 | 49 | out.write('\r\n') |
|
47 | 50 | |
|
48 | 51 | out.write(data) |
|
49 | 52 | out.flush() |
|
50 | 53 | |
|
51 | 54 | def start_response(status, response_headers, exc_info=None): |
|
52 | 55 | if exc_info: |
|
53 | 56 | try: |
|
54 | 57 | if headers_sent: |
|
55 | 58 | # Re-raise original exception if headers sent |
|
56 | 59 | raise exc_info[0](exc_info[1], exc_info[2]) |
|
57 | 60 | finally: |
|
58 | 61 | exc_info = None # avoid dangling circular ref |
|
59 | 62 | elif headers_set: |
|
60 | 63 | raise AssertionError("Headers already set!") |
|
61 | 64 | |
|
62 | 65 | headers_set[:] = [status, response_headers] |
|
63 | 66 | return write |
|
64 | 67 | |
|
65 | 68 | content = application(environ, start_response) |
|
66 | 69 | for chunk in content: |
|
67 | 70 | write(chunk) |
General Comments 0
You need to be logged in to leave comments.
Login now