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