From ac74d5079d56a3aec3144df70b0209237ab6d805 Mon Sep 17 00:00:00 2001 From: benoitc Date: Thu, 25 Feb 2010 02:59:44 +0100 Subject: [PATCH] fix chunked decoding in TeeInput. We need to make sure we didn't have already in the buffer the last chunk. it fixes socket timeout. Discovered when hacing on it with restkit. --- gunicorn/http/tee.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/gunicorn/http/tee.py b/gunicorn/http/tee.py index 904fa0f5..14dcffda 100644 --- a/gunicorn/http/tee.py +++ b/gunicorn/http/tee.py @@ -48,6 +48,15 @@ class TeeInput(object): self.tmp.seek(pos) self._len = self._tmp_size() return self._len + + def seek(self, offset, whence=0): + if self._is_socket: + pos = self.tmp.tell() + while True: + if not self._tee(CHUNK_SIZE): + break + self.tmp.seek(pos) + self.tmp.seek(offset, whence) def flush(self): self.tmp.flush() @@ -116,14 +125,17 @@ class TeeInput(object): def _tee(self, length): """ fetch partial body""" - while not self.parser.body_eof(): - data = read_partial(self.socket, length) - self.buf += data + while True: chunk, self.buf = self.parser.filter_body(self.buf) if chunk: self.tmp.write(chunk) self.tmp.seek(0, os.SEEK_END) - return chunk + return chunk + + if self.parser.body_eof(): break + + data = read_partial(self.socket, length) + self.buf += data self._finalize() return "" @@ -131,12 +143,6 @@ class TeeInput(object): """ here we wil fetch final trailers if any.""" if self.parser.body_eof(): - # handle trailing headers - if self.parser.is_chunked and self._is_socket: - while not self.parser.trailing_header(self.buf): - data = read_partial(self.socket, CHUNK_SIZE) - if not data: break - self.buf += data del self.buf self._is_socket = False