mirror of
https://github.com/panda3d/panda3d.git
synced 2025-09-30 16:58:40 -04:00
unicode support for http responses
This commit is contained in:
parent
382bd8296b
commit
3d0a7ab83b
@ -62,8 +62,8 @@ class LandingPage:
|
|||||||
bodyTag.append(ET.Comment(''))
|
bodyTag.append(ET.Comment(''))
|
||||||
|
|
||||||
fileStr = StringIO()
|
fileStr = StringIO()
|
||||||
ET.ElementTree(headTag).write(fileStr)
|
ET.ElementTree(headTag).write(fileStr, encoding='utf-8')
|
||||||
headTagStr = fileStr.getvalue()
|
headTagStr = unicodeUtf8(fileStr.getvalue())
|
||||||
# remove the tag closer
|
# remove the tag closer
|
||||||
# </head>
|
# </head>
|
||||||
headTagStr = headTagStr[:headTagStr.rindex('<')]
|
headTagStr = headTagStr[:headTagStr.rindex('<')]
|
||||||
@ -74,8 +74,8 @@ class LandingPage:
|
|||||||
LandingPageHTML.addBodyHeaderAndContent(landing, titleStr, self.getMenuTags(activeTab))
|
LandingPageHTML.addBodyHeaderAndContent(landing, titleStr, self.getMenuTags(activeTab))
|
||||||
|
|
||||||
fileStr = StringIO()
|
fileStr = StringIO()
|
||||||
ET.ElementTree(landing).write(fileStr)
|
ET.ElementTree(landing).write(fileStr, encoding='utf-8')
|
||||||
landingStr = fileStr.getvalue()
|
landingStr = unicodeUtf8(fileStr.getvalue())
|
||||||
# remove <body>
|
# remove <body>
|
||||||
landingStr = landingStr[landingStr.index('>')+1:]
|
landingStr = landingStr[landingStr.index('>')+1:]
|
||||||
# remove tag closers
|
# remove tag closers
|
||||||
@ -86,8 +86,8 @@ class LandingPage:
|
|||||||
landingStr = landingStr[:landingStr.rindex('<')]
|
landingStr = landingStr[:landingStr.rindex('<')]
|
||||||
|
|
||||||
fileStr = StringIO()
|
fileStr = StringIO()
|
||||||
ET.ElementTree(bodyTag).write(fileStr)
|
ET.ElementTree(bodyTag).write(fileStr, encoding='utf-8')
|
||||||
bodyTagStr = fileStr.getvalue()
|
bodyTagStr = unicodeUtf8(fileStr.getvalue())
|
||||||
# extract <body>
|
# extract <body>
|
||||||
bodyStr = bodyTagStr[bodyTagStr.index('>')+1:]
|
bodyStr = bodyTagStr[bodyTagStr.index('>')+1:]
|
||||||
bodyTagStr = bodyTagStr[:bodyTagStr.index('>')+1]
|
bodyTagStr = bodyTagStr[:bodyTagStr.index('>')+1]
|
||||||
|
@ -34,24 +34,24 @@ class WebRequest(object):
|
|||||||
|
|
||||||
def respondHTTP(self,status,body):
|
def respondHTTP(self,status,body):
|
||||||
status = str(status)
|
status = str(status)
|
||||||
msg = "HTTP/1.0 %s\r\nContent-Type: text/html\r\n\r\n%s" % (status,body)
|
msg = u"HTTP/1.0 %s\r\nContent-Type: text/html\r\n\r\n%s" % (status,body)
|
||||||
self.connection.SendThisResponse(msg)
|
self.connection.SendThisResponse(encodedUtf8(msg))
|
||||||
|
|
||||||
def respond(self,body):
|
def respond(self,body):
|
||||||
self.respondHTTP("200 OK",body)
|
self.respondHTTP("200 OK",body)
|
||||||
|
|
||||||
def respondXML(self,body):
|
def respondXML(self,body):
|
||||||
msg = "HTTP/1.0 200 OK\r\nContent-Type: text/xml\r\n\r\n%s" % body
|
msg = u"HTTP/1.0 200 OK\r\nContent-Type: text/xml\r\n\r\n%s" % body
|
||||||
self.connection.SendThisResponse(msg)
|
self.connection.SendThisResponse(encodedUtf8(msg))
|
||||||
|
|
||||||
def respondCustom(self,contentType,body):
|
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",]:
|
if contentType in ["text/css",]:
|
||||||
msg += "\nCache-Control: max-age=313977290\nExpires: Tue, 02 May 2017 04:08:44 GMT\n"
|
msg += "\nCache-Control: max-age=313977290\nExpires: Tue, 02 May 2017 04:08:44 GMT\n"
|
||||||
|
|
||||||
msg += "\r\n\r\n%s" % (body)
|
msg += u"\r\n\r\n%s" % (body)
|
||||||
self.connection.SendThisResponse(msg)
|
self.connection.SendThisResponse(encodedUtf8(msg))
|
||||||
|
|
||||||
def timeout(self):
|
def timeout(self):
|
||||||
resp = "<html><body>Error 504: Request timed out</body></html>\r\n"
|
resp = "<html><body>Error 504: Request timed out</body></html>\r\n"
|
||||||
|
@ -4324,11 +4324,23 @@ def bpdbGetEnabled():
|
|||||||
bpdb.setEnabledCallback(bpdbGetEnabled)
|
bpdb.setEnabledCallback(bpdbGetEnabled)
|
||||||
bpdb.setConfigCallback(lambda cfg: ConfigVariableBool('want-bp-%s' % (cfg.lower(),), 0).getValue())
|
bpdb.setConfigCallback(lambda cfg: ConfigVariableBool('want-bp-%s' % (cfg.lower(),), 0).getValue())
|
||||||
|
|
||||||
def u2ascii(str):
|
def u2ascii(s):
|
||||||
if type(str) is types.UnicodeType:
|
# Unicode -> ASCII
|
||||||
return unicodedata.normalize('NFKD', str).encode('ascii','ignore')
|
if type(s) is types.UnicodeType:
|
||||||
|
return unicodedata.normalize('NFKD', s).encode('ascii', 'backslashreplace')
|
||||||
else:
|
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__
|
import __builtin__
|
||||||
__builtin__.Functor = Functor
|
__builtin__.Functor = Functor
|
||||||
@ -4392,3 +4404,5 @@ __builtin__.histogramDict = histogramDict
|
|||||||
__builtin__.repeatableRepr = repeatableRepr
|
__builtin__.repeatableRepr = repeatableRepr
|
||||||
__builtin__.bpdb = bpdb
|
__builtin__.bpdb = bpdb
|
||||||
__builtin__.u2ascii = u2ascii
|
__builtin__.u2ascii = u2ascii
|
||||||
|
__builtin__.unicodeUtf8 = unicodeUtf8
|
||||||
|
__builtin__.encodedUtf8 = encodedUtf8
|
||||||
|
Loading…
x
Reference in New Issue
Block a user