From 440016aa3a534e5924ddf2c524d43ebfd1b5d068 Mon Sep 17 00:00:00 2001
From: Bob Mottram <bob@freedombone.net>
Date: Sat, 9 Nov 2019 12:33:48 +0000
Subject: [PATCH] Use ensure_ascii when encoding to utf8

---
 daemon.py | 36 ++++++++++++++++++------------------
 posts.py  |  2 +-
 tests.py  |  4 ++--
 3 files changed, 21 insertions(+), 21 deletions(-)

diff --git a/daemon.py b/daemon.py
index f1b63f430..dd9b4541e 100644
--- a/daemon.py
+++ b/daemon.py
@@ -341,7 +341,7 @@ class PubServer(BaseHTTPRequestHandler):
             print('DEBUG: WEBFINGER lookup '+self.path+' '+str(self.server.baseDir))
         wfResult=webfingerLookup(self.path,self.server.baseDir,self.server.port,self.server.debug)
         if wfResult:
-            msg=json.dumps(wfResult).encode('utf-8')
+            msg=json.dumps(wfResult, ensure_ascii=False).encode('utf-8')
             self._set_headers('application/jrd+json; charset=utf-8',len(msg),None)
             self._write(msg)
         else:
@@ -1666,7 +1666,7 @@ class PubServer(BaseHTTPRequestHandler):
                                     self._write(msg)
                                 else:
                                     if self._fetchAuthenticated():
-                                        msg=json.dumps(postJsonObject).encode('utf-8')
+                                        msg=json.dumps(postJsonObject, ensure_ascii=False).encode('utf-8')
                                         self._set_headers('application/json; charset=utf-8',len(msg),None)
                                         self._write(msg)
                                     else:
@@ -1733,7 +1733,7 @@ class PubServer(BaseHTTPRequestHandler):
                                         self._write(msg)
                                     else:
                                         if self._fetchAuthenticated():
-                                            msg=json.dumps(repliesJson).encode('utf-8')
+                                            msg=json.dumps(repliesJson, ensure_ascii=False).encode('utf-8')
                                             self._set_headers('application/json; charset=utf-8',len(msg),None)
                                             self._write(msg)
                                         else:
@@ -1782,7 +1782,7 @@ class PubServer(BaseHTTPRequestHandler):
                                         self._write(msg)
                                     else:
                                         if self._fetchAuthenticated():
-                                            msg=json.dumps(repliesJson).encode('utf-8')
+                                            msg=json.dumps(repliesJson, ensure_ascii=False).encode('utf-8')
                                             self._set_headers('application/json; charset=utf-8',len(msg),None)
                                             self._write(msg)
                                         else:
@@ -1824,7 +1824,7 @@ class PubServer(BaseHTTPRequestHandler):
                                     self._write(msg)
                             else:
                                 if self._fetchAuthenticated():
-                                    msg=json.dumps(actorJson['roles']).encode('utf-8')
+                                    msg=json.dumps(actorJson['roles'], ensure_ascii=False).encode('utf-8')
                                     self._set_headers('application/json; charset=utf-8',len(msg),None)
                                     self._write(msg)
                                 else:
@@ -1867,7 +1867,7 @@ class PubServer(BaseHTTPRequestHandler):
                                     self._write(msg)
                             else:
                                 if self._fetchAuthenticated():
-                                    msg=json.dumps(actorJson['skills']).encode('utf-8')
+                                    msg=json.dumps(actorJson['skills'], ensure_ascii=False).encode('utf-8')
                                     self._set_headers('application/json; charset=utf-8',len(msg),None)
                                     self._write(msg)
                                 else:
@@ -1923,7 +1923,7 @@ class PubServer(BaseHTTPRequestHandler):
                                     self._write(msg)
                                 else:
                                     if self._fetchAuthenticated():
-                                        msg=json.dumps(postJsonObject).encode('utf-8')
+                                        msg=json.dumps(postJsonObject, ensure_ascii=False).encode('utf-8')
                                         self._set_headers('application/json; charset=utf-8',len(msg),None)
                                         self._write(msg)
                                     else:
@@ -1986,7 +1986,7 @@ class PubServer(BaseHTTPRequestHandler):
                         else:
                             # don't need authenticated fetch here because there is
                             # already the authorization check
-                            msg=json.dumps(inboxFeed).encode('utf-8')
+                            msg=json.dumps(inboxFeed, ensure_ascii=False).encode('utf-8')
                             self._set_headers('application/json; charset=utf-8',len(msg),None)
                             self._write(msg)
                         self.server.GETbusy=False
@@ -2056,7 +2056,7 @@ class PubServer(BaseHTTPRequestHandler):
                         else:
                             # don't need authenticated fetch here because there is
                             # already the authorization check
-                            msg=json.dumps(inboxDMFeed).encode('utf-8')
+                            msg=json.dumps(inboxDMFeed, ensure_ascii=False).encode('utf-8')
                             self._set_headers('application/json; charset=utf-8',len(msg),None)
                             self._write(msg)
                         self.server.GETbusy=False
@@ -2129,7 +2129,7 @@ class PubServer(BaseHTTPRequestHandler):
                     else:
                         # don't need authenticated fetch here because there is
                         # already the authorization check
-                        msg=json.dumps(inboxRepliesFeed).encode('utf-8')
+                        msg=json.dumps(inboxRepliesFeed, ensure_ascii=False).encode('utf-8')
                         self._set_headers('application/json; charset=utf-8',len(msg),None)
                         self._write(msg)
                     self.server.GETbusy=False
@@ -2202,7 +2202,7 @@ class PubServer(BaseHTTPRequestHandler):
                     else:
                         # don't need authenticated fetch here because there is
                         # already the authorization check
-                        msg=json.dumps(inboxMediaFeed).encode('utf-8')
+                        msg=json.dumps(inboxMediaFeed, ensure_ascii=False).encode('utf-8')
                         self._set_headers('application/json; charset=utf-8',len(msg),None)
                         self._write(msg)
                     self.server.GETbusy=False
@@ -2307,7 +2307,7 @@ class PubServer(BaseHTTPRequestHandler):
                 self._write(msg)
             else:
                 if self._fetchAuthenticated():
-                    msg=json.dumps(outboxFeed).encode('utf-8')
+                    msg=json.dumps(outboxFeed, ensure_ascii=False).encode('utf-8')
                     self._set_headers('application/json; charset=utf-8',len(msg),None)
                     self._write(msg)
                 else:
@@ -2370,7 +2370,7 @@ class PubServer(BaseHTTPRequestHandler):
                         else:
                             # don't need authenticated fetch here because there is
                             # already the authorization check
-                            msg=json.dumps(moderationFeed).encode('utf-8')
+                            msg=json.dumps(moderationFeed, ensure_ascii=False).encode('utf-8')
                             self._set_headers('application/json; charset=utf-8',len(msg),None)
                             self._write(msg)
                         self.server.GETbusy=False
@@ -2440,7 +2440,7 @@ class PubServer(BaseHTTPRequestHandler):
                     return
             else:
                 if self._fetchAuthenticated():
-                    msg=json.dumps(shares).encode('utf-8')
+                    msg=json.dumps(shares, ensure_ascii=False).encode('utf-8')
                     self._set_headers('application/json; charset=utf-8',len(msg),None)
                     self._write(msg)
                 else:
@@ -2496,7 +2496,7 @@ class PubServer(BaseHTTPRequestHandler):
                     return
             else:
                 if self._fetchAuthenticated():
-                    msg=json.dumps(following).encode('utf-8')
+                    msg=json.dumps(following, ensure_ascii=False).encode('utf-8')
                     self._set_headers('application/json; charset=utf-8',len(msg),None)
                     self._write(msg)
                 else:
@@ -2551,7 +2551,7 @@ class PubServer(BaseHTTPRequestHandler):
                     return
             else:
                 if self._fetchAuthenticated():
-                    msg=json.dumps(followers).encode('utf-8')
+                    msg=json.dumps(followers, ensure_ascii=False).encode('utf-8')
                     self._set_headers('application/json; charset=utf-8',len(msg),None)
                     self._write(msg)
                 else:
@@ -2584,7 +2584,7 @@ class PubServer(BaseHTTPRequestHandler):
                 self._write(msg)
             else:
                 if self._fetchAuthenticated():
-                    msg=json.dumps(getPerson).encode('utf-8')
+                    msg=json.dumps(getPerson, ensure_ascii=False).encode('utf-8')
                     self._set_headers('application/json; charset=utf-8',len(msg),None)
                     self._write(msg)
                 else:
@@ -2610,7 +2610,7 @@ class PubServer(BaseHTTPRequestHandler):
             with open(filename, 'r', encoding='utf-8') as File:
                 content = File.read()
                 contentJson=json.loads(content)
-                msg=json.dumps(contentJson).encode('utf-8')
+                msg=json.dumps(contentJson, ensure_ascii=False).encode('utf-8')
                 self._set_headers('application/json; charset=utf-8',len(msg),None)
                 self._write(msg)
         else:
diff --git a/posts.py b/posts.py
index 416f0c37c..bb484d6ed 100644
--- a/posts.py
+++ b/posts.py
@@ -1420,7 +1420,7 @@ def sendSignedJson(postJsonObject: {},session,baseDir: str, \
     
     # convert json to string so that there are no
     # subsequent conversions after creating message body digest
-    postJsonStr=json.dumps(postJsonObject)
+    postJsonStr=json.dumps(postJsonObject, ensure_ascii=False)
 
     # construct the http header, including the message body digest
     signatureHeaderJson = \
diff --git a/tests.py b/tests.py
index cce1f68a8..94e230163 100644
--- a/tests.py
+++ b/tests.py
@@ -1614,8 +1614,8 @@ def testCommentJson():
         receivedJson=commentjson.load(fp)
     assert receivedJson
     assert receivedJson['content']==messageStr
-    encodedStr=json.dumps(testJson)
-    assert 'Cr\\u00e8me br\\u00fbl\\u00e9e \\u092f\\u0939 \\u090f\\u0915 \\u092a\\u0930\\u0940\\u0915\\u094d\\u0937\\u0923 \\u0939' in encodedStr
+    encodedStr=json.dumps(testJson, ensure_ascii=False)
+    assert messageStr in encodedStr
 
 def runAllTests():
     print('Running tests...')