Ensure request media-types are sane
This ensures both the Accept and Content-Type headers have agreeable values. Change-Id: If84c137299063884bf4c6805a0506664504862c0
This commit is contained in:
@@ -18,6 +18,7 @@ import webob.dec
|
||||
from stevedore import extension
|
||||
from stevedore import named
|
||||
from werkzeug import exceptions as wexceptions
|
||||
from werkzeug import wrappers
|
||||
from werkzeug.routing import BaseConverter
|
||||
from werkzeug.routing import ValidationError
|
||||
from designate.openstack.common import cfg
|
||||
@@ -36,8 +37,30 @@ cfg.CONF.register_opts([
|
||||
], group='service:api')
|
||||
|
||||
|
||||
class DesignateRequest(flask.Request, wrappers.AcceptMixin,
|
||||
wrappers.CommonRequestDescriptorsMixin):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(DesignateRequest, self).__init__(*args, **kwargs)
|
||||
|
||||
self._validate_content_type()
|
||||
self._validate_accept()
|
||||
|
||||
def _validate_content_type(self):
|
||||
if (self.method in ['POST', 'PUT', 'PATCH']
|
||||
and self.mimetype != 'application/json'):
|
||||
|
||||
msg = 'Unsupported Content-Type: %s' % self.mimetype
|
||||
raise exceptions.UnsupportedContentType(msg)
|
||||
|
||||
def _validate_accept(self):
|
||||
if 'accept' in self.headers and not self.accept_mimetypes.accept_json:
|
||||
msg = 'Unsupported Accept: %s' % self.accept_mimetypes
|
||||
raise exceptions.UnsupportedAccept(msg)
|
||||
|
||||
|
||||
def factory(global_config, **local_conf):
|
||||
app = flask.Flask('designate.api.v1')
|
||||
app.request_class = DesignateRequest
|
||||
app.config.update(
|
||||
PROPAGATE_EXCEPTIONS=True
|
||||
)
|
||||
|
||||
@@ -65,6 +65,16 @@ class BadRequest(Base):
|
||||
error_type = 'bad_request'
|
||||
|
||||
|
||||
class UnsupportedAccept(BadRequest):
|
||||
error_code = 406
|
||||
error_type = 'unsupported_accept'
|
||||
|
||||
|
||||
class UnsupportedContentType(BadRequest):
|
||||
error_code = 415
|
||||
error_type = 'unsupported_content_type'
|
||||
|
||||
|
||||
class InvalidDomainName(Base):
|
||||
error_code = 400
|
||||
error_type = 'invalid_domain_name'
|
||||
|
||||
Reference in New Issue
Block a user