fix bug where client mangles tracker port

The client was converting the binary (compact) response to a utf8
string which would modify the data in some cases. Keeping it as a
buffer the whole time solves the issue.

Fixes #19.
This commit is contained in:
Feross Aboukhadijeh 2014-05-18 18:36:40 -07:00
parent 67be6227c0
commit e83328babe
2 changed files with 5 additions and 7 deletions

View File

@ -4,6 +4,7 @@ exports.Server = Server
var BN = require('bn.js')
var bncode = require('bncode')
var compact2string = require('compact2string')
var concat = require('concat-stream')
var dgram = require('dgram')
var EventEmitter = require('events').EventEmitter
var extend = require('extend.js')
@ -146,18 +147,14 @@ Tracker.prototype._requestHttp = function (requestUrl, opts) {
var fullUrl = requestUrl + '?' + querystring.stringify(opts)
var req = http.get(fullUrl, function (res) {
var data = ''
if (res.statusCode !== 200) {
res.resume() // consume the whole stream
self.client.emit('error', new Error('Invalid response code ' + res.statusCode + ' from tracker ' + requestUrl))
return
}
res.on('data', function (chunk) {
data += chunk
})
res.on('end', function () {
res.pipe(concat(function (data) {
self._handleResponse(requestUrl, data)
})
}))
})
req.on('error', function (err) {
@ -640,7 +637,6 @@ Server.prototype._onHttpRequest = function (req, res) {
if (warning) {
response['warning message'] = warning
}
res.end(bncode.encode(response))
} else if (s[0] === '/scrape') { // unofficial scrape message
@ -698,6 +694,7 @@ Server.prototype._getPeersCompact = function (swarm) {
var peer = swarm.peers[peerId]
return peer.ip + ':' + peer.port
})
return string2compact(addrs)
}

View File

@ -14,6 +14,7 @@
"bn.js": "^0.7.1",
"bncode": "^0.5.3",
"compact2string": "^1.2.0",
"concat-stream": "^1.4.5",
"extend.js": "0.0.1",
"hat": "0.0.3",
"inherits": "^2.0.1",