Skip to content

UTF-8 decoding is wrong #74

New issue

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

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

Already on GitHub? Sign in to your account

Open
Artiom-M opened this issue Jul 18, 2014 · 2 comments
Open

UTF-8 decoding is wrong #74

Artiom-M opened this issue Jul 18, 2014 · 2 comments

Comments

@Artiom-M
Copy link

When receiving an UTF-8 string with multi-byte characters like "é" "ю" etc. it is decoded byte by byte giving the result as:

é �

Sending doesn't work at all:

SEND
destination:/topic/spr
content-length:4

ùé
<<< PONG

PING
<<< PONG
PING
<<< ERROR
message:Expected null terminator after 4 content bytes

@Artiom-M
Copy link
Author

My current workaround is to encode the message in JSON with ensure_ascii=True (I am using python for tests)

@tobiassodergren
Copy link

I have gotten the decoding part to work by including functionality from:
http://snipplr.com/view.php?codeview&id=31206

I introduced it in the compiled version of stomp.js, inside this.ws.onmessage. I don't have enough coffee-script skills to do an actual patch.

  this.ws.onmessage = (function(_this) {
    return function(evt) {
      var arr, c, client, data, frame, messageID, onreceive, subscription, unmarshalledData, _i, _len, _ref, _results;
      data = typeof ArrayBuffer !== 'undefined' && evt.data instanceof ArrayBuffer ? (arr = new Uint8Array(evt.data), typeof _this.debug === "function" ? _this.debug("--- got data length: " + arr.length) : void 0, ((function() {
        var _i, _len, _results;
        _results = [];
          for (_i = 0, _len = arr.length; _i < _len; _i++) {

              var byte1 = arr[_i];
              if( byte1 < 0x80 ) {
                  _results.push(String.fromCharCode(byte1));
              } else if( byte1 >= 0xC2 && byte1 < 0xE0 ) {
                  var byte2 = arr[++_i];
                  _results.push(String.fromCharCode(((byte1&0x1F)<<6) + (byte2&0x3F)));
              } else if( byte1 >= 0xE0 && byte1 < 0xF0 ) {
                  var byte2 = arr[++_i];
                  var byte3 = arr[++_i];
                  _results.push(String.fromCharCode(((byte1&0xFF)<<12) + ((byte2&0x3F)<<6) + (byte3&0x3F)));
              } else if( byte1 >= 0xF0 && byte1 < 0xF5) {
                  var byte2 = arr[++_i];
                  var byte3 = arr[++_i];
                  var byte4 = arr[++_i];
                  var codepoint = ((byte1&0x07)<<18) + ((byte2&0x3F)<<12)+ ((byte3&0x3F)<<6) + (byte4&0x3F);
                  codepoint -= 0x10000;
                  _results.push(String.fromCharCode(
                      (codepoint>>10) + 0xD800,
                      (codepoint&0x3FF) + 0xDC00
                  ));
              }
        }
        return _results;
      })()).join('')) : evt.data;

tobiassodergren pushed a commit to tobiassodergren/stomp-websocket that referenced this issue Nov 21, 2014
tobiassodergren pushed a commit to tobiassodergren/stomp-websocket that referenced this issue Nov 21, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants