Ensure text/* content types always include a charset declaration

This commit is contained in:
Kovid Goyal 2015-05-22 11:52:44 +05:30
parent 03347846d9
commit 439ddab5e5
2 changed files with 7 additions and 4 deletions

View File

@ -586,6 +586,10 @@ class HTTPPair(object):
if not self.close_connection: if not self.close_connection:
self.outheaders.set('Connection', 'Keep-Alive') self.outheaders.set('Connection', 'Keep-Alive')
ct = self.outheaders.get('Content-Type', '')
if ct.startswith('text/') and 'charset=' not in ct:
self.outheaders.set('Content-Type', ct + '; charset=UTF-8')
buf = [HTTP11 + (' %d ' % self.status_code) + httplib.responses[self.status_code]] buf = [HTTP11 + (' %d ' % self.status_code) + httplib.responses[self.status_code]]
for header, value in sorted(self.outheaders.iteritems(), key=itemgetter(0)): for header, value in sorted(self.outheaders.iteritems(), key=itemgetter(0)):
buf.append('%s: %s' % (header, value)) buf.append('%s: %s' % (header, value))

View File

@ -210,10 +210,9 @@ class DynamicOutput(object):
self.data = output self.data = output
else: else:
self.data = output.encode('utf-8') self.data = output.encode('utf-8')
ct = outheaders.get('Content-Type', 'text/plain') ct = outheaders.get('Content-Type')
if 'charset=' not in ct: if not ct:
ct += '; charset=UTF-8' outheaders.set('Content-Type', 'text/plain; charset=UTF-8', replace_all=True)
outheaders.set('Content-Type', ct, replace_all=True)
self.content_length = len(self.data) self.content_length = len(self.data)
self.etag = None self.etag = None
self.accept_ranges = False self.accept_ranges = False