diff options
author | Chris Dent <cdent@anticdent.org> | 2018-01-15 20:58:48 +0000 |
---|---|---|
committer | Chris Dent <cdent@anticdent.org> | 2018-03-06 22:05:12 +0000 |
commit | ef6f4e4c8ec82e2c9f9988fe2e04591ee01220e6 (patch) | |
tree | 14838753c47fc99c23fb85e55472fe8cbcdc4911 /nova/api/openstack/wsgi.py | |
parent | 5f881ff030c119b50bf1929b9829f52493ac447c (diff) | |
download | nova-ef6f4e4c8ec82e2c9f9988fe2e04591ee01220e6.tar.gz |
Refactor WSGI apps and utils to limit imports
The file nova/api/openstack/__init__.py had imported a lot of
modules, notably nova.utils. This means that any code which
runs within that package, notably the placement service, imports
all those modules, even if it is not going to use them. This
results in scripts/binaries that are heavier than they need
to be and in some cases including modules, like eventlet, that
it would feel safe to not have in the stack.
Unfortunately we cannot sinply rename nova/api/openstack/__init__.py
to another name because it contains FaultWrapper and FaultWrapper
is referred to, by package path, from the paste.ini file and that
file is out there in config land, and something we prefer not to
change. Therefore alternate methods of cleaning up were explored
and this has led to some useful changes:
Fault wrapper is the only consumer of walk_class_hierarchy so
there is no reason for it it to be in nova.utils.
nova.wsgi contains a mismash of WSGI middleware and applications,
which need only a small number of imports, and Server classes
which are more complex and not required by the WSGI wares.
Therefore nova.wsgi was split into nova.wsgi and nova.api.wsgi.
The name choices may not be ideal, but they were chosen to limit
the cascades of changes that are needed across code and tests.
Where utils.utf8 was used it has been replaced with the similar (but not
exactly equivalient) method from oslo_utils.encodeutils.
Change-Id: I297f30aa6eb01fe3b53fd8c9b7853949be31156d
Partial-Bug: #1743120
Diffstat (limited to 'nova/api/openstack/wsgi.py')
-rw-r--r-- | nova/api/openstack/wsgi.py | 39 |
1 files changed, 26 insertions, 13 deletions
diff --git a/nova/api/openstack/wsgi.py b/nova/api/openstack/wsgi.py index 3f7eebc237..2eef33c012 100644 --- a/nova/api/openstack/wsgi.py +++ b/nova/api/openstack/wsgi.py @@ -26,11 +26,10 @@ import webob from nova.api.openstack import api_version_request as api_version from nova.api.openstack import versioned_method +from nova.api import wsgi from nova import exception from nova import i18n from nova.i18n import _ -from nova import utils -from nova import wsgi LOG = logging.getLogger(__name__) @@ -334,16 +333,28 @@ class ResponseObject(object): if self.obj is not None: body = serializer.serialize(self.obj) response = webob.Response(body=body) - if response.headers.get('Content-Length'): - # NOTE(andreykurilin): we need to encode 'Content-Length' header, - # since webob.Response auto sets it if "body" attr is presented. - # https://github.com/Pylons/webob/blob/1.5.0b0/webob/response.py#L147 - response.headers['Content-Length'] = utils.utf8( - response.headers['Content-Length']) response.status_int = self.code - for hdr, value in self._headers.items(): - response.headers[hdr] = utils.utf8(value) - response.headers['Content-Type'] = utils.utf8(content_type) + for hdr, val in self._headers.items(): + if not isinstance(val, six.text_type): + val = six.text_type(val) + if six.PY2: + # In Py2.X Headers must be byte strings + response.headers[hdr] = encodeutils.safe_encode(val) + else: + # In Py3.X Headers must be utf-8 strings + response.headers[hdr] = encodeutils.safe_decode( + encodeutils.safe_encode(val)) + # Deal with content_type + if not isinstance(content_type, six.text_type): + content_type = six.text_type(content_type) + if six.PY2: + # In Py2.X Headers must be byte strings + response.headers['Content-Type'] = encodeutils.safe_encode( + content_type) + else: + # In Py3.X Headers must be utf-8 strings + response.headers['Content-Type'] = encodeutils.safe_decode( + encodeutils.safe_encode(content_type)) return response @property @@ -658,13 +669,15 @@ class Resource(wsgi.Application): if hasattr(response, 'headers'): for hdr, val in list(response.headers.items()): + if not isinstance(val, six.text_type): + val = six.text_type(val) if six.PY2: # In Py2.X Headers must be byte strings - response.headers[hdr] = utils.utf8(val) + response.headers[hdr] = encodeutils.safe_encode(val) else: # In Py3.X Headers must be utf-8 strings response.headers[hdr] = encodeutils.safe_decode( - utils.utf8(val)) + encodeutils.safe_encode(val)) if not request.api_version_request.is_null(): response.headers[API_VERSION_REQUEST_HEADER] = \ |