From 94feea69e269efe4870fda16bd4d383708c4526e Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Thu, 22 Jan 2015 14:23:53 -0800 Subject: [PATCH] Support http tracker redirects, gzip/deflate --- client.js | 22 ++++------------ package.json | 2 +- test/scrape.js | 68 +++++++++++++++++++++++--------------------------- 3 files changed, 37 insertions(+), 55 deletions(-) diff --git a/client.js b/client.js index 2b49bfd..deaa14e 100644 --- a/client.js +++ b/client.js @@ -4,11 +4,11 @@ var bencode = require('bencode') var BN = require('bn.js') var common = require('./lib/common') var compact2string = require('compact2string') -var concat = require('concat-stream') var debug = require('debug')('bittorrent-tracker') var dgram = require('dgram') var EventEmitter = require('events').EventEmitter var extend = require('extend.js') +var get = require('simple-get') var hat = require('hat') var http = require('http') var https = require('https') @@ -268,22 +268,10 @@ Tracker.prototype._requestHttp = function (requestUrl, opts) { } } - var protocol = url.parse(self._announceUrl).protocol - var fullUrl = requestUrl + '?' + common.querystringStringify(opts) - - var req = (protocol === 'https:' ? https : http).get(fullUrl, function (res) { - if (res.statusCode !== 200) { - res.resume() // consume the whole stream - self.client.emit('warning', new Error('Invalid response code ' + res.statusCode + ' from tracker ' + requestUrl)) - return - } - res.pipe(concat(function (data) { - if (data && data.length) self._handleResponse(requestUrl, data) - })) - }) - - req.on('error', function (err) { - self.client.emit('warning', err) + get.concat(requestUrl + '?' + common.querystringStringify(opts), function (err, data, res) { + if (err) return self.client.emit('warning', err) + if (res.statusCode !== 200) return self.client.emit('warning', new Error('Non-200 response code ' + res.statusCode + ' from ' + requestUrl)) + if (data && data.length) self._handleResponse(requestUrl, data) }) } diff --git a/package.json b/package.json index 067895d..2837d27 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,6 @@ "bn.js": "^1.0.0", "buffer-equal": "0.0.1", "compact2string": "^1.2.0", - "concat-stream": "^1.4.5", "debug": "^2.0.0", "extend.js": "0.0.2", "hat": "0.0.3", @@ -27,6 +26,7 @@ "once": "^1.3.0", "portfinder": "^0.3.0", "run-series": "^1.0.2", + "simple-get": "^1.3.0", "string2compact": "^1.1.1" }, "devDependencies": { diff --git a/test/scrape.js b/test/scrape.js index 33d80ae..81e8d53 100644 --- a/test/scrape.js +++ b/test/scrape.js @@ -2,9 +2,8 @@ var bencode = require('bencode') var Client = require('../') var commonLib = require('../lib/common') var commonTest = require('./common') -var concat = require('concat-stream') var fs = require('fs') -var http = require('http') +var get = require('simple-get') var parseTorrent = require('parse-torrent') var portfinder = require('portfinder') var Server = require('../').Server @@ -95,29 +94,27 @@ test('server: multiple info_hash scrape', function (t) { var url = scrapeUrl + '?' + commonLib.querystringStringify({ info_hash: [ binaryInfoHash1, binaryInfoHash2 ] }) - http.get(url, function (res) { + get.concat(url, function (err, data, res) { + if (err) throw err t.equal(res.statusCode, 200) - res.pipe(concat(function (data) { - data = bencode.decode(data) - t.ok(data.files) - t.equal(Object.keys(data.files).length, 2) - t.ok(data.files[binaryInfoHash1]) - t.equal(typeof data.files[binaryInfoHash1].complete, 'number') - t.equal(typeof data.files[binaryInfoHash1].incomplete, 'number') - t.equal(typeof data.files[binaryInfoHash1].downloaded, 'number') + data = bencode.decode(data) + t.ok(data.files) + t.equal(Object.keys(data.files).length, 2) - t.ok(data.files[binaryInfoHash2]) - t.equal(typeof data.files[binaryInfoHash2].complete, 'number') - t.equal(typeof data.files[binaryInfoHash2].incomplete, 'number') - t.equal(typeof data.files[binaryInfoHash2].downloaded, 'number') + t.ok(data.files[binaryInfoHash1]) + t.equal(typeof data.files[binaryInfoHash1].complete, 'number') + t.equal(typeof data.files[binaryInfoHash1].incomplete, 'number') + t.equal(typeof data.files[binaryInfoHash1].downloaded, 'number') - server.close(function () { - t.end() - }) - })) - }).on('error', function (err) { - t.error(err) + t.ok(data.files[binaryInfoHash2]) + t.equal(typeof data.files[binaryInfoHash2].complete, 'number') + t.equal(typeof data.files[binaryInfoHash2].incomplete, 'number') + t.equal(typeof data.files[binaryInfoHash2].downloaded, 'number') + + server.close(function () { + t.end() + }) }) }) }) @@ -152,26 +149,23 @@ test('server: all info_hash scrape', function (t) { server.once('start', function () { // now do a scrape of everything by omitting the info_hash param - http.get(scrapeUrl, function (res) { + get.concat(scrapeUrl, function (err, data, res) { + if (err) throw err t.equal(res.statusCode, 200) - res.pipe(concat(function (data) { - data = bencode.decode(data) - t.ok(data.files) - t.equal(Object.keys(data.files).length, 1) + data = bencode.decode(data) + t.ok(data.files) + t.equal(Object.keys(data.files).length, 1) - t.ok(data.files[binaryBitlove]) - t.equal(typeof data.files[binaryBitlove].complete, 'number') - t.equal(typeof data.files[binaryBitlove].incomplete, 'number') - t.equal(typeof data.files[binaryBitlove].downloaded, 'number') + t.ok(data.files[binaryBitlove]) + t.equal(typeof data.files[binaryBitlove].complete, 'number') + t.equal(typeof data.files[binaryBitlove].incomplete, 'number') + t.equal(typeof data.files[binaryBitlove].downloaded, 'number') - client.stop() - server.close(function () { - t.end() - }) - })) - }).on('error', function (err) { - t.error(err) + client.stop() + server.close(function () { + t.end() + }) }) }) })