websockify-js
websockify-js copied to clipboard
websock.send_string doesn't convert to utf8
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.
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); } ) );
}
Thanks. I'll look into this when I get a chance. Thanks!