Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

websock.send_string doesn't convert to utf8 #11

Open
imuli opened this issue Jun 25, 2014 · 2 comments
Open

websock.send_string doesn't convert to utf8 #11

imuli opened this issue Jun 25, 2014 · 2 comments

Comments

@imuli
Copy link

imuli commented Jun 25, 2014

Characters above U+00FF are sent modulo 256, due to (in binary mode) websock.js:164 "new Uint8Array(sQ)". I havn't tested it, but the base64 encoder appears to do the same thing in a different way.

@imuli
Copy link
Author

imuli commented Jun 26, 2014

Patch

Note, this code will fail if passed unpaired UTF-16 surrogates, values 0xd800-0xdfff, but then it's not really a string.

diff --git a/include/util.js b/include/util.js
index 67d2133..e498f94 100644
--- a/include/util.js
+++ b/include/util.js
@@ -377,3 +377,11 @@ Util.Flash = (function(){
     version = v.match(/\d+/g);
     return {version: parseInt(version[0] || 0 + '.' + version[1], 10) || 0, build: parseInt(version[2], 10) || 0};
 }()); 
+
+Util.toUTF8 = (function(s){
+   return unescape(encodeURIComponent(s));
+}());
+
+Util.fromUTF8 = (function(s){
+   return decodeURIComponent(escape(s));
+}());
diff --git a/include/websock.js b/include/websock.js
index 01a24c3..e4a694e 100644
--- a/include/websock.js
+++ b/include/websock.js
@@ -119,7 +119,7 @@ function rQshiftStr(len) {
     if (typeof(len) === 'undefined') { len = rQlen(); }
     var arr = rQ.slice(rQi, rQi + len);
     rQi += len;
-    return String.fromCharCode.apply(null, arr);
+    return Util.fromUTF8(String.fromCharCode.apply(null, arr));
 }
 function rQshiftBytes(len) {
     if (typeof(len) === 'undefined') { len = rQlen(); }
@@ -216,7 +216,7 @@ function send(arr) {

 function send_string(str) {
     //Util.Debug(">> send_string: " + str);
-    api.send(str.split('').map(
+    api.send(Util.toUTF8(str).split('').map(
         function (chr) { return chr.charCodeAt(0); } ) );
 }

@DirectXMan12
Copy link
Member

Thanks. I'll look into this when I get a chance. Thanks!

@samhed samhed transferred this issue from novnc/websockify Jul 12, 2019
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants