This patch make several WSGI related alterations.
First, it changes the server to be almost a generic WSGI server.
Second, it changes request.py to have wsgiapplication and
_wsgirequest. wsgiapplication is a class that creates _wsgirequests
when called by a WSGI compliant server. It needs to know whether
or not it should create hgwebdir or hgweb requests.
Lastly, wsgicgi.py is added, and the CGI scripts are altered to
use it to launch wsgiapplications in a WSGI compliant way.
As a side effect, all the keepalive code has been removed from
request.py. This code needs to be moved so that it is exclusively
in server.py
--- a/hgweb.cgi Tue Jun 27 00:09:31 2006 -0700
+++ b/hgweb.cgi Tue Jun 27 00:09:33 2006 -0700
@@ -6,7 +6,11 @@
cgitb.enable()
# sys.path.insert(0, "/path/to/python/lib") # if not a system-wide install
-from mercurial import hgweb
+from mercurial.hgweb.hgweb_mod import hgweb
+from mercurial.hgweb.request import wsgiapplication
+import mercurial.hgweb.wsgicgi as wsgicgi
-h = hgweb.hgweb("/path/to/repo", "repository name")
-h.run()
+def make_web_app():
+ return hgweb("/path/to/repo", "repository name")
+
+wsgicgi.launch(wsgiapplication(make_web_app))
--- a/hgwebdir.cgi Tue Jun 27 00:09:31 2006 -0700
+++ b/hgwebdir.cgi Tue Jun 27 00:09:33 2006 -0700
@@ -6,7 +6,9 @@
cgitb.enable()
# sys.path.insert(0, "/path/to/python/lib") # if not a system-wide install
-from mercurial import hgweb
+from mercurial.hgweb.hgwebdir_mod import hgwebdir
+from mercurial.hgweb.request import wsgiapplication
+import mercurial.hgweb.wsgicgi as wsgicgi
# The config file looks like this. You can have paths to individual
# repos, collections of repos in a directory tree, or both.
@@ -27,5 +29,7 @@
# Alternatively you can pass a list of ('virtual/path', '/real/path') tuples
# or use a dictionary with entries like 'virtual/path': '/real/path'
-h = hgweb.hgwebdir("hgweb.config")
-h.run()
+def make_web_app():
+ return hgwebdir("hgweb.config")
+
+wsgicgi.launch(wsgiapplication(make_web_app))
--- a/mercurial/hgweb/hgweb_mod.py Tue Jun 27 00:09:31 2006 -0700
+++ b/mercurial/hgweb/hgweb_mod.py Tue Jun 27 00:09:33 2006 -0700
@@ -12,7 +12,6 @@
from mercurial.demandload import demandload
demandload(globals(), "re zlib ConfigParser cStringIO sys tempfile")
demandload(globals(), "mercurial:mdiff,ui,hg,util,archival,templater")
-demandload(globals(), "mercurial.hgweb.request:hgrequest")
demandload(globals(), "mercurial.hgweb.common:get_mtime,staticfile")
from mercurial.node import *
from mercurial.i18n import gettext as _
@@ -652,7 +651,7 @@
raise Exception("suspicious path")
return p
- def run(self, req=hgrequest()):
+ def run(self, req):
def header(**map):
yield self.t("header", **map)
@@ -725,7 +724,6 @@
method(req)
else:
req.write(self.t("error"))
- req.done()
def do_changelog(self, req):
hi = self.repo.changelog.count() - 1
--- a/mercurial/hgweb/hgwebdir_mod.py Tue Jun 27 00:09:31 2006 -0700
+++ b/mercurial/hgweb/hgwebdir_mod.py Tue Jun 27 00:09:33 2006 -0700
@@ -11,7 +11,6 @@
demandload(globals(), "ConfigParser")
demandload(globals(), "mercurial:ui,hg,util,templater")
demandload(globals(), "mercurial.hgweb.hgweb_mod:hgweb")
-demandload(globals(), "mercurial.hgweb.request:hgrequest")
demandload(globals(), "mercurial.hgweb.common:get_mtime,staticfile")
from mercurial.i18n import gettext as _
@@ -47,7 +46,7 @@
self.repos.append((name.lstrip(os.sep), repo))
self.repos.sort()
- def run(self, req=hgrequest()):
+ def run(self, req):
def header(**map):
yield tmpl("header", **map)
--- a/mercurial/hgweb/request.py Tue Jun 27 00:09:31 2006 -0700
+++ b/mercurial/hgweb/request.py Tue Jun 27 00:09:33 2006 -0700
@@ -10,40 +10,73 @@
demandload(globals(), "socket sys cgi os errno")
from mercurial.i18n import gettext as _
-class hgrequest(object):
- def __init__(self, inp=None, out=None, env=None):
- self.inp = inp or sys.stdin
- self.out = out or sys.stdout
- self.env = env or os.environ
+class wsgiapplication(object):
+ def __init__(self, destmaker):
+ self.destmaker = destmaker
+
+ def __call__(self, wsgienv, start_response):
+ return _wsgirequest(self.destmaker(), wsgienv, start_response)
+
+class _wsgioutputfile(object):
+ def __init__(self, request):
+ self.request = request
+
+ def write(self, data):
+ self.request.write(data)
+ def writelines(self, lines):
+ for line in lines:
+ self.write(line)
+ def flush(self):
+ return None
+ def close(self):
+ return None
+
+class _wsgirequest(object):
+ def __init__(self, destination, wsgienv, start_response):
+ version = wsgienv['wsgi.version']
+ if (version < (1,0)) or (version >= (2, 0)):
+ raise RuntimeError("Unknown and unsupported WSGI version %d.%d" \
+ % version)
+ self.inp = wsgienv['wsgi.input']
+ self.out = _wsgioutputfile(self)
+ self.server_write = None
+ self.err = wsgienv['wsgi.errors']
+ self.threaded = wsgienv['wsgi.multithread']
+ self.multiprocess = wsgienv['wsgi.multiprocess']
+ self.run_once = wsgienv['wsgi.run_once']
+ self.env = wsgienv
self.form = cgi.parse(self.inp, self.env, keep_blank_values=1)
- self.will_close = True
+ self.start_response = start_response
+ self.headers = []
+ destination.run(self)
+
+ def __iter__(self):
+ return iter([])
def read(self, count=-1):
return self.inp.read(count)
def write(self, *things):
+ if self.server_write is None:
+ if not self.headers:
+ raise RuntimeError("request.write called before headers sent.")
+ self.server_write = self.start_response('200 Script output follows',
+ self.headers)
+ self.start_response = None
+ self.headers = None
for thing in things:
if hasattr(thing, "__iter__"):
for part in thing:
self.write(part)
else:
try:
- self.out.write(str(thing))
+ self.server_write(str(thing))
except socket.error, inst:
if inst[0] != errno.ECONNRESET:
raise
- def done(self):
- if self.will_close:
- self.inp.close()
- self.out.close()
- else:
- self.out.flush()
-
def header(self, headers=[('Content-type','text/html')]):
- for header in headers:
- self.out.write("%s: %s\r\n" % header)
- self.out.write("\r\n")
+ self.headers.extend(headers)
def httphdr(self, type, filename=None, length=0, headers={}):
headers = headers.items()
@@ -51,12 +84,6 @@
if filename:
headers.append(('Content-disposition', 'attachment; filename=%s' %
filename))
- # we do not yet support http 1.1 chunked transfer, so we have
- # to force connection to close if content-length not known
if length:
headers.append(('Content-length', str(length)))
- self.will_close = False
- else:
- headers.append(('Connection', 'close'))
- self.will_close = True
self.header(headers)
--- a/mercurial/hgweb/server.py Tue Jun 27 00:09:31 2006 -0700
+++ b/mercurial/hgweb/server.py Tue Jun 27 00:09:33 2006 -0700
@@ -10,7 +10,7 @@
import os, sys, errno
demandload(globals(), "urllib BaseHTTPServer socket SocketServer")
demandload(globals(), "mercurial:ui,hg,util,templater")
-demandload(globals(), "hgweb_mod:hgweb hgwebdir_mod:hgwebdir request:hgrequest")
+demandload(globals(), "hgweb_mod:hgweb hgwebdir_mod:hgwebdir request:wsgiapplication")
from mercurial.i18n import gettext as _
def _splitURI(uri):
@@ -25,6 +25,17 @@
path, query = uri, ''
return urllib.unquote(path), query
+class _error_logger(object):
+ def __init__(self, handler):
+ self.handler = handler
+ def flush(self):
+ pass
+ def write(str):
+ self.writelines(str.split('\n'))
+ def writelines(seq):
+ for msg in seq:
+ self.handler.log_error("HG error: %s", msg)
+
class _hgwebhandler(object, BaseHTTPServer.BaseHTTPRequestHandler):
def __init__(self, *args, **kargs):
self.protocol_version = 'HTTP/1.1'
@@ -84,10 +95,50 @@
if hval:
env[hkey] = hval
env['SERVER_PROTOCOL'] = self.request_version
+ env['wsgi.version'] = (1, 0)
+ env['wsgi.url_scheme'] = 'http'
+ env['wsgi.input'] = self.rfile
+ env['wsgi.errors'] = _error_logger(self)
+ env['wsgi.multithread'] = isinstance(self.server,
+ SocketServer.ThreadingMixIn)
+ env['wsgi.multiprocess'] = isinstance(self.server,
+ SocketServer.ForkingMixIn)
+ env['wsgi.run_once'] = 0
- req = hgrequest(self.rfile, self.wfile, env)
- self.send_response(200, "Script output follows")
- self.close_connection = self.server.make_and_run_handler(req)
+ self.close_connection = True
+ self.saved_status = None
+ self.saved_headers = []
+ self.sent_headers = False
+ req = self.server.reqmaker(env, self._start_response)
+ for data in req:
+ if data:
+ self._write(data)
+
+ def send_headers(self):
+ if not self.saved_status:
+ raise AssertionError("Sending headers before start_response() called")
+ saved_status = self.saved_status.split(None, 1)
+ saved_status[0] = int(saved_status[0])
+ self.send_response(*saved_status)
+ for h in self.saved_headers:
+ self.send_header(*h)
+ self.end_headers()
+ self.sent_headers = True
+
+ def _start_response(self, http_status, headers, exc_info=None):
+ code, msg = http_status.split(None, 1)
+ code = int(code)
+ self.saved_status = http_status
+ self.saved_headers = headers
+ return self._write
+
+ def _write(self, data):
+ if not self.saved_status:
+ raise AssertionError("data written before start_response() called")
+ elif not self.sent_headers:
+ self.send_headers()
+ self.wfile.write(data)
+ self.wfile.flush()
def create_server(ui, repo):
use_threads = True
@@ -127,8 +178,9 @@
self.webdir_conf = webdir_conf
self.webdirmaker = hgwebdir
self.repoviewmaker = hgweb
+ self.reqmaker = wsgiapplication(self.make_handler)
- def make_and_run_handler(self, req):
+ def make_handler(self):
if self.webdir_conf:
hgwebobj = self.webdirmaker(self.webdir_conf)
elif self.repo is not None:
@@ -136,8 +188,7 @@
repo.origroot))
else:
raise hg.RepoError(_('no repo found'))
- hgwebobj.run(req)
- return req.will_close
+ return hgwebobj
class IPv6HTTPServer(MercurialHTTPServer):
address_family = getattr(socket, 'AF_INET6', None)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mercurial/hgweb/wsgicgi.py Tue Jun 27 00:09:33 2006 -0700
@@ -0,0 +1,69 @@
+# hgweb/wsgicgi.py - CGI->WSGI translator
+#
+# Copyright 2006 Eric Hopper <hopper@omnifarious.org>
+#
+# This software may be used and distributed according to the terms
+# of the GNU General Public License, incorporated herein by reference.
+#
+# This was originally copied from the public domain code at
+# http://www.python.org/dev/peps/pep-0333/#the-server-gateway-side
+
+import os, sys
+
+def launch(application):
+
+ environ = dict(os.environ.items())
+ environ['wsgi.input'] = sys.stdin
+ environ['wsgi.errors'] = sys.stderr
+ environ['wsgi.version'] = (1,0)
+ environ['wsgi.multithread'] = False
+ environ['wsgi.multiprocess'] = True
+ environ['wsgi.run_once'] = True
+
+ if environ.get('HTTPS','off') in ('on','1'):
+ environ['wsgi.url_scheme'] = 'https'
+ else:
+ environ['wsgi.url_scheme'] = 'http'
+
+ headers_set = []
+ headers_sent = []
+
+ def write(data):
+ if not headers_set:
+ raise AssertionError("write() before start_response()")
+
+ elif not headers_sent:
+ # Before the first output, send the stored headers
+ status, response_headers = headers_sent[:] = headers_set
+ sys.stdout.write('Status: %s\r\n' % status)
+ for header in response_headers:
+ sys.stdout.write('%s: %s\r\n' % header)
+ sys.stdout.write('\r\n')
+
+ sys.stdout.write(data)
+ sys.stdout.flush()
+
+ def start_response(status,response_headers,exc_info=None):
+ if exc_info:
+ try:
+ if headers_sent:
+ # Re-raise original exception if headers sent
+ raise exc_info[0], exc_info[1], exc_info[2]
+ finally:
+ exc_info = None # avoid dangling circular ref
+ elif headers_set:
+ raise AssertionError("Headers already set!")
+
+ headers_set[:] = [status,response_headers]
+ return write
+
+ result = application(environ, start_response)
+ try:
+ for data in result:
+ if data: # don't send headers until body appears
+ write(data)
+ if not headers_sent:
+ write('') # send headers now if body was empty
+ finally:
+ if hasattr(result,'close'):
+ result.close()