Merge "Experimental implementation for GET a single stream for multiple files"
This commit is contained in:
commit
e155b047b6
|
@ -0,0 +1,59 @@
|
||||||
|
# Copyright (c) 2010-2016 OpenStack Foundation
|
||||||
|
#
|
||||||
|
# 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.
|
||||||
|
|
||||||
|
import random
|
||||||
|
|
||||||
|
|
||||||
|
class MultiInputMIMEStorlet(object):
|
||||||
|
def __init__(self, logger):
|
||||||
|
self.logger = logger
|
||||||
|
|
||||||
|
def __call__(self, in_files, out_files, params):
|
||||||
|
"""
|
||||||
|
The function called for storlet invocation
|
||||||
|
|
||||||
|
:param in_files: a list of StorletInputFile
|
||||||
|
:param out_files: a list of StorletOutputFile
|
||||||
|
:param params: a dict of request parameters
|
||||||
|
"""
|
||||||
|
|
||||||
|
metadata = {}
|
||||||
|
for input_file in in_files:
|
||||||
|
metadata.update(input_file.get_metadata())
|
||||||
|
mime_boundary = "%.64x" % random.randint(0, 16 ** 64)
|
||||||
|
metadata['Content-Type'] = \
|
||||||
|
'multipart/mixed; boundary=%s' % mime_boundary
|
||||||
|
out_files[0].set_metadata(metadata)
|
||||||
|
|
||||||
|
self.logger.debug('Start to return object data')
|
||||||
|
while in_files:
|
||||||
|
input_file = in_files.pop(0)
|
||||||
|
while True:
|
||||||
|
buf = input_file.read(16)
|
||||||
|
if not buf:
|
||||||
|
break
|
||||||
|
self.logger.debug('Recieved %d bytes' % len(buf))
|
||||||
|
out_files[0].write(buf)
|
||||||
|
input_file.close()
|
||||||
|
if in_files:
|
||||||
|
# in_files still have items
|
||||||
|
out_files[0].write('\n--%s\n' % mime_boundary)
|
||||||
|
else:
|
||||||
|
# this is the end of input_files so the boundary should end
|
||||||
|
# the content
|
||||||
|
out_files[0].write('\n--%s--' % mime_boundary)
|
||||||
|
|
||||||
|
self.logger.debug('Complete')
|
||||||
|
out_files[0].close()
|
|
@ -392,7 +392,15 @@ class StorletBaseHandler(object):
|
||||||
# coming from swift request or response.
|
# coming from swift request or response.
|
||||||
if user_metadata:
|
if user_metadata:
|
||||||
for key, val in user_metadata.items():
|
for key, val in user_metadata.items():
|
||||||
headers['X-Object-Meta-%s' % key] = val
|
# TODO(kota_): we may need to discuss what type of special
|
||||||
|
# system metadata can be overwritten in the HTTP header and
|
||||||
|
# should encapsulate in StorletRequest/Response class
|
||||||
|
if key.lower() == 'content-type':
|
||||||
|
# If and only if it's content-type, storlet app is able
|
||||||
|
# to overwrite the header due to the application
|
||||||
|
headers[key] = val
|
||||||
|
else:
|
||||||
|
headers['X-Object-Meta-%s' % key] = val
|
||||||
|
|
||||||
def _call_gateway(self, resp):
|
def _call_gateway(self, resp):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -98,5 +98,54 @@ class TestMultiInputStorletOnProxy(TestMultiInputStorlet):
|
||||||
self.additional_headers = {'X-Storlet-Run-On-Proxy': ''}
|
self.additional_headers = {'X-Storlet-Run-On-Proxy': ''}
|
||||||
|
|
||||||
|
|
||||||
|
class TestMultiInputMIMEStorlet(StorletPythonFunctionalTest):
|
||||||
|
def setUp(self):
|
||||||
|
self.additional_headers = {}
|
||||||
|
super(TestMultiInputMIMEStorlet, self).setUp(
|
||||||
|
storlet_dir='multi_input',
|
||||||
|
storlet_name='multi_input_mime.py',
|
||||||
|
storlet_main='multi_input_mime.MultiInputMIMEStorlet',
|
||||||
|
storlet_file=None,
|
||||||
|
headers={})
|
||||||
|
|
||||||
|
def test_get_multipart_mime_response(self):
|
||||||
|
obj = 'small'
|
||||||
|
obj2 = 'small2'
|
||||||
|
body = '0123456789abcd'
|
||||||
|
body2 = 'efghijklmnopqr'
|
||||||
|
c.put_object(self.url, self.token,
|
||||||
|
self.container, obj, body)
|
||||||
|
c.put_object(self.url, self.token,
|
||||||
|
self.container, obj2, body2)
|
||||||
|
|
||||||
|
headers = {
|
||||||
|
'X-Run-Storlet': self.storlet_name,
|
||||||
|
'X-Storlet-Extra-Resources':
|
||||||
|
os.path.join('/' + self.container, obj2)
|
||||||
|
}
|
||||||
|
headers.update(self.additional_headers)
|
||||||
|
|
||||||
|
resp_headers, resp_content = c.get_object(
|
||||||
|
self.url, self.token, self.container, obj,
|
||||||
|
headers=headers)
|
||||||
|
|
||||||
|
multipart_prefix = 'multipart/mixed; boundary='
|
||||||
|
# N.B. swiftclient makes the header key as lower case
|
||||||
|
self.assertIn('content-type', resp_headers)
|
||||||
|
self.assertIn(
|
||||||
|
multipart_prefix, resp_headers['content-type'])
|
||||||
|
boundary = resp_headers['content-type'][len(multipart_prefix):]
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
'%s\n--%s\n%s\n--%s--' % (body, boundary, body2, boundary),
|
||||||
|
resp_content)
|
||||||
|
|
||||||
|
|
||||||
|
class TestMultiInputMIMEStorletOnProxy(TestMultiInputMIMEStorlet):
|
||||||
|
def setUp(self):
|
||||||
|
super(TestMultiInputMIMEStorletOnProxy, self).setUp()
|
||||||
|
self.additional_headers = {'X-Storlet-Run-On-Proxy': ''}
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
Loading…
Reference in New Issue