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 util.py and FieldStorage enhancements.
Date Wed, 06 Apr 2005 01:18:51 GMT
Hi all,

OK this is pretty much the best one Ive got....Ill re-iterate the usage 
points from the previous so this email may be considered definitive.

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 postings with 
multiple files.
4. Fixed error where MSIE returns invalid MIME type/subtype when the 
file has no extension (duh!) so default of application/octet-stream is 
used instead.

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.
2. Added StringField creation callback, allowing client to override both 
the creation/deletion semantics and behaviour - this may be used in a 
similar manner as the file creation callback above.

Both these callbacks can be ignored in general use - additionally cgi 
style inheritence and overriding of make_file and make_field is possible 
- or you can supply the callback instead of inheriting.

These behaviours allow the following functionality to be implemented by 
the client (no implementation is given here)

a. When string reaches a given size it is placed in a file.
b. control of maximum file actions such as error or truncate after 
maximum is reached.

I have tested the above with MSIE, Firefox and Opera. I can sucessfully 
upload files of 1.94GB (but IE then times out)...Firefox can cope with 
much above 1.29GB and opera a similar figure - thus - the new util.py 
out-performs the browsers that are out there currently!

If you were to fire data at the server using an app such as a python 
program and the HTTPConnection class then I reckon you could pretty much 
fill any disk space you wish at your leisure!

Mission (should have been) accomplished - comments please!!!!

Obviously I am putting the code forward for inclusion in a future 
release of mod_python.


---------------------
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.


class Storage(file):

     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.

class Storage(file):

     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