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 = {
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 22 | scheme: "ccnx",
|
| 23 | protocolFlags: nsIProtocolHandler.URI_NORELATIVE |
|
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 24 | nsIProtocolHandler.URI_NOAUTH |
|
| 25 | nsIProtocolHandler.URI_LOADABLE_BY_ANYONE,
|
| 26 |
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 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 | },
|
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 33 |
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 34 | newChannel: function(aURI)
|
| 35 | {
|
| 36 | try {
|
Jeff Thompson | d4617ff | 2012-10-25 20:40:53 -0700 | [diff] [blame] | 37 | var trimmedSpec = aURI.spec.trim();
|
Jeff Thompson | df0a6f7 | 2012-10-21 15:58:58 -0700 | [diff] [blame] | 38 |
|
Jeff Thompson | 6576bbb | 2012-10-28 22:20:02 -0700 | [diff] [blame] | 39 | var contentChannel;
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 40 | var requestContent = function(contentListener) {
|
Jeff Thompson | 25b0641 | 2012-10-21 20:07:57 -0700 | [diff] [blame] | 41 | // Set nameString to the URI without the protocol.
|
Jeff Thompson | d4617ff | 2012-10-25 20:40:53 -0700 | [diff] [blame] | 42 | var nameString = trimmedSpec;
|
Jeff Thompson | 25b0641 | 2012-10-21 20:07:57 -0700 | [diff] [blame] | 43 | var colonIndex = nameString.indexOf(':');
|
| 44 | if (colonIndex >= 0)
|
Jeff Thompson | d4617ff | 2012-10-25 20:40:53 -0700 | [diff] [blame] | 45 | nameString = nameString.substr
|
| 46 | (colonIndex + 1, nameString.length - colonIndex - 1).trim();
|
Jeff Thompson | 25b0641 | 2012-10-21 20:07:57 -0700 | [diff] [blame] | 47 |
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 48 | var name = new Name(nameString);
|
Jeff Thompson | 10de459 | 2012-10-21 23:54:18 -0700 | [diff] [blame] | 49 | // TODO: Strip off an ending implicit digest before checking the last component?
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 50 | var uriEndsWithSegmentNumber = endsWithSegmentNumber(name);
|
Jeff Thompson | 10de459 | 2012-10-21 23:54:18 -0700 | [diff] [blame] | 51 |
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 52 | var ndn = new NDN("lioncub.metwi.ucla.edu");
|
| 53 | ndn.expressInterest(name, new ContentClosure
|
| 54 | (ndn, contentListener, uriEndsWithSegmentNumber, aURI.originCharset));
|
| 55 | };
|
Jeff Thompson | 57d0738 | 2012-10-29 23:25:54 -0700 | [diff] [blame] | 56 |
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 57 | contentChannel = new ContentChannel(aURI, requestContent);
|
Jeff Thompson | 6576bbb | 2012-10-28 22:20:02 -0700 | [diff] [blame] | 58 | return contentChannel;
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 59 | } catch (ex) {
|
| 60 | dump("CcnxProtocol.newChannel exception: " + ex + "\n");
|
| 61 | }
|
| 62 | },
|
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 63 |
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 64 | classDescription: "ccnx Protocol Handler",
|
| 65 | contractID: "@mozilla.org/network/protocol;1?name=" + "ccnx",
|
| 66 | classID: Components.ID('{8122e660-1012-11e2-892e-0800200c9a66}'),
|
| 67 | QueryInterface: XPCOMUtils.generateQI([Ci.nsIProtocolHandler])
|
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 68 | }
|
| 69 |
|
| 70 | if (XPCOMUtils.generateNSGetFactory)
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 71 | var NSGetFactory = XPCOMUtils.generateNSGetFactory([CcnxProtocol]);
|
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 72 | else
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 73 | var NSGetModule = XPCOMUtils.generateNSGetModule([CcnxProtocol]);
|
| 74 |
|
| 75 | /*
|
| 76 | * Create a closure for calling expressInterest.
|
| 77 | * contentListener is from the call to requestContent.
|
| 78 | * uriEndsWithSegmentNumber is true if the URI passed to newChannel has a segment number
|
| 79 | * (used to determine whether to request only that segment number and for updating the URL bar).
|
| 80 | * uriOriginCharset is the charset of the URI passed to newChannel (used for making a new URI)
|
| 81 | */
|
| 82 | var ContentClosure = function ContentClosure
|
| 83 | (ndn, contentListener, uriEndsWithSegmentNumber, uriOriginCharset) {
|
| 84 | // Inherit from Closure.
|
| 85 | Closure.call(this);
|
| 86 |
|
| 87 | this.ndn = ndn;
|
| 88 | this.contentListener = contentListener;
|
| 89 | this.uriEndsWithSegmentNumber = uriEndsWithSegmentNumber;
|
| 90 | this.uriOriginCharset = uriOriginCharset;
|
| 91 | this.firstReceivedSegmentNumber = null;
|
| 92 | this.firstReceivedContentObject = null;
|
| 93 | }
|
| 94 |
|
| 95 | ContentClosure.prototype.upcall = function(kind, upcallInfo) {
|
| 96 | if (!(kind == Closure.UPCALL_CONTENT ||
|
| 97 | kind == Closure.UPCALL_CONTENT_UNVERIFIED))
|
| 98 | // The upcall is not for us.
|
| 99 | return Closure.RESULT_ERR;
|
| 100 |
|
| 101 | var contentObject = upcallInfo.contentObject;
|
| 102 | if (contentObject.content == null) {
|
| 103 | dump("CcnxProtocol.ContentClosure: contentObject.content is null\n");
|
| 104 | return Closure.RESULT_ERR;
|
| 105 | }
|
| 106 |
|
| 107 | // If !this.uriEndsWithSegmentNumber, we use the segmentNumber to load multiple segments.
|
| 108 | var segmentNumber = null;
|
| 109 | if (!this.uriEndsWithSegmentNumber && endsWithSegmentNumber(contentObject.name)) {
|
| 110 | segmentNumber = DataUtils.bigEndianToUnsignedInt
|
| 111 | (contentObject.name.components[contentObject.name.components.length - 1]);
|
| 112 | if (this.firstReceivedSegmentNumber == null) {
|
| 113 | // This is the first call.
|
| 114 | this.firstReceivedSegmentNumber = segmentNumber;
|
| 115 | if (segmentNumber != 0) {
|
| 116 | // Special case: Save this content object for later and request segment zero.
|
| 117 | this.firstReceivedContentObject = contentObject;
|
| 118 | var componentsForZero = contentObject.name.components.slice
|
| 119 | (0, contentObject.name.components.length - 1);
|
| 120 | componentsForZero.push([0]);
|
| 121 | this.ndn.expressInterest(new Name(componentsForZero), this);
|
| 122 | return Closure.RESULT_OK;
|
| 123 | }
|
| 124 | }
|
| 125 | }
|
| 126 |
|
| 127 | if (this.uriEndsWithSegmentNumber || segmentNumber == null || segmentNumber == 0) {
|
| 128 | // This is the first or only segment, so start.
|
| 129 | // Get the URI from the ContentObject including the version.
|
| 130 | var contentUriSpec;
|
| 131 | if (!this.uriEndsWithSegmentNumber && endsWithSegmentNumber(contentObject.name)) {
|
| 132 | var nameWithoutSegmentNumber = new Name
|
| 133 | (contentObject.name.components.slice
|
| 134 | (0, contentObject.name.components.length - 1));
|
| 135 | contentUriSpec = "ccnx:" + nameWithoutSegmentNumber.to_uri();
|
| 136 | }
|
| 137 | else
|
| 138 | contentUriSpec = "ccnx:" + contentObject.name.to_uri();
|
| 139 |
|
| 140 | var contentTypeEtc = getContentTypeAndCharset(contentObject.name);
|
| 141 | var ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
|
| 142 | this.contentListener.onStart(contentTypeEtc.contentType, contentTypeEtc.contentCharset,
|
| 143 | ioService.newURI(contentUriSpec, this.uriOriginCharset, null));
|
| 144 | }
|
| 145 |
|
| 146 | this.contentListener.onReceivedContent(DataUtils.toString(contentObject.content));
|
| 147 |
|
| 148 | // Check for the special case if the saved content is for the next segment that we need.
|
| 149 | if (this.firstReceivedContentObject != null &&
|
| 150 | this.firstReceivedSegmentNumber == segmentNumber + 1) {
|
| 151 | // Substitute the saved contentObject send its content and keep going.
|
| 152 | contentObject = this.firstReceivedContentObject;
|
| 153 | segmentNumber = segmentNumber + 1;
|
| 154 | // Clear firstReceivedContentObject to save memory.
|
| 155 | this.firstReceivedContentObject = null;
|
| 156 |
|
| 157 | this.contentListener.onReceivedContent(DataUtils.toString(contentObject.content));
|
| 158 | }
|
| 159 |
|
| 160 | var finalSegmentNumber = null;
|
| 161 | if (contentObject.signedInfo != null && contentObject.signedInfo.finalBlockID != null)
|
| 162 | finalSegmentNumber = DataUtils.bigEndianToUnsignedInt(contentObject.signedInfo.finalBlockID);
|
| 163 |
|
| 164 | if (!this.uriEndsWithSegmentNumber &&
|
| 165 | segmentNumber != null &&
|
| 166 | (finalSegmentNumber == null || segmentNumber != finalSegmentNumber)) {
|
| 167 | // Make a name for the next segment and get it.
|
| 168 | var nextSegmentNumber = DataUtils.nonNegativeIntToBigEndian(segmentNumber + 1);
|
| 169 | nextSegmentNumber.unshift(0);
|
| 170 | var components = contentObject.name.components.slice
|
| 171 | (0, contentObject.name.components.length - 1);
|
| 172 | components.push(nextSegmentNumber);
|
| 173 | this.ndn.expressInterest(new Name(components), this);
|
| 174 | }
|
| 175 | else
|
| 176 | // Finished.
|
| 177 | this.contentListener.onStop();
|
| 178 |
|
| 179 | return Closure.RESULT_OK;
|
| 180 | };
|
| 181 |
|
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 182 |
|
Jeff Thompson | df0a6f7 | 2012-10-21 15:58:58 -0700 | [diff] [blame] | 183 | /*
|
Jeff Thompson | 25b0641 | 2012-10-21 20:07:57 -0700 | [diff] [blame] | 184 | * Scan the name from the last component to the first (skipping special CCNx components)
|
| 185 | * for a recognized file name extension, and return an object with properties contentType and charset.
|
Jeff Thompson | df0a6f7 | 2012-10-21 15:58:58 -0700 | [diff] [blame] | 186 | */
|
| 187 | function getContentTypeAndCharset(name) {
|
Jeff Thompson | 25b0641 | 2012-10-21 20:07:57 -0700 | [diff] [blame] | 188 | for (var i = name.components.length - 1; i >= 0; --i) {
|
| 189 | var component = name.components[i];
|
| 190 | if (component.length <= 0)
|
| 191 | continue;
|
| 192 |
|
| 193 | // Skip special components which just may have ".gif", etc.
|
| 194 | if (component[0] == 0 || component[0] == 0xC0 || component[0] == 0xC1 ||
|
| 195 | (component[0] >= 0xF5 && component[0] <= 0xFF))
|
| 196 | continue;
|
| 197 |
|
| 198 | var str = DataUtils.toString(component).toLowerCase();
|
| 199 | if (str.indexOf(".gif") >= 0)
|
Jeff Thompson | 10de459 | 2012-10-21 23:54:18 -0700 | [diff] [blame] | 200 | return { contentType: "image/gif", charset: "ISO-8859-1" }
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 201 | else if (str.indexOf(".jpg") >= 0 ||
|
| 202 | str.indexOf(".jpeg") >= 0)
|
Jeff Thompson | 10de459 | 2012-10-21 23:54:18 -0700 | [diff] [blame] | 203 | return { contentType: "image/jpeg", charset: "ISO-8859-1" }
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 204 | else if (str.indexOf(".png") >= 0)
|
Jeff Thompson | 10de459 | 2012-10-21 23:54:18 -0700 | [diff] [blame] | 205 | return { contentType: "image/png", charset: "ISO-8859-1" }
|
Jeff Thompson | 25b0641 | 2012-10-21 20:07:57 -0700 | [diff] [blame] | 206 | else if (str.indexOf(".bmp") >= 0)
|
Jeff Thompson | 10de459 | 2012-10-21 23:54:18 -0700 | [diff] [blame] | 207 | return { contentType: "image/bmp", charset: "ISO-8859-1" }
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 208 | else if (str.indexOf(".css") >= 0)
|
Jeff Thompson | 10de459 | 2012-10-21 23:54:18 -0700 | [diff] [blame] | 209 | return { contentType: "text/css", charset: "utf-8" }
|
Jeff Thompson | 25b0641 | 2012-10-21 20:07:57 -0700 | [diff] [blame] | 210 | }
|
Jeff Thompson | df0a6f7 | 2012-10-21 15:58:58 -0700 | [diff] [blame] | 211 |
|
Jeff Thompson | 25b0641 | 2012-10-21 20:07:57 -0700 | [diff] [blame] | 212 | // default
|
| 213 | return { contentType: "text/html", charset: "utf-8" };
|
Jeff Thompson | df0a6f7 | 2012-10-21 15:58:58 -0700 | [diff] [blame] | 214 | }
|
Jeff Thompson | 10de459 | 2012-10-21 23:54:18 -0700 | [diff] [blame] | 215 |
|
| 216 | /*
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 217 | * Return true if the last component in the name is a segment number..
|
Jeff Thompson | 10de459 | 2012-10-21 23:54:18 -0700 | [diff] [blame] | 218 | */
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 219 | function endsWithSegmentNumber(name) {
|
Jeff Thompson | 10de459 | 2012-10-21 23:54:18 -0700 | [diff] [blame] | 220 | return name.components != null && name.components.length >= 1 &&
|
| 221 | name.components[name.components.length - 1].length >= 1 &&
|
| 222 | name.components[name.components.length - 1][0] == 0;
|
| 223 | } |