Less indirection in the WSGI web interface. This simplifies some code, and makes it more compliant with WSGI.
--- a/hgweb.cgi Wed Nov 28 13:58:31 2007 -0800
+++ b/hgweb.cgi Fri Nov 30 18:23:18 2007 +0100
@@ -22,10 +22,7 @@
#os.environ["HGENCODING"] = "UTF-8"
from mercurial.hgweb.hgweb_mod import hgweb
-from mercurial.hgweb.request import wsgiapplication
import mercurial.hgweb.wsgicgi as wsgicgi
-def make_web_app():
- return hgweb("/path/to/repo", "repository name")
-
-wsgicgi.launch(wsgiapplication(make_web_app))
+application = hgweb("/path/to/repo", "repository name")
+wsgicgi.launch(application)
--- a/hgwebdir.cgi Wed Nov 28 13:58:31 2007 -0800
+++ b/hgwebdir.cgi Fri Nov 30 18:23:18 2007 +0100
@@ -22,7 +22,6 @@
#os.environ["HGENCODING"] = "UTF-8"
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
@@ -44,7 +43,5 @@
# Alternatively you can pass a list of ('virtual/path', '/real/path') tuples
# or use a dictionary with entries like 'virtual/path': '/real/path'
-def make_web_app():
- return hgwebdir("hgweb.config")
-
-wsgicgi.launch(wsgiapplication(make_web_app))
+application = hgwebdir('hgweb.config')
+wsgicgi.launch(application)
--- a/mercurial/hgweb/hgweb_mod.py Wed Nov 28 13:58:31 2007 -0800
+++ b/mercurial/hgweb/hgweb_mod.py Fri Nov 30 18:23:18 2007 +0100
@@ -13,6 +13,7 @@
from mercurial import mdiff, ui, hg, util, archival, streamclone, patch
from mercurial import revlog, templater
from common import ErrorResponse, get_mtime, staticfile, style_map, paritygen
+from request import wsgirequest
def _up(p):
if p[0] != "/":
@@ -671,10 +672,12 @@
if not os.environ.get('GATEWAY_INTERFACE', '').startswith("CGI/1."):
raise RuntimeError("This function is only intended to be called while running as a CGI script.")
import mercurial.hgweb.wsgicgi as wsgicgi
- from request import wsgiapplication
- def make_web_app():
- return self
- wsgicgi.launch(wsgiapplication(make_web_app))
+ wsgicgi.launch(self)
+
+ def __call__(self, env, respond):
+ req = wsgirequest(env, respond)
+ self.run_wsgi(req)
+ return req
def run_wsgi(self, req):
def header(**map):
--- a/mercurial/hgweb/hgwebdir_mod.py Wed Nov 28 13:58:31 2007 -0800
+++ b/mercurial/hgweb/hgwebdir_mod.py Fri Nov 30 18:23:18 2007 +0100
@@ -11,6 +11,7 @@
from mercurial import ui, hg, util, templater
from common import ErrorResponse, get_mtime, staticfile, style_map, paritygen
from hgweb_mod import hgweb
+from request import wsgirequest
# This is a stopgap
class hgwebdir(object):
@@ -60,10 +61,12 @@
if not os.environ.get('GATEWAY_INTERFACE', '').startswith("CGI/1."):
raise RuntimeError("This function is only intended to be called while running as a CGI script.")
import mercurial.hgweb.wsgicgi as wsgicgi
- from request import wsgiapplication
- def make_web_app():
- return self
- wsgicgi.launch(wsgiapplication(make_web_app))
+ wsgicgi.launch(self)
+
+ def __call__(self, env, respond):
+ req = wsgirequest(env, respond)
+ self.run_wsgi(req)
+ return req
def run_wsgi(self, req):
def header(**map):
--- a/mercurial/hgweb/request.py Wed Nov 28 13:58:31 2007 -0800
+++ b/mercurial/hgweb/request.py Fri Nov 30 18:23:18 2007 +0100
@@ -10,15 +10,8 @@
from mercurial.i18n import gettext as _
from common import ErrorResponse, statusmessage
-class wsgiapplication(object):
- def __init__(self, destmaker):
- self.destmaker = destmaker
-
- def __call__(self, wsgienv, start_response):
- return _wsgirequest(self.destmaker(), wsgienv, start_response)
-
-class _wsgirequest(object):
- def __init__(self, destination, wsgienv, start_response):
+class wsgirequest(object):
+ def __init__(self, wsgienv, start_response):
version = wsgienv['wsgi.version']
if (version < (1, 0)) or (version >= (2, 0)):
raise RuntimeError("Unknown and unsupported WSGI version %d.%d"
@@ -33,7 +26,6 @@
self.form = cgi.parse(self.inp, self.env, keep_blank_values=1)
self.start_response = start_response
self.headers = []
- destination.run_wsgi(self)
out = property(lambda self: self)
@@ -92,3 +84,9 @@
if length:
headers.append(('Content-length', str(length)))
self.header(headers)
+
+def wsgiapplication(app_maker):
+ application = app_maker()
+ def run_wsgi(env, respond):
+ application(env, respond)
+ return run_wsgi
--- a/mercurial/hgweb/server.py Wed Nov 28 13:58:31 2007 -0800
+++ b/mercurial/hgweb/server.py Fri Nov 30 18:23:18 2007 +0100
@@ -10,7 +10,6 @@
from mercurial import ui, hg, util, templater
from hgweb_mod import hgweb
from hgwebdir_mod import hgwebdir
-from request import wsgiapplication
from mercurial.i18n import gettext as _
def _splitURI(uri):
@@ -121,10 +120,7 @@
self.saved_headers = []
self.sent_headers = False
self.length = None
- req = self.server.reqmaker(env, self._start_response)
- for data in req:
- if data:
- self._write(data)
+ self.server.application(env, self._start_response)
def send_headers(self):
if not self.saved_status:
@@ -250,7 +246,7 @@
raise hg.RepoError(_("There is no Mercurial repository here"
" (.hg not found)"))
return hgwebobj
- self.reqmaker = wsgiapplication(make_handler)
+ self.application = make_handler()
addr = address
if addr in ('', '::'):
--- a/tests/test-non-interactive-wsgi Wed Nov 28 13:58:31 2007 -0800
+++ b/tests/test-non-interactive-wsgi Fri Nov 30 18:23:18 2007 +0100
@@ -11,7 +11,6 @@
cat > request.py <<EOF
from mercurial import dispatch
from mercurial.hgweb.hgweb_mod import hgweb
-from mercurial.hgweb.request import _wsgirequest
from mercurial.ui import ui
from mercurial import hg
from StringIO import StringIO
@@ -62,7 +61,7 @@
'SERVER_PROTOCOL': 'HTTP/1.0'
}
-_wsgirequest(hgweb('.'), env, startrsp)
+hgweb('.')(env, startrsp)
print '---- ERRORS'
print errors.getvalue()
EOF