From 85c91a4e82e319cb17194b89be103542e286319b Mon Sep 17 00:00:00 2001 From: John Leach Date: Tue, 25 Mar 2014 11:48:13 +0000 Subject: [PATCH] Allow empty reseller prefixes in domain_remap Change-Id: I2013e4686fce1c06626ec974f8d721721e6ca6e7 --- swift/common/middleware/domain_remap.py | 25 ++++++++++--------- .../common/middleware/test_domain_remap.py | 8 ++++++ 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/swift/common/middleware/domain_remap.py b/swift/common/middleware/domain_remap.py index 898aca192b..cabd32aca7 100644 --- a/swift/common/middleware/domain_remap.py +++ b/swift/common/middleware/domain_remap.py @@ -98,18 +98,19 @@ class DomainRemapMiddleware(object): body='Bad domain in host header', content_type='text/plain') return resp(env, start_response) - if '_' not in account and '-' in account: - account = account.replace('-', '_', 1) - account_reseller_prefix = account.split('_', 1)[0].lower() - if account_reseller_prefix not in self.reseller_prefixes_lower: - # account prefix is not in config list. bail. - return self.app(env, start_response) - prefix_index = self.reseller_prefixes_lower.index( - account_reseller_prefix) - real_prefix = self.reseller_prefixes[prefix_index] - if not account.startswith(real_prefix): - account_suffix = account[len(real_prefix):] - account = real_prefix + account_suffix + if len(self.reseller_prefixes) > 0: + if '_' not in account and '-' in account: + account = account.replace('-', '_', 1) + account_reseller_prefix = account.split('_', 1)[0].lower() + if account_reseller_prefix not in self.reseller_prefixes_lower: + # account prefix is not in config list. bail. + return self.app(env, start_response) + prefix_index = self.reseller_prefixes_lower.index( + account_reseller_prefix) + real_prefix = self.reseller_prefixes[prefix_index] + if not account.startswith(real_prefix): + account_suffix = account[len(real_prefix):] + account = real_prefix + account_suffix path = env['PATH_INFO'].strip('/') new_path_parts = ['', self.path_root, account] if container: diff --git a/test/unit/common/middleware/test_domain_remap.py b/test/unit/common/middleware/test_domain_remap.py index 79192072d9..dd86da633f 100644 --- a/test/unit/common/middleware/test_domain_remap.py +++ b/test/unit/common/middleware/test_domain_remap.py @@ -130,6 +130,14 @@ class TestDomainRemap(unittest.TestCase): resp = self.app(req.environ, start_response) self.assertEquals(resp, '/test') + def test_domain_remap_configured_with_no_prefixes(self): + conf = {'reseller_prefixes': ''} + self.app = domain_remap.DomainRemapMiddleware(FakeApp(), conf) + req = Request.blank('/test', environ={'REQUEST_METHOD': 'GET'}, + headers={'Host': 'c.uuid.example.com'}) + resp = self.app(req.environ, start_response) + self.assertEquals(resp, '/v1/uuid/c/test') + if __name__ == '__main__': unittest.main()