Skip to content

Commit

Permalink
Default to new Public Gateway URL
Browse files Browse the repository at this point in the history
- support old URLs: gateway.ipfs.io/ip(ns|fs)/
- default to new gateway: ipfs.io/ip(ns|fs)/
- closes #21
  • Loading branch information
lidel committed Sep 9, 2015
1 parent 5afde57 commit 8636a3a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
ipfs-firefox-addon.xpi
node_modules
firefox
9 changes: 5 additions & 4 deletions lib/gateways.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ var ioservice = Cc['@mozilla.org/network/io-service;1'].getService(Ci.nsIIOServi
var gui = require('./gui.js');
var proto = require('./protocols.js');

const PUBLIC_GATEWAY_URI = ioservice.newURI('http://gateway.ipfs.io', null, null);
const PUBLIC_GATEWAY_REGEX = new RegExp('^' + PUBLIC_GATEWAY_URI.spec.replace(/[^\w\s]/g, '\\$&') + '(' + proto.ipfsScheme + '|' + proto.ipnsScheme + ')\\/');
const PUBLIC_GATEWAY_URI = ioservice.newURI('https://ipfs.io', null, null);
const PUBLIC_GATEWAY_REGEX = /^https?:\/\/(gateway\.|)ipfs\.io\//;
const PUBLIC_IPFS_RESOURCE = /^https?:\/\/(gateway\.|)ipfs\.io\/ip(fs|ns)\//;
var CUSTOM_GATEWAY_URI;


Expand All @@ -20,11 +21,11 @@ var ipfsRequestObserver = {
if (topic == 'http-on-modify-request') {
let channel = subject.QueryInterface(Ci.nsIHttpChannel);
let httpUrl = channel.URI.spec;
if (httpUrl.match(PUBLIC_GATEWAY_REGEX)) {
if (httpUrl.match(PUBLIC_IPFS_RESOURCE)) {
channel.setRequestHeader('x-ipfs-firefox-addon', 'true', false);
if (prefs.useCustomGateway) {
//console.info('Detected HTTP request to the public gateway: ' + channel.URI.spec);
let uri = ioservice.newURI(httpUrl.replace(PUBLIC_GATEWAY_URI.spec, CUSTOM_GATEWAY_URI.spec), null, null);
let uri = ioservice.newURI(httpUrl.replace(PUBLIC_GATEWAY_REGEX, CUSTOM_GATEWAY_URI.spec), null, null);
//console.info('Redirecting to custom gateway: ' + uri.spec);
channel.redirectTo(uri);
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"id": "[email protected]",
"description": "Access IPFS resources via custom HTTP2IPFS gateway",
"author": "Marcin Rataj",
"version": "1.0.2",
"version": "1.0.3",
"license": "CC0",
"homepage": "https://github.com/lidel/ipfs-firefox-addon",
"icon": "data/icon-on-64.png",
Expand All @@ -19,7 +19,7 @@
},
{
"name": "customGatewayHost",
"title": "Custom IPFS Gateway Host",
"title": "Custom IPFS Gateway Address",
"type": "string",
"value": "127.0.0.1"
},
Expand Down
44 changes: 42 additions & 2 deletions test/test-gateways.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,51 @@ const ipfsPath = 'ipfs/QmTkzDwWqPbnAh5YiV5VwcTLnGdwSNsNTn2aDxdXBFca7D/';
const ipnsPath = 'ipns/ipfs.git.sexy/';
const button = gui.toggleButton;

exports['test enabled /ipfs/ redirect'] = function(assert, done) {
exports['test enabled /ipfs/ redirect (old http gw)'] = function(assert, done) {
// open Public Gateway URL and check if it gets redirected to gw.customUri()
gw.enableHttpGatewayRedirect(button);
// HTTP
tabs.open({
url: gw.publicUri().spec + ipfsPath,
url: 'http://gateway.ipfs.io/' + ipfsPath,
onReady: function onReady(tab) {
assert.equal(tab.url, gw.customUri().spec + ipfsPath, 'expected redirect');
tab.close(done);
}
});
};

exports['test enabled /ipfs/ redirect (old https gw)'] = function(assert, done) {
// open Public Gateway URL and check if it gets redirected to gw.customUri()
gw.enableHttpGatewayRedirect(button);
// HTTPS
tabs.open({
url: 'https://gateway.ipfs.io/' + ipfsPath,
onReady: function onReady(tab) {
assert.equal(tab.url, gw.customUri().spec + ipfsPath, 'expected redirect');
tab.close(done);
}
});
};

exports['test enabled /ipfs/ redirect (http gw)'] = function(assert, done) {
// open Public Gateway URL and check if it gets redirected to gw.customUri()
gw.enableHttpGatewayRedirect(button);
// HTTP
tabs.open({
url: 'http://ipfs.io/' + ipfsPath,
onReady: function onReady(tab) {
assert.equal(tab.url, gw.customUri().spec + ipfsPath, 'expected redirect');
tab.close(done);
}
});
};

exports['test enabled /ipfs/ redirect (https gw)'] = function(assert, done) {
// open Public Gateway URL and check if it gets redirected to gw.customUri()
gw.enableHttpGatewayRedirect(button);
// HTTPS
tabs.open({
url: 'https://ipfs.io/' + ipfsPath,
onReady: function onReady(tab) {
assert.equal(tab.url, gw.customUri().spec + ipfsPath, 'expected redirect');
tab.close(done);
Expand Down

0 comments on commit 8636a3a

Please sign in to comment.