Skip to content

Commit

Permalink
Liberal protocol handlers
Browse files Browse the repository at this point in the history
- closes #49
- if protocol prefix is present in path, leave it as-is
- if protocol prefix is missing, add value from handler template
  • Loading branch information
lidel committed Jan 24, 2016
1 parent d2f9043 commit 9d7ea4e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
14 changes: 11 additions & 3 deletions lib/protocols.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 8 additions & 0 deletions test/test-protocols.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ exports['test newURI(web+ipfs://<path>)'] = 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/<path>)'] = 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:<path>)'] = function (assert) {
var newURI = ipnsHandler.newURI('ipns:' + ipnsPath, 'utf8', null)
Expand All @@ -69,6 +73,10 @@ exports['test newURI(web+ipns://<path>)'] = 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/<path>)'] = function (assert) {
var newURI = ipnsHandler.newURI('ipns:/ipns/' + ipnsPath, 'utf8', null)
assert.equal(newURI.spec, pubGwUri.spec + 'ipns/' + ipnsPath, 'newURI spec')
}

// The fs:<resource> protocol for easier interoperability with legacy applications
// It is a simple prefix to the canonical UNIX-like IPFS address
Expand Down

0 comments on commit 9d7ea4e

Please sign in to comment.