2006-06-27 11:09:33 +04:00
|
|
|
# hgweb/wsgicgi.py - CGI->WSGI translator
|
|
|
|
#
|
|
|
|
# Copyright 2006 Eric Hopper <hopper@omnifarious.org>
|
|
|
|
#
|
2009-04-26 03:08:54 +04:00
|
|
|
# This software may be used and distributed according to the terms of the
|
2010-01-20 07:20:08 +03:00
|
|
|
# GNU General Public License version 2 or any later version.
|
2006-06-27 11:09:33 +04:00
|
|
|
#
|
|
|
|
# This was originally copied from the public domain code at
|
|
|
|
# http://www.python.org/dev/peps/pep-0333/#the-server-gateway-side
|
|
|
|
|
2015-10-31 16:07:40 +03:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
from .. import (
|
2016-12-17 23:24:36 +03:00
|
|
|
encoding,
|
2015-10-31 16:07:40 +03:00
|
|
|
util,
|
|
|
|
)
|
|
|
|
|
|
|
|
from . import (
|
|
|
|
common,
|
|
|
|
)
|
2006-06-27 11:09:33 +04:00
|
|
|
|
|
|
|
def launch(application):
|
2016-10-20 17:53:36 +03:00
|
|
|
util.setbinary(util.stdin)
|
|
|
|
util.setbinary(util.stdout)
|
2006-06-27 11:09:33 +04:00
|
|
|
|
2016-12-17 23:24:36 +03:00
|
|
|
environ = dict(encoding.environ.iteritems())
|
2017-10-05 21:22:02 +03:00
|
|
|
environ.setdefault(r'PATH_INFO', '')
|
|
|
|
if environ.get(r'SERVER_SOFTWARE', r'').startswith(r'Microsoft-IIS'):
|
2012-08-16 00:38:42 +04:00
|
|
|
# IIS includes script_name in PATH_INFO
|
2017-10-05 21:22:02 +03:00
|
|
|
scriptname = environ[r'SCRIPT_NAME']
|
|
|
|
if environ[r'PATH_INFO'].startswith(scriptname):
|
|
|
|
environ[r'PATH_INFO'] = environ[r'PATH_INFO'][len(scriptname):]
|
2008-11-25 19:54:49 +03:00
|
|
|
|
2016-10-20 17:53:36 +03:00
|
|
|
stdin = util.stdin
|
2017-10-05 21:22:02 +03:00
|
|
|
if environ.get(r'HTTP_EXPECT', r'').lower() == r'100-continue':
|
2016-10-20 17:53:36 +03:00
|
|
|
stdin = common.continuereader(stdin, util.stdout.write)
|
2010-02-06 13:27:28 +03:00
|
|
|
|
2017-10-05 21:22:02 +03:00
|
|
|
environ[r'wsgi.input'] = stdin
|
|
|
|
environ[r'wsgi.errors'] = util.stderr
|
|
|
|
environ[r'wsgi.version'] = (1, 0)
|
|
|
|
environ[r'wsgi.multithread'] = False
|
|
|
|
environ[r'wsgi.multiprocess'] = True
|
|
|
|
environ[r'wsgi.run_once'] = True
|
2006-06-27 11:09:33 +04:00
|
|
|
|
2017-10-05 21:22:02 +03:00
|
|
|
if environ.get(r'HTTPS', r'off').lower() in (r'on', r'1', r'yes'):
|
|
|
|
environ[r'wsgi.url_scheme'] = r'https'
|
2006-06-27 11:09:33 +04:00
|
|
|
else:
|
2017-10-05 21:22:02 +03:00
|
|
|
environ[r'wsgi.url_scheme'] = r'http'
|
2006-06-27 11:09:33 +04:00
|
|
|
|
|
|
|
headers_set = []
|
|
|
|
headers_sent = []
|
2016-10-20 17:53:36 +03:00
|
|
|
out = util.stdout
|
2006-06-27 11:09:33 +04:00
|
|
|
|
|
|
|
def write(data):
|
|
|
|
if not headers_set:
|
2006-11-17 10:06:54 +03:00
|
|
|
raise AssertionError("write() before start_response()")
|
2006-06-27 11:09:33 +04:00
|
|
|
|
|
|
|
elif not headers_sent:
|
2006-11-17 10:06:54 +03:00
|
|
|
# Before the first output, send the stored headers
|
|
|
|
status, response_headers = headers_sent[:] = headers_set
|
|
|
|
out.write('Status: %s\r\n' % status)
|
|
|
|
for header in response_headers:
|
|
|
|
out.write('%s: %s\r\n' % header)
|
|
|
|
out.write('\r\n')
|
2006-06-27 11:09:33 +04:00
|
|
|
|
2006-07-03 07:33:19 +04:00
|
|
|
out.write(data)
|
|
|
|
out.flush()
|
2006-06-27 11:09:33 +04:00
|
|
|
|
2006-11-17 10:06:54 +03:00
|
|
|
def start_response(status, response_headers, exc_info=None):
|
2006-06-27 11:09:33 +04:00
|
|
|
if exc_info:
|
|
|
|
try:
|
|
|
|
if headers_sent:
|
|
|
|
# Re-raise original exception if headers sent
|
2008-09-08 15:07:00 +04:00
|
|
|
raise exc_info[0](exc_info[1], exc_info[2])
|
2006-06-27 11:09:33 +04:00
|
|
|
finally:
|
|
|
|
exc_info = None # avoid dangling circular ref
|
|
|
|
elif headers_set:
|
|
|
|
raise AssertionError("Headers already set!")
|
|
|
|
|
2006-11-17 10:06:54 +03:00
|
|
|
headers_set[:] = [status, response_headers]
|
2006-06-27 11:09:33 +04:00
|
|
|
return write
|
|
|
|
|
2008-08-21 12:55:24 +04:00
|
|
|
content = application(environ, start_response)
|
2010-03-22 17:16:27 +03:00
|
|
|
try:
|
|
|
|
for chunk in content:
|
|
|
|
write(chunk)
|
2013-02-05 02:25:25 +04:00
|
|
|
if not headers_sent:
|
|
|
|
write('') # send headers now if body was empty
|
2010-03-22 17:16:27 +03:00
|
|
|
finally:
|
2017-09-29 18:48:34 +03:00
|
|
|
getattr(content, 'close', lambda: None)()
|