Support http tracker redirects, gzip/deflate

This commit is contained in:
Feross Aboukhadijeh 2015-01-22 14:23:53 -08:00
parent b3ea3f73aa
commit 94feea69e2
3 changed files with 37 additions and 55 deletions

View File

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

View File

@ -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": {

View File

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