~ubuntu-cloud-archive/ubuntu/precise/cinder/trunk

« back to all changes in this revision

Viewing changes to cinder/api/middleware/sizelimit.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-11-23 08:39:28 UTC
  • mfrom: (1.1.9)
  • Revision ID: package-import@ubuntu.com-20121123083928-xvzet603cjfj9p1t
Tags: 2013.1~g1-0ubuntu1
* New upstream release.
* debian/patches/avoid_setuptools_git_dependency.patch:
  Avoid git installation. (LP: #1075948) 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
 
 
3
# Copyright (c) 2012 OpenStack, LLC
 
4
#
 
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
 
8
#
 
9
#         http://www.apache.org/licenses/LICENSE-2.0
 
10
#
 
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
 
15
#    under the License.
 
16
"""
 
17
Request Body limiting middleware.
 
18
 
 
19
"""
 
20
 
 
21
import webob.dec
 
22
import webob.exc
 
23
 
 
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
 
28
 
 
29
 
 
30
#default request size is 112k
 
31
max_request_body_size_opt = cfg.IntOpt('osapi_max_request_body_size',
 
32
                                       default=114688,
 
33
                                       help='Max size for body of a request')
 
34
 
 
35
FLAGS = flags.FLAGS
 
36
FLAGS.register_opt(max_request_body_size_opt)
 
37
LOG = logging.getLogger(__name__)
 
38
 
 
39
 
 
40
class RequestBodySizeLimiter(wsgi.Middleware):
 
41
    """Add a 'cinder.context' to WSGI environ."""
 
42
 
 
43
    def __init__(self, *args, **kwargs):
 
44
        super(RequestBodySizeLimiter, self).__init__(*args, **kwargs)
 
45
 
 
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)
 
52
        else:
 
53
            return self.application