forked from ipfs/ipfs-companion
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support for fs: URIs without redirecting to http:
fixes ipfs#48
- Loading branch information
Showing
8 changed files
with
210 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
'use strict' | ||
|
||
const {Ci} = require('chrome') | ||
const events = require('sdk/system/events') | ||
const gw = require('./gateways.js') | ||
|
||
/* | ||
const catman = Cc['@mozilla.org/categorymanager;1'].getService(CI.nsICategoryManager) | ||
// category ("net-channel-event-sinks") | ||
catman.addCategoryEntry(in string aCategory, in string aEntry, in string aValue, in boolean aPersist, in boolean aReplace) | ||
*/ | ||
|
||
function fixupRequest (e) { | ||
if (e.type !== 'http-on-modify-request') { | ||
return | ||
} | ||
if (!gw.fsUris) { | ||
return | ||
} | ||
const channel = e.subject.QueryInterface(Ci.nsIHttpChannel) | ||
|
||
channel.QueryInterface(Ci.nsIHttpChannelInternal) | ||
channel.QueryInterface(Ci.nsIPropertyBag2) | ||
channel.QueryInterface(Ci.nsIPropertyBag) | ||
|
||
let isIpfsReq = null | ||
try { | ||
isIpfsReq = channel.hasKey('ipfs-uri') | ||
} catch (e) { | ||
// console.log(e) | ||
} | ||
|
||
if (isIpfsReq && channel.originalURI.scheme === 'fs') { | ||
/* | ||
// TODO: investigate effects of the following flags | ||
// cookies make no sense in the ipfs context, we don't want to carry different gateway cookies into the page | ||
channel.loadFlags |= Ci.nsIRequest.LOAD_ANONYMOUS | ||
// should only do that for /ipfs/ paths since those are stable | ||
channel.loadFlags |= Ci.nsIRequest.VALIDATE_NEVER | ||
*/ | ||
|
||
// prevent redirects from replacing the effective URI | ||
channel.loadFlags &= ~Ci.nsIChannel.LOAD_REPLACE | ||
|
||
} | ||
|
||
} | ||
|
||
events.on('http-on-modify-request', fixupRequest) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
'use strict' | ||
|
||
const tabs = require('sdk/tabs') | ||
const protocols = require('../lib/protocols.js') | ||
const fsFactory = protocols.fs | ||
|
||
protocols.register() | ||
|
||
const fs = fsFactory.createInstance() | ||
const gw = require('../lib/gateways.js') | ||
const self = require('sdk/self') | ||
const testpage = self.data.url('linkify-demo.html') | ||
const sripage = 'fs:/ipfs/QmSrCRJmzE4zE1nAfWPbzVfanKQNBhp7ZWmMnEdbiLvYNh/mdown#sample.md' | ||
const parent = require('sdk/remote/parent') | ||
|
||
const childMain = require.resolve('../lib/child-main.js') | ||
|
||
parent.remoteRequire(childMain) | ||
|
||
const {Cc, Ci} = require('chrome') | ||
const ioservice = Cc['@mozilla.org/network/io-service;1'].getService(Ci.nsIIOService) | ||
|
||
ioservice.newURI('fs:/ipns/foo', null, null) | ||
|
||
exports['test newURI'] = function (assert) { | ||
require('sdk/simple-prefs').prefs.fsUris = true | ||
|
||
assert.equal(fs.newURI('fs:/ipns/foo', null, null).spec, 'fs:/ipns/foo', 'keeps fs:/ uris as-is') | ||
} | ||
|
||
exports['test newChannel'] = function (assert) { | ||
require('sdk/simple-prefs').prefs.fsUris = true | ||
|
||
let uri = fs.newURI('fs:///ipns/foo', null, null) | ||
let chan = fs.newChannel(uri) | ||
|
||
assert.equal(chan.originalURI.spec, 'fs:/ipns/foo', "keeps fs: URI as channel's originalURI") | ||
|
||
// double and triple slashes lead to gateway redirects, which cause CORS troubles -> check normalization | ||
assert.equal(chan.URI.spec, 'https://ipfs.io/ipns/foo', 'channel has normalized http urls') | ||
} | ||
|
||
// https://github.com/lidel/ipfs-firefox-addon/issues/3 | ||
exports['test subresource loading'] = function (assert, done) { | ||
require('sdk/simple-prefs').prefs.fsUris = true | ||
gw.redirectEnabled = false | ||
|
||
tabs.open({ | ||
url: testpage, | ||
onReady: (tab) => { | ||
|
||
// first load somehow doesn't have protocol handlers registered. so load resource:// first, then redirect to fs:/ page | ||
if (tab.url !== sripage) { | ||
tab.url = sripage | ||
tab.reload() | ||
return | ||
} | ||
|
||
let worker = tab.attach({ | ||
contentScript: ` | ||
let obs = new MutationObserver(function(mutations) { | ||
let result = (document.querySelector("#ipfs-markdown-reader") instanceof HTMLHeadingElement) | ||
self.port.emit("test result", {result: result}) | ||
}) | ||
obs.observe(document.body,{childList: true}) | ||
` | ||
}) | ||
worker.port.on('test result', (msg) => { | ||
assert.equal(msg.result, true, 'subresource loaded successfully') | ||
|
||
require('sdk/simple-prefs').prefs.fsUris = false | ||
tab.close(done) | ||
}) | ||
} | ||
}) | ||
} | ||
|
||
require('./prefs-util.js').isolateTestCases(exports) | ||
require('sdk/test').run(exports) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters