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 | {
|
Jeff Thompson | d6b61e4 | 2012-11-24 12:10:35 -0800 | [diff] [blame] | 30 | // We have to trim now because nsIURI converts spaces to %20 and we can't trim in newChannel.
|
| 31 | var spec = aSpec.trim();
|
| 32 | var preSearch = spec.split('?', 1)[0];
|
| 33 | var searchAndHash = spec.substr(preSearch.length).trim();
|
| 34 |
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 35 | var uri = Cc["@mozilla.org/network/simple-uri;1"].createInstance(Ci.nsIURI);
|
Jeff Thompson | d6b61e4 | 2012-11-24 12:10:35 -0800 | [diff] [blame] | 36 | uri.spec = preSearch.trim() + searchAndHash;
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 37 | return uri;
|
| 38 | },
|
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 39 |
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 40 | newChannel: function(aURI)
|
| 41 | {
|
Jeff Thompson | 17a9da8 | 2012-11-12 01:11:01 -0800 | [diff] [blame] | 42 | var thisCcnxProtocol = this;
|
| 43 |
|
Jeff Thompson | 1eea632 | 2012-11-23 16:56:18 -0800 | [diff] [blame] | 44 | try {
|
Jeff Thompson | d6b61e4 | 2012-11-24 12:10:35 -0800 | [diff] [blame] | 45 | // Decode manually since nsIURI doesn't have selectors for hash, etc.
|
Jeff Thompson | 1eea632 | 2012-11-23 16:56:18 -0800 | [diff] [blame] | 46 | var spec = aURI.spec.trim();
|
| 47 | var preHash = spec.split('#', 1)[0];
|
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 48 | var hash = spec.substr(preHash.length).trim();
|
Jeff Thompson | 1eea632 | 2012-11-23 16:56:18 -0800 | [diff] [blame] | 49 | var preSearch = preHash.split('?', 1)[0];
|
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 50 | var search = preHash.substr(preSearch.length).trim();
|
Jeff Thompson | 1eea632 | 2012-11-23 16:56:18 -0800 | [diff] [blame] | 51 | // Set nameString to the preSearch without the protocol.
|
| 52 | var nameString = preSearch.trim();
|
| 53 | if (nameString.indexOf(':') >= 0)
|
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 54 | nameString = nameString.substr(nameString.indexOf(':') + 1).trim();
|
Jeff Thompson | df0a6f7 | 2012-10-21 15:58:58 -0700 | [diff] [blame] | 55 |
|
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 56 | var template = new Interest(new Name([]));
|
| 57 | // Use the same default as NDN.expressInterest.
|
| 58 | template.interestLifetime = 4200;
|
| 59 | var searchWithoutCcnx = extractCcnxSearch(search, template);
|
| 60 |
|
Jeff Thompson | 1eea632 | 2012-11-23 16:56:18 -0800 | [diff] [blame] | 61 | var requestContent = function(contentListener) {
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 62 | var name = new Name(nameString);
|
Jeff Thompson | 10de459 | 2012-10-21 23:54:18 -0700 | [diff] [blame] | 63 | // TODO: Strip off an ending implicit digest before checking the last component?
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 64 | var uriEndsWithSegmentNumber = endsWithSegmentNumber(name);
|
Jeff Thompson | 10de459 | 2012-10-21 23:54:18 -0700 | [diff] [blame] | 65 |
|
Jeff Thompson | 17a9da8 | 2012-11-12 01:11:01 -0800 | [diff] [blame] | 66 | var ndn = new NDN({ host: "lioncub.metwi.ucla.edu", port: 9695,
|
| 67 | // Use the same transport object each time.
|
| 68 | getTransport: function() { return thisCcnxProtocol.transport; } });
|
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 69 | ndn.expressInterest(name,
|
| 70 | new ContentClosure(ndn, contentListener, uriEndsWithSegmentNumber,
|
| 71 | aURI.originCharset, searchWithoutCcnx + hash),
|
| 72 | template);
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 73 | };
|
Jeff Thompson | 57d0738 | 2012-10-29 23:25:54 -0700 | [diff] [blame] | 74 |
|
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 75 | return new ContentChannel(aURI, requestContent);
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 76 | } catch (ex) {
|
| 77 | dump("CcnxProtocol.newChannel exception: " + ex + "\n");
|
| 78 | }
|
| 79 | },
|
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 80 |
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 81 | classDescription: "ccnx Protocol Handler",
|
| 82 | contractID: "@mozilla.org/network/protocol;1?name=" + "ccnx",
|
| 83 | classID: Components.ID('{8122e660-1012-11e2-892e-0800200c9a66}'),
|
| 84 | QueryInterface: XPCOMUtils.generateQI([Ci.nsIProtocolHandler])
|
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 85 | }
|
| 86 |
|
| 87 | if (XPCOMUtils.generateNSGetFactory)
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 88 | var NSGetFactory = XPCOMUtils.generateNSGetFactory([CcnxProtocol]);
|
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 89 | else
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 90 | var NSGetModule = XPCOMUtils.generateNSGetModule([CcnxProtocol]);
|
| 91 |
|
| 92 | /*
|
| 93 | * Create a closure for calling expressInterest.
|
| 94 | * contentListener is from the call to requestContent.
|
| 95 | * uriEndsWithSegmentNumber is true if the URI passed to newChannel has a segment number
|
| 96 | * (used to determine whether to request only that segment number and for updating the URL bar).
|
| 97 | * uriOriginCharset is the charset of the URI passed to newChannel (used for making a new URI)
|
Jeff Thompson | 1eea632 | 2012-11-23 16:56:18 -0800 | [diff] [blame] | 98 | * uriSearchAndHash is the search and hash part of the URI passed to newChannel, including the '?'
|
| 99 | * and/or '#' but without the interest selector fields.
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 100 | */
|
| 101 | var ContentClosure = function ContentClosure
|
Jeff Thompson | 1eea632 | 2012-11-23 16:56:18 -0800 | [diff] [blame] | 102 | (ndn, contentListener, uriEndsWithSegmentNumber, uriOriginCharset, uriSearchAndHash) {
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 103 | // Inherit from Closure.
|
| 104 | Closure.call(this);
|
| 105 |
|
| 106 | this.ndn = ndn;
|
| 107 | this.contentListener = contentListener;
|
| 108 | this.uriEndsWithSegmentNumber = uriEndsWithSegmentNumber;
|
| 109 | this.uriOriginCharset = uriOriginCharset;
|
Jeff Thompson | 1eea632 | 2012-11-23 16:56:18 -0800 | [diff] [blame] | 110 | this.uriSearchAndHash = uriSearchAndHash;
|
| 111 |
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 112 | this.firstReceivedSegmentNumber = null;
|
| 113 | this.firstReceivedContentObject = null;
|
| 114 | }
|
| 115 |
|
| 116 | ContentClosure.prototype.upcall = function(kind, upcallInfo) {
|
| 117 | if (!(kind == Closure.UPCALL_CONTENT ||
|
| 118 | kind == Closure.UPCALL_CONTENT_UNVERIFIED))
|
| 119 | // The upcall is not for us.
|
| 120 | return Closure.RESULT_ERR;
|
| 121 |
|
| 122 | var contentObject = upcallInfo.contentObject;
|
| 123 | if (contentObject.content == null) {
|
| 124 | dump("CcnxProtocol.ContentClosure: contentObject.content is null\n");
|
| 125 | return Closure.RESULT_ERR;
|
| 126 | }
|
| 127 |
|
| 128 | // If !this.uriEndsWithSegmentNumber, we use the segmentNumber to load multiple segments.
|
| 129 | var segmentNumber = null;
|
| 130 | if (!this.uriEndsWithSegmentNumber && endsWithSegmentNumber(contentObject.name)) {
|
| 131 | segmentNumber = DataUtils.bigEndianToUnsignedInt
|
| 132 | (contentObject.name.components[contentObject.name.components.length - 1]);
|
| 133 | if (this.firstReceivedSegmentNumber == null) {
|
| 134 | // This is the first call.
|
| 135 | this.firstReceivedSegmentNumber = segmentNumber;
|
| 136 | if (segmentNumber != 0) {
|
| 137 | // Special case: Save this content object for later and request segment zero.
|
| 138 | this.firstReceivedContentObject = contentObject;
|
| 139 | var componentsForZero = contentObject.name.components.slice
|
| 140 | (0, contentObject.name.components.length - 1);
|
| 141 | componentsForZero.push([0]);
|
| 142 | this.ndn.expressInterest(new Name(componentsForZero), this);
|
| 143 | return Closure.RESULT_OK;
|
| 144 | }
|
| 145 | }
|
| 146 | }
|
| 147 |
|
| 148 | if (this.uriEndsWithSegmentNumber || segmentNumber == null || segmentNumber == 0) {
|
| 149 | // This is the first or only segment, so start.
|
| 150 | // Get the URI from the ContentObject including the version.
|
| 151 | var contentUriSpec;
|
| 152 | if (!this.uriEndsWithSegmentNumber && endsWithSegmentNumber(contentObject.name)) {
|
| 153 | var nameWithoutSegmentNumber = new Name
|
| 154 | (contentObject.name.components.slice
|
| 155 | (0, contentObject.name.components.length - 1));
|
| 156 | contentUriSpec = "ccnx:" + nameWithoutSegmentNumber.to_uri();
|
| 157 | }
|
| 158 | else
|
| 159 | contentUriSpec = "ccnx:" + contentObject.name.to_uri();
|
| 160 |
|
Jeff Thompson | 1eea632 | 2012-11-23 16:56:18 -0800 | [diff] [blame] | 161 | // Include the search and hash.
|
| 162 | contentUriSpec += this.uriSearchAndHash;
|
| 163 |
|
Jeff Thompson | e769c51 | 2012-11-04 17:25:07 -0800 | [diff] [blame] | 164 | var contentTypeEtc = getNameContentTypeAndCharset(contentObject.name);
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 165 | var ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
|
| 166 | this.contentListener.onStart(contentTypeEtc.contentType, contentTypeEtc.contentCharset,
|
| 167 | ioService.newURI(contentUriSpec, this.uriOriginCharset, null));
|
| 168 | }
|
| 169 |
|
| 170 | this.contentListener.onReceivedContent(DataUtils.toString(contentObject.content));
|
| 171 |
|
| 172 | // Check for the special case if the saved content is for the next segment that we need.
|
| 173 | if (this.firstReceivedContentObject != null &&
|
| 174 | this.firstReceivedSegmentNumber == segmentNumber + 1) {
|
| 175 | // Substitute the saved contentObject send its content and keep going.
|
| 176 | contentObject = this.firstReceivedContentObject;
|
| 177 | segmentNumber = segmentNumber + 1;
|
| 178 | // Clear firstReceivedContentObject to save memory.
|
| 179 | this.firstReceivedContentObject = null;
|
| 180 |
|
| 181 | this.contentListener.onReceivedContent(DataUtils.toString(contentObject.content));
|
| 182 | }
|
| 183 |
|
| 184 | var finalSegmentNumber = null;
|
| 185 | if (contentObject.signedInfo != null && contentObject.signedInfo.finalBlockID != null)
|
| 186 | finalSegmentNumber = DataUtils.bigEndianToUnsignedInt(contentObject.signedInfo.finalBlockID);
|
| 187 |
|
| 188 | if (!this.uriEndsWithSegmentNumber &&
|
| 189 | segmentNumber != null &&
|
| 190 | (finalSegmentNumber == null || segmentNumber != finalSegmentNumber)) {
|
| 191 | // Make a name for the next segment and get it.
|
Jeff Thompson | d0327ff | 2012-11-11 20:30:12 -0800 | [diff] [blame] | 192 | var segmentNumberPlus1 = DataUtils.nonNegativeIntToBigEndian(segmentNumber + 1);
|
| 193 | // Put a 0 byte in front.
|
| 194 | var nextSegmentNumber = new Uint8Array(segmentNumberPlus1.length + 1);
|
| 195 | nextSegmentNumber.set(segmentNumberPlus1, 1);
|
| 196 |
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 197 | var components = contentObject.name.components.slice
|
| 198 | (0, contentObject.name.components.length - 1);
|
Jeff Thompson | 5a5a282 | 2012-11-24 22:01:20 -0800 | [diff] [blame] | 199 | components.push(nextSegmentNumber);
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 200 | this.ndn.expressInterest(new Name(components), this);
|
| 201 | }
|
| 202 | else
|
| 203 | // Finished.
|
| 204 | this.contentListener.onStop();
|
| 205 |
|
| 206 | return Closure.RESULT_OK;
|
| 207 | };
|
Jeff Thompson | 1eea632 | 2012-11-23 16:56:18 -0800 | [diff] [blame] | 208 |
|
Jeff Thompson | df0a6f7 | 2012-10-21 15:58:58 -0700 | [diff] [blame] | 209 | /*
|
Jeff Thompson | 25b0641 | 2012-10-21 20:07:57 -0700 | [diff] [blame] | 210 | * Scan the name from the last component to the first (skipping special CCNx components)
|
| 211 | * 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] | 212 | */
|
Jeff Thompson | e769c51 | 2012-11-04 17:25:07 -0800 | [diff] [blame] | 213 | function getNameContentTypeAndCharset(name) {
|
| 214 | var filename = "";
|
Jeff Thompson | 25b0641 | 2012-10-21 20:07:57 -0700 | [diff] [blame] | 215 | for (var i = name.components.length - 1; i >= 0; --i) {
|
| 216 | var component = name.components[i];
|
| 217 | if (component.length <= 0)
|
| 218 | continue;
|
| 219 |
|
| 220 | // Skip special components which just may have ".gif", etc.
|
| 221 | if (component[0] == 0 || component[0] == 0xC0 || component[0] == 0xC1 ||
|
| 222 | (component[0] >= 0xF5 && component[0] <= 0xFF))
|
| 223 | continue;
|
| 224 |
|
Jeff Thompson | e769c51 | 2012-11-04 17:25:07 -0800 | [diff] [blame] | 225 | filename = DataUtils.toString(component).toLowerCase();
|
| 226 | break;
|
Jeff Thompson | 25b0641 | 2012-10-21 20:07:57 -0700 | [diff] [blame] | 227 | }
|
Jeff Thompson | df0a6f7 | 2012-10-21 15:58:58 -0700 | [diff] [blame] | 228 |
|
Jeff Thompson | e769c51 | 2012-11-04 17:25:07 -0800 | [diff] [blame] | 229 | return MimeTypes.getContentTypeAndCharset(filename);
|
Jeff Thompson | df0a6f7 | 2012-10-21 15:58:58 -0700 | [diff] [blame] | 230 | }
|
Jeff Thompson | 10de459 | 2012-10-21 23:54:18 -0700 | [diff] [blame] | 231 |
|
| 232 | /*
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 233 | * 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] | 234 | */
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 235 | function endsWithSegmentNumber(name) {
|
Jeff Thompson | 10de459 | 2012-10-21 23:54:18 -0700 | [diff] [blame] | 236 | return name.components != null && name.components.length >= 1 &&
|
| 237 | name.components[name.components.length - 1].length >= 1 &&
|
| 238 | name.components[name.components.length - 1][0] == 0;
|
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 239 | }
|
| 240 |
|
| 241 | /*
|
| 242 | * Find all search keys starting with "ccnx." and set the attribute in template.
|
| 243 | * Return the search string including the starting "?" but with the "ccnx." keys removed,
|
| 244 | * or return "" if there are no search terms left.
|
| 245 | */
|
| 246 | function extractCcnxSearch(search, template) {
|
| 247 | if (!(search.length >= 1 && search[0] == '?'))
|
| 248 | return search;
|
| 249 |
|
| 250 | var terms = search.substr(1).split('&');
|
| 251 | var i = 0;
|
| 252 | while (i < terms.length) {
|
| 253 | var keyValue = terms[i].split('=');
|
| 254 | var key = keyValue[0].trim();
|
| 255 | if (key.substr(0, 5) == "ccnx.") {
|
| 256 | if (keyValue.length >= 1) {
|
Jeff Thompson | 754652d | 2012-11-24 16:23:43 -0800 | [diff] [blame] | 257 | var value = keyValue[1].trim();
|
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 258 | var nonNegativeInt = parseInt(value);
|
| 259 |
|
| 260 | if (key == "ccnx.MinSuffixComponents" && nonNegativeInt >= 0)
|
| 261 | template.minSuffixComponents = nonNegativeInt;
|
| 262 | if (key == "ccnx.MaxSuffixComponents" && nonNegativeInt >= 0)
|
| 263 | template.maxSuffixComponents = nonNegativeInt;
|
| 264 | if (key == "ccnx.ChildSelector" && nonNegativeInt >= 0)
|
| 265 | template.childSelector = nonNegativeInt;
|
| 266 | if (key == "ccnx.AnswerOriginKind" && nonNegativeInt >= 0)
|
| 267 | template.answerOriginKind = nonNegativeInt;
|
| 268 | if (key == "ccnx.Scope" && nonNegativeInt >= 0)
|
| 269 | template.scope = nonNegativeInt;
|
| 270 | if (key == "ccnx.InterestLifetime" && nonNegativeInt >= 0)
|
| 271 | template.interestLifetime = nonNegativeInt;
|
| 272 | if (key == "ccnx.PublisherPublicKeyDigest" && nonNegativeInt >= 0)
|
| 273 | template.publisherPublicKeyDigest = DataUtils.toNumbersFromString(unescape(value));
|
| 274 | if (key == "ccnx.Nonce" && nonNegativeInt >= 0)
|
| 275 | template.nonce = DataUtils.toNumbersFromString(unescape(value));
|
| 276 | // TODO: handle Exclude.
|
| 277 | }
|
| 278 |
|
| 279 | // Remove the "ccnx." term and don't advance i.
|
| 280 | terms.splice(i, 1);
|
| 281 | }
|
| 282 | else
|
| 283 | ++i;
|
| 284 | }
|
| 285 |
|
| 286 | if (terms.length == 0)
|
| 287 | return "";
|
| 288 | else
|
| 289 | return "?" + terms.join('&');
|
Jeff Thompson | 10de459 | 2012-10-21 23:54:18 -0700 | [diff] [blame] | 290 | } |