From 38787d0fb5102e41b153e4629d2ef374a02965e9 Mon Sep 17 00:00:00 2001 From: Samuel Merritt Date: Tue, 26 May 2015 16:19:54 -0700 Subject: [PATCH] Remove simplejson from staticweb Since we're dropping Python 2.6 support, we can rely on stdlib's json and get rid of our dependency on simplejson. This lets us get rid of some redundant Unicode encoding. Before, we would take the container-listing response off the wire, JSON-deserialize it (str -> unicode), then pass each of several fields from each entry to get_valid_utf8_str(), which would encode it, (unicode -> str), decode it (str -> unicode), and then encode it again (unicode -> str) for good measure. The net effect was that each object's name would, in the proxy server, go str -> unicode -> str -> unicode -> str. By replacing simplejson with stdlib json, we get a guarantee that each container-listing entry's name, hash, content_type, and last_modified are unicodes, so we can stop worrying about them being valid UTF-8 or not. This takes an encode and decode out of the path, so we just have str -> unicode -> str. While it'd be ideal to avoid this, the first transform (str -> unicode) happens when we decode the container-listing response body (json.loads()), so there's no way out. Change-Id: I00aedf952d691a809c23025b89131ea0f02b6431 --- swift/common/middleware/staticweb.py | 18 +++++++------ test/unit/common/middleware/test_staticweb.py | 26 +------------------ 2 files changed, 11 insertions(+), 33 deletions(-) diff --git a/swift/common/middleware/staticweb.py b/swift/common/middleware/staticweb.py index 34b102ea53..d16b5ae3e7 100644 --- a/swift/common/middleware/staticweb.py +++ b/swift/common/middleware/staticweb.py @@ -117,10 +117,11 @@ Example usage of this middleware via ``swift``: import cgi +import json import time from swift.common.utils import human_readable, split_path, config_true_value, \ - json, quote, get_valid_utf8_str, register_swift_info + quote, register_swift_info from swift.common.wsgi import make_pre_authed_env, WSGIContext from swift.common.http import is_success, is_redirection, HTTP_NOT_FOUND from swift.common.swob import Response, HTTPMovedPermanently, HTTPNotFound @@ -289,7 +290,7 @@ class _StaticWebContext(WSGIContext): ' \n' for item in listing: if 'subdir' in item: - subdir = get_valid_utf8_str(item['subdir']) + subdir = item['subdir'].encode("utf-8") if prefix: subdir = subdir[len(prefix):] body += ' \n' \ @@ -300,13 +301,14 @@ class _StaticWebContext(WSGIContext): (quote(subdir), cgi.escape(subdir)) for item in listing: if 'name' in item: - name = get_valid_utf8_str(item['name']) + name = item['name'].encode("utf-8") if prefix: name = name[len(prefix):] - content_type = get_valid_utf8_str(item['content_type']) - bytes = get_valid_utf8_str(human_readable(item['bytes'])) - last_modified = (cgi.escape(item['last_modified']). - split('.')[0].replace('T', ' ')) + content_type = item['content_type'].encode("utf-8") + bytes = human_readable(item['bytes']) + last_modified = ( + cgi.escape(item['last_modified'].encode("utf-8")). + split('.')[0].replace('T', ' ')) body += ' \n' \ ' %s\n' \ ' %s\n' \ @@ -315,7 +317,7 @@ class _StaticWebContext(WSGIContext): (' '.join('type-' + cgi.escape(t.lower(), quote=True) for t in content_type.split('/')), quote(name), cgi.escape(name), - bytes, get_valid_utf8_str(last_modified)) + bytes, last_modified) body += ' \n' \ ' \n' \ '\n' diff --git a/test/unit/common/middleware/test_staticweb.py b/test/unit/common/middleware/test_staticweb.py index a5c61b1c31..fe361fde7a 100644 --- a/test/unit/common/middleware/test_staticweb.py +++ b/test/unit/common/middleware/test_staticweb.py @@ -13,15 +13,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -try: - import simplejson as json -except ImportError: - import json -import json as stdlib_json +import json import unittest -import mock - from swift.common.swob import Request, Response from swift.common.middleware import staticweb @@ -699,24 +693,6 @@ class TestStaticWeb(unittest.TestCase): self.assert_('listing.css' not in resp.body) self.assert_('