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