diff --git a/direct/src/http/LandingPage.py b/direct/src/http/LandingPage.py index 47092eb21d..927994d326 100755 --- a/direct/src/http/LandingPage.py +++ b/direct/src/http/LandingPage.py @@ -62,8 +62,8 @@ class LandingPage: bodyTag.append(ET.Comment('')) fileStr = StringIO() - ET.ElementTree(headTag).write(fileStr) - headTagStr = fileStr.getvalue() + ET.ElementTree(headTag).write(fileStr, encoding='utf-8') + headTagStr = unicodeUtf8(fileStr.getvalue()) # remove the tag closer # headTagStr = headTagStr[:headTagStr.rindex('<')] @@ -74,8 +74,8 @@ class LandingPage: LandingPageHTML.addBodyHeaderAndContent(landing, titleStr, self.getMenuTags(activeTab)) fileStr = StringIO() - ET.ElementTree(landing).write(fileStr) - landingStr = fileStr.getvalue() + ET.ElementTree(landing).write(fileStr, encoding='utf-8') + landingStr = unicodeUtf8(fileStr.getvalue()) # remove landingStr = landingStr[landingStr.index('>')+1:] # remove tag closers @@ -86,8 +86,8 @@ class LandingPage: landingStr = landingStr[:landingStr.rindex('<')] fileStr = StringIO() - ET.ElementTree(bodyTag).write(fileStr) - bodyTagStr = fileStr.getvalue() + ET.ElementTree(bodyTag).write(fileStr, encoding='utf-8') + bodyTagStr = unicodeUtf8(fileStr.getvalue()) # extract bodyStr = bodyTagStr[bodyTagStr.index('>')+1:] bodyTagStr = bodyTagStr[:bodyTagStr.index('>')+1] diff --git a/direct/src/http/WebRequest.py b/direct/src/http/WebRequest.py index 8f75ec2b91..ea8094ddd3 100755 --- a/direct/src/http/WebRequest.py +++ b/direct/src/http/WebRequest.py @@ -34,24 +34,24 @@ class WebRequest(object): def respondHTTP(self,status,body): status = str(status) - msg = "HTTP/1.0 %s\r\nContent-Type: text/html\r\n\r\n%s" % (status,body) - self.connection.SendThisResponse(msg) + msg = u"HTTP/1.0 %s\r\nContent-Type: text/html\r\n\r\n%s" % (status,body) + self.connection.SendThisResponse(encodedUtf8(msg)) def respond(self,body): self.respondHTTP("200 OK",body) def respondXML(self,body): - msg = "HTTP/1.0 200 OK\r\nContent-Type: text/xml\r\n\r\n%s" % body - self.connection.SendThisResponse(msg) + msg = u"HTTP/1.0 200 OK\r\nContent-Type: text/xml\r\n\r\n%s" % body + self.connection.SendThisResponse(encodedUtf8(msg)) def respondCustom(self,contentType,body): - msg = "HTTP/1.0 200 OK\r\nContent-Type: %s" % contentType + msg = u"HTTP/1.0 200 OK\r\nContent-Type: %s" % contentType if contentType in ["text/css",]: msg += "\nCache-Control: max-age=313977290\nExpires: Tue, 02 May 2017 04:08:44 GMT\n" - msg += "\r\n\r\n%s" % (body) - self.connection.SendThisResponse(msg) + msg += u"\r\n\r\n%s" % (body) + self.connection.SendThisResponse(encodedUtf8(msg)) def timeout(self): resp = "Error 504: Request timed out\r\n" diff --git a/direct/src/showbase/PythonUtil.py b/direct/src/showbase/PythonUtil.py index b221dbf208..fd8ce385fa 100644 --- a/direct/src/showbase/PythonUtil.py +++ b/direct/src/showbase/PythonUtil.py @@ -4324,11 +4324,23 @@ def bpdbGetEnabled(): bpdb.setEnabledCallback(bpdbGetEnabled) bpdb.setConfigCallback(lambda cfg: ConfigVariableBool('want-bp-%s' % (cfg.lower(),), 0).getValue()) -def u2ascii(str): - if type(str) is types.UnicodeType: - return unicodedata.normalize('NFKD', str).encode('ascii','ignore') +def u2ascii(s): + # Unicode -> ASCII + if type(s) is types.UnicodeType: + return unicodedata.normalize('NFKD', s).encode('ascii', 'backslashreplace') else: - return str + return str(s) + +def unicodeUtf8(s): + # * -> Unicode UTF-8 + if type(s) is types.UnicodeType: + return s + else: + return unicode(str(s), 'utf-8') + +def encodedUtf8(s): + # * -> 8-bit-encoded UTF-8 + return unicodeUtf8(s).encode('utf-8') import __builtin__ __builtin__.Functor = Functor @@ -4392,3 +4404,5 @@ __builtin__.histogramDict = histogramDict __builtin__.repeatableRepr = repeatableRepr __builtin__.bpdb = bpdb __builtin__.u2ascii = u2ascii +__builtin__.unicodeUtf8 = unicodeUtf8 +__builtin__.encodedUtf8 = encodedUtf8