Skip to content
This repository has been archived by the owner on Nov 16, 2020. It is now read-only.

Send breaks if body contains multi-byte utf-8 characters #2

Closed
klemetsson opened this issue Mar 24, 2013 · 5 comments
Closed

Send breaks if body contains multi-byte utf-8 characters #2

klemetsson opened this issue Mar 24, 2013 · 5 comments
Assignees
Milestone

Comments

@klemetsson
Copy link

In stomp.js, the Frame.prototype.toString function sets the content-length to the number of characters in the body. This is only valid if there is one byte per character.

I added the following function to calculate the number of bytes in the body:

function countBytes(string) {
  var escapedStr = encodeURI(string);
  if (escapedStr.indexOf("%") != -1) {
    var count = escapedStr.split("%").length - 1;
    if (count == 0) count++;
    var tmp = escapedStr.length - (count * 3);
    return count + tmp;
  }
  else return escapedStr.length;
}

and changed

lines.push("content-length:" + ('' + this.body).length);

to

lines.push("content-length:" + countBytes('' + this.body));

Maybe there is a cleaner way of doing this. But this change worked for me.
There is possibly more issues of this type in stomp.js. The .length gives the number of characters and not the number of bytes in a string.

@michaelklishin
Copy link
Member

@essen is this still relevant in 3.6.x and master?

@essen essen self-assigned this Jan 20, 2016
@essen
Copy link
Collaborator

essen commented Jan 20, 2016

I'll check.

@essen
Copy link
Collaborator

essen commented Jan 20, 2016

It's been fixed upstream. Perhaps we should just update the stomp.js file.

On a related note, upstream (https://github.com/jmesnil/stomp-websocket) is not maintained anymore. The author seemed interested to donate the project to ActiveMQ (http://activemq.2283324.n4.nabble.com/Code-donation-for-stomp-js-td4694260.html). Either way we can probably move to the latest version for now and deal with that later if needed.

@michaelklishin
Copy link
Member

Yes, I think we should upgrade and see what's going to happen with the JS library.

@michaelklishin
Copy link
Member

Fixed in #10 by @essen. Thank you!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants