encode special characters @*/+ in http tracker urls

Fixes https://github.com/feross/webtorrent/issues/196
This commit is contained in:
Feross Aboukhadijeh 2014-12-17 21:18:46 -08:00
parent 538bb3e20a
commit 69211515c2
2 changed files with 20 additions and 0 deletions

View File

@ -59,6 +59,10 @@ exports.querystringStringify = function (obj) {
var saved = querystring.escape var saved = querystring.escape
querystring.escape = escape // global querystring.escape = escape // global
var ret = querystring.stringify(obj) 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 querystring.escape = saved
return ret return ret
} }

16
test/querystring.js Normal file
View File

@ -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()
})