diff options
author | Feross Aboukhadijeh <feross@feross.org> | 2014-12-28 06:53:28 +0300 |
---|---|---|
committer | Feross Aboukhadijeh <feross@feross.org> | 2014-12-28 06:53:28 +0300 |
commit | 4c0843058d42f0424944b7c62fc3e8254b3fc208 (patch) | |
tree | 23ae922fde31e9ff5bbbbdb7c4b848f930467655 /lib/torrent.js | |
parent | ad04d449a17c8915c92e864e4e077289095e1f53 (diff) |
support .torrent urls with gzip/deflate response
use `simple-get` module
https://npmjs.org/package/simple-get
Diffstat (limited to 'lib/torrent.js')
-rw-r--r-- | lib/torrent.js | 47 |
1 files changed, 8 insertions, 39 deletions
diff --git a/lib/torrent.js b/lib/torrent.js index 7d18af5..ad9783d 100644 --- a/lib/torrent.js +++ b/lib/torrent.js @@ -6,20 +6,19 @@ var debug = require('debug')('webtorrent:torrent') var Discovery = require('torrent-discovery') var EventEmitter = require('events').EventEmitter var fs = require('fs') // browser exclude -var hh = require('http-https') // browser exclude +var get = require('simple-get') // browser exclude var inherits = require('inherits') -var once = require('once') var parallel = require('run-parallel') var parseTorrent = require('parse-torrent') -var RarityMap = require('./rarity-map') var reemit = require('re-emitter') -var Server = require('./server') // browser exclude -var Storage = require('./storage') var Swarm = require('bittorrent-swarm') // `webtorrent-swarm` in browser -var url = require('url') var ut_metadata = require('ut_metadata') var ut_pex = require('ut_pex') // browser exclude +var RarityMap = require('./rarity-map') +var Server = require('./server') // browser exclude +var Storage = require('./storage') + var MAX_BLOCK_LENGTH = 128 * 1024 var PIECE_TIMEOUT = 10000 var CHOKE_TIMEOUT = 5000 @@ -77,12 +76,12 @@ function Torrent (torrentId, opts) { if (parsedTorrent && parsedTorrent.infoHash) { onTorrentId(parsedTorrent) - } else if (typeof hh.get === 'function' && /^https?:/.test(torrentId)) { + } else if (typeof get === 'function' && /^https?:/.test(torrentId)) { // http or https url to torrent file - httpGet(torrentId, function (err, torrent) { + get(torrentId, function (err, res) { if (err) return self.emit('error', new Error('error downloading torrent: ' + err.message)) - onTorrentId(torrent) + res.pipe(concat(onTorrentId)) }) } else if (typeof fs.readFile === 'function') { @@ -1008,33 +1007,3 @@ function randomizedForEach (array, cb) { cb(array[index], index, array) }) } - -/** - * Make http or https request, following redirects. - * @param {string} u - * @param {function} cb - * @param {number=} maxRedirects - * @return {http.ClientRequest} - */ -function httpGet (u, cb, maxRedirects) { - cb = once(cb) - if (!maxRedirects) maxRedirects = 5 - if (maxRedirects === 0) return cb(new Error('too many redirects')) - - hh.get(u, function (res) { - // Check for redirect - if (res.statusCode >= 300 && res.statusCode < 400 && 'location' in res.headers) { - var location = res.headers.location - if (!url.parse(location).host) { - // If relative redirect, prepend host of current url - var parsed = url.parse(u) - location = parsed.protocol + '//' + parsed.host + location - } - res.resume() // discard response - return httpGet(location, cb, --maxRedirects) - } - res.pipe(concat(function (data) { - cb(null, data) - })) - }).on('error', cb) -} |