Fix s3bucketpath handling for IP based requests (#765)

* check HTTP header for IPv4 or IPv6 addresses and default to path based S3

* improved IPv4 and IPv6 checking with optional ports

* typo
This commit is contained in:
mfranke 2016-11-24 02:05:34 +01:00 committed by Steve Pulec
parent 09ca1c4388
commit 040c2cd8cc

View File

@ -4,6 +4,8 @@ import re
import six import six
from six.moves.urllib.parse import parse_qs, urlparse from six.moves.urllib.parse import parse_qs, urlparse
import socket
import xmltodict import xmltodict
from moto.core.responses import _TemplateEnvironmentMixin from moto.core.responses import _TemplateEnvironmentMixin
@ -44,10 +46,29 @@ class ResponseObject(_TemplateEnvironmentMixin):
def subdomain_based_buckets(self, request): def subdomain_based_buckets(self, request):
host = request.headers.get('host', request.headers.get('Host')) host = request.headers.get('host', request.headers.get('Host'))
if host.startswith("localhost"):
if not host or host.startswith("localhost"):
# For localhost, default to path-based buckets # For localhost, default to path-based buckets
return False return False
match = re.match(r'^([^\[\]:]+)(:\d+)?$', host)
if match:
try:
socket.inet_pton(socket.AF_INET, match.groups()[0])
# For IPv4, default to path-based buckets
return False
except socket.error:
pass
match = re.match(r'^\[(.+)\](:\d+)?$', host)
if match:
try:
socket.inet_pton(socket.AF_INET6, match.groups()[0])
# For IPv6, default to path-based buckets
return False
except socket.error:
pass
path_based = (host == 's3.amazonaws.com' or re.match(r"s3[\.\-]([^.]*)\.amazonaws\.com", host)) path_based = (host == 's3.amazonaws.com' or re.match(r"s3[\.\-]([^.]*)\.amazonaws\.com", host))
return not path_based return not path_based