olibre / dwst

Dark WebSocket Terminal is a WS client that provides netcat like functionality for conversing with a WS server.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Display text-only responses as text (not as hexadecimal)

olibre opened this issue · comments

{
    key: 'blog',
    value: function blog(buffer, type) {
	  function _buffer_to_text(buffer) {
		var dv = new DataView(buffer);
		var textLines = '';
		for (var i = 0; i < buffer.byteLength; i++) {
			var oneByte = dv.getUint8(i);
			if (oneByte < 0x20 || 0x7e < oneByte)
				throw new TypeError("non-printable byte");
			var asChar = String.fromCharCode(oneByte);
			textLines += asChar;
		}
		return textLines;
      }
	  function _buffer_to_hex(buffer) {
        var hd = this._hexdump(buffer);
        var hexLines = hd.map(function (line) {
          return {
            type: 'hexline',
            text: line.text,
            hexes: line.hexes
          };
        });
		return hexLines
	  }
	  var msg = ''
	  var response = ''
	  try {
		msg = '<' + buffer.byteLength + 'B of text data>';
	    response = _buffer_to_text(buffer)
	  }
	  catch (err) {
        msg = '<' + buffer.byteLength + 'B of binary data>';
		response = _buffer_to_hex(buffer)
	  }
      this.mlog([msg].concat(response), type);
    }