question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Converting buffer toString() has issues with decodeURIComponent

See original GitHub issue

In Parser.js headText is the encoded String of Buffer. var headText = this.buffer.toString(this.encoding, 0, headEnd);

its trying to decode decodeURIComponent('TA-Sasivarunan%ED%A0%BD%ED%B1%8D-September%E2%80%932%E2%80%932016%20%E2%80%93%201.2/Dalvik/2.1.0%20(Linux%3B%20U%3B%20Android%205.0.1%3B%20GT-I9500%20Build/LRX22C)%20(belle-sip/1.4.1)')

Actual user agent string was 'TA-Sasivarunan👍-September–2–2016 – 1.2/Dalvik/2.1.0 (Linux; U; Android 5.0.1; GT-I9500 Build/LRX22C) (belle-sip/1.4.1)'

URIError: URI malformed
    at decodeURIComponent (native)
    at /mnt/node_modules/modesl/lib/esl/Parser.js:110:44
    at Array.reduce (native)
    at Parser._parseHeaderText (/mnt/node_modules/modesl/lib/esl/Parser.js:107:28)
    at Parser._parseHeaders (/mnt/node_modules/modesl/lib/esl/Parser.js:68:25)
    at Parser._onData (/mnt/node_modules/modesl/lib/esl/Parser.js:36:21)
    at Socket.emit (events.js:107:17)
    at readableAddChunk (_stream_readable.js:163:16)
    at Socket.Readable.push (_stream_readable.js:126:10)
    at TCP.onread (net.js:540:20)

For now i’ve update the code to

Parser.prototype._parseHeaderText = function(txt) {
    return txt.split('\n').reduce(function(headers, line) {
        var data = line.split(': '),
            key = data.shift(),
            value; 
        try{
            value = decodeURIComponent(data.join(': '));
        } catch(ex){
            console.log("ERROR DECODING URI: " + ex.message + " : "  + data);
        }
        if(key === 'Content-Length') {
            headers[key] = parseInt(value, 10);
        } else {
            headers[key] = value;
        }
        return headers;
    }, {});
};

Please shed some lights on now to prevent this error.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
englercjcommented, Oct 16, 2016

@svarunan Buffer.toString() definitely does not URI encode stuff, FSW does that when it sends them over the wire. The protocol is text (utf8) and it sends items URI encoded so you can see the delimiters.

If the encoding is invalid, it is because of FSW. I would reach out to them and see if they can fix this. I had to fix their JSON encoding as well when I first made this library, so its likely this is probably fixable as well.

0reactions
englercjcommented, Feb 1, 2017

Going to close this for now since I am pretty sure this is an FSW issue, not a node-esl issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

decodeURIComponent vs. binary Buffer.toString() + UTF-8 ...
(Note that normal multibyte UTF-8 characters go through both the same way, and seem to come out fine in both cases.) I'm mostly...
Read more >
node.js - NodeJS decodeURIComponent not working properly
You can convert first to second using new Buffer('Ulysses Guimarães - lado par', 'binary').toString('utf8') , but it's a workaround, ...
Read more >
Buffer.toString() didn't trim the tailing `\0` · Issue #4775 - GitHub
Buffer.toString() does exactly what it says on the box: Convert the bytes to characters using the given encoding.
Read more >
encodeURI() - JavaScript - MDN Web Docs
The encodeURI() function encodes a URI by replacing each instance of certain characters by one, two, three, or four escape sequences ...
Read more >
Decoding a base64 string - Question - AppGyver forums
Hi folks. I have a base64 string that I wish to decode in a JS node. My problem is that we cannot use...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found