1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2010 OpenStack LLC.
6
# Licensed under the Apache License, Version 2.0 (the "License"); you may
7
# not use this file except in compliance with the License. You may obtain
8
# a copy of the License at
10
# http://www.apache.org/licenses/LICENSE-2.0
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
# License for the specific language governing permissions and limitations
19
A few utility routines used throughout Glance
23
def image_meta_to_http_headers(image_meta):
25
Returns a set of image metadata into a dict
26
of HTTP headers that can be fed to either a Webob
27
Request object or an httplib.HTTP(S)Connection object
29
:param image_meta: Mapping of image metadata
32
for k, v in image_meta.items():
34
for pk, pv in v.items():
35
headers["x-image-meta-property-%s"
38
headers["x-image-meta-%s" % k.lower()] = v
42
def inject_image_meta_into_headers(response, image_meta):
44
Given a response and mapping of image metadata, injects
45
the Response with a set of HTTP headers for the image
46
metadata. Each main image metadata field is injected
47
as a HTTP header with key 'x-image-meta-<FIELD>' except
48
for the properties field, which is further broken out
49
into a set of 'x-image-meta-property-<KEY>' headers
51
:param response: The Webob Response object
52
:param image_meta: Mapping of image metadata
54
headers = image_meta_to_http_headers(image_meta)
56
for k, v in headers.items():
57
response.headers.add(k, v)
60
def get_image_meta_from_headers(response):
62
Processes HTTP headers from a supplied response that
63
match the x-image-meta and x-image-meta-property and
64
returns a mapping of image metadata and properties
66
:param response: Response to process
71
if hasattr(response, 'getheaders'): # httplib.HTTPResponse
72
headers = response.getheaders()
73
else: # webob.Response
74
headers = response.headers.items()
76
for key, value in headers:
77
key = str(key.lower())
78
if key.startswith('x-image-meta-property-'):
79
properties[key[len('x-image-meta-property-'):]] = value
80
if key.startswith('x-image-meta-'):
81
field_name = key[len('x-image-meta-'):].replace('-', '_')
82
result[field_name] = value
83
result['properties'] = properties