Skip to content

Commit

Permalink
Hex-escape unprintable characters in terminal tab
Browse files Browse the repository at this point in the history
If there's any binary going over the line like null bytes, we want to
see that here too.
  • Loading branch information
foosel committed Feb 20, 2017
1 parent e1eaf4e commit 7f0eb8d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
33 changes: 33 additions & 0 deletions src/octoprint/static/js/app/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -883,3 +883,36 @@ var getQueryParameterByName = function(name, url) {
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
};

/**
* Escapes unprintable ASCII characters in the provided string.
*
* E.g. turns a null byte in the string into "\x00".
*
* Only characters 0 to 31, 127 and 255 will be escaped, that
* should leave printable characters and unicode alone.
*
* Originally based on
* https://gist.github.com/mathiasbynens/1243213#gistcomment-53590
*
* @param str The string to escape
* @returns {string}
*/
var escapeUnprintableCharacters = function(str) {
var result = "";
var index = 0;
var charCode;

while (!isNaN(charCode = str.charCodeAt(index))) {
if (charCode < 32 || charCode == 127 || charCode == 255) {
// special hex chars
result += "\\x" + (charCode > 15 ? "" : "0") + charCode.toString(16)
} else {
// anything else
result += str[index];
}

index++;
}
return result;
};
2 changes: 1 addition & 1 deletion src/octoprint/static/js/app/viewmodels/terminal.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ $(function() {
if (type == undefined) {
type = "line";
}
return {line: line, type: type}
return {line: escapeUnprintableCharacters(line), type: type}
};

self._processStateData = function(data) {
Expand Down

0 comments on commit 7f0eb8d

Please sign in to comment.