Merge pull request #1035 from whummer/fix/py2-s3-names-unicode

Allow non-ascii characters in request URLs
This commit is contained in:
Jack Danger 2017-08-05 21:07:33 -07:00 committed by GitHub
commit 38dfcca269
2 changed files with 25 additions and 0 deletions

View File

@ -3,6 +3,7 @@ import json
import re
import sys
import argparse
import six
from six.moves.urllib.parse import urlencode
@ -47,6 +48,13 @@ class DomainDispatcherApplication(object):
def get_application(self, environ):
path_info = environ.get('PATH_INFO', '')
# The URL path might contain non-ASCII text, for instance unicode S3 bucket names
if six.PY2 and isinstance(path_info, str):
path_info = six.u(path_info)
if six.PY3 and isinstance(path_info, six.binary_type):
path_info = path_info.decode('utf-8')
if path_info.startswith("/moto-api") or path_info == "/favicon.ico":
host = "moto_api"
elif path_info.startswith("/latest/meta-data/"):

View File

@ -1,3 +1,5 @@
# coding=utf-8
from __future__ import unicode_literals
import sure # noqa
@ -78,3 +80,18 @@ def test_s3_server_post_without_content_length():
res = test_client.post('/', "https://tester.localhost:5000/", environ_overrides={'CONTENT_LENGTH': ''})
res.status_code.should.equal(411)
def test_s3_server_post_unicode_bucket_key():
# Make sure that we can deal with non-ascii characters in request URLs (e.g., S3 object names)
dispatcher = server.DomainDispatcherApplication(server.create_backend_app)
backend_app = dispatcher.get_application({
'HTTP_HOST': 's3.amazonaws.com',
'PATH_INFO': '/test-bucket/test-object-てすと'
})
assert backend_app
backend_app = dispatcher.get_application({
'HTTP_HOST': 's3.amazonaws.com',
'PATH_INFO': '/test-bucket/test-object-てすと'.encode('utf-8')
})
assert backend_app