From 979033a14ebc06d3f360c8a17542c68efb30a032 Mon Sep 17 00:00:00 2001 From: Samuel Merritt Date: Thu, 12 Dec 2013 16:13:42 -0800 Subject: [PATCH] Expose allowed tempurl methods in /info Clients can construct tempurls for any method, but they only work if they're in this list, so it's helpful for clients to see the list. Change-Id: Id852f457d65b62c4fe79db01b1d7029a5fa5aa09 --- swift/common/middleware/tempurl.py | 20 +++++++++++++---- test/unit/common/middleware/test_tempurl.py | 24 +++++++++++++++++++-- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/swift/common/middleware/tempurl.py b/swift/common/middleware/tempurl.py index 2b91ee8815..c9b9d9437d 100644 --- a/swift/common/middleware/tempurl.py +++ b/swift/common/middleware/tempurl.py @@ -80,6 +80,15 @@ above example:: https://swift-cluster.example.com/v1/AUTH_account/container/object? temp_url_sig=da39a3ee5e6b4b0d3255bfef95601890afd80709& temp_url_expires=1323479485&filename=My+Test+File.pdf + +If you do not want the object to be downloaded, you can cause +"Content-Disposition: inline" to be set on the response by adding the "inline" +parameter to the query string, like so:: + + https://swift-cluster.example.com/v1/AUTH_account/container/object? + temp_url_sig=da39a3ee5e6b4b0d3255bfef95601890afd80709& + temp_url_expires=1323479485&inline + """ __all__ = ['TempURL', 'filter_factory', @@ -183,14 +192,14 @@ class TempURL(object): :param conf: The configuration dict for the middleware. """ - def __init__(self, app, conf): + def __init__(self, app, conf, methods=('GET', 'HEAD', 'PUT')): #: The next WSGI application/filter in the paste.deploy pipeline. self.app = app #: The filter configuration dict. self.conf = conf #: The methods allowed with Temp URLs. - self.methods = conf.get('methods', 'GET HEAD PUT').split() + self.methods = methods headers = DEFAULT_INCOMING_REMOVE_HEADERS if 'incoming_remove_headers' in conf: @@ -474,5 +483,8 @@ def filter_factory(global_conf, **local_conf): """Returns the WSGI filter for use with paste.deploy.""" conf = global_conf.copy() conf.update(local_conf) - register_swift_info('tempurl') - return lambda app: TempURL(app, conf) + + methods = conf.get('methods', 'GET HEAD PUT').split() + register_swift_info('tempurl', methods=methods) + + return lambda app: TempURL(app, conf, methods=methods) diff --git a/test/unit/common/middleware/test_tempurl.py b/test/unit/common/middleware/test_tempurl.py index c2a9e1c27a..157186aef4 100644 --- a/test/unit/common/middleware/test_tempurl.py +++ b/test/unit/common/middleware/test_tempurl.py @@ -20,7 +20,7 @@ from time import time from swift.common.middleware import tempauth, tempurl from swift.common.swob import Request, Response, HeaderKeyDict -from swift.common.utils import split_path +from swift.common import utils class FakeApp(object): @@ -59,7 +59,7 @@ class TestTempURL(unittest.TestCase): if environ is None: environ = {} - _junk, account, _junk, _junk = split_path(path, 2, 4) + _junk, account, _junk, _junk = utils.split_path(path, 2, 4) self._fake_cache_environ(environ, account, keys) req = Request.blank(path, environ=environ, **kwargs) return req @@ -869,5 +869,25 @@ class TestTempURL(unittest.TestCase): self.assertTrue(isinstance(str_value, str)) +class TestSwiftInfo(unittest.TestCase): + def setUp(self): + utils._swift_info = {} + utils._swift_admin_info = {} + + def test_registered_defaults(self): + tempurl.filter_factory({}) + swift_info = utils.get_swift_info() + self.assertTrue('tempurl' in swift_info) + self.assertEqual(set(swift_info['tempurl']['methods']), + set(('GET', 'HEAD', 'PUT'))) + + def test_non_default_methods(self): + tempurl.filter_factory({'methods': 'GET HEAD PUT POST DELETE'}) + swift_info = utils.get_swift_info() + self.assertTrue('tempurl' in swift_info) + self.assertEqual(set(swift_info['tempurl']['methods']), + set(('GET', 'HEAD', 'PUT', 'POST', 'DELETE'))) + + if __name__ == '__main__': unittest.main()