From 9a939d2bb1646fc2fa420d549ebae30a6cebfaaf Mon Sep 17 00:00:00 2001 From: zohead Date: Sat, 26 Mar 2016 17:22:22 +0800 Subject: [PATCH 1/2] Fix staticserver can't request unicode filename or filename with special characters Add StringToBinary function, fix serveFileListing doesn't handle unicode filename correctly bug --- server.js | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/server.js b/server.js index b3feafa..04da24a 100644 --- a/server.js +++ b/server.js @@ -12,25 +12,48 @@ function getContentType(path) { return mimeTypes[ext] || "application/octet-stream"; } +function StringToBinary(string) { + var chars, code, i, isUCS2, len, _i; + + len = string.length; + chars = []; + isUCS2 = false; + for (i = _i = 0; 0 <= len ? _i < len : _i > len; i = 0 <= len ? ++_i : --_i) { + code = String.prototype.charCodeAt.call(string, i); + if (code > 255) { + isUCS2 = true; + chars = null; + break; + } else { + chars.push(code); + } + } + if (isUCS2 === true) { + return unescape(encodeURIComponent(string)); + } else { + return String.fromCharCode.apply(null, Array.prototype.slice.apply(chars)); + } +} + function serveFileListing() { return fs.listFiles().then(function(allFiles) { var html = "

File listing