This file is indexed.

/usr/share/pyshared/glance/api/middleware/version_negotiation.py is in python-glance 2012.1.3+stable~20120821-120fcf-0ubuntu1.5.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2011 OpenStack LLC.
# All Rights Reserved.
#
#    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.

"""
A filter middleware that inspects the requested URI for a version string
and/or Accept headers and attempts to negotiate an API controller to
return
"""

import logging
import re

from glance.api import versions
from glance.common import wsgi

logger = logging.getLogger('glance.api.middleware.version_negotiation')


class VersionNegotiationFilter(wsgi.Middleware):

    def __init__(self, app, conf, **local_conf):
        self.versions_app = versions.Controller(conf)
        self.version_uri_regex = re.compile(r"^v(\d+)\.?(\d+)?")
        self.conf = conf
        super(VersionNegotiationFilter, self).__init__(app)

    def process_request(self, req):
        """
        If there is a version identifier in the URI, simply
        return the correct API controller, otherwise, if we
        find an Accept: header, process it
        """
        # See if a version identifier is in the URI passed to
        # us already. If so, simply return the right version
        # API controller
        msg = _("Processing request: %(method)s %(path)s Accept: "
                "%(accept)s") % ({'method': req.method,
                'path': req.path, 'accept': req.accept})
        logger.debug(msg)

        # If the request is for /versions, just return the versions container
        if req.path_info_peek() == "versions":
            return self.versions_app

        match = self._match_version_string(req.path_info_peek(), req)
        if match:
            if (req.environ['api.major_version'] == 1 and
                req.environ['api.minor_version'] == 0):
                logger.debug(_("Matched versioned URI. Version: %d.%d"),
                             req.environ['api.major_version'],
                             req.environ['api.minor_version'])
                # Strip the version from the path
                req.path_info_pop()
                return None
            else:
                logger.debug(_("Unknown version in versioned URI: %d.%d. "
                             "Returning version choices."),
                             req.environ['api.major_version'],
                             req.environ['api.minor_version'])
                return self.versions_app

        accept = str(req.accept)
        if accept.startswith('application/vnd.openstack.images-'):
            token_loc = len('application/vnd.openstack.images-')
            accept_version = accept[token_loc:]
            match = self._match_version_string(accept_version, req)
            if match:
                if (req.environ['api.major_version'] == 1 and
                    req.environ['api.minor_version'] == 0):
                    logger.debug(_("Matched versioned media type. "
                                 "Version: %d.%d"),
                                 req.environ['api.major_version'],
                                 req.environ['api.minor_version'])
                    return None
                else:
                    logger.debug(_("Unknown version in accept header: %d.%d..."
                                 "returning version choices."),
                                 req.environ['api.major_version'],
                                 req.environ['api.minor_version'])
                    return self.versions_app
        else:
            if req.accept not in ('*/*', ''):
                logger.debug(_("Unknown accept header: %s..."
                             "returning version choices."), req.accept)
            return self.versions_app
        return None

    def _match_version_string(self, subject, req):
        """
        Given a subject string, tries to match a major and/or
        minor version number. If found, sets the api.major_version
        and api.minor_version environ variables.

        Returns True if there was a match, false otherwise.

        :param subject: The string to check
        :param req: Webob.Request object
        """
        match = self.version_uri_regex.match(subject)
        if match:
            major_version, minor_version = match.groups(0)
            major_version = int(major_version)
            minor_version = int(minor_version)
            req.environ['api.major_version'] = major_version
            req.environ['api.minor_version'] = minor_version
        return match is not None