##// END OF EJS Templates
Less indirection in the WSGI web interface. This simplifies some code, and makes it more compliant with WSGI.
Dirkjan Ochtman -
r5566:d74fc8de default
parent child Browse files
Show More
@@ -22,10 +22,7 b' cgitb.enable()'
22 #os.environ["HGENCODING"] = "UTF-8"
22 #os.environ["HGENCODING"] = "UTF-8"
23
23
24 from mercurial.hgweb.hgweb_mod import hgweb
24 from mercurial.hgweb.hgweb_mod import hgweb
25 from mercurial.hgweb.request import wsgiapplication
26 import mercurial.hgweb.wsgicgi as wsgicgi
25 import mercurial.hgweb.wsgicgi as wsgicgi
27
26
28 def make_web_app():
27 application = hgweb("/path/to/repo", "repository name")
29 return hgweb("/path/to/repo", "repository name")
28 wsgicgi.launch(application)
30
31 wsgicgi.launch(wsgiapplication(make_web_app))
@@ -22,7 +22,6 b' cgitb.enable()'
22 #os.environ["HGENCODING"] = "UTF-8"
22 #os.environ["HGENCODING"] = "UTF-8"
23
23
24 from mercurial.hgweb.hgwebdir_mod import hgwebdir
24 from mercurial.hgweb.hgwebdir_mod import hgwebdir
25 from mercurial.hgweb.request import wsgiapplication
26 import mercurial.hgweb.wsgicgi as wsgicgi
25 import mercurial.hgweb.wsgicgi as wsgicgi
27
26
28 # The config file looks like this. You can have paths to individual
27 # The config file looks like this. You can have paths to individual
@@ -44,7 +43,5 b' import mercurial.hgweb.wsgicgi as wsgicg'
44 # Alternatively you can pass a list of ('virtual/path', '/real/path') tuples
43 # Alternatively you can pass a list of ('virtual/path', '/real/path') tuples
45 # or use a dictionary with entries like 'virtual/path': '/real/path'
44 # or use a dictionary with entries like 'virtual/path': '/real/path'
46
45
47 def make_web_app():
46 application = hgwebdir('hgweb.config')
48 return hgwebdir("hgweb.config")
47 wsgicgi.launch(application)
49
50 wsgicgi.launch(wsgiapplication(make_web_app))
@@ -13,6 +13,7 b' from mercurial.i18n import gettext as _'
13 from mercurial import mdiff, ui, hg, util, archival, streamclone, patch
13 from mercurial import mdiff, ui, hg, util, archival, streamclone, patch
14 from mercurial import revlog, templater
14 from mercurial import revlog, templater
15 from common import ErrorResponse, get_mtime, staticfile, style_map, paritygen
15 from common import ErrorResponse, get_mtime, staticfile, style_map, paritygen
16 from request import wsgirequest
16
17
17 def _up(p):
18 def _up(p):
18 if p[0] != "/":
19 if p[0] != "/":
@@ -671,10 +672,12 b' class hgweb(object):'
671 if not os.environ.get('GATEWAY_INTERFACE', '').startswith("CGI/1."):
672 if not os.environ.get('GATEWAY_INTERFACE', '').startswith("CGI/1."):
672 raise RuntimeError("This function is only intended to be called while running as a CGI script.")
673 raise RuntimeError("This function is only intended to be called while running as a CGI script.")
673 import mercurial.hgweb.wsgicgi as wsgicgi
674 import mercurial.hgweb.wsgicgi as wsgicgi
674 from request import wsgiapplication
675 wsgicgi.launch(self)
675 def make_web_app():
676
676 return self
677 def __call__(self, env, respond):
677 wsgicgi.launch(wsgiapplication(make_web_app))
678 req = wsgirequest(env, respond)
679 self.run_wsgi(req)
680 return req
678
681
679 def run_wsgi(self, req):
682 def run_wsgi(self, req):
680 def header(**map):
683 def header(**map):
@@ -11,6 +11,7 b' from mercurial.i18n import gettext as _'
11 from mercurial import ui, hg, util, templater
11 from mercurial import ui, hg, util, templater
12 from common import ErrorResponse, get_mtime, staticfile, style_map, paritygen
12 from common import ErrorResponse, get_mtime, staticfile, style_map, paritygen
13 from hgweb_mod import hgweb
13 from hgweb_mod import hgweb
14 from request import wsgirequest
14
15
15 # This is a stopgap
16 # This is a stopgap
16 class hgwebdir(object):
17 class hgwebdir(object):
@@ -60,10 +61,12 b' class hgwebdir(object):'
60 if not os.environ.get('GATEWAY_INTERFACE', '').startswith("CGI/1."):
61 if not os.environ.get('GATEWAY_INTERFACE', '').startswith("CGI/1."):
61 raise RuntimeError("This function is only intended to be called while running as a CGI script.")
62 raise RuntimeError("This function is only intended to be called while running as a CGI script.")
62 import mercurial.hgweb.wsgicgi as wsgicgi
63 import mercurial.hgweb.wsgicgi as wsgicgi
63 from request import wsgiapplication
64 wsgicgi.launch(self)
64 def make_web_app():
65
65 return self
66 def __call__(self, env, respond):
66 wsgicgi.launch(wsgiapplication(make_web_app))
67 req = wsgirequest(env, respond)
68 self.run_wsgi(req)
69 return req
67
70
68 def run_wsgi(self, req):
71 def run_wsgi(self, req):
69 def header(**map):
72 def header(**map):
@@ -10,15 +10,8 b' import socket, cgi, errno'
10 from mercurial.i18n import gettext as _
10 from mercurial.i18n import gettext as _
11 from common import ErrorResponse, statusmessage
11 from common import ErrorResponse, statusmessage
12
12
13 class wsgiapplication(object):
13 class wsgirequest(object):
14 def __init__(self, destmaker):
14 def __init__(self, wsgienv, start_response):
15 self.destmaker = destmaker
16
17 def __call__(self, wsgienv, start_response):
18 return _wsgirequest(self.destmaker(), wsgienv, start_response)
19
20 class _wsgirequest(object):
21 def __init__(self, destination, wsgienv, start_response):
22 version = wsgienv['wsgi.version']
15 version = wsgienv['wsgi.version']
23 if (version < (1, 0)) or (version >= (2, 0)):
16 if (version < (1, 0)) or (version >= (2, 0)):
24 raise RuntimeError("Unknown and unsupported WSGI version %d.%d"
17 raise RuntimeError("Unknown and unsupported WSGI version %d.%d"
@@ -33,7 +26,6 b' class _wsgirequest(object):'
33 self.form = cgi.parse(self.inp, self.env, keep_blank_values=1)
26 self.form = cgi.parse(self.inp, self.env, keep_blank_values=1)
34 self.start_response = start_response
27 self.start_response = start_response
35 self.headers = []
28 self.headers = []
36 destination.run_wsgi(self)
37
29
38 out = property(lambda self: self)
30 out = property(lambda self: self)
39
31
@@ -92,3 +84,9 b' class _wsgirequest(object):'
92 if length:
84 if length:
93 headers.append(('Content-length', str(length)))
85 headers.append(('Content-length', str(length)))
94 self.header(headers)
86 self.header(headers)
87
88 def wsgiapplication(app_maker):
89 application = app_maker()
90 def run_wsgi(env, respond):
91 application(env, respond)
92 return run_wsgi
@@ -10,7 +10,6 b' import os, sys, errno, urllib, BaseHTTPS'
10 from mercurial import ui, hg, util, templater
10 from mercurial import ui, hg, util, templater
11 from hgweb_mod import hgweb
11 from hgweb_mod import hgweb
12 from hgwebdir_mod import hgwebdir
12 from hgwebdir_mod import hgwebdir
13 from request import wsgiapplication
14 from mercurial.i18n import gettext as _
13 from mercurial.i18n import gettext as _
15
14
16 def _splitURI(uri):
15 def _splitURI(uri):
@@ -121,10 +120,7 b' class _hgwebhandler(object, BaseHTTPServ'
121 self.saved_headers = []
120 self.saved_headers = []
122 self.sent_headers = False
121 self.sent_headers = False
123 self.length = None
122 self.length = None
124 req = self.server.reqmaker(env, self._start_response)
123 self.server.application(env, self._start_response)
125 for data in req:
126 if data:
127 self._write(data)
128
124
129 def send_headers(self):
125 def send_headers(self):
130 if not self.saved_status:
126 if not self.saved_status:
@@ -250,7 +246,7 b' def create_server(ui, repo):'
250 raise hg.RepoError(_("There is no Mercurial repository here"
246 raise hg.RepoError(_("There is no Mercurial repository here"
251 " (.hg not found)"))
247 " (.hg not found)"))
252 return hgwebobj
248 return hgwebobj
253 self.reqmaker = wsgiapplication(make_handler)
249 self.application = make_handler()
254
250
255 addr = address
251 addr = address
256 if addr in ('', '::'):
252 if addr in ('', '::'):
@@ -11,7 +11,6 b' hg tip'
11 cat > request.py <<EOF
11 cat > request.py <<EOF
12 from mercurial import dispatch
12 from mercurial import dispatch
13 from mercurial.hgweb.hgweb_mod import hgweb
13 from mercurial.hgweb.hgweb_mod import hgweb
14 from mercurial.hgweb.request import _wsgirequest
15 from mercurial.ui import ui
14 from mercurial.ui import ui
16 from mercurial import hg
15 from mercurial import hg
17 from StringIO import StringIO
16 from StringIO import StringIO
@@ -62,7 +61,7 b' env = {'
62 'SERVER_PROTOCOL': 'HTTP/1.0'
61 'SERVER_PROTOCOL': 'HTTP/1.0'
63 }
62 }
64
63
65 _wsgirequest(hgweb('.'), env, startrsp)
64 hgweb('.')(env, startrsp)
66 print '---- ERRORS'
65 print '---- ERRORS'
67 print errors.getvalue()
66 print errors.getvalue()
68 EOF
67 EOF
General Comments 0
You need to be logged in to leave comments. Login now