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
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from .. import (
|
|
|
|
util,
|
|
|
|
)
|
|
|
|
|
|
|
|
from . import (
|
|
|
|
common,
|
|
|
|
)
|
2006-06-27 11:09:33 +04:00
|
|
|
|
|
|
|
def launch(application):
|
2011-05-06 17:25:35 +04:00
|
|
|
util.setbinary(sys.stdin)
|
|
|
|
util.setbinary(sys.stdout)
|
2006-06-27 11:09:33 +04:00
|
|
|
|
2009-01-12 11:16:03 +03:00
|
|
|
environ = dict(os.environ.iteritems())
|
2007-12-02 12:12:26 +03:00
|
|
|
environ.setdefault('PATH_INFO', '')
|
2010-01-03 19:26:39 +03:00
|
|
|
if environ.get('SERVER_SOFTWARE', '').startswith('Microsoft-IIS'):
|
2012-08-16 00:38:42 +04:00
|
|
|
# IIS includes script_name in PATH_INFO
|
2010-01-03 19:26:39 +03:00
|
|
|
scriptname = environ['SCRIPT_NAME']
|
|
|
|
if environ['PATH_INFO'].startswith(scriptname):
|
|
|
|
environ['PATH_INFO'] = environ['PATH_INFO'][len(scriptname):]
|
2008-11-25 19:54:49 +03:00
|
|
|
|
2010-02-06 13:27:28 +03:00
|
|
|
stdin = sys.stdin
|
|
|
|
if environ.get('HTTP_EXPECT', '').lower() == '100-continue':
|
|
|
|
stdin = common.continuereader(stdin, sys.stdout.write)
|
|
|
|
|
|
|
|
environ['wsgi.input'] = stdin
|
2006-11-17 10:06:54 +03:00
|
|
|
environ['wsgi.errors'] = sys.stderr
|
|
|
|
environ['wsgi.version'] = (1, 0)
|
|
|
|
environ['wsgi.multithread'] = False
|
2006-06-27 11:09:33 +04:00
|
|
|
environ['wsgi.multiprocess'] = True
|
2006-11-17 10:06:54 +03:00
|
|
|
environ['wsgi.run_once'] = True
|
2006-06-27 11:09:33 +04:00
|
|
|
|
2010-02-05 20:50:08 +03:00
|
|
|
if environ.get('HTTPS', 'off').lower() in ('on', '1', 'yes'):
|
2006-06-27 11:09:33 +04:00
|
|
|
environ['wsgi.url_scheme'] = 'https'
|
|
|
|
else:
|
|
|
|
environ['wsgi.url_scheme'] = 'http'
|
|
|
|
|
|
|
|
headers_set = []
|
|
|
|
headers_sent = []
|
2006-07-03 07:33:19 +04:00
|
|
|
out = sys.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:
|
2011-07-26 00:38:10 +04:00
|
|
|
getattr(content, 'close', lambda : None)()
|