From 4873b41b33fc901ed304248a3d926d0cf076c0ad Mon Sep 17 00:00:00 2001 From: benoitc Date: Mon, 8 Mar 2010 12:14:47 +0100 Subject: [PATCH] use cstringio --- gunicorn/http/parser.py | 10 +++++++--- gunicorn/http/request.py | 10 ++++++++-- gunicorn/http/tee.py | 19 +++++++++++-------- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/gunicorn/http/parser.py b/gunicorn/http/parser.py index 26d4611a..a07f3f72 100644 --- a/gunicorn/http/parser.py +++ b/gunicorn/http/parser.py @@ -3,7 +3,11 @@ # This file is part of gunicorn released under the MIT license. # See the NOTICE for more information. -from StringIO import StringIO +try: + from cStringIO import StringIO +except ImportError: + from StringIO import StringIO + import urlparse class BadStatusLine(Exception): @@ -195,7 +199,7 @@ class Parser(object): chunk = line[self.start_offset:self.start_offset+self.chunk_size] end_offset = self.start_offset + self.chunk_size + 2 # we wait CRLF else return None - if buf.len >= end_offset: + if len(buf.getvalue()) >= end_offset: buf2.write(line[end_offset:]) self.chunk_size = 0 return chunk, buf2 @@ -211,7 +215,7 @@ class Parser(object): Filter body and return a tuple: (body_chunk, new_buffer) Both can be None, and new_buffer is always None if its empty. """ - dlen = buf.len + dlen = len(buf.getvalue()) chunk = '' if self.is_chunked: diff --git a/gunicorn/http/request.py b/gunicorn/http/request.py index ccac7bfc..29f610ed 100644 --- a/gunicorn/http/request.py +++ b/gunicorn/http/request.py @@ -6,7 +6,11 @@ import logging import os import re -from StringIO import StringIO +try: + from cStringIO import StringIO +except ImportError: + from StringIO import StringIO + import sys from urllib import unquote @@ -76,8 +80,10 @@ class Request(object): if not self.parser.content_len and not self.parser.is_chunked: wsgi_input = StringIO() + content_length = "0" else: wsgi_input = TeeInput(self._sock, self.parser, buf2, self.conf) + content_length = str(wsgi_input.len) # This value should evaluate true if an equivalent application # object may be simultaneously invoked by another process, and @@ -132,7 +138,7 @@ class Request(object): "QUERY_STRING": self.parser.query_string, "RAW_URI": self.parser.raw_path, "CONTENT_TYPE": self.parser.headers_dict.get('Content-Type', ''), - "CONTENT_LENGTH": str(wsgi_input.len), + "CONTENT_LENGTH": content_length, "REMOTE_ADDR": remote_addr[0], "REMOTE_PORT": str(remote_addr[1]), "SERVER_NAME": server_address[0], diff --git a/gunicorn/http/tee.py b/gunicorn/http/tee.py index fe1fbacd..8183adc8 100644 --- a/gunicorn/http/tee.py +++ b/gunicorn/http/tee.py @@ -11,7 +11,10 @@ read or restart etc ... It's based on TeeInput from Gunicorn. """ import os -from StringIO import StringIO +try: + from cStringIO import StringIO +except ImportError: + from StringIO import StringIO import tempfile from gunicorn import util @@ -33,7 +36,7 @@ class TeeInput(object): else: self.tmp = tempfile.TemporaryFile(dir=self.conf['tmp_upload_dir']) - if buf.len > 0: + if len(buf.getvalue()) > 0: chunk, self.buf = parser.filter_body(buf) if chunk: self.tmp.write(chunk) @@ -176,18 +179,18 @@ class TeeInput(object): self._is_socket = False def _tmp_size(self): - if isinstance(self.tmp, StringIO): - return self.tmp.len - else: + if hasattr(self.tmp, 'fileno'): return int(os.fstat(self.tmp.fileno())[6]) + else: + return len(self.tmp.getvalue()) def _ensure_length(self, dest, length): - if not dest.len or not self._len: + if not len(dest.getvalue()) or not self._len: return dest.getvalue() while True: - if dest.len >= length: + if len(dest.getvalue()) >= length: break - data = self._tee(length - dest.len) + data = self._tee(length - len(dest.getvalue())) if not data: break dest.write(data)