[Inteproxy-commits] r323 - in branches/streaming: . inteproxy

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Fri Dec 23 17:43:28 CET 2011


Author: teichmann
Date: 2011-12-23 17:43:28 +0100 (Fri, 23 Dec 2011)
New Revision: 323

Added:
   branches/streaming/inteproxy/chunkedwriter.py
Modified:
   branches/streaming/ChangeLog
Log:
Added writer for chunked transfer encoding.

Modified: branches/streaming/ChangeLog
===================================================================
--- branches/streaming/ChangeLog	2011-12-23 09:50:40 UTC (rev 322)
+++ branches/streaming/ChangeLog	2011-12-23 16:43:28 UTC (rev 323)
@@ -1,5 +1,12 @@
 2011-12-23	Sascha L. Teichmann	<sascha.teichmann at intevation.de>
 
+	* inteproxy/chunkedwriter.py(ChunkedTransferEncodingWriter): New.
+	  Added class to write HTTP chunked transfer encoding. Useful
+	  if the input is given as short byte arrays to be aggregated
+	  into chunks of given a size which are streamed out.
+
+2011-12-23	Sascha L. Teichmann	<sascha.teichmann at intevation.de>
+
 	* inteproxy/transcoder.py: Refactored a bit. Introduced
 	  function url_rewriter which returns a function which 
 	  can be used to do url rewriting for a given string.

Added: branches/streaming/inteproxy/chunkedwriter.py
===================================================================
--- branches/streaming/inteproxy/chunkedwriter.py	2011-12-23 09:50:40 UTC (rev 322)
+++ branches/streaming/inteproxy/chunkedwriter.py	2011-12-23 16:43:28 UTC (rev 323)
@@ -0,0 +1,59 @@
+# Copyright (C) 2011 by Intevation GmbH
+# Authors:
+# Sascha L. Teichmann <sascha.teichmann at intevation.de>
+#
+# This program is free software under the GPL (>=v2)
+# Read the file COPYING coming with the software for details
+
+"""TODO: Document me!"""
+
+class ChunkedTransferEncodingWriter(object):
+    """TODO: Document me!"""
+
+    def __init__(self, write, block_size=4096):
+        """TODO: Document me!"""
+
+        self.write = write
+        self.block_size = block_size
+        self.blocks = []
+        self.size = 0
+
+    def append(self, block):
+        """TODO: Document me!"""
+
+        write = self.write
+        block_size = self.block_size
+        blocks = self.blocks
+
+        while True:
+            size = len(block)
+            if size == 0:
+                break
+            new_size = self.size + size
+            if new_size < block_size:
+                blocks.append(block)
+                self.size = new_size
+                break
+            tail_size = new_size - block_size
+            head_size = size - tail_size
+            blocks.append(block[0:head_size])
+            data = ''.join(blocks)
+            del blocks[:]
+            self.size = 0
+            write("%x\r\n" % block_size)
+            write(data)
+            data = None
+            write("\r\n")
+            block = block[head_size:]
+
+    def finish(self):
+        """TODO: Document me!"""
+
+        write = self.write
+        if self.size:
+            write("%x\r\n" % self.size)
+            write(''.join(self.blocks))
+            write("\r\n")
+            del self.blocks[:]
+            self.size = 0
+        write("0\r\n\r\n")



More information about the Inteproxy-commits mailing list