New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement exact source (xs) for magnet URIs #799
Merged
+107
−0
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.
| @@ -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 | ||
feross
Member
|
||
|
|
||
| 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) { | ||
| @@ -0,0 +1,65 @@ | ||
| var fixtures = require('webtorrent-fixtures') | ||
| var http = require('http') | ||
| var test = require('tape') | ||
| var WebTorrent = require('../../') | ||
|
|
||
| function createServer (data, cb) { | ||
| var server = http.createServer(function (req, res) { | ||
| res.end(data) | ||
| }).listen(function () { | ||
| var address = server.address() | ||
| if (address.family === 'IPv6') { | ||
| address.address = '[' + address.address + ']' | ||
| } | ||
| var url = 'http://' + address.address + ':' + address.port + '/' | ||
| cb(url, next) | ||
| }) | ||
|
|
||
| function next () { | ||
| server.close() | ||
| } | ||
| } | ||
|
|
||
| test('Download metadata for magnet URI with xs parameter', function (t) { | ||
| t.plan(2) | ||
|
|
||
| var client = new WebTorrent({ dht: false, tracker: false }) | ||
|
|
||
| createServer(fixtures.leaves.torrent, function (url, next) { | ||
| client.add(fixtures.leaves.magnetURI + '&xs=' + encodeURIComponent(url), function (torrent) { | ||
| t.equal(torrent.name, 'Leaves of Grass by Walt Whitman.epub') | ||
feross
Member
|
||
|
|
||
| client.destroy(function (err) { t.error(err, 'client destroyed') }) | ||
| next() | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| test('Download metadata for magnet URI with xs parameter', function (t) { | ||
| t.plan(2) | ||
|
|
||
| var client = new WebTorrent({ dht: false, tracker: false }) | ||
|
|
||
| createServer(fixtures.leaves.torrent, function (url, next) { | ||
| var encoded = encodeURIComponent(url) | ||
| var uri = fixtures.leaves.magnetURI + '&xs=' + encoded + '&xs=' + encoded + '2' | ||
|
|
||
| client.add(uri, function (torrent) { | ||
| t.equal(torrent.name, 'Leaves of Grass by Walt Whitman.epub') | ||
|
|
||
| client.destroy(function (err) { t.error(err, 'client destroyed') }) | ||
| next() | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| test('Download metadata magnet URI with unsupported protocol in xs parameter', function (t) { | ||
| t.plan(2) | ||
|
|
||
| var client = new WebTorrent({ dht: false, tracker: false }) | ||
| client.add(fixtures.leaves.magnetURI + '&xs=' + encodeURIComponent('invalidurl:example')) | ||
| setTimeout(function () { | ||
| t.ok(true, 'no crash') | ||
feross
Member
|
||
| client.destroy(function (err) { t.error(err, 'client destroyed') }) | ||
| }, 100) | ||
| }) | ||
ProTip!
Use n and p to navigate between commits in a pull request.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Array.isArray()is the right way to check if something is an Array.