changeset 11626:2f8adc60e013

protocol: use generators instead of req.write() for hgweb stream responses
author Dirkjan Ochtman <dirkjan@ochtman.nl>
date Tue, 20 Jul 2010 09:56:37 +0200
parents cdeb861335d5
children 04f76a954842
files mercurial/hgweb/protocol.py
diffstat 1 files changed, 11 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/hgweb/protocol.py	Tue Jul 20 20:53:33 2010 +0200
+++ b/mercurial/hgweb/protocol.py	Tue Jul 20 09:56:37 2010 +0200
@@ -43,25 +43,6 @@
                 break
             yield z.compress(chunk)
         yield z.flush()
-    def sendresponse(self, s):
-        self.req.respond(HTTP_OK, HGTYPE, length=len(s))
-        self.response = s
-    def sendstream(self, source):
-        self.req.respond(HTTP_OK, HGTYPE)
-        for chunk in source.gen:
-            self.req.write(chunk)
-    def sendpushresponse(self, rsp):
-        val = sys.stdout.getvalue()
-        sys.stdout, sys.stderr = self.oldio
-        self.req.respond(HTTP_OK, HGTYPE)
-        self.response = '%d\n%s' % (rsp.res, val)
-
-    handlers = {
-        str: sendresponse,
-        wireproto.streamres: sendstream,
-        wireproto.pushres: sendpushresponse,
-    }
-
     def _client(self):
         return 'remote:%s:%s:%s' % (
             self.req.env.get('wsgi.url_scheme') or 'http',
@@ -74,5 +55,14 @@
 def call(repo, req, cmd):
     p = webproto(req)
     rsp = wireproto.dispatch(repo, p, cmd)
-    webproto.handlers[rsp.__class__](p, rsp)
-    return [p.response]
+    if isinstance(rsp, str):
+        req.respond(HTTP_OK, HGTYPE, length=len(rsp))
+        return [rsp]
+    elif isinstance(rsp, wireproto.streamres):
+        req.respond(HTTP_OK, HGTYPE)
+        return rsp.gen
+    elif isinstance(rsp, wireproto.pushres):
+        val = sys.stdout.getvalue()
+        sys.stdout, sys.stderr = p.oldio
+        req.respond(HTTP_OK, HGTYPE)
+        return ['%d\n%s' % (rsp.res, val)]