Fix for UTF-8 encoding/quoting issues in staticweb

Also added ; charset=UTF-8 to the content-type.

Change-Id: I144a9fa2647b47663863b06636968beaa68a5d60
This commit is contained in:
gholt
2012-01-18 16:37:58 +00:00
parent e1597a0ae2
commit a7cc6e1cb9
2 changed files with 15 additions and 4 deletions

View File

@@ -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 = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 ' \
'Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">\n' \
'<html>\n' \

View File

@@ -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_('</style>' not in resp.body)
self.assert_('<link' in resp.body)
self.assert_('listing.css' in resp.body)
self.assertEquals(resp.headers['content-type'],
'text/html; charset=UTF-8')
def test_container4onetxt(self):
resp = Request.blank(