[mod_python] in modpython ,how to restrict the upload file size?

Clodoaldo clodoaldo.pinto.neto at gmail.com
Sat Feb 3 08:41:16 EST 2007


2007/2/2, Graham Dumpleton <grahamd at dscpl.com.au>:
> I haven't said anything because I have been too busy and also because
> this topic has been covered before on the mailing list and it is in the
> archives. The only trick is working out the right search terms to
> find the
> answer. To save you the trouble:
>
>    http://www.modpython.org/pipermail/mod_python/2006-April/020867.html
>
> Also see:
>
>    http://www.modpython.org/pipermail/mod_python/2006-July/021610.html
>
> Ie. you can always just use Apache LimitRequestBody directive. And:
>
>    http://www.modpython.org/pipermail/mod_python/2006-July/021611.html
>
> Ie., used for fixuphandler approach, don't use HTTP_BAD_REQUEST, but
> HTTP_REQUEST_ENTITY_TOO_LARGE. Also see comments about
> length being required.

Thanks for the tips. Based on those posts I produced this fixup handler:

#
# _upload_limit.py
#
from mod_python import apache

UPLOAD_LIMIT = 1 * 1024 * 1024

def fixuphandler(req):

   if req.method == 'POST':

      length = req.headers_in.get('Content-Length')
      if length is None:

         req.status = apache.HTTP_LENGTH_REQUIRED
         req.log_error('fixupHandler: %s %s' % \
            (req.status, 'HTTP_LENGTH_REQUIRED'))
         return apache.DONE

      elif int(length) > UPLOAD_LIMIT:

         req.status = apache.HTTP_REQUEST_ENTITY_TOO_LARGE
         req.log_error('fixupHandler: %s %s' % \
            (req.status, 'HTTP_REQUEST_ENTITY_TOO_LARGE'))
         req.content_type = 'text/html'
         html = """\
         <html><body>
         <p>Failed upload: too large.</p>
         <p>Size limit: %s bytes.</p>
         <p><a href="javascript:history.back();">Go Back</a></p>
         </body></html>
         """ % UPLOAD_LIMIT
         req.write(html)
         return apache.DONE

   return apache.OK
#############################

httpd.conf:

    <Directory /var/www/html/carroarodo.com>
       SetHandler mod_python
       PythonHandler ~/_publisher.py
       PythonOption mod_python.importer.path "['~/mod']"
       PythonFixupHandler _upload_limit
    </Directory>

BTW apache.DONE is not documented in the current manual. This Google
search returns nothing:
apache.DONE site:modpython.org/live/current/doc-html/

Regards,
-- 
Clodoaldo Pinto Neto


More information about the Mod_python mailing list