Back to index

moin  1.9.0~rc2
limiter.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 """
00003     werkzeug.contrib.limiter
00004     ~~~~~~~~~~~~~~~~~~~~~~~~
00005 
00006     A middleware that limits incoming data.  This works around problems with
00007     Trac_ or Django_ because those directly stream into the memory.
00008 
00009     .. _Trac: http://trac.edgewall.org/
00010     .. _Django: http://www.djangoproject.com/
00011 
00012     :copyright: (c) 2009 by the Werkzeug Team, see AUTHORS for more details.
00013     :license: BSD, see LICENSE for more details.
00014 """
00015 from warnings import warn
00016 from werkzeug.utils import LimitedStream as LimitedStreamBase
00017 
00018 
00019 class _SilentLimitedStream(LimitedStreamBase):
00020 
00021     def __init__(self, environ, limit):
00022         LimitedStreamBase.__init__(self,
00023             environ['wsgi.input'],
00024             min(limit, int(environ.get('CONTENT_LENGTH') or 0)),
00025             silent=True
00026         )
00027 
00028 
00029 class LimitedStream(_SilentLimitedStream):
00030 
00031     def __init__(self, environ, limit):
00032         _SilentLimitedStream.__init__(self, environ, limit)
00033         warn(DeprecationWarning('contrib limited stream is deprecated, use '
00034                                 'werkzeug.LimitedStream instead.'),
00035              stacklevel=2)
00036 
00037 
00038 class StreamLimitMiddleware(object):
00039     """Limits the input stream to a given number of bytes.  This is useful if
00040     you have a WSGI application that reads form data into memory (django for
00041     example) and you don't want users to harm the server by uploading tons of
00042     data.
00043 
00044     Default is 10MB
00045     """
00046 
00047     def __init__(self, app, maximum_size=1024 * 1024 * 10):
00048         self.app = app
00049         self.maximum_size = maximum_size
00050 
00051     def __call__(self, environ, start_response):
00052         environ['wsgi.input'] = _SilentLimitedStream(environ, self.maximum_size)
00053         return self.app(environ, start_response)