"Fossies" - the Fresh Open Source Software Archive

Member "glance-19.0.0/glance/api/middleware/gzip.py" (16 Oct 2019, 2279 Bytes) of package /linux/misc/openstack/glance-19.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "gzip.py" see the Fossies "Dox" file reference documentation.

    1 # Copyright 2013 Red Hat, Inc.
    2 # All Rights Reserved.
    3 #
    4 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
    5 #    not use this file except in compliance with the License. You may obtain
    6 #    a copy of the License at
    7 #
    8 #         http://www.apache.org/licenses/LICENSE-2.0
    9 #
   10 #    Unless required by applicable law or agreed to in writing, software
   11 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   12 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   13 #    License for the specific language governing permissions and limitations
   14 #    under the License.
   15 
   16 """
   17 Use gzip compression if the client accepts it.
   18 """
   19 
   20 import re
   21 
   22 from oslo_log import log as logging
   23 
   24 from glance.common import wsgi
   25 from glance.i18n import _LI
   26 
   27 LOG = logging.getLogger(__name__)
   28 
   29 
   30 class GzipMiddleware(wsgi.Middleware):
   31 
   32     re_zip = re.compile(r'\bgzip\b')
   33 
   34     def __init__(self, app):
   35         LOG.info(_LI("Initialized gzip middleware"))
   36         super(GzipMiddleware, self).__init__(app)
   37 
   38     def process_response(self, response):
   39         request = response.request
   40         accept_encoding = request.headers.get('Accept-Encoding', '')
   41 
   42         if self.re_zip.search(accept_encoding):
   43             # NOTE(flaper87): Webob removes the content-md5 when
   44             # app_iter is called. We'll keep it and reset it later
   45             checksum = response.headers.get("Content-MD5")
   46 
   47             # NOTE(flaper87): We'll use lazy for images so
   48             # that they can be compressed without reading
   49             # the whole content in memory. Notice that using
   50             # lazy will set response's content-length to 0.
   51             content_type = response.headers.get("Content-Type", "")
   52             lazy = content_type == "application/octet-stream"
   53 
   54             # NOTE(flaper87): Webob takes care of the compression
   55             # process, it will replace the body either with a
   56             # compressed body or a generator - used for lazy com
   57             # pression - depending on the lazy value.
   58             #
   59             # Webob itself will set the Content-Encoding header.
   60             response.encode_content(lazy=lazy)
   61 
   62             if checksum:
   63                 response.headers['Content-MD5'] = checksum
   64 
   65         return response