Show More
@@ -1,73 +1,77 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 of the |
|
5 | # This software may be used and distributed according to the terms of the | |
6 | # GNU General Public License version 2 or any later version. |
|
6 | # GNU General Public License version 2 or any later version. | |
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.iteritems()) |
|
18 | environ = dict(os.environ.iteritems()) | |
19 | environ.setdefault('PATH_INFO', '') |
|
19 | environ.setdefault('PATH_INFO', '') | |
20 | if environ.get('SERVER_SOFTWARE', '').startswith('Microsoft-IIS'): |
|
20 | if environ.get('SERVER_SOFTWARE', '').startswith('Microsoft-IIS'): | |
21 | # IIS includes script_name in path_info |
|
21 | # IIS includes script_name in path_info | |
22 | scriptname = environ['SCRIPT_NAME'] |
|
22 | scriptname = environ['SCRIPT_NAME'] | |
23 | if environ['PATH_INFO'].startswith(scriptname): |
|
23 | if environ['PATH_INFO'].startswith(scriptname): | |
24 | environ['PATH_INFO'] = environ['PATH_INFO'][len(scriptname):] |
|
24 | environ['PATH_INFO'] = environ['PATH_INFO'][len(scriptname):] | |
25 |
|
25 | |||
26 | environ['wsgi.input'] = sys.stdin |
|
26 | environ['wsgi.input'] = sys.stdin | |
27 | environ['wsgi.errors'] = sys.stderr |
|
27 | environ['wsgi.errors'] = sys.stderr | |
28 | environ['wsgi.version'] = (1, 0) |
|
28 | environ['wsgi.version'] = (1, 0) | |
29 | environ['wsgi.multithread'] = False |
|
29 | environ['wsgi.multithread'] = False | |
30 | environ['wsgi.multiprocess'] = True |
|
30 | environ['wsgi.multiprocess'] = True | |
31 | environ['wsgi.run_once'] = True |
|
31 | environ['wsgi.run_once'] = True | |
32 |
|
32 | |||
33 | if environ.get('HTTPS', 'off').lower() in ('on', '1', 'yes'): |
|
33 | if environ.get('HTTPS', 'off').lower() in ('on', '1', 'yes'): | |
34 | environ['wsgi.url_scheme'] = 'https' |
|
34 | environ['wsgi.url_scheme'] = 'https' | |
35 | else: |
|
35 | else: | |
36 | environ['wsgi.url_scheme'] = 'http' |
|
36 | environ['wsgi.url_scheme'] = 'http' | |
37 |
|
37 | |||
38 | headers_set = [] |
|
38 | headers_set = [] | |
39 | headers_sent = [] |
|
39 | headers_sent = [] | |
40 | out = sys.stdout |
|
40 | out = sys.stdout | |
41 |
|
41 | |||
42 | def write(data): |
|
42 | def write(data): | |
43 | if not headers_set: |
|
43 | if not headers_set: | |
44 | raise AssertionError("write() before start_response()") |
|
44 | raise AssertionError("write() before start_response()") | |
45 |
|
45 | |||
46 | elif not headers_sent: |
|
46 | elif not headers_sent: | |
47 | # Before the first output, send the stored headers |
|
47 | # Before the first output, send the stored headers | |
48 | status, response_headers = headers_sent[:] = headers_set |
|
48 | status, response_headers = headers_sent[:] = headers_set | |
49 | out.write('Status: %s\r\n' % status) |
|
49 | out.write('Status: %s\r\n' % status) | |
50 | for header in response_headers: |
|
50 | for header in response_headers: | |
51 | out.write('%s: %s\r\n' % header) |
|
51 | out.write('%s: %s\r\n' % header) | |
52 | out.write('\r\n') |
|
52 | out.write('\r\n') | |
53 |
|
53 | |||
54 | out.write(data) |
|
54 | out.write(data) | |
55 | out.flush() |
|
55 | out.flush() | |
56 |
|
56 | |||
57 | def start_response(status, response_headers, exc_info=None): |
|
57 | def start_response(status, response_headers, exc_info=None): | |
58 | if exc_info: |
|
58 | if exc_info: | |
59 | try: |
|
59 | try: | |
60 | if headers_sent: |
|
60 | if headers_sent: | |
61 | # Re-raise original exception if headers sent |
|
61 | # Re-raise original exception if headers sent | |
62 | raise exc_info[0](exc_info[1], exc_info[2]) |
|
62 | raise exc_info[0](exc_info[1], exc_info[2]) | |
63 | finally: |
|
63 | finally: | |
64 | exc_info = None # avoid dangling circular ref |
|
64 | exc_info = None # avoid dangling circular ref | |
65 | elif headers_set: |
|
65 | elif headers_set: | |
66 | raise AssertionError("Headers already set!") |
|
66 | raise AssertionError("Headers already set!") | |
67 |
|
67 | |||
68 | headers_set[:] = [status, response_headers] |
|
68 | headers_set[:] = [status, response_headers] | |
69 | return write |
|
69 | return write | |
70 |
|
70 | |||
71 | content = application(environ, start_response) |
|
71 | content = application(environ, start_response) | |
72 | for chunk in content: |
|
72 | try: | |
73 | write(chunk) |
|
73 | for chunk in content: | |
|
74 | write(chunk) | |||
|
75 | finally: | |||
|
76 | if hasattr(content, 'close'): | |||
|
77 | content.close() |
General Comments 0
You need to be logged in to leave comments.
Login now