##// END OF EJS Templates
Switch CGI stdout to binary on windows...
Alexis S. L. Carvalho -
r4076:5a89c61c default
parent child Browse files
Show More
@@ -1,70 +1,72 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 from mercurial import util
12 13
13 14 def launch(application):
15 util.set_binary(sys.stdout)
14 16
15 17 environ = dict(os.environ.items())
16 18 environ['wsgi.input'] = sys.stdin
17 19 environ['wsgi.errors'] = sys.stderr
18 20 environ['wsgi.version'] = (1, 0)
19 21 environ['wsgi.multithread'] = False
20 22 environ['wsgi.multiprocess'] = True
21 23 environ['wsgi.run_once'] = True
22 24
23 25 if environ.get('HTTPS','off') in ('on','1'):
24 26 environ['wsgi.url_scheme'] = 'https'
25 27 else:
26 28 environ['wsgi.url_scheme'] = 'http'
27 29
28 30 headers_set = []
29 31 headers_sent = []
30 32 out = sys.stdout
31 33
32 34 def write(data):
33 35 if not headers_set:
34 36 raise AssertionError("write() before start_response()")
35 37
36 38 elif not headers_sent:
37 39 # Before the first output, send the stored headers
38 40 status, response_headers = headers_sent[:] = headers_set
39 41 out.write('Status: %s\r\n' % status)
40 42 for header in response_headers:
41 43 out.write('%s: %s\r\n' % header)
42 44 out.write('\r\n')
43 45
44 46 out.write(data)
45 47 out.flush()
46 48
47 49 def start_response(status, response_headers, exc_info=None):
48 50 if exc_info:
49 51 try:
50 52 if headers_sent:
51 53 # Re-raise original exception if headers sent
52 54 raise exc_info[0], exc_info[1], exc_info[2]
53 55 finally:
54 56 exc_info = None # avoid dangling circular ref
55 57 elif headers_set:
56 58 raise AssertionError("Headers already set!")
57 59
58 60 headers_set[:] = [status, response_headers]
59 61 return write
60 62
61 63 result = application(environ, start_response)
62 64 try:
63 65 for data in result:
64 66 if data: # don't send headers until body appears
65 67 write(data)
66 68 if not headers_sent:
67 69 write('') # send headers now if body was empty
68 70 finally:
69 71 if hasattr(result,'close'):
70 72 result.close()
General Comments 0
You need to be logged in to leave comments. Login now