ironic/ironic/api/middleware/json_ext.py
Dmitry Tantsur cfc167eadf Stop guessing mime types based on URLs
Currently we have a pecan feature enabled that strips extensions
from the end of the URL and treat it like requested content type.
E.g. /v1/nodes.json is treated as /v1/nodes with requested content
type Application/Json. However, this prevents certain node names:
e.g. /v1/nodes/small.1 is treated like /v1/nodes/small with content
type of a man page. It does not make any sense for ironic API,
as we only support Application/Json content type (and .json suffix).

This change disabled this pecan feature. To keep backward compability
a new middleware stips the .json prefix and saves a flag in the
environment about its presence. API accepting names try to find
their resource first without, then with .json suffix.

The following endpoints are special-cased to support names with .json:
* Node GET, PATCH and DELETE
* Ramdisk heartbeat
* Port group GET, PATCH and DELETE

VIF API is not updated, so VIF IDs still cannot have .json suffix.

Change-Id: I789ecfeac9b64a9c4105a20619f7bf5dfc133189
Closes-Bug: #1643995
2018-02-07 20:58:35 +00:00

44 lines
1.5 KiB
Python

# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from oslo_log import log
from ironic.common import utils
LOG = log.getLogger(__name__)
class JsonExtensionMiddleware(object):
"""Simplified processing of .json extension.
Previously Ironic API used the "guess_content_type_from_ext" feature.
It was never needed, as we never allowed non-JSON content types anyway.
Now that it is removed, this middleware strips .json extension for
backward compatibility.
"""
def __init__(self, app):
self.app = app
def __call__(self, env, start_response):
path = utils.safe_rstrip(env.get('PATH_INFO'), '/')
if path and path.endswith('.json'):
LOG.debug('Stripping .json prefix from %s for compatibility '
'with pecan', path)
env['PATH_INFO'] = path[:-5]
env['HAS_JSON_SUFFIX'] = True
else:
env['HAS_JSON_SUFFIX'] = False
return self.app(env, start_response)