changeset 11623:31d0a6d50ee2

protocol: extract compression from streaming mechanics
author Dirkjan Ochtman <dirkjan@ochtman.nl>
date Fri, 16 Jul 2010 22:20:10 +0200
parents 1d48681b17a4
children 67260651d09d
files mercurial/hgweb/protocol.py mercurial/sshserver.py mercurial/wireproto.py
diffstat 3 files changed, 15 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- 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)