diff options
author | Alex Xu <alex_y_xu@yahoo.ca> | 2014-12-09 19:18:15 -0500 |
---|---|---|
committer | Alex Xu <alex_y_xu@yahoo.ca> | 2014-12-09 19:18:15 -0500 |
commit | 7817f670d4cce62937e6a5a286210dc3267615c3 (patch) | |
tree | 37254a42a7619997ad8a4cb68d7882d06f3f49ea /src/bootstrap.js | |
parent | 3a93c00d336cc9e9201e41a9bacfcd2e916368b7 (diff) | |
download | eib-7817f670d4cce62937e6a5a286210dc3267615c3.tar.xz eib-7817f670d4cce62937e6a5a286210dc3267615c3.zip |
WIP stuff.wip
Diffstat (limited to 'src/bootstrap.js')
-rw-r--r-- | src/bootstrap.js | 128 |
1 files changed, 73 insertions, 55 deletions
diff --git a/src/bootstrap.js b/src/bootstrap.js index 162c857..e98037b 100644 --- a/src/bootstrap.js +++ b/src/bootstrap.js @@ -6,70 +6,86 @@ if (typeof EIB === "undefined") const Cc = Components.classes, Ci = Components.interfaces, Cu = Components.utils, CC = Components.Constructor, Cr = Components.results; const ScriptableInputStream = CC("@mozilla.org/scriptableinputstream;1", "nsIScriptableInputStream", "init"); +const FileInputStream = CC("@mozilla.org/network/file-input-stream;1", "nsIFileInputStream", "init"); +const LocalFileFromPath = CC("@mozilla.org/file/local;1", "nsILocalFile", "initWithPath"); Cu.import("resource://gre/modules/Services.jsm"); function install() {} function uninstall() {} -EIB.listen = function () { - this.serverSocket = Cc["@mozilla.org/network/server-socket;1"] - .createInstance(Ci.nsIServerSocket); - this.serverSocket.init(26001, true, -1); - const tm = Cc["@mozilla.org/thread-manager;1"].getService(); - this.serverSocket.asyncListen({ - onSocketAccepted: function (socket, transport) { - var is = transport.openInputStream(0, 0, 0); - var os = transport.openOutputStream(Ci.nsITransport.OPEN_BLOCKING, 0, 0); - var sis = new ScriptableInputStream(is); - var buf = ""; - // TODO: run on separate thread - is.asyncWait({ - onInputStreamReady: function () { - while (sis.available()) { - buf += sis.read(2048); - } - if (buf.indexOf("\n\n") !== 2) { - var headers = {}; - var rx = /\n(EVE_[^:]*): (.*)$/gm, arr; - while ((arr = rx.exec(buf)) !== null) { - headers[arr[1]] = arr[2]; - } +var ServerListener = { + onSocketAccepted: function (socket, transport) { + var is = transport.openInputStream(0, 0, 0); + var os = transport.openOutputStream(Ci.nsITransport.OPEN_BLOCKING, 0, 0); + var sis = new ScriptableInputStream(is); + var buf = ""; + // TODO: run on separate thread + is.asyncWait({ + onInputStreamReady: function () { + while (sis.available()) + buf += sis.read(2048); + + if (buf.indexOf("\r\n\r\n") > -1) { + is.close(); + + var path = / ([^ ]*) /.exec(buf)[1]; + + var headers = {}; + var rx = /\n(EVE_[^:]*): (.*)$/gm, arr; + while ((arr = rx.exec(buf)) !== null) + headers[arr[1]] = arr[2]; + + if (headers.EVE_TRUSTED == "Yes") EIB.headers = headers; - var resp = "HTTP/1.0 200 OK\r\n"; - resp += "Content-Type: text/html\r\n\r\n"; - resp += "<!DOCTYPE html><html><head><title>EVE-IGB Bridge</title>"; - if (headers["EVE_TRUSTED"]) { - resp += '<script>'; - if (headers["EVE_TRUSTED"] == "Yes") { - var delay = 100; - if (EIB.pending.length) { - resp += EIB.pending.shift(); - delay = 1000; - } - resp += 'setTimeout(function () { location.reload(); }, ' + delay + ');'; - } else { - resp += 'CCPEVE.requestTrust("http://127.0.0.1:' + EIB.serverSocket.port + '");'; - } - resp += "</script></head><body>"; - resp += headers["EVE_TRUSTED"] == "Yes" ? - "Connected to the EVE-IGB Bridge. Please do not close this window. You may minimize it, however. Note that this window will refresh repeatedly; this does not indicate a malfunction and will not count towards your internet data usage if you have one." : - "Please grant trust to this site, then refresh the page."; - resp += "</body></html>"; - } else { - resp += "</head><body>Please open this page in the EVE in-game browser.</body></html>"; - } - os.write(resp, resp.length); - - is.close(); + var type, file; + switch (path) { + case '/': + type = "text/html"; + file = headers.EVE_TRUSTED === "Yes" ? "trusted.html" : + headers.EVE_TRUSTED === "No" ? "untrusted.html" : + "unknown.html"; + break; + case '/trusted.js': + type = "application/javascript"; + file = "trusted.js"; + break; + case '/trusted': + type = "text/html"; + } + + var resp = "HTTP/1.0 " + type ? + "200 OK\r\nContent-Type: " + type + "\r\n\r\n" : + "404 Not Found\r\nContent-Type: text/plain\r\n\r\n404 Not Found\r\n"; + if (!type) { os.close(); + } else if (file) { + var channel = Services.io.newChannel("chrome://eib/content/igb/" + file, null, null); + channel.asyncOpen({ + onStartRequest: function () {}, + onDataAvailable: function (req, ctx, fis, offset, cnt) { + os.writeFrom(fis, cnt); + }, + onStopRequest: function (req, ctx, status) { + os.close(); + channel.close(); + } + }, null); } + } - }, 0, 0, tm.mainThread); - } - }); -} + } + }, 0, 0, Services.tm.mainThread); + } +}; + +EIB.listen = function () { + this.serverSocket = Cc["@mozilla.org/network/server-socket;1"] + .createInstance(Ci.nsIServerSocket); + this.serverSocket.init(26001, true, -1); + this.serverSocket.asyncListen(ServerListener); +}; var HttpObserver = { observe: function (subject, topic, data) { @@ -108,7 +124,7 @@ var WindowListener = { var TrustedReparser = { observe: function () { EIB.trusted = EIB.prefs.prefHasUserValue("trusted") ? - JSON.parse(EIB.prefs.getCharPref("trusted").map(function (v) { return new RegExp(v); }) : + JSON.parse(EIB.prefs.getCharPref("trusted")).map(function (v) { return new RegExp(v); }) : []; } }; @@ -148,11 +164,13 @@ function checkTrusted(href) { }); } +const exportFunctions = ["openEveMail", "showInfo", "showPreview", "showRouteTo", "showMap", "showFitting", "showContract", "showMarketDetails", "setDestination", "addWaypoint", "joinChannel", "joinMailingList", "createContract", "buyType", "findInContracts", "addToMarketQuickBar", "addContact", "removeContact", "addCorpContact", "removeCorpContact", "block", "addBounty", "inviteToFleet", "startConversation", "showContracts", "showOnMap", "editMember", "awardDecoration", "sendMail", "showContents", "bookmark"]; + function injectCCPEVE(e) { var window = e.originalTarget.defaultView; if (checkTrusted(window.location.href)) { var CCPEVE = Cu.createObjectIn(window, {defineAs: "CCPEVE"}); - ["openEveMail", "showInfo", "showPreview", "showRouteTo", "showMap", "showFitting", "showContract", "showMarketDetails", "setDestination", "addWaypoint", "joinChannel", "joinMailingList", "createContract", "buyType", "findInContracts", "addToMarketQuickBar", "addContact", "removeContact", "addCorpContact", "removeCorpContact", "block", "addBounty", "inviteToFleet", "startConversation", "showContracts", "showOnMap", "editMember", "awardDecoration", "sendMail", "showContents", "bookmark"].forEach(function (n) { + exportFunctions.forEach(function (n) { Object.defineProperty(CCPEVE, n, { value: Cu.exportFunction(function () { EIB.pending.push('CCPEVE.' + n + '(' + JSON.stringify([].slice.call(arguments)).slice(1, -1) + ');'); |