Browse Source

unicode support for http responses

Darren Ranalli 15 years ago
parent
commit
3d0a7ab83b
3 changed files with 31 additions and 17 deletions
  1. 6 6
      direct/src/http/LandingPage.py
  2. 7 7
      direct/src/http/WebRequest.py
  3. 18 4
      direct/src/showbase/PythonUtil.py

+ 6 - 6
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
         # </head>
         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 <body>
         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 <body>
         bodyStr = bodyTagStr[bodyTagStr.index('>')+1:]
         bodyTagStr = bodyTagStr[:bodyTagStr.index('>')+1]

+ 7 - 7
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 = "<html><body>Error 504: Request timed out</body></html>\r\n"

+ 18 - 4
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