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