From e83328babe8159cf1170bc0b950a8ce334582693 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Sun, 18 May 2014 18:36:40 -0700 Subject: [PATCH] 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. --- index.js | 11 ++++------- package.json | 1 + 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 55cd368..a7248fd 100644 --- a/index.js +++ b/index.js @@ -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) } diff --git a/package.json b/package.json index 3a75951..6d81537 100644 --- a/package.json +++ b/package.json @@ -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",