Merge "Allow CONTENT_LENGTH to be present but empty"

This commit is contained in:
Jenkins 2017-04-26 12:06:23 +00:00 committed by Gerrit Code Review
commit 658b7c207e
2 changed files with 51 additions and 6 deletions

View File

@ -191,13 +191,19 @@ class PlacementHandler(object):
raise webob.exc.HTTPForbidden(
_('admin required'),
json_formatter=util.json_error_formatter)
# Check that an incoming request with a content-length
# header also has a content-type header. If not raise a 400.
if int(environ.get('CONTENT_LENGTH', 0)):
if 'CONTENT_TYPE' not in environ:
# Check that an incoming request with a content-length header
# that is an integer > 0 and not empty, also has a content-type
# header that is not empty. If not raise a 400.
clen = environ.get('CONTENT_LENGTH')
try:
if clen and (int(clen) > 0) and not environ.get('CONTENT_TYPE'):
raise webob.exc.HTTPBadRequest(
_('content-type header required'),
json_formatter=util.json_error_formatter)
_('content-type header required when content-length > 0'),
json_formatter=util.json_error_formatter)
except ValueError as exc:
raise webob.exc.HTTPBadRequest(
_('content-length header must be an integer'),
json_formatter=util.json_error_formatter)
try:
return dispatch(environ, start_response, self._map)
# Trap the NotFound exceptions raised by the objects used

View File

@ -142,3 +142,42 @@ class DeclarationsTest(test.NoDBTestCase):
environ = _environ(path='')
result = self.mapper.match(environ=environ)
self.assertEqual(root.home, result['action'])
class ContentHeadersTest(test.NoDBTestCase):
def setUp(self):
super(ContentHeadersTest, self).setUp()
self.environ = _environ(path='/')
self.app = handler.PlacementHandler()
def test_no_content_type(self):
self.environ['CONTENT_LENGTH'] = '10'
self.assertRaisesRegex(webob.exc.HTTPBadRequest,
"content-type header required when "
"content-length > 0", self.app,
self.environ, start_response)
def test_non_integer_content_length(self):
self.environ['CONTENT_LENGTH'] = 'foo'
self.assertRaisesRegex(webob.exc.HTTPBadRequest,
"content-length header must be an integer",
self.app, self.environ, start_response)
def test_empty_content_type(self):
self.environ['CONTENT_LENGTH'] = '10'
self.environ['CONTENT_TYPE'] = ''
self.assertRaisesRegex(webob.exc.HTTPBadRequest,
"content-type header required when "
"content-length > 0", self.app,
self.environ, start_response)
def test_empty_content_length_and_type_works(self):
self.environ['CONTENT_LENGTH'] = ''
self.environ['CONTENT_TYPE'] = ''
self.app(self.environ, start_response)
def test_content_length_and_type_works(self):
self.environ['CONTENT_LENGTH'] = '10'
self.environ['CONTENT_TYPE'] = 'foo'
self.app(self.environ, start_response)