# HG changeset patch # User Dirkjan Ochtman # Date 2008-02-20 09:50:10 # Node ID b023915aa1bc5980e7048c36ad74c8520d220864 # Parent 31d81d476a7f62941cc6c89f0e0a77f587019f36 hgweb: separate protocol calls from interface calls (issue996) The protocol functions are already pretty careful about not raising exceptions to the caller, and have their own error handling. We can formalize this a little bit to make it clearer (before, the exception handlers for a limited number of exceptions coming from the interface bits would blow up because some variables aren't instantiated for the protocol calls). diff --git a/mercurial/hgweb/hgweb_mod.py b/mercurial/hgweb/hgweb_mod.py --- a/mercurial/hgweb/hgweb_mod.py +++ b/mercurial/hgweb/hgweb_mod.py @@ -199,35 +199,38 @@ class hgweb(object): req.form['node'] = [fn[:-len(ext)]] req.form['type'] = [type_] - # actually process the request + # process this if it's a protocol request + + cmd = req.form.get('cmd', [''])[0] + if cmd in protocol.__all__: + method = getattr(protocol, cmd) + method(self, req) + return + + # process the web interface request try: - cmd = req.form.get('cmd', [''])[0] - if cmd in protocol.__all__: - method = getattr(protocol, cmd) - method(self, req) - else: - tmpl = self.templater(req) - ctype = tmpl('mimetype', encoding=self.encoding) - ctype = templater.stringify(ctype) - - if cmd == '': - req.form['cmd'] = [tmpl.cache['default']] - cmd = req.form['cmd'][0] + tmpl = self.templater(req) + ctype = tmpl('mimetype', encoding=self.encoding) + ctype = templater.stringify(ctype) + + if cmd == '': + req.form['cmd'] = [tmpl.cache['default']] + cmd = req.form['cmd'][0] - if cmd not in webcommands.__all__: - msg = 'No such method: %s' % cmd - raise ErrorResponse(HTTP_BAD_REQUEST, msg) - elif cmd == 'file' and 'raw' in req.form.get('style', []): - self.ctype = ctype - content = webcommands.rawfile(self, req, tmpl) - else: - content = getattr(webcommands, cmd)(self, req, tmpl) - req.respond(HTTP_OK, ctype) + if cmd not in webcommands.__all__: + msg = 'No such method: %s' % cmd + raise ErrorResponse(HTTP_BAD_REQUEST, msg) + elif cmd == 'file' and 'raw' in req.form.get('style', []): + self.ctype = ctype + content = webcommands.rawfile(self, req, tmpl) + else: + content = getattr(webcommands, cmd)(self, req, tmpl) + req.respond(HTTP_OK, ctype) - req.write(content) - del tmpl + req.write(content) + del tmpl except revlog.LookupError, err: req.respond(HTTP_NOT_FOUND, ctype)