Merge pull request #52 from feross/encode-special-chars

encode special characters @*/+ in http tracker urls
This commit is contained in:
Feross Aboukhadijeh 2014-12-22 01:26:13 -08:00
commit 943223dfa6
3 changed files with 24 additions and 0 deletions

View File

@ -454,14 +454,18 @@ Tracker.prototype._handleResponse = function (requestUrl, data) {
}
var failure = data['failure reason']
if (failure) {
debug('failure from ' + requestUrl + ' (' + failure + ')')
return self.client.emit('warning', new Error(failure))
}
var warning = data['warning message']
if (warning) {
debug('warning from ' + requestUrl + ' (' + warning + ')')
self.client.emit('warning', new Error(warning))
}
debug('response from ' + requestUrl)
if (requestUrl === self._announceUrl) {
var interval = data.interval || data['min interval']
if (interval && !self._opts.interval && self._intervalMs !== 0) {

View File

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

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