From a7cc6e1cb9909aa68db70e6c11e395526ca4fb7d Mon Sep 17 00:00:00 2001 From: gholt Date: Wed, 18 Jan 2012 16:37:58 +0000 Subject: [PATCH] Fix for UTF-8 encoding/quoting issues in staticweb Also added ; charset=UTF-8 to the content-type. Change-Id: I144a9fa2647b47663863b06636968beaa68a5d60 --- swift/common/middleware/staticweb.py | 13 +++++++++++-- test/unit/common/middleware/test_staticweb.py | 6 ++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/swift/common/middleware/staticweb.py b/swift/common/middleware/staticweb.py index 8465aa6815..bd799a8743 100644 --- a/swift/common/middleware/staticweb.py +++ b/swift/common/middleware/staticweb.py @@ -116,7 +116,7 @@ except ImportError: import cgi import time -from urllib import unquote, quote +from urllib import unquote, quote as urllib_quote from webob import Response, Request from webob.exc import HTTPMovedPermanently, HTTPNotFound @@ -125,6 +125,15 @@ from swift.common.utils import cache_from_env, get_logger, human_readable, \ split_path, TRUE_VALUES +def quote(value, safe='/'): + """ + Patched version of urllib.quote that encodes utf-8 strings before quoting + """ + if isinstance(value, unicode): + value = value.encode('utf-8') + return urllib_quote(value, safe) + + class StaticWeb(object): """ The Static Web WSGI middleware filter; serves container data as a static @@ -289,7 +298,7 @@ class StaticWeb(object): if not listing: resp = HTTPNotFound()(env, self._start_response) return self._error_response(resp, env, start_response) - headers = {'Content-Type': 'text/html'} + headers = {'Content-Type': 'text/html; charset=UTF-8'} body = '\n' \ '\n' \ diff --git a/test/unit/common/middleware/test_staticweb.py b/test/unit/common/middleware/test_staticweb.py index 960496e926..bae185d925 100644 --- a/test/unit/common/middleware/test_staticweb.py +++ b/test/unit/common/middleware/test_staticweb.py @@ -269,7 +269,7 @@ class FakeApp(object): "hash":"c85c1dcd19cf5cbac84e6043c31bb63e", "bytes":20, "content_type":"text/plain", "last_modified":"2011-03-24T04:27:52.734140"}, - {"name":"subdir/omgomg.txt", + {"name":"subdir/\u2603.txt", "hash":"7337d028c093130898d937c319cc9865", "bytes":72981, "content_type":"text/plain", "last_modified":"2011-03-24T04:27:52.735460"}, @@ -290,7 +290,7 @@ class FakeApp(object): 'Content-Type': 'text/plain; charset=utf-8'}) body = '\n'.join(['401error.html', '404error.html', 'index.html', 'listing.css', 'one.txt', 'subdir/1.txt', - 'subdir/2.txt', 'subdir/omgomg.txt', 'subdir2', + 'subdir/2.txt', u'subdir/\u2603.txt', 'subdir2', 'subdir3/subsubdir/index.html', 'two.txt']) return Response(status='200 Ok', headers=headers, body=body)(env, start_response) @@ -480,6 +480,8 @@ class TestStaticWeb(unittest.TestCase): self.assert_('' not in resp.body) self.assert_('