1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright (c) 2012 OpenStack, LLC
5
# Licensed under the Apache License, Version 2.0 (the "License"); you may
6
# not use this file except in compliance with the License. You may obtain
7
# a copy of the License at
9
# http://www.apache.org/licenses/LICENSE-2.0
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
# License for the specific language governing permissions and limitations
17
Request Body limiting middleware.
24
from cinder import flags
25
from cinder.openstack.common import cfg
26
from cinder.openstack.common import log as logging
27
from cinder import wsgi
30
#default request size is 112k
31
max_request_body_size_opt = cfg.IntOpt('osapi_max_request_body_size',
33
help='Max size for body of a request')
36
FLAGS.register_opt(max_request_body_size_opt)
37
LOG = logging.getLogger(__name__)
40
class RequestBodySizeLimiter(wsgi.Middleware):
41
"""Add a 'cinder.context' to WSGI environ."""
43
def __init__(self, *args, **kwargs):
44
super(RequestBodySizeLimiter, self).__init__(*args, **kwargs)
46
@webob.dec.wsgify(RequestClass=wsgi.Request)
47
def __call__(self, req):
48
if (req.content_length > FLAGS.osapi_max_request_body_size
49
or len(req.body) > FLAGS.osapi_max_request_body_size):
50
msg = _("Request is too large.")
51
raise webob.exc.HTTPBadRequest(explanation=msg)
53
return self.application