Show More
@@ -1,94 +1,94 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 of the |
|
6 | 6 | # GNU General Public License version 2 or any later version. |
|
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 | from __future__ import absolute_import |
|
12 | 12 | |
|
13 | 13 | import os |
|
14 | 14 | |
|
15 | 15 | from ..pycompat import getattr |
|
16 | 16 | from .. import pycompat |
|
17 | 17 | |
|
18 | 18 | from ..utils import procutil |
|
19 | 19 | |
|
20 | 20 | from . import common |
|
21 | 21 | |
|
22 | 22 | |
|
23 | 23 | def launch(application): |
|
24 | 24 | procutil.setbinary(procutil.stdin) |
|
25 | 25 | procutil.setbinary(procutil.stdout) |
|
26 | 26 | |
|
27 | 27 | environ = dict(pycompat.iteritems(os.environ)) # re-exports |
|
28 | 28 | environ.setdefault('PATH_INFO', b'') |
|
29 | 29 | if environ.get('SERVER_SOFTWARE', '').startswith('Microsoft-IIS'): |
|
30 | 30 | # IIS includes script_name in PATH_INFO |
|
31 | 31 | scriptname = environ['SCRIPT_NAME'] |
|
32 | 32 | if environ['PATH_INFO'].startswith(scriptname): |
|
33 | 33 | environ['PATH_INFO'] = environ['PATH_INFO'][len(scriptname) :] |
|
34 | 34 | |
|
35 | 35 | stdin = procutil.stdin |
|
36 | 36 | if environ.get('HTTP_EXPECT', '').lower() == '100-continue': |
|
37 | 37 | stdin = common.continuereader(stdin, procutil.stdout.write) |
|
38 | 38 | |
|
39 | 39 | environ['wsgi.input'] = stdin |
|
40 | 40 | environ['wsgi.errors'] = procutil.stderr |
|
41 | 41 | environ['wsgi.version'] = (1, 0) |
|
42 | 42 | environ['wsgi.multithread'] = False |
|
43 | 43 | environ['wsgi.multiprocess'] = True |
|
44 | 44 | environ['wsgi.run_once'] = True |
|
45 | 45 | |
|
46 | 46 | if environ.get('HTTPS', 'off').lower() in ('on', '1', 'yes'): |
|
47 | 47 | environ['wsgi.url_scheme'] = 'https' |
|
48 | 48 | else: |
|
49 | 49 | environ['wsgi.url_scheme'] = 'http' |
|
50 | 50 | |
|
51 | 51 | headers_set = [] |
|
52 | 52 | headers_sent = [] |
|
53 | 53 | out = procutil.stdout |
|
54 | 54 | |
|
55 | 55 | def write(data): |
|
56 | 56 | if not headers_set: |
|
57 | 57 | raise AssertionError(b"write() before start_response()") |
|
58 | 58 | |
|
59 | 59 | elif not headers_sent: |
|
60 | 60 | # Before the first output, send the stored headers |
|
61 | 61 | status, response_headers = headers_sent[:] = headers_set |
|
62 | 62 | out.write(b'Status: %s\r\n' % pycompat.bytesurl(status)) |
|
63 | 63 | for hk, hv in response_headers: |
|
64 | 64 | out.write( |
|
65 | 65 | b'%s: %s\r\n' |
|
66 | 66 | % (pycompat.bytesurl(hk), pycompat.bytesurl(hv)) |
|
67 | 67 | ) |
|
68 | 68 | out.write(b'\r\n') |
|
69 | 69 | |
|
70 | 70 | out.write(data) |
|
71 | 71 | out.flush() |
|
72 | 72 | |
|
73 | 73 | def start_response(status, response_headers, exc_info=None): |
|
74 | 74 | if exc_info: |
|
75 | 75 | try: |
|
76 | 76 | if headers_sent: |
|
77 | 77 | # Re-raise original exception if headers sent |
|
78 | 78 | raise exc_info[0](exc_info[1], exc_info[2]) |
|
79 | 79 | finally: |
|
80 |
exc_info |
|
|
80 | del exc_info # avoid dangling circular ref | |
|
81 | 81 | elif headers_set: |
|
82 | 82 | raise AssertionError(b"Headers already set!") |
|
83 | 83 | |
|
84 | 84 | headers_set[:] = [status, response_headers] |
|
85 | 85 | return write |
|
86 | 86 | |
|
87 | 87 | content = application(environ, start_response) |
|
88 | 88 | try: |
|
89 | 89 | for chunk in content: |
|
90 | 90 | write(chunk) |
|
91 | 91 | if not headers_sent: |
|
92 | 92 | write(b'') # send headers now if body was empty |
|
93 | 93 | finally: |
|
94 | 94 | getattr(content, 'close', lambda: None)() |
General Comments 0
You need to be logged in to leave comments.
Login now