quetz-mod_python-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Barry Pearce <barry.pea...@copyrightwitness.net>
Subject Re: util.py - field attributes incorrectly set
Date Mon, 28 Mar 2005 21:30:52 GMT
Hi all,

Here is a new util.py based on the latest from subversion (attached).

Fixes:

1. Memory fault when streaming extremely large files to disk when 
filename is sent from client.
2. Fixed headers being overwritten by headers for last field. Headers 
are now available for all fields (assumed as original intention).
3. Fixed filename being overwritten by last file sent in multipart/mixed 
posting with multiple files.

Enhancements:

1. Added file creation callback, allowing client to override both file 
creation/deletion semantics and location - examples of ways to use this 
are provided.

NOTE: This does not fix the issue of streaming to memory when no 
filename is supplied by the browser.

---------------------
Examples for new file callback usage - 2 techniques are shown. The first 
uses the class constructor to create the file object and uses simple 
control. It is not advisable to add class variables to this if serving 
multiple sites from apache - in this case to provide greater control 
over the constructor parameters use the factory method. It should be 
noted that the code shown here is used purely for example usage. The 
issue of temporary file location and security must be considered when 
providing such overrides with mod_python.

Both methods are common in that they derive from FileType. Thereby 
providing extended file functionality.

Indeed. It is possible to subclass tempfile.TemporaryFile and gain the 
same security semantics.

-----------------------------------------------------------------
Technique 1: Simple file control using class constructor.


from types import FileType

class Storage(FileType):

     def __init__(self, advisoryFilename):
         self.m_advisoryFilename = advisoryFilename
         self.m_deleteOnClose    = True
         self.m_deletedAlready   = False
	self.m_realFilename     = '/someTempDir/thingy-unique-thingy'
         super(Storage, self).__init__(self.m_realFilename, 'w+b')

     def close(self):
         if self.m_deletedAlready:
             return
         super(Storage, self).close()
         if self.m_deleteOnClose:
             self.m_deletedAlready = True
             os.remove(self.m_realFilename)
         return

requestData = util.FieldStorage(request, True, file_callback = Storage)

-----------------------------------------------------------------
Technique 2: Advanced file control using object factory.

from types import FileType

class Storage(FileType):

     def __init__(self, directory, advisoryFilename):
         self.m_advisoryFilename = advisoryFilename
         self.m_deleteOnClose    = True
         self.m_deletedAlready   = False
	self.m_realFilename     = directory + '/thingy-unique-thingy'
         super(Storage, self).__init__(self.m_realFilename, 'w+b')

     def close(self):
         if self.m_deletedAlready:
             return
         super(Storage, self).close()
         if self.m_deleteOnClose:
             self.m_deletedAlready = True
             os.remove(self.m_realFilename)
         return

class StorageFactory:

     def __init__(self, directory):
         self.m_dir = directory

     def create(self, advisoryFilename):
         return Storage(self.m_dir, advisoryFilename)

fileFactory = StorageFactory(someDirectory)
[...sometime later...]
requestData = util.FieldStorage(request, True, file_callback = 
fileFactory.create)
-----------------------------------------------------------------

Hope this helps,
Barry Pearce

Mime
View raw message