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:
Kiall Mac Innes
2013-06-19 23:07:27 +01:00
parent 6c53d66020
commit d04e66dd72
2 changed files with 33 additions and 0 deletions

View File

@@ -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
)

View File

@@ -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'