Skip to content

Commit

Permalink
just make sure we write at the end everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
benoitc committed Feb 28, 2010
1 parent d1011ff commit 05d4673
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
28 changes: 7 additions & 21 deletions gunicorn/http/tee.py
Expand Up @@ -15,7 +15,7 @@
import StringIO
import tempfile

from gunicorn.util import MAX_BODY, CHUNK_SIZE, read_partial
from gunicorn.util import MAX_BODY, CHUNK_SIZE, read_partial, fsize, fwrite

class TeeInput(object):

Expand All @@ -33,10 +33,7 @@ def __init__(self, socket, parser, buf):
if len(buf) > 0:
chunk, self.buf = parser.filter_body(buf)
if chunk:
self.tmp.write(chunk)
self.tmp.flush()
if hasattr(self.tmp, 'fileno'):
os.fsync(self.tmp.fileno())
fwrite(self.tmp, chunk)
self._finalize()
self.tmp.seek(0)

Expand All @@ -45,23 +42,17 @@ def len(self):
if self._len: return self._len

if self._is_socket:
pos = self.tmp.tell()
while True:
self.tmp.seek(self._tmp_size())
if not self._tee(CHUNK_SIZE):
break
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:
self.tmp.seek(self._tmp_size())
if not self._tee(CHUNK_SIZE):
break
self.tmp.seek(pos)
self.tmp.seek(offset, whence)

def flush(self):
Expand Down Expand Up @@ -140,11 +131,7 @@ def _tee(self, length):
while True:
chunk, self.buf = self.parser.filter_body(self.buf)
if chunk:
self.tmp.write(chunk)
self.tmp.flush()
if hasattr(self.tmp, 'fileno'):
os.fsync(self.tmp.fileno())
self.tmp.seek(0, os.SEEK_END)
fwrite(self.tmp, chunk)
return chunk

if self.parser.body_eof():
Expand All @@ -163,14 +150,13 @@ def _finalize(self):
self._is_socket = False

def _tmp_size(self):
if isinstance(self.tmp, StringIO.StringIO):
return self.tmp.len
else:
return int(os.fstat(self.tmp.fileno())[6])
return fsize(self.tmp)

def _ensure_length(self, buf, length):
if not buf or not self._len:
return buf
while len(buf) < length and self.len != self.tmp.tell():
buf += self._tee(length - len(buf))
data = self._tee(length - len(buf))
if not data: break
buf += data
return buf
18 changes: 18 additions & 0 deletions gunicorn/util.py
Expand Up @@ -10,6 +10,7 @@
import resource
import select
import socket
import StringIO
import textwrap
import time

Expand Down Expand Up @@ -140,6 +141,23 @@ def write_error(sock, msg):
%s
""") % (len(html), html)
write_nonblock(sock, http)

def fwrite(f, data):
pos = f.tell()
f.seek(fsize(f))
f.write(data)
f.flush()
if hasattr(f, 'fileno'):
os.fsync(f.fileno())
f.seek(0, os.SEEK_END)
f.seek(pos)

def fsize(f):
if isinstance(f, StringIO.StringIO):
return f.len
else:
return int(os.fstat(f.fileno())[6])


def normalize_name(name):
return "-".join([w.lower().capitalize() for w in name.split("-")])
Expand Down

0 comments on commit 05d4673

Please sign in to comment.