diff --git a/lib/common.js b/lib/common.js index 1e2b792..9d8939d 100644 --- a/lib/common.js +++ b/lib/common.js @@ -59,6 +59,10 @@ exports.querystringStringify = function (obj) { var saved = querystring.escape querystring.escape = escape // global var ret = querystring.stringify(obj) + ret = ret.replace(/[\@\*\/\+]/g, function (char) { + // `escape` doesn't encode the characters @*/+ so we do it manually + return '%' + char.charCodeAt(0).toString(16).toUpperCase() + }) querystring.escape = saved return ret } diff --git a/test/querystring.js b/test/querystring.js new file mode 100644 index 0000000..cf9d33d --- /dev/null +++ b/test/querystring.js @@ -0,0 +1,16 @@ +var common = require('../lib/common') +var test = require('tape') + +// https://github.com/feross/webtorrent/issues/196 +test('encode special chars +* in http tracker urls', function (t) { + var q = { + info_hash: new Buffer('a2a15537542b22925ad10486bf7a8b2a9c42f0d1', 'hex').toString('binary') + } + var encoded = 'info_hash=%A2%A1U7T%2B%22%92Z%D1%04%86%BFz%8B%2A%9CB%F0%D1' + t.equal(common.querystringStringify(q), encoded) + + // sanity check that encode-decode matches up + t.deepEqual(common.querystringParse(common.querystringStringify(q)), q) + + t.end() +})