<commit_before>var {
  Cc, Ci, Cu, Cm, components
} = require('chrome');
var {
  ToggleButton
} = require('sdk/ui/button/toggle');
Cu.import('resource://gre/modules/XPCOMUtils.jsm');
var ioservice = Cc['@mozilla.org/network/io-service;1'].getService(Ci.nsIIOService);
var events = require('sdk/system/events');
var prefs = require('sdk/simple-prefs').prefs;

const version = require('./package.json').version;
const addonTitle = require('./package.json').title;

const IPFS_SCHEME = 'ipfs';
const PUBLIC_GATEWAY_URI = ioservice.newURI('http://gateway.ipfs.io/ipfs/', null, null);

function httpGatewayRoot() {
  if (prefs.useCustomGatewayRoot) {
    return prefs.customGatewayRoot;
  } else {
    return PUBLIC_GATEWAY_URI.spec;
  }
}


function httpGatewayListener(event) {
  let channel = event.subject.QueryInterface(Ci.nsIHttpChannel);
  let requestToPublicGateway = channel.URI.spec.startsWith(PUBLIC_GATEWAY_URI.spec);
  if (requestToPublicGateway && prefs.useCustomGatewayRoot) {
    console.info('Detected request to public HTTP gateway: ' + channel.URI.spec);
    let uri = ioservice.newURI(channel.URI.spec.replace(PUBLIC_GATEWAY_URI.spec, prefs.customGatewayRoot), null, null);
    console.info('Redirecting to custom HTTP gateway: ' + uri.spec);
    channel.setRequestHeader('x-ipfs-firefox-addon-version', version, false);
    channel.redirectTo(uri);
  }
}

function IpfsProtocolHandler() {}
IpfsProtocolHandler.prototype = Object.freeze({
  classDescription: 'IPFS Basic Protocol Handler',
  contractID: '@mozilla.org/network/protocol;1?name=' + IPFS_SCHEME,
  classID: components.ID('{34023afe-f3aa-4b3d-b546-cb66f6e14c4b}'),
  QueryInterface: XPCOMUtils.generateQI([Ci.nsIProtocolHandler]),

  scheme: IPFS_SCHEME,
  defaultPort: -1,
  allowPort: function(port, scheme) {
    return false;
  },

  protocolFlags: Ci.nsIProtocolHandler.URI_NOAUTH |
    Ci.nsIProtocolHandler.ALLOWS_PROXY_HTTP |
    Ci.nsIProtocolHandler.URI_LOADABLE_BY_ANYONE,

  newURI: function(aSpec, aOriginCharset, aBaseURI) {
    // presence of aBaseURI means a dependent resource or a relative link
    // and we need to return correct http URI
    if (aBaseURI && aBaseURI.scheme == IPFS_SCHEME) {
      //console.log('newURI/aSpec: ' + aSpec);
      //console.log('newURI/aBaseURI.spec: ' + aBaseURI.spec);
      let httpBaseURI = ioservice.newURI(PUBLIC_GATEWAY_URI.spec + aBaseURI.path, null, null);
      //console.log('newURI/httpBaseURI.spec: ' + httpBaseURI.spec);
      let newURI = ioservice.newURI(aSpec, aOriginCharset, httpBaseURI);
      //console.log('newURI/newURI.spec: ' + newURI.spec);
      return newURI;
    }
    let uri = Cc['@mozilla.org/network/simple-uri;1'].createInstance(Ci.nsIURI);
    uri.spec = aSpec;
    //uri.spec = (aBaseURI === null) ? aSpec : PUBLIC_GATEWAY_URI.resolve(aSpec);
    return uri;
  },

  newChannel: function(aURI) {
    console.info('Detected request using IPFS protocol hadler: ' + aURI.spec);
    let genericHttpLocation = PUBLIC_GATEWAY_URI.spec + aURI.path;
    let channel = ioservice.newChannel(genericHttpLocation, aURI.originCharset, null);
    console.info('Request routed to HTTP gateway:  ' + genericHttpLocation);

    // line below would keep nice protocol URL in GUI
    // but is disabled for now due to issues like
    // https://github.com/lidel/ipfs-firefox-addon/issues/3
    //channel.originalURI = aURI;

    return channel;
  },

  factory: Object.freeze({
    createInstance: function(aOuter, aIID) {
      if (aOuter) {
        throw Cr.NS_ERROR_NO_AGGREGATION;
      }
      //return (new IpfsProtocolHandler()).QueryInterface(aIID);
      return new IpfsProtocolHandler();
    },
    register: function() {
      let m = Cm.QueryInterface(Ci.nsIComponentRegistrar);
      let p = IpfsProtocolHandler.prototype;
      m.registerFactory(p.classID, p.classDescription, p.contractID, this);
      console.info('IPFS Protocol Handler registered.');
    },
    unregister: function() {
      let m = Cm.QueryInterface(Ci.nsIComponentRegistrar);
      m.unregisterFactory(IpfsProtocolHandler.prototype.classID, this);
      console.info('IPFS Protocol Handler unregistered.');
    },
  }),

});

function enableHttpGatewayRedirect() {
  prefs.useCustomGatewayRoot = true;
  events.on('http-on-modify-request', httpGatewayListener);
}

function disableHttpGatewayRedirect() {
  prefs.useCustomGatewayRoot = false;
  events.off('http-on-modify-request', httpGatewayListener);<commit_msg>Redirect ipfs: protocol using real HTTP gateway URI<commit_after>var {
  Cc, Ci, Cu, Cm, components
} = require('chrome');
var {
  ToggleButton
} = require('sdk/ui/button/toggle');
Cu.import('resource://gre/modules/XPCOMUtils.jsm');
var ioservice = Cc['@mozilla.org/network/io-service;1'].getService(Ci.nsIIOService);
var events = require('sdk/system/events');
var prefs = require('sdk/simple-prefs').prefs;

const version = require('./package.json').version;
const addonTitle = require('./package.json').title;

const IPFS_SCHEME = 'ipfs';
const PUBLIC_GATEWAY_URI = ioservice.newURI('http://gateway.ipfs.io/ipfs/', null, null);

function httpGatewayRoot() {
  if (prefs.useCustomGatewayRoot) {
    return prefs.customGatewayRoot;
  } else {
    return PUBLIC_GATEWAY_URI.spec;
  }
}


function httpGatewayListener(event) {
  let channel = event.subject.QueryInterface(Ci.nsIHttpChannel);
  let requestToPublicGateway = channel.URI.spec.startsWith(PUBLIC_GATEWAY_URI.spec);
  if (requestToPublicGateway && prefs.useCustomGatewayRoot) {
    console.info('Detected request to public HTTP gateway: ' + channel.URI.spec);
    let uri = ioservice.newURI(channel.URI.spec.replace(PUBLIC_GATEWAY_URI.spec, prefs.customGatewayRoot), null, null);
    console.info('Redirecting to custom HTTP gateway: ' + uri.spec);
    channel.setRequestHeader('x-ipfs-firefox-addon-version', version, false);
    channel.redirectTo(uri);
  }
}

function IpfsProtocolHandler() {}
IpfsProtocolHandler.prototype = Object.freeze({
  classDescription: 'IPFS Basic Protocol Handler',
  contractID: '@mozilla.org/network/protocol;1?name=' + IPFS_SCHEME,
  classID: components.ID('{34023afe-f3aa-4b3d-b546-cb66f6e14c4b}'),
  QueryInterface: XPCOMUtils.generateQI([Ci.nsIProtocolHandler]),

  scheme: IPFS_SCHEME,
  defaultPort: -1,
  allowPort: function(port, scheme) {
    return false;
  },

  protocolFlags: Ci.nsIProtocolHandler.URI_NOAUTH |
    Ci.nsIProtocolHandler.URI_LOADABLE_BY_ANYONE,

  newURI: function(aSpec, aOriginCharset, aBaseURI) {
    // presence of aBaseURI means a dependent resource or a relative link
    // and we need to return correct http URI
    if (aBaseURI && aBaseURI.scheme == this.scheme) {
      let httpBaseURI = ioservice.newURI(PUBLIC_GATEWAY_URI.spec + aBaseURI.path, null, null);
      let newURI = ioservice.newURI(aSpec, aOriginCharset, httpBaseURI);
      return newURI;
    }
    /* Left for future use (if we enable channel.originalURI in newChannel method)
    let uri = Cc['@mozilla.org/network/simple-uri;1'].createInstance(Ci.nsIURI);
    uri.spec = aSpec;
    return uri;
    */
    let ipfsPath = aSpec.replace(/^ipfs:/,'');
    let newURI = ioservice.newURI(PUBLIC_GATEWAY_URI.spec + ipfsPath, aOriginCharset, null);
    return newURI;
  },

  newChannel: function(aURI) {
    console.info('Detected request using IPFS protocol hadler: ' + aURI.spec);
    let genericHttpLocation = PUBLIC_GATEWAY_URI.spec + aURI.path;
    let channel = ioservice.newChannel(genericHttpLocation, aURI.originCharset, null);
    console.info('Request routed to HTTP gateway:  ' + genericHttpLocation);

    // line below would keep nice protocol URL in GUI
    // but is disabled for now due to issues like
    // https://github.com/lidel/ipfs-firefox-addon/issues/3
    //channel.originalURI = aURI;

    return channel;
  },

  factory: Object.freeze({
    createInstance: function(aOuter, aIID) {
      if (aOuter) {
        throw Cr.NS_ERROR_NO_AGGREGATION;
      }
      return new IpfsProtocolHandler();
    },
    register: function() {
      let m = Cm.QueryInterface(Ci.nsIComponentRegistrar);
      let p = IpfsProtocolHandler.prototype;
      m.registerFactory(p.classID, p.classDescription, p.contractID, this);
      console.info('IPFS Protocol Handler registered.');
    },
    unregister: function() {
      let m = Cm.QueryInterface(Ci.nsIComponentRegistrar);
      m.unregisterFactory(IpfsProtocolHandler.prototype.classID, this);
      console.info('IPFS Protocol Handler unregistered.');
    },
  }),

});

function enableHttpGatewayRedirect() {
  prefs.useCustomGatewayRoot = true;
  events.on('http-on-modify-request', httpGatewayListener);
}

function disableHttpGatewayRedirect() {
  prefs.useCustomGatewayRoot = false;
  events.off('http-on-modify-request', httpGatewayListener);
{'fraction_non_alphanumeric': 0.1271418779986292, 'fraction_numerical': 0.005026273703449852, 'mean_word_length': 4.18968583283936, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '429549', 'n_tokens_mistral': 2942, 'n_tokens_neox': 2893, 'n_words': 545}