diff options
author | Feross Aboukhadijeh <feross@feross.org> | 2016-05-19 03:40:20 +0300 |
---|---|---|
committer | Feross Aboukhadijeh <feross@feross.org> | 2016-05-19 03:40:20 +0300 |
commit | 389402479e9d9b2f5343f3eed6d3f5eb111666df (patch) | |
tree | 4563d0560460b41c07df393381db37373f8238eb /lib/torrent.js | |
parent | e0fed34e747d2e267fa28e10672d2de1da3cebb6 (diff) | |
parent | d498f11b7668e15fa2ee6e9518e20d917f63da17 (diff) |
Merge pull request #799 from Sebmaster/feature/magnet-xs
Implement exact source (xs) for magnet URIs
Diffstat (limited to 'lib/torrent.js')
-rw-r--r-- | lib/torrent.js | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/lib/torrent.js b/lib/torrent.js index 2e082b9..431e2ee 100644 --- a/lib/torrent.js +++ b/lib/torrent.js @@ -12,6 +12,7 @@ var extend = require('xtend') var extendMutable = require('xtend/mutable') var fs = require('fs') var FSChunkStore = require('fs-chunk-store') // browser: `memory-chunk-store` +var get = require('simple-get') var ImmediateChunkStore = require('immediate-chunk-store') var inherits = require('inherits') var MultiStream = require('multistream') @@ -113,6 +114,7 @@ function Torrent (torrentId, client, opts) { // for cleanup this._servers = [] + this._xsRequest = null // TODO: remove this and expose a hook instead // optimization: don't recheck every file if it hasn't changed @@ -269,6 +271,38 @@ Torrent.prototype._onParsedTorrent = function (parsedTorrent) { self._onListening() }) } + + if (parsedTorrent.xs && !self.info) { + // TODO: Could try multiple in parallel here + var src = parsedTorrent.xs instanceof Array ? parsedTorrent.xs[0] : parsedTorrent.xs + var isHTTP = src.substring(0, 7) === 'http://' || src.substring(0, 8) === 'https://' + + if (isHTTP) { + var opts = { + url: src, + method: 'GET', + headers: { + 'user-agent': 'WebTorrent (http://webtorrent.io)' + } + } + this._xsRequest = get.concat(opts, function (err, res, torrent) { + if (err || res.statusCode !== 200 || !torrent.length) return + + var parsedTorrent + try { + parsedTorrent = parseTorrent(torrent) + } catch (err) {} + + if (parsedTorrent) { + if (parsedTorrent.infoHash !== self.infoHash) { + return + } + + self._onMetadata(parsedTorrent) + } + }) + } + } } Torrent.prototype._processParsedTorrent = function (parsedTorrent) { @@ -371,6 +405,10 @@ Torrent.prototype._onListening = function () { */ Torrent.prototype._onMetadata = function (metadata) { var self = this + if (self._xsRequest) { + self._xsRequest.abort() + self._xsRequest = null + } if (self.metadata || self.destroyed) return self._debug('got metadata') @@ -553,6 +591,9 @@ Torrent.prototype._destroy = function (err, cb) { self.destroyed = true self._debug('destroy') + if (self._xsRequest) { + self._xsRequest.abort() + } self.client._remove(self) clearInterval(self._rechokeIntervalId) @@ -612,6 +653,7 @@ Torrent.prototype._destroy = function (err, cb) { self._rarityMap = null self._peers = null self._servers = null + self._xsRequest = null } Torrent.prototype.addPeer = function (peer) { |