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

Commit

Permalink
Merge pull request #11 from valsaven/master
Browse files Browse the repository at this point in the history
fix(http): Fix wrong counting of content length for non-ASCII strings
  • Loading branch information
ondras committed Apr 18, 2017
2 parents a221990 + 5c07663 commit 49c306e
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/http.js
Expand Up @@ -286,8 +286,22 @@ ClientRequest.prototype.send = function(follow) {
});
}
} else {
function byteLength(str) {
// Returns the byte length of an utf8 string
var s = str.length;

for (var i = str.length - 1; i >= 0; i--) {
var code = str.charCodeAt(i);
if (code > 0x7f && code <= 0x7ff) s++;
else if (code > 0x7ff && code <= 0xffff) s+=2;
if (code >= 0xDC00 && code <= 0xDFFF) i--; // Trail surrogate
}

return s;
}

post = this.post;
this.header({"Content-Length": post.length});
this.header({"Content-Length": byteLength(post)});
}

/* build request */
Expand Down

0 comments on commit 49c306e

Please sign in to comment.