use cstringio

This commit is contained in:
benoitc 2010-03-08 12:14:47 +01:00
parent 985c0857ab
commit 4873b41b33
3 changed files with 26 additions and 13 deletions

View File

@ -3,7 +3,11 @@
# This file is part of gunicorn released under the MIT license. # This file is part of gunicorn released under the MIT license.
# See the NOTICE for more information. # See the NOTICE for more information.
from StringIO import StringIO try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
import urlparse import urlparse
class BadStatusLine(Exception): class BadStatusLine(Exception):
@ -195,7 +199,7 @@ class Parser(object):
chunk = line[self.start_offset:self.start_offset+self.chunk_size] chunk = line[self.start_offset:self.start_offset+self.chunk_size]
end_offset = self.start_offset + self.chunk_size + 2 end_offset = self.start_offset + self.chunk_size + 2
# we wait CRLF else return None # we wait CRLF else return None
if buf.len >= end_offset: if len(buf.getvalue()) >= end_offset:
buf2.write(line[end_offset:]) buf2.write(line[end_offset:])
self.chunk_size = 0 self.chunk_size = 0
return chunk, buf2 return chunk, buf2
@ -211,7 +215,7 @@ class Parser(object):
Filter body and return a tuple: (body_chunk, new_buffer) Filter body and return a tuple: (body_chunk, new_buffer)
Both can be None, and new_buffer is always None if its empty. Both can be None, and new_buffer is always None if its empty.
""" """
dlen = buf.len dlen = len(buf.getvalue())
chunk = '' chunk = ''
if self.is_chunked: if self.is_chunked:

View File

@ -6,7 +6,11 @@
import logging import logging
import os import os
import re import re
from StringIO import StringIO try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
import sys import sys
from urllib import unquote from urllib import unquote
@ -76,8 +80,10 @@ class Request(object):
if not self.parser.content_len and not self.parser.is_chunked: if not self.parser.content_len and not self.parser.is_chunked:
wsgi_input = StringIO() wsgi_input = StringIO()
content_length = "0"
else: else:
wsgi_input = TeeInput(self._sock, self.parser, buf2, self.conf) 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 # This value should evaluate true if an equivalent application
# object may be simultaneously invoked by another process, and # object may be simultaneously invoked by another process, and
@ -132,7 +138,7 @@ class Request(object):
"QUERY_STRING": self.parser.query_string, "QUERY_STRING": self.parser.query_string,
"RAW_URI": self.parser.raw_path, "RAW_URI": self.parser.raw_path,
"CONTENT_TYPE": self.parser.headers_dict.get('Content-Type', ''), "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_ADDR": remote_addr[0],
"REMOTE_PORT": str(remote_addr[1]), "REMOTE_PORT": str(remote_addr[1]),
"SERVER_NAME": server_address[0], "SERVER_NAME": server_address[0],

View File

@ -11,7 +11,10 @@ read or restart etc ... It's based on TeeInput from Gunicorn.
""" """
import os import os
from StringIO import StringIO try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
import tempfile import tempfile
from gunicorn import util from gunicorn import util
@ -33,7 +36,7 @@ class TeeInput(object):
else: else:
self.tmp = tempfile.TemporaryFile(dir=self.conf['tmp_upload_dir']) 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) chunk, self.buf = parser.filter_body(buf)
if chunk: if chunk:
self.tmp.write(chunk) self.tmp.write(chunk)
@ -176,18 +179,18 @@ class TeeInput(object):
self._is_socket = False self._is_socket = False
def _tmp_size(self): def _tmp_size(self):
if isinstance(self.tmp, StringIO): if hasattr(self.tmp, 'fileno'):
return self.tmp.len
else:
return int(os.fstat(self.tmp.fileno())[6]) return int(os.fstat(self.tmp.fileno())[6])
else:
return len(self.tmp.getvalue())
def _ensure_length(self, dest, length): 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() return dest.getvalue()
while True: while True:
if dest.len >= length: if len(dest.getvalue()) >= length:
break break
data = self._tee(length - dest.len) data = self._tee(length - len(dest.getvalue()))
if not data: if not data:
break break
dest.write(data) dest.write(data)