From 9d7ea4ee7e59654bb8a89026400186cbe56dc197 Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Sun, 24 Jan 2016 23:35:46 +0100 Subject: [PATCH] Liberal protocol handlers - closes #49 - if protocol prefix is present in path, leave it as-is - if protocol prefix is missing, add value from handler template --- lib/protocols.js | 14 +++++++++++--- package.json | 2 +- test/test-protocols.js | 8 ++++++++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/lib/protocols.js b/lib/protocols.js index 931803591..a22565775 100644 --- a/lib/protocols.js +++ b/lib/protocols.js @@ -47,6 +47,16 @@ CommonProtocolHandler.prototype = Object.freeze({ protocolFlags: Ci.nsIProtocolHandler.URI_NOAUTH | Ci.nsIProtocolHandler.URI_LOADABLE_BY_ANYONE, + normalizedIpfsPath: function (uriSpec) { + let schemeExp = this.scheme.replace(/\+/, '\\+') // fix for web+fs etc + let ipfsPath = uriSpec.replace(new RegExp('^' + schemeExp + '\\:\\/*'), '') + // add protocol prefix if missing + if (!(/^ip(?:f|n)s\//.test(ipfsPath))) { + ipfsPath = this.pathPrefix + ipfsPath + } + return ipfsPath + }, + newURI: function (aSpec, aOriginCharset, aBaseURI) { // console.info('Detected newURI with IPFS protocol: ' + aSpec) @@ -65,9 +75,7 @@ CommonProtocolHandler.prototype = Object.freeze({ return uri */ - let schemeExp = this.scheme.replace(/\+/, '\\+') // web+fs etc - let ipfsPath = aSpec.replace(new RegExp('^' + schemeExp + '\\:\\/*'), '') - let http = gw.publicUri.spec + this.pathPrefix + ipfsPath + let http = gw.publicUri.spec + this.normalizedIpfsPath(aSpec) let uri = ioservice.newURI(http, aOriginCharset, null) // console.info('newURI routed to HTTP gateway: ' + uri.spec) diff --git a/package.json b/package.json index 2312b6caa..2f361f7d8 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "id": "ipfs-firefox-addon@lidel.org", "description": "Access IPFS resources via custom HTTP2IPFS gateway", "author": "Marcin Rataj", - "version": "1.4.1", + "version": "1.4.2", "license": "CC0-1.0", "homepage": "https://github.com/lidel/ipfs-firefox-addon", "icon": "data/icon-on-64.png", diff --git a/test/test-protocols.js b/test/test-protocols.js index 655bf648f..f29e245c6 100644 --- a/test/test-protocols.js +++ b/test/test-protocols.js @@ -52,6 +52,10 @@ exports['test newURI(web+ipfs://)'] = function (assert) { var newURI = webIpfsHandler.newURI('web+ipfs://' + ipfsPath, 'utf8', null) assert.equal(newURI.spec, pubGwUri.spec + 'ipfs/' + ipfsPath, 'newURI spec') } +exports['test newURI(ipfs:/ipfs/)'] = function (assert) { + var newURI = ipfsHandler.newURI('ipfs:/ipfs/' + ipfsPath, 'utf8', null) + assert.equal(newURI.spec, pubGwUri.spec + 'ipfs/' + ipfsPath, 'newURI spec') +} exports['test newURI(ipns:)'] = function (assert) { var newURI = ipnsHandler.newURI('ipns:' + ipnsPath, 'utf8', null) @@ -69,6 +73,10 @@ exports['test newURI(web+ipns://)'] = function (assert) { var newURI = webIpnsHandler.newURI('web+ipns://' + ipnsPath, 'utf8', null) assert.equal(newURI.spec, pubGwUri.spec + 'ipns/' + ipnsPath, 'newURI spec') } +exports['test newURI(ipns:/ipns/)'] = function (assert) { + var newURI = ipnsHandler.newURI('ipns:/ipns/' + ipnsPath, 'utf8', null) + assert.equal(newURI.spec, pubGwUri.spec + 'ipns/' + ipnsPath, 'newURI spec') +} // The fs: protocol for easier interoperability with legacy applications // It is a simple prefix to the canonical UNIX-like IPFS address