From 24d1562d2fff60963b3497792634e33c08acf1d8 Mon Sep 17 00:00:00 2001 From: Waldemar Hummer Date: Sat, 5 Aug 2017 20:29:40 +1000 Subject: [PATCH] allow non-ascii characters in request URLs --- moto/server.py | 8 ++++++++ tests/test_s3/test_server.py | 17 +++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/moto/server.py b/moto/server.py index be41f1ed0..8d0103cc2 100644 --- a/moto/server.py +++ b/moto/server.py @@ -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/"): diff --git a/tests/test_s3/test_server.py b/tests/test_s3/test_server.py index 5353ec209..c3ca3c3ff 100644 --- a/tests/test_s3/test_server.py +++ b/tests/test_s3/test_server.py @@ -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