mirror of
https://github.com/webtorrent/bittorrent-tracker.git
synced 2025-01-05 22:10:11 +00:00
Support http tracker redirects, gzip/deflate
This commit is contained in:
parent
b3ea3f73aa
commit
94feea69e2
22
client.js
22
client.js
@ -4,11 +4,11 @@ var bencode = require('bencode')
|
|||||||
var BN = require('bn.js')
|
var BN = require('bn.js')
|
||||||
var common = require('./lib/common')
|
var common = require('./lib/common')
|
||||||
var compact2string = require('compact2string')
|
var compact2string = require('compact2string')
|
||||||
var concat = require('concat-stream')
|
|
||||||
var debug = require('debug')('bittorrent-tracker')
|
var debug = require('debug')('bittorrent-tracker')
|
||||||
var dgram = require('dgram')
|
var dgram = require('dgram')
|
||||||
var EventEmitter = require('events').EventEmitter
|
var EventEmitter = require('events').EventEmitter
|
||||||
var extend = require('extend.js')
|
var extend = require('extend.js')
|
||||||
|
var get = require('simple-get')
|
||||||
var hat = require('hat')
|
var hat = require('hat')
|
||||||
var http = require('http')
|
var http = require('http')
|
||||||
var https = require('https')
|
var https = require('https')
|
||||||
@ -268,22 +268,10 @@ Tracker.prototype._requestHttp = function (requestUrl, opts) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var protocol = url.parse(self._announceUrl).protocol
|
get.concat(requestUrl + '?' + common.querystringStringify(opts), function (err, data, res) {
|
||||||
var fullUrl = requestUrl + '?' + common.querystringStringify(opts)
|
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))
|
||||||
var req = (protocol === 'https:' ? https : http).get(fullUrl, function (res) {
|
if (data && data.length) self._handleResponse(requestUrl, data)
|
||||||
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)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,7 +18,6 @@
|
|||||||
"bn.js": "^1.0.0",
|
"bn.js": "^1.0.0",
|
||||||
"buffer-equal": "0.0.1",
|
"buffer-equal": "0.0.1",
|
||||||
"compact2string": "^1.2.0",
|
"compact2string": "^1.2.0",
|
||||||
"concat-stream": "^1.4.5",
|
|
||||||
"debug": "^2.0.0",
|
"debug": "^2.0.0",
|
||||||
"extend.js": "0.0.2",
|
"extend.js": "0.0.2",
|
||||||
"hat": "0.0.3",
|
"hat": "0.0.3",
|
||||||
@ -27,6 +26,7 @@
|
|||||||
"once": "^1.3.0",
|
"once": "^1.3.0",
|
||||||
"portfinder": "^0.3.0",
|
"portfinder": "^0.3.0",
|
||||||
"run-series": "^1.0.2",
|
"run-series": "^1.0.2",
|
||||||
|
"simple-get": "^1.3.0",
|
||||||
"string2compact": "^1.1.1"
|
"string2compact": "^1.1.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
@ -2,9 +2,8 @@ var bencode = require('bencode')
|
|||||||
var Client = require('../')
|
var Client = require('../')
|
||||||
var commonLib = require('../lib/common')
|
var commonLib = require('../lib/common')
|
||||||
var commonTest = require('./common')
|
var commonTest = require('./common')
|
||||||
var concat = require('concat-stream')
|
|
||||||
var fs = require('fs')
|
var fs = require('fs')
|
||||||
var http = require('http')
|
var get = require('simple-get')
|
||||||
var parseTorrent = require('parse-torrent')
|
var parseTorrent = require('parse-torrent')
|
||||||
var portfinder = require('portfinder')
|
var portfinder = require('portfinder')
|
||||||
var Server = require('../').Server
|
var Server = require('../').Server
|
||||||
@ -95,29 +94,27 @@ test('server: multiple info_hash scrape', function (t) {
|
|||||||
var url = scrapeUrl + '?' + commonLib.querystringStringify({
|
var url = scrapeUrl + '?' + commonLib.querystringStringify({
|
||||||
info_hash: [ binaryInfoHash1, binaryInfoHash2 ]
|
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)
|
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])
|
data = bencode.decode(data)
|
||||||
t.equal(typeof data.files[binaryInfoHash1].complete, 'number')
|
t.ok(data.files)
|
||||||
t.equal(typeof data.files[binaryInfoHash1].incomplete, 'number')
|
t.equal(Object.keys(data.files).length, 2)
|
||||||
t.equal(typeof data.files[binaryInfoHash1].downloaded, 'number')
|
|
||||||
|
|
||||||
t.ok(data.files[binaryInfoHash2])
|
t.ok(data.files[binaryInfoHash1])
|
||||||
t.equal(typeof data.files[binaryInfoHash2].complete, 'number')
|
t.equal(typeof data.files[binaryInfoHash1].complete, 'number')
|
||||||
t.equal(typeof data.files[binaryInfoHash2].incomplete, 'number')
|
t.equal(typeof data.files[binaryInfoHash1].incomplete, 'number')
|
||||||
t.equal(typeof data.files[binaryInfoHash2].downloaded, 'number')
|
t.equal(typeof data.files[binaryInfoHash1].downloaded, 'number')
|
||||||
|
|
||||||
server.close(function () {
|
t.ok(data.files[binaryInfoHash2])
|
||||||
t.end()
|
t.equal(typeof data.files[binaryInfoHash2].complete, 'number')
|
||||||
})
|
t.equal(typeof data.files[binaryInfoHash2].incomplete, 'number')
|
||||||
}))
|
t.equal(typeof data.files[binaryInfoHash2].downloaded, 'number')
|
||||||
}).on('error', function (err) {
|
|
||||||
t.error(err)
|
server.close(function () {
|
||||||
|
t.end()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@ -152,26 +149,23 @@ test('server: all info_hash scrape', function (t) {
|
|||||||
server.once('start', function () {
|
server.once('start', function () {
|
||||||
|
|
||||||
// now do a scrape of everything by omitting the info_hash param
|
// 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)
|
t.equal(res.statusCode, 200)
|
||||||
res.pipe(concat(function (data) {
|
data = bencode.decode(data)
|
||||||
data = bencode.decode(data)
|
t.ok(data.files)
|
||||||
t.ok(data.files)
|
t.equal(Object.keys(data.files).length, 1)
|
||||||
t.equal(Object.keys(data.files).length, 1)
|
|
||||||
|
|
||||||
t.ok(data.files[binaryBitlove])
|
t.ok(data.files[binaryBitlove])
|
||||||
t.equal(typeof data.files[binaryBitlove].complete, 'number')
|
t.equal(typeof data.files[binaryBitlove].complete, 'number')
|
||||||
t.equal(typeof data.files[binaryBitlove].incomplete, 'number')
|
t.equal(typeof data.files[binaryBitlove].incomplete, 'number')
|
||||||
t.equal(typeof data.files[binaryBitlove].downloaded, 'number')
|
t.equal(typeof data.files[binaryBitlove].downloaded, 'number')
|
||||||
|
|
||||||
client.stop()
|
client.stop()
|
||||||
server.close(function () {
|
server.close(function () {
|
||||||
t.end()
|
t.end()
|
||||||
})
|
})
|
||||||
}))
|
|
||||||
}).on('error', function (err) {
|
|
||||||
t.error(err)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user