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