""" * ==================================================================== * Copyright (c) 2001 Jon Travis. All rights reserved. * See the file COPYING for information on usage and redistribution. * ==================================================================== """ try: from cStringIO import StringIO except: from StringIO import StringIO class RequestFile: """RequestFile: An object that acts like a standard file, and wraps around a request_rec's IO routines. It supports only a few of the file methods, as needed by cgi.py""" CHUNKSIZE = 1024 def __init__(self, request): self.__req = request self.__sio = StringIO() def __sio_length(self): sio = self.__sio cur_pos = sio.tell() sio.seek(0,2) res = sio.tell() sio.seek(cur_pos) def __readmore(self, once): """Read all the data from the req into the sio""" cur_pos = self.__sio.tell() self.__sio.seek(0, 2) eofres = 0 while 1: data, dataerr = self.__req.get_client_block(self.CHUNKSIZE) if dataerr <= 0: eofres = -1 break self.__sio.write(data) if once: break self.__sio.seek(cur_pos) return eofres def readline(self): res = '' sio = self.__sio while 1: # Keep reading in until we have a line, or end of file res = res + sio.readline() if res[-1:] == '\n': cur_pos = sio.tell() sio_len = self.__sio_length() if cur_pos == sio_len: # The end was read self.__sio = StringIO() return res else: # Otherwise, try to read more data readres = self.__readmore(1) if readres < 0: # End of input return res def read(self, *bytes): if not len(bytes): # Read until we can read no more self.__readmore(0) data = self.__sio.read() self.__sio = StringIO() return data return self.__sio.read(bytes[0]) def write(self, str): nwrote = 0 while nwrote != len(str): just_wrote = self.__req.rwrite(str[nwrote:]) if just_wrote < 0: return # XXX -- Client closed connexxion! nwrote = nwrote + just_wrote