From 439ddab5e5be615bf8d8074bbe4ec4a563413a0c Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 22 May 2015 11:52:44 +0530 Subject: [PATCH] Ensure text/* content types always include a charset declaration --- src/calibre/srv/http.py | 4 ++++ src/calibre/srv/respond.py | 7 +++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/calibre/srv/http.py b/src/calibre/srv/http.py index a2e50658cb..7f90ae55f8 100644 --- a/src/calibre/srv/http.py +++ b/src/calibre/srv/http.py @@ -586,6 +586,10 @@ class HTTPPair(object): if not self.close_connection: 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]] for header, value in sorted(self.outheaders.iteritems(), key=itemgetter(0)): buf.append('%s: %s' % (header, value)) diff --git a/src/calibre/srv/respond.py b/src/calibre/srv/respond.py index 9c16879d3f..d48ea94e9a 100644 --- a/src/calibre/srv/respond.py +++ b/src/calibre/srv/respond.py @@ -210,10 +210,9 @@ class DynamicOutput(object): self.data = output else: self.data = output.encode('utf-8') - ct = outheaders.get('Content-Type', 'text/plain') - if 'charset=' not in ct: - ct += '; charset=UTF-8' - outheaders.set('Content-Type', ct, replace_all=True) + ct = outheaders.get('Content-Type') + if not ct: + outheaders.set('Content-Type', 'text/plain; charset=UTF-8', replace_all=True) self.content_length = len(self.data) self.etag = None self.accept_ranges = False