merged with gholt's changes
This commit is contained in:
commit
2d82d1d543
@ -21,10 +21,10 @@ class DomainRemapMiddleware(object):
|
||||
"""
|
||||
Middleware that translates container and account parts of a domain to
|
||||
path parameters that the proxy server understands.
|
||||
|
||||
|
||||
container.account.storageurl/object gets translated to
|
||||
container.account.storageurl/path_root/account/container/object
|
||||
|
||||
|
||||
account.storageurl/path_root/container/object gets translated to
|
||||
account.storageurl/path_root/account/container/object
|
||||
"""
|
||||
@ -32,15 +32,18 @@ class DomainRemapMiddleware(object):
|
||||
def __init__(self, app, conf):
|
||||
self.app = app
|
||||
self.storage_domain = conf.get('storage_domain', 'example.com')
|
||||
if self.storage_domain and self.storage_domain[0] != '.':
|
||||
self.storage_domain = '.' + self.storage_domain
|
||||
self.path_root = conf.get('path_root', 'v1').strip('/')
|
||||
|
||||
def __call__(self, env, start_response):
|
||||
if not self.storage_domain:
|
||||
return self.app(env, start_response)
|
||||
given_domain = env['HTTP_HOST']
|
||||
port = ''
|
||||
if ':' in given_domain:
|
||||
given_domain, port = given_domain.rsplit(':', 1)
|
||||
if given_domain != self.storage_domain and \
|
||||
given_domain.endswith(self.storage_domain):
|
||||
if given_domain.endswith(self.storage_domain):
|
||||
parts_to_parse = given_domain[:-len(self.storage_domain)]
|
||||
parts_to_parse = parts_to_parse.strip('.').split('.')
|
||||
len_parts_to_parse = len(parts_to_parse)
|
||||
|
@ -19,13 +19,17 @@ from webob import Request
|
||||
|
||||
from swift.common.middleware import domain_remap
|
||||
|
||||
|
||||
class FakeApp(object):
|
||||
|
||||
def __call__(self, env, start_response):
|
||||
return env['PATH_INFO']
|
||||
|
||||
|
||||
def start_response(*args):
|
||||
pass
|
||||
|
||||
|
||||
class TestDomainRemap(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
@ -87,5 +91,20 @@ class TestDomainRemap(unittest.TestCase):
|
||||
resp = self.app(req.environ, start_response)
|
||||
self.assertEquals(resp, '/v1/a/c/obj')
|
||||
|
||||
def test_domain_remap_account_matching_ending_not_domain(self):
|
||||
req = Request.blank('/dontchange', environ={'REQUEST_METHOD': 'GET'},
|
||||
headers={'Host': 'c.aexample.com'})
|
||||
resp = self.app(req.environ, start_response)
|
||||
self.assertEquals(resp, '/dontchange')
|
||||
|
||||
def test_domain_remap_configured_with_empty_storage_domain(self):
|
||||
self.app = domain_remap.DomainRemapMiddleware(FakeApp(),
|
||||
{'storage_domain': ''})
|
||||
req = Request.blank('/test', environ={'REQUEST_METHOD': 'GET'},
|
||||
headers={'Host': 'c.a.example.com'})
|
||||
resp = self.app(req.environ, start_response)
|
||||
self.assertEquals(resp, '/test')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
unittest.main()
|
||||
|
Loading…
x
Reference in New Issue
Block a user