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