Refactor backend parsing.
This commit is contained in:
parent
e3eb4d1809
commit
4dd2b66b04
@ -58,8 +58,6 @@ class DomainDispatcherApplication(object):
|
|||||||
if re.match(url_base, 'http://%s' % host):
|
if re.match(url_base, 'http://%s' % host):
|
||||||
return backend_name
|
return backend_name
|
||||||
|
|
||||||
raise RuntimeError('Invalid host: "%s"' % host)
|
|
||||||
|
|
||||||
def infer_service_region(self, environ):
|
def infer_service_region(self, environ):
|
||||||
auth = environ.get('HTTP_AUTHORIZATION')
|
auth = environ.get('HTTP_AUTHORIZATION')
|
||||||
if auth:
|
if auth:
|
||||||
@ -95,14 +93,16 @@ class DomainDispatcherApplication(object):
|
|||||||
if six.PY3 and isinstance(path_info, six.binary_type):
|
if six.PY3 and isinstance(path_info, six.binary_type):
|
||||||
path_info = path_info.decode('utf-8')
|
path_info = path_info.decode('utf-8')
|
||||||
|
|
||||||
host = None
|
|
||||||
if path_info.startswith("/moto-api") or path_info == "/favicon.ico":
|
if path_info.startswith("/moto-api") or path_info == "/favicon.ico":
|
||||||
host = "moto_api"
|
host = "moto_api"
|
||||||
elif path_info.startswith("/latest/meta-data/"):
|
elif path_info.startswith("/latest/meta-data/"):
|
||||||
host = "instance_metadata"
|
host = "instance_metadata"
|
||||||
else:
|
else:
|
||||||
host = environ['HTTP_HOST'].split(':')[0]
|
host = environ['HTTP_HOST'].split(':')[0]
|
||||||
if host is None:
|
|
||||||
|
with self.lock:
|
||||||
|
backend = self.get_backend_for_host(host)
|
||||||
|
if not backend:
|
||||||
service, region = self.infer_service_region(environ)
|
service, region = self.infer_service_region(environ)
|
||||||
if service == 'dynamodb':
|
if service == 'dynamodb':
|
||||||
if environ['HTTP_X_AMZ_TARGET'].startswith('DynamoDBStreams'):
|
if environ['HTTP_X_AMZ_TARGET'].startswith('DynamoDBStreams'):
|
||||||
@ -115,9 +115,10 @@ class DomainDispatcherApplication(object):
|
|||||||
else:
|
else:
|
||||||
host = "{service}.{region}.amazonaws.com".format(
|
host = "{service}.{region}.amazonaws.com".format(
|
||||||
service=service, region=region)
|
service=service, region=region)
|
||||||
|
|
||||||
with self.lock:
|
|
||||||
backend = self.get_backend_for_host(host)
|
backend = self.get_backend_for_host(host)
|
||||||
|
if not backend:
|
||||||
|
raise RuntimeError('Invalid host: "%s"' % host)
|
||||||
|
|
||||||
app = self.app_instances.get(backend, None)
|
app = self.app_instances.get(backend, None)
|
||||||
if app is None:
|
if app is None:
|
||||||
app = self.create_app(backend)
|
app = self.create_app(backend)
|
||||||
|
@ -38,12 +38,6 @@ def test_domain_dispatched():
|
|||||||
keys[0].should.equal('EmailResponse.dispatch')
|
keys[0].should.equal('EmailResponse.dispatch')
|
||||||
|
|
||||||
|
|
||||||
def test_domain_without_matches():
|
|
||||||
dispatcher = DomainDispatcherApplication(create_backend_app)
|
|
||||||
dispatcher.get_application.when.called_with(
|
|
||||||
{"HTTP_HOST": "not-matching-anything.com"}).should.throw(RuntimeError)
|
|
||||||
|
|
||||||
|
|
||||||
def test_domain_dispatched_with_service():
|
def test_domain_dispatched_with_service():
|
||||||
# If we pass a particular service, always return that.
|
# If we pass a particular service, always return that.
|
||||||
dispatcher = DomainDispatcherApplication(create_backend_app, service="s3")
|
dispatcher = DomainDispatcherApplication(create_backend_app, service="s3")
|
||||||
|
Loading…
Reference in New Issue
Block a user