[mod_python] finding length of user file before uploading

Graham Dumpleton grahamd at dscpl.com.au
Mon Apr 17 17:44:30 EDT 2006

On 18/04/2006, at 3:05 AM, greg wrote:

> Thanks Graham, for sharing your insight.
> A question about the example:
> What does the normal content handler do with the request after the  
> fixup  handler returns apache.DONE?

Nothing, no further handler phases for the request will be called.  
DONE means the
request has been satisfied and nothing else needs to be done.

> I expect I will keep the file check and upload monitoring inside a  
> single handler for now. I am working on commercial deployment, so I  
> have to take a 'wait-and-see' approach with features, despite my  
> personal curiousity about how the dev team is shaping the libs.
> What would mod_python.conf look like? If one handler is doing  
> everything, is this correct?
> <Directory myserver/www/uploads>
>      AddHandler mod_python *

You might mean:

   SetHandler mod_python

if your expectation is that everything against the directory is to be  
processed by the handler.


   AddHandler mod_python .py

if request must have .py extension in URL.

Ie., can't be '*'.

>      PythonPath "['/foo/mypy/'] + sys.path"
>      PythonHandler fixup
>      #PythonDebug On
> </Directory>

The reason why I did it as a separate handler is that you can't avoid  
it if you are using
something like mod_python.publisher or mod_python.psp to implement  
your upload
handler. This is because both process the form before any code of you  
own would have
a chance to run, thus making the check useless.

If you are using mod_python.publisher you choices thus are:

<Directory myserver/www/uploads>
     SetHandler mod_python
     PythonFixupHandler check_for_large_uploads
     PythonHandler mod_python.publisher

or if you have some philosophical problem with using the fixup  
handler phase, use
a stacked handler in content phase.

<Directory myserver/www/uploads>
     SetHandler mod_python
     PythonHandler check_for_large_uploads
     PythonHandler mod_python.publisher

If you aren't using mod_python.publisher, then yes you could  
incorporate the check
from check_for_large_uploads into your own custom handler instead.

<Directory myserver/www/uploads>
     SetHandler mod_python
     PythonHandler your_upload_handler


> Greg
>> Because some handlers such as mod_python.publisher have already
>> consumed request content by the time your code has been called,
>> the simplest thing to do is to do the check in a fixup handler  
>> which is
>> run in addition too, but before the normal content handler.
>> For example:
>>   PythonFixupHandler check_for_large_uploads
>>   # check_for_large_uploads.py
>>   from mod_python import apache
>>   UPLOAD_LIMIT = 1000000
>>   def fixuphandler(req):
>>     length = int(req.headers_in.get("Content-Length", "0"))
>>     if length >= UPLOAD_LIMIT:
>>       req.content_type = 'text/plain'
>>       req.status = apache.HTTP_BAD_REQUEST
>>       req.write('upload too big\n')
>>       return apache.DONE
>>     return apache.OK
>> The important bit is that the fixup handler must cause call to  
>> following
>> content handler to be aborted. This is why it explicitly sets  
>> req.status
>> and then returns apache.DONE instead of apache.OK. The handler
>> needs to also construct any custom response.
>> Not sure there is a simple way of handling an upload that takes too
>> long. I haven't looked at the file_callback feature to see whether  
>> that
>> might make it easier.
>> Graham
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python

More information about the Mod_python mailing list