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