Skip to content

Commit 6921151

Browse files
committed
encode special characters @*/+ in http tracker urls
Fixes webtorrent/webtorrent#196
1 parent 538bb3e commit 6921151

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

lib/common.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ exports.querystringStringify = function (obj) {
5959
var saved = querystring.escape
6060
querystring.escape = escape // global
6161
var ret = querystring.stringify(obj)
62+
ret = ret.replace(/[\@\*\/\+]/g, function (char) {
63+
// `escape` doesn't encode the characters @*/+ so we do it manually
64+
return '%' + char.charCodeAt(0).toString(16).toUpperCase()
65+
})
6266
querystring.escape = saved
6367
return ret
6468
}

test/querystring.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var common = require('../lib/common')
2+
var test = require('tape')
3+
4+
// https://github.com/feross/webtorrent/issues/196
5+
test('encode special chars +* in http tracker urls', function (t) {
6+
var q = {
7+
info_hash: new Buffer('a2a15537542b22925ad10486bf7a8b2a9c42f0d1', 'hex').toString('binary')
8+
}
9+
var encoded = 'info_hash=%A2%A1U7T%2B%22%92Z%D1%04%86%BFz%8B%2A%9CB%F0%D1'
10+
t.equal(common.querystringStringify(q), encoded)
11+
12+
// sanity check that encode-decode matches up
13+
t.deepEqual(common.querystringParse(common.querystringStringify(q)), q)
14+
15+
t.end()
16+
})

0 commit comments

Comments
 (0)