Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 1 | /*
|
| 2 | * @author: ucla-cs
|
| 3 | * This is the ccnx protocol handler.
|
| 4 | * Protocol handling code derived from http://mike.kaply.com/2011/01/18/writing-a-firefox-protocol-handler/
|
| 5 | */
|
| 6 |
|
| 7 | const Cc = Components.classes;
|
| 8 | const Ci = Components.interfaces;
|
| 9 | const Cr = Components.results;
|
| 10 |
|
| 11 | const nsIProtocolHandler = Ci.nsIProtocolHandler;
|
| 12 |
|
| 13 | Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
|
| 14 | Components.utils.import("chrome://modules/content/ndn-js.jsm");
|
| 15 | Components.utils.import("chrome://modules/content/ContentChannel.jsm");
|
| 16 |
|
| 17 | function CcnxProtocol() {
|
| 18 | }
|
| 19 |
|
| 20 | CcnxProtocol.prototype = {
|
| 21 | scheme: "ccnx",
|
| 22 | protocolFlags: nsIProtocolHandler.URI_NORELATIVE |
|
| 23 | nsIProtocolHandler.URI_NOAUTH |
|
| 24 | nsIProtocolHandler.URI_LOADABLE_BY_ANYONE,
|
| 25 |
|
| 26 | newURI: function(aSpec, aOriginCharset, aBaseURI)
|
| 27 | {
|
| 28 | var uri = Cc["@mozilla.org/network/simple-uri;1"].createInstance(Ci.nsIURI);
|
| 29 | uri.spec = aSpec;
|
| 30 | return uri;
|
| 31 | },
|
| 32 |
|
| 33 | newChannel: function(aURI)
|
| 34 | {
|
| 35 | try {
|
| 36 | var requestContent = function(contentListener) {
|
| 37 | var interest = aURI.spec.split(":")[1];
|
Jeff Thompson | f076145 | 2012-10-09 23:17:40 -0700 | [diff] [blame] | 38 | // 131.179.141.18 is lioncub.metwi.ucla.edu .
|
| 39 | var ndn = new NDN('131.179.141.18');
|
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 40 |
|
| 41 | var coListener = {
|
| 42 | onReceivedContentObject : function(contentObject) {
|
| 43 | // Set up defaults.
|
| 44 | var content = "";
|
| 45 | var contentType = "text/html";
|
| 46 | var contentCharset = "utf-8";
|
Jeff Thompson | f076145 | 2012-10-09 23:17:40 -0700 | [diff] [blame] | 47 |
|
| 48 | // TODO: Need to check the signature, confirm that the name matches, etc.
|
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 49 | if (contentObject.content != null) {
|
| 50 | content = DataUtils.toString(contentObject.content);
|
Jeff Thompson | 3651251 | 2012-10-10 03:23:29 -0700 | [diff] [blame^] | 51 |
|
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 52 | // TODO: Should look at the returned Name to get contentType. For now,
|
Jeff Thompson | 3651251 | 2012-10-10 03:23:29 -0700 | [diff] [blame^] | 53 | // just look for a file extension in the original interest.
|
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 54 | var interestLowerCase = interest.toLowerCase();
|
| 55 | if (interestLowerCase.indexOf(".gif") >= 0) {
|
| 56 | contentType = "image/gif";
|
| 57 | contentCharset = "ISO-8859-1";
|
| 58 | }
|
| 59 | else if (interestLowerCase.indexOf(".jpg") >= 0 ||
|
| 60 | interestLowerCase.indexOf(".jpeg") >= 0) {
|
| 61 | contentType = "image/jpeg";
|
| 62 | contentCharset = "ISO-8859-1";
|
| 63 | }
|
| 64 | else if (interestLowerCase.indexOf(".png") >= 0) {
|
| 65 | contentType = "image/png";
|
| 66 | contentCharset = "ISO-8859-1";
|
| 67 | }
|
| 68 | else if (interestLowerCase.indexOf(".bmp") >= 0) {
|
| 69 | contentType = "image/bmp";
|
| 70 | contentCharset = "ISO-8859-1";
|
| 71 | }
|
Jeff Thompson | 3651251 | 2012-10-10 03:23:29 -0700 | [diff] [blame^] | 72 | else if (interestLowerCase.indexOf(".css") >= 0)
|
| 73 | contentType = "text/css";
|
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 74 | }
|
| 75 |
|
| 76 | contentListener.onReceivedContent(content, contentType, contentCharset);
|
| 77 | }
|
| 78 | };
|
| 79 |
|
| 80 | ndn.getAsync(interest, coListener);
|
| 81 | };
|
| 82 |
|
| 83 | return new ContentChannel(aURI, requestContent);
|
| 84 | } catch (ex) {
|
| 85 | dump("CcnxProtocol.newChannel exception: " + ex + "\n");
|
| 86 | }
|
| 87 | },
|
| 88 |
|
| 89 | classDescription: "ccnx Protocol Handler",
|
| 90 | contractID: "@mozilla.org/network/protocol;1?name=" + "ccnx",
|
| 91 | classID: Components.ID('{8122e660-1012-11e2-892e-0800200c9a66}'),
|
| 92 | QueryInterface: XPCOMUtils.generateQI([Ci.nsIProtocolHandler])
|
| 93 | }
|
| 94 |
|
| 95 | if (XPCOMUtils.generateNSGetFactory)
|
| 96 | var NSGetFactory = XPCOMUtils.generateNSGetFactory([CcnxProtocol]);
|
| 97 | else
|
| 98 | var NSGetModule = XPCOMUtils.generateNSGetModule([CcnxProtocol]);
|
| 99 |
|
| 100 |
|