mirror of
https://github.com/frappe/gunicorn.git
synced 2026-01-14 11:09:11 +08:00
use cstringio
This commit is contained in:
parent
985c0857ab
commit
4873b41b33
@ -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:
|
||||
|
||||
@ -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],
|
||||
|
||||
@ -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)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user