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