hgweb: centralize req.write() calls
authorDirkjan Ochtman <dirkjan@ochtman.nl>
Mon, 28 Jan 2008 15:10:17 +0100
changeset 5964 1cd1582ef25f
parent 5963 5be210afe1b8
child 5965 abe373e16fe6
hgweb: centralize req.write() calls
mercurial/hgweb/hgweb_mod.py
mercurial/hgweb/webcommands.py
--- a/mercurial/hgweb/hgweb_mod.py	Mon Jan 28 14:58:03 2008 +0100
+++ b/mercurial/hgweb/hgweb_mod.py	Mon Jan 28 15:10:17 2008 +0100
@@ -214,18 +214,18 @@
                 if cmd not in webcommands.__all__:
                     raise ErrorResponse(400, 'No such method: ' + cmd)
                 elif cmd == 'file' and 'raw' in req.form.get('style', []):
-                    webcommands.rawfile(self, req, tmpl)
+                    content = webcommands.rawfile(self, req, tmpl)
                 else:
-                    getattr(webcommands, cmd)(self, req, tmpl)
+                    content = getattr(webcommands, cmd)(self, req, tmpl)
 
+                req.write(content)
                 del tmpl
 
         except revlog.LookupError, err:
             req.respond(404, tmpl(
                         'error', error='revision not found: %s' % err.name))
         except (hg.RepoError, revlog.RevlogError), inst:
-            req.respond('500 Internal Server Error',
-                        tmpl('error', error=str(inst)))
+            req.respond(500, tmpl('error', error=str(inst)))
         except ErrorResponse, inst:
             req.respond(inst.code, tmpl('error', error=inst.message))
 
--- a/mercurial/hgweb/webcommands.py	Mon Jan 28 14:58:03 2008 +0100
+++ b/mercurial/hgweb/webcommands.py	Mon Jan 28 15:10:17 2008 +0100
@@ -20,21 +20,19 @@
 
 def log(web, req, tmpl):
     if 'file' in req.form and req.form['file'][0]:
-        filelog(web, req, tmpl)
+        return filelog(web, req, tmpl)
     else:
-        changelog(web, req, tmpl)
+        return changelog(web, req, tmpl)
 
 def rawfile(web, req, tmpl):
     path = web.cleanpath(req.form.get('file', [''])[0])
     if not path:
-        req.write(web.manifest(tmpl, web.changectx(req), path))
-        return
+        return web.manifest(tmpl, web.changectx(req), path)
 
     try:
         fctx = web.filectx(req)
     except revlog.LookupError:
-        req.write(web.manifest(tmpl, web.changectx(req), path))
-        return
+        return web.manifest(tmpl, web.changectx(req), path)
 
     path = fctx.path()
     text = fctx.data()
@@ -43,18 +41,17 @@
         mt = mt or 'application/octet-stream'
 
     req.httphdr(mt, path, len(text))
-    req.write(text)
+    return [text]
 
 def file(web, req, tmpl):
     path = web.cleanpath(req.form.get('file', [''])[0])
     if path:
         try:
-            req.write(web.filerevision(tmpl, web.filectx(req)))
-            return
+            return web.filerevision(tmpl, web.filectx(req))
         except revlog.LookupError:
             pass
 
-    req.write(web.manifest(tmpl, web.changectx(req), path))
+    return web.manifest(tmpl, web.changectx(req), path)
 
 def changelog(web, req, tmpl, shortlog = False):
     if 'node' in req.form:
@@ -67,39 +64,38 @@
         try:
             ctx = web.repo.changectx(hi)
         except hg.RepoError:
-            req.write(web.search(tmpl, hi)) # XXX redirect to 404 page?
-            return
+            return web.search(tmpl, hi) # XXX redirect to 404 page?
 
-    req.write(web.changelog(tmpl, ctx, shortlog = shortlog))
+    return web.changelog(tmpl, ctx, shortlog = shortlog)
 
 def shortlog(web, req, tmpl):
-    changelog(web, req, tmpl, shortlog = True)
+    return changelog(web, req, tmpl, shortlog = True)
 
 def changeset(web, req, tmpl):
-    req.write(web.changeset(tmpl, web.changectx(req)))
+    return web.changeset(tmpl, web.changectx(req))
 
 rev = changeset
 
 def manifest(web, req, tmpl):
-    req.write(web.manifest(tmpl, web.changectx(req),
-                           web.cleanpath(req.form['path'][0])))
+    return web.manifest(tmpl, web.changectx(req),
+                        web.cleanpath(req.form['path'][0]))
 
 def tags(web, req, tmpl):
-    req.write(web.tags(tmpl))
+    return web.tags(tmpl)
 
 def summary(web, req, tmpl):
-    req.write(web.summary(tmpl))
+    return web.summary(tmpl)
 
 def filediff(web, req, tmpl):
-    req.write(web.filediff(tmpl, web.filectx(req)))
+    return web.filediff(tmpl, web.filectx(req))
 
 diff = filediff
 
 def annotate(web, req, tmpl):
-    req.write(web.fileannotate(tmpl, web.filectx(req)))
+    return web.fileannotate(tmpl, web.filectx(req))
 
 def filelog(web, req, tmpl):
-    req.write(web.filelog(tmpl, web.filectx(req)))
+    return web.filelog(tmpl, web.filectx(req))
 
 def archive(web, req, tmpl):
     type_ = req.form['type'][0]
@@ -107,7 +103,7 @@
     if (type_ in web.archives and (type_ in allowed or
         web.configbool("web", "allow" + type_, False))):
         web.archive(tmpl, req, req.form['node'][0], type_)
-        return
+        return []
 
     raise ErrorResponse(400, 'Unsupported archive type: %s' % type_)
 
@@ -118,4 +114,4 @@
     static = web.config("web", "static",
                         os.path.join(web.templatepath, "static"),
                         untrusted=False)
-    req.write(staticfile(static, fname, req))
+    return [staticfile(static, fname, req)]