[mod_python] finding length of user file before uploading

greg girty at cogeco.ca
Mon Apr 17 13:05:08 EDT 2006


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?

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 *
      PythonPath "['/foo/mypy/'] + sys.path"
      PythonHandler fixup
      #PythonDebug On
</Directory>

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



More information about the Mod_python mailing list