protocol: extract compression from streaming mechanics
authorDirkjan Ochtman <dirkjan@ochtman.nl>
Fri, 16 Jul 2010 22:20:10 +0200
changeset 11623 31d0a6d50ee2
parent 11622 1d48681b17a4
child 11624 67260651d09d
protocol: extract compression from streaming mechanics
mercurial/hgweb/protocol.py
mercurial/sshserver.py
mercurial/wireproto.py
--- a/mercurial/hgweb/protocol.py	Fri Jul 16 18:18:35 2010 +0200
+++ b/mercurial/hgweb/protocol.py	Fri Jul 16 22:20:10 2010 +0200
@@ -35,22 +35,21 @@
     def redirect(self):
         self.oldio = sys.stdout, sys.stderr
         sys.stderr = sys.stdout = cStringIO.StringIO()
-    def sendresponse(self, s):
-        self.req.respond(HTTP_OK, HGTYPE, length=len(s))
-        self.response = s
-    def sendchangegroup(self, cg):
-        self.req.respond(HTTP_OK, HGTYPE)
+    def groupchunks(self, cg):
         z = zlib.compressobj()
         while 1:
             chunk = cg.read(4096)
             if not chunk:
                 break
-            self.req.write(z.compress(chunk))
-        self.req.write(z.flush())
+            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:
-            self.req.write(chunk)
+            self.req.write(str(chunk))
     def sendpushresponse(self, ret):
         val = sys.stdout.getvalue()
         sys.stdout, sys.stderr = self.oldio
--- a/mercurial/sshserver.py	Fri Jul 16 18:18:35 2010 +0200
+++ b/mercurial/sshserver.py	Fri Jul 16 22:20:10 2010 +0200
@@ -59,18 +59,16 @@
     def redirect(self):
         pass
 
-    def sendresponse(self, v):
-        self.fout.write("%d\n" % len(v))
-        self.fout.write(v)
-        self.fout.flush()
-
-    def sendchangegroup(self, changegroup):
+    def groupchunks(self, changegroup):
         while True:
             d = changegroup.read(4096)
             if not d:
                 break
-            self.fout.write(d)
+            yield d
 
+    def sendresponse(self, v):
+        self.fout.write("%d\n" % len(v))
+        self.fout.write(v)
         self.fout.flush()
 
     def sendstream(self, source):
--- a/mercurial/wireproto.py	Fri Jul 16 18:18:35 2010 +0200
+++ b/mercurial/wireproto.py	Fri Jul 16 22:20:10 2010 +0200
@@ -173,13 +173,13 @@
 def changegroup(repo, proto, roots):
     nodes = decodelist(roots)
     cg = repo.changegroup(nodes, 'serve')
-    proto.sendchangegroup(cg)
+    proto.sendstream(proto.groupchunks(cg))
 
 def changegroupsubset(repo, proto, bases, heads):
     bases = decodelist(bases)
     heads = decodelist(heads)
     cg = repo.changegroupsubset(bases, heads, 'serve')
-    proto.sendchangegroup(cg)
+    proto.sendstream(proto.groupchunks(cg))
 
 def heads(repo, proto):
     h = repo.heads()
@@ -215,10 +215,7 @@
     return '%s\n' % int(r)
 
 def stream(repo, proto):
-    try:
-        proto.sendstream(streamclone.stream_out(repo))
-    except streamclone.StreamException, inst:
-        return str(inst)
+    proto.sendstream(streamclone.stream_out(repo))
 
 def unbundle(repo, proto, heads):
     their_heads = decodelist(heads)