blob: ca8cc1bd3b668ed4bef765f44076a9664b2a596a [file] [log] [blame]
Jeff Thompson08ab3cd2012-10-08 02:56:20 -07001/*
Jeff Thompson17a9da82012-11-12 01:11:01 -08002 * @author: Jeff Thompson
Jeff Thompson745026e2012-10-13 12:49:20 -07003 * See COPYING for copyright and distribution information.
Jeff Thompsonbd829262012-11-30 22:28:37 -08004 * This is the ndn protocol handler.
Jeff Thompson08ab3cd2012-10-08 02:56:20 -07005 * Protocol handling code derived from http://mike.kaply.com/2011/01/18/writing-a-firefox-protocol-handler/
6 */
7
8const Cc = Components.classes;
9const Ci = Components.interfaces;
10const Cr = Components.results;
11
12const nsIProtocolHandler = Ci.nsIProtocolHandler;
13
14Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
15Components.utils.import("chrome://modules/content/ndn-js.jsm");
16Components.utils.import("chrome://modules/content/ContentChannel.jsm");
Jeff Thompson6ad5c362012-12-27 17:57:02 -080017Components.utils.import("chrome://modules/content/NdnProtocolInfo.jsm");
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070018
Jeff Thompsonbd829262012-11-30 22:28:37 -080019function NdnProtocol() {
Jeff Thompson2a67a542012-12-16 17:48:13 -080020 // TODO: Remove host: null when null is the default.
21 this.ndn = new NDN({ getTransport: function() { return new XpcomTransport(); }, host: null });
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070022}
23
Jeff Thompsonbd829262012-11-30 22:28:37 -080024NdnProtocol.prototype = {
25 scheme: "ndn",
Jeff Thompson9e6dff02012-11-04 09:20:47 -080026 protocolFlags: nsIProtocolHandler.URI_NORELATIVE |
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070027 nsIProtocolHandler.URI_NOAUTH |
28 nsIProtocolHandler.URI_LOADABLE_BY_ANYONE,
29
Jeff Thompson9e6dff02012-11-04 09:20:47 -080030 newURI: function(aSpec, aOriginCharset, aBaseURI)
31 {
Jeff Thompsond6b61e42012-11-24 12:10:35 -080032 // We have to trim now because nsIURI converts spaces to %20 and we can't trim in newChannel.
33 var spec = aSpec.trim();
34 var preSearch = spec.split('?', 1)[0];
35 var searchAndHash = spec.substr(preSearch.length).trim();
36
Jeff Thompson9e6dff02012-11-04 09:20:47 -080037 var uri = Cc["@mozilla.org/network/simple-uri;1"].createInstance(Ci.nsIURI);
Jeff Thompsond6b61e42012-11-24 12:10:35 -080038 uri.spec = preSearch.trim() + searchAndHash;
Jeff Thompson9e6dff02012-11-04 09:20:47 -080039 return uri;
40 },
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070041
Jeff Thompson9e6dff02012-11-04 09:20:47 -080042 newChannel: function(aURI)
43 {
Jeff Thompsonbd829262012-11-30 22:28:37 -080044 var thisNdnProtocol = this;
Jeff Thompson17a9da82012-11-12 01:11:01 -080045
Jeff Thompson1eea6322012-11-23 16:56:18 -080046 try {
Jeff Thompsond6b61e42012-11-24 12:10:35 -080047 // Decode manually since nsIURI doesn't have selectors for hash, etc.
Jeff Thompson1eea6322012-11-23 16:56:18 -080048 var spec = aURI.spec.trim();
49 var preHash = spec.split('#', 1)[0];
Jeff Thompson5fc9b672012-11-24 10:00:56 -080050 var hash = spec.substr(preHash.length).trim();
Jeff Thompson1eea6322012-11-23 16:56:18 -080051 var preSearch = preHash.split('?', 1)[0];
Jeff Thompson5fc9b672012-11-24 10:00:56 -080052 var search = preHash.substr(preSearch.length).trim();
Jeff Thompson1eea6322012-11-23 16:56:18 -080053 // Set nameString to the preSearch without the protocol.
54 var nameString = preSearch.trim();
55 if (nameString.indexOf(':') >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -080056 nameString = nameString.substr(nameString.indexOf(':') + 1).trim();
Jeff Thompsondf0a6f72012-10-21 15:58:58 -070057
Jeff Thompson5fc9b672012-11-24 10:00:56 -080058 var template = new Interest(new Name([]));
59 // Use the same default as NDN.expressInterest.
Jeff Thompson42806a12012-12-29 18:19:39 -080060 template.interestLifetime = 4000; // milliseconds
Jeff Thompsonbd829262012-11-30 22:28:37 -080061 var searchWithoutNdn = extractNdnSearch(search, template);
Jeff Thompsone5a88282013-01-05 21:02:06 -080062
63 var segmentTemplate = new Interest(new Name([]));
64 // Only use the interest selectors which make sense for fetching further segments.
65 segmentTemplate.publisherPublicKeyDigest = template.publisherPublicKeyDigest;
66 segmentTemplate.scope = template.scope;
67 segmentTemplate.interestLifetime = template.interestLifetime;
Jeff Thompson5fc9b672012-11-24 10:00:56 -080068
Jeff Thompson1eea6322012-11-23 16:56:18 -080069 var requestContent = function(contentListener) {
Jeff Thompson9e6dff02012-11-04 09:20:47 -080070 var name = new Name(nameString);
Jeff Thompson10de4592012-10-21 23:54:18 -070071 // TODO: Strip off an ending implicit digest before checking the last component?
Jeff Thompson9e6dff02012-11-04 09:20:47 -080072 var uriEndsWithSegmentNumber = endsWithSegmentNumber(name);
Jeff Thompson10de4592012-10-21 23:54:18 -070073
Jeff Thompson3d6ce942012-12-16 12:11:42 -080074 // Use the same NDN object each time.
75 thisNdnProtocol.ndn.expressInterest(name,
76 new ContentClosure(thisNdnProtocol.ndn, contentListener, uriEndsWithSegmentNumber,
Jeff Thompsone5a88282013-01-05 21:02:06 -080077 aURI.originCharset, searchWithoutNdn + hash, segmentTemplate),
Jeff Thompson5fc9b672012-11-24 10:00:56 -080078 template);
Jeff Thompson9e6dff02012-11-04 09:20:47 -080079 };
Jeff Thompson57d07382012-10-29 23:25:54 -070080
Jeff Thompson5fc9b672012-11-24 10:00:56 -080081 return new ContentChannel(aURI, requestContent);
Jeff Thompson9e6dff02012-11-04 09:20:47 -080082 } catch (ex) {
Jeff Thompsonbd829262012-11-30 22:28:37 -080083 dump("NdnProtocol.newChannel exception: " + ex + "\n" + ex.stack);
Jeff Thompson9e6dff02012-11-04 09:20:47 -080084 }
85 },
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070086
Jeff Thompsonbd829262012-11-30 22:28:37 -080087 classDescription: "ndn Protocol Handler",
88 contractID: "@mozilla.org/network/protocol;1?name=" + "ndn",
Jeff Thompson9e6dff02012-11-04 09:20:47 -080089 classID: Components.ID('{8122e660-1012-11e2-892e-0800200c9a66}'),
90 QueryInterface: XPCOMUtils.generateQI([Ci.nsIProtocolHandler])
Jeff Thompson3d6ce942012-12-16 12:11:42 -080091};
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070092
93if (XPCOMUtils.generateNSGetFactory)
Jeff Thompsonbd829262012-11-30 22:28:37 -080094 var NSGetFactory = XPCOMUtils.generateNSGetFactory([NdnProtocol]);
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070095else
Jeff Thompsonbd829262012-11-30 22:28:37 -080096 var NSGetModule = XPCOMUtils.generateNSGetModule([NdnProtocol]);
Jeff Thompson9e6dff02012-11-04 09:20:47 -080097
98/*
99 * Create a closure for calling expressInterest.
100 * contentListener is from the call to requestContent.
101 * uriEndsWithSegmentNumber is true if the URI passed to newChannel has a segment number
102 * (used to determine whether to request only that segment number and for updating the URL bar).
103 * uriOriginCharset is the charset of the URI passed to newChannel (used for making a new URI)
Jeff Thompson1eea6322012-11-23 16:56:18 -0800104 * uriSearchAndHash is the search and hash part of the URI passed to newChannel, including the '?'
105 * and/or '#' but without the interest selector fields.
Jeff Thompsone5a88282013-01-05 21:02:06 -0800106 * segmentTemplate is the template used in expressInterest to fetch further segments.
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800107 */
108var ContentClosure = function ContentClosure
Jeff Thompsone5a88282013-01-05 21:02:06 -0800109 (ndn, contentListener, uriEndsWithSegmentNumber, uriOriginCharset, uriSearchAndHash,
110 segmentTemplate) {
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800111 // Inherit from Closure.
112 Closure.call(this);
113
114 this.ndn = ndn;
115 this.contentListener = contentListener;
116 this.uriEndsWithSegmentNumber = uriEndsWithSegmentNumber;
117 this.uriOriginCharset = uriOriginCharset;
Jeff Thompson1eea6322012-11-23 16:56:18 -0800118 this.uriSearchAndHash = uriSearchAndHash;
Jeff Thompsone5a88282013-01-05 21:02:06 -0800119 this.segmentTemplate = segmentTemplate;
Jeff Thompson1eea6322012-11-23 16:56:18 -0800120
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800121 this.firstReceivedSegmentNumber = null;
122 this.firstReceivedContentObject = null;
Jeff Thompson963d2da2012-12-02 23:31:22 -0800123 this.contentSha256 = null;
Jeff Thompson3d6ce942012-12-16 12:11:42 -0800124};
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800125
126ContentClosure.prototype.upcall = function(kind, upcallInfo) {
127 if (!(kind == Closure.UPCALL_CONTENT ||
128 kind == Closure.UPCALL_CONTENT_UNVERIFIED))
129 // The upcall is not for us.
130 return Closure.RESULT_ERR;
131
132 var contentObject = upcallInfo.contentObject;
133 if (contentObject.content == null) {
Jeff Thompsonbd829262012-11-30 22:28:37 -0800134 dump("NdnProtocol.ContentClosure: contentObject.content is null\n");
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800135 return Closure.RESULT_ERR;
136 }
Jeff Thompson6ad5c362012-12-27 17:57:02 -0800137
138 // Now that we're connected, report the host and port.
139 setConnectedNdnHub(this.ndn.host, this.ndn.port);
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800140
141 // If !this.uriEndsWithSegmentNumber, we use the segmentNumber to load multiple segments.
142 var segmentNumber = null;
143 if (!this.uriEndsWithSegmentNumber && endsWithSegmentNumber(contentObject.name)) {
144 segmentNumber = DataUtils.bigEndianToUnsignedInt
145 (contentObject.name.components[contentObject.name.components.length - 1]);
146 if (this.firstReceivedSegmentNumber == null) {
147 // This is the first call.
148 this.firstReceivedSegmentNumber = segmentNumber;
149 if (segmentNumber != 0) {
150 // Special case: Save this content object for later and request segment zero.
151 this.firstReceivedContentObject = contentObject;
152 var componentsForZero = contentObject.name.components.slice
153 (0, contentObject.name.components.length - 1);
154 componentsForZero.push([0]);
Jeff Thompsone5a88282013-01-05 21:02:06 -0800155 this.ndn.expressInterest(new Name(componentsForZero), this, this.segmentTemplate);
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800156 return Closure.RESULT_OK;
157 }
158 }
159 }
160
161 if (this.uriEndsWithSegmentNumber || segmentNumber == null || segmentNumber == 0) {
162 // This is the first or only segment, so start.
163 // Get the URI from the ContentObject including the version.
164 var contentUriSpec;
165 if (!this.uriEndsWithSegmentNumber && endsWithSegmentNumber(contentObject.name)) {
166 var nameWithoutSegmentNumber = new Name
Jeff Thompson963d2da2012-12-02 23:31:22 -0800167 (contentObject.name.components.slice(0, contentObject.name.components.length - 1));
Jeff Thompsonbd829262012-11-30 22:28:37 -0800168 contentUriSpec = "ndn:" + nameWithoutSegmentNumber.to_uri();
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800169 }
170 else
Jeff Thompsonbd829262012-11-30 22:28:37 -0800171 contentUriSpec = "ndn:" + contentObject.name.to_uri();
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800172
Jeff Thompson1eea6322012-11-23 16:56:18 -0800173 // Include the search and hash.
174 contentUriSpec += this.uriSearchAndHash;
175
Jeff Thompsone769c512012-11-04 17:25:07 -0800176 var contentTypeEtc = getNameContentTypeAndCharset(contentObject.name);
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800177 var ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
178 this.contentListener.onStart(contentTypeEtc.contentType, contentTypeEtc.contentCharset,
179 ioService.newURI(contentUriSpec, this.uriOriginCharset, null));
Jeff Thompson963d2da2012-12-02 23:31:22 -0800180
Jeff Thompson869e8192012-12-16 12:18:24 -0800181 this.contentSha256 = new Sha256();
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800182 }
183
184 this.contentListener.onReceivedContent(DataUtils.toString(contentObject.content));
Jeff Thompson869e8192012-12-16 12:18:24 -0800185 this.contentSha256.update(contentObject.content);
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800186
187 // Check for the special case if the saved content is for the next segment that we need.
188 if (this.firstReceivedContentObject != null &&
189 this.firstReceivedSegmentNumber == segmentNumber + 1) {
190 // Substitute the saved contentObject send its content and keep going.
191 contentObject = this.firstReceivedContentObject;
192 segmentNumber = segmentNumber + 1;
193 // Clear firstReceivedContentObject to save memory.
194 this.firstReceivedContentObject = null;
195
196 this.contentListener.onReceivedContent(DataUtils.toString(contentObject.content));
Jeff Thompson869e8192012-12-16 12:18:24 -0800197 this.contentSha256.update(contentObject.content);
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800198 }
199
200 var finalSegmentNumber = null;
201 if (contentObject.signedInfo != null && contentObject.signedInfo.finalBlockID != null)
202 finalSegmentNumber = DataUtils.bigEndianToUnsignedInt(contentObject.signedInfo.finalBlockID);
203
204 if (!this.uriEndsWithSegmentNumber &&
205 segmentNumber != null &&
206 (finalSegmentNumber == null || segmentNumber != finalSegmentNumber)) {
207 // Make a name for the next segment and get it.
Jeff Thompsond0327ff2012-11-11 20:30:12 -0800208 var segmentNumberPlus1 = DataUtils.nonNegativeIntToBigEndian(segmentNumber + 1);
209 // Put a 0 byte in front.
210 var nextSegmentNumber = new Uint8Array(segmentNumberPlus1.length + 1);
211 nextSegmentNumber.set(segmentNumberPlus1, 1);
212
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800213 var components = contentObject.name.components.slice
214 (0, contentObject.name.components.length - 1);
Jeff Thompson5a5a2822012-11-24 22:01:20 -0800215 components.push(nextSegmentNumber);
Jeff Thompsone5a88282013-01-05 21:02:06 -0800216 this.ndn.expressInterest(new Name(components), this, this.segmentTemplate);
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800217 }
Jeff Thompson963d2da2012-12-02 23:31:22 -0800218 else {
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800219 // Finished.
Jeff Thompson963d2da2012-12-02 23:31:22 -0800220 this.contentListener.onStop();
Jeff Thompson869e8192012-12-16 12:18:24 -0800221 var nameContentDigest = contentObject.name.getContentDigestValue();
222 if (nameContentDigest != null &&
223 !DataUtils.arraysEqual(nameContentDigest, this.contentSha256.finalize()))
224 // TODO: How to show the user an error for invalid digest?
225 dump("Content does not match digest in name " + contentObject.name.to_uri());
Jeff Thompson963d2da2012-12-02 23:31:22 -0800226 }
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800227
228 return Closure.RESULT_OK;
229};
Jeff Thompson1eea6322012-11-23 16:56:18 -0800230
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700231/*
Jeff Thompsonbd829262012-11-30 22:28:37 -0800232 * Scan the name from the last component to the first (skipping special name components)
Jeff Thompson25b06412012-10-21 20:07:57 -0700233 * for a recognized file name extension, and return an object with properties contentType and charset.
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700234 */
Jeff Thompsone769c512012-11-04 17:25:07 -0800235function getNameContentTypeAndCharset(name) {
Jeff Thompson16a35f72012-11-25 08:07:33 -0800236 var iFileName = name.indexOfFileName();
237 if (iFileName < 0)
238 // Get the default mime type.
239 return MimeTypes.getContentTypeAndCharset("");
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700240
Jeff Thompson16a35f72012-11-25 08:07:33 -0800241 return MimeTypes.getContentTypeAndCharset
242 (DataUtils.toString(name.components[iFileName]).toLowerCase());
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700243}
Jeff Thompson10de4592012-10-21 23:54:18 -0700244
245/*
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800246 * Return true if the last component in the name is a segment number..
Jeff Thompson10de4592012-10-21 23:54:18 -0700247 */
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800248function endsWithSegmentNumber(name) {
Jeff Thompson10de4592012-10-21 23:54:18 -0700249 return name.components != null && name.components.length >= 1 &&
250 name.components[name.components.length - 1].length >= 1 &&
251 name.components[name.components.length - 1][0] == 0;
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800252}
253
254/*
Jeff Thompsonbd829262012-11-30 22:28:37 -0800255 * Find all search keys starting with "ndn." and set the attribute in template.
256 * Return the search string including the starting "?" but with the "ndn." keys removed,
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800257 * or return "" if there are no search terms left.
258 */
Jeff Thompsonbd829262012-11-30 22:28:37 -0800259function extractNdnSearch(search, template) {
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800260 if (!(search.length >= 1 && search[0] == '?'))
261 return search;
262
263 var terms = search.substr(1).split('&');
264 var i = 0;
265 while (i < terms.length) {
266 var keyValue = terms[i].split('=');
267 var key = keyValue[0].trim();
Jeff Thompsonbd829262012-11-30 22:28:37 -0800268 if (key.substr(0, 4) == "ndn.") {
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800269 if (keyValue.length >= 1) {
Jeff Thompson754652d2012-11-24 16:23:43 -0800270 var value = keyValue[1].trim();
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800271 var nonNegativeInt = parseInt(value);
272
Jeff Thompsonbd829262012-11-30 22:28:37 -0800273 if (key == "ndn.MinSuffixComponents" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800274 template.minSuffixComponents = nonNegativeInt;
Jeff Thompsonbd829262012-11-30 22:28:37 -0800275 if (key == "ndn.MaxSuffixComponents" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800276 template.maxSuffixComponents = nonNegativeInt;
Jeff Thompsonbd829262012-11-30 22:28:37 -0800277 if (key == "ndn.ChildSelector" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800278 template.childSelector = nonNegativeInt;
Jeff Thompsonbd829262012-11-30 22:28:37 -0800279 if (key == "ndn.AnswerOriginKind" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800280 template.answerOriginKind = nonNegativeInt;
Jeff Thompsonbd829262012-11-30 22:28:37 -0800281 if (key == "ndn.Scope" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800282 template.scope = nonNegativeInt;
Jeff Thompsonbd829262012-11-30 22:28:37 -0800283 if (key == "ndn.InterestLifetime" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800284 template.interestLifetime = nonNegativeInt;
Jeff Thompsonbd829262012-11-30 22:28:37 -0800285 if (key == "ndn.PublisherPublicKeyDigest" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800286 template.publisherPublicKeyDigest = DataUtils.toNumbersFromString(unescape(value));
Jeff Thompsonbd829262012-11-30 22:28:37 -0800287 if (key == "ndn.Nonce" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800288 template.nonce = DataUtils.toNumbersFromString(unescape(value));
289 // TODO: handle Exclude.
290 }
291
Jeff Thompsonbd829262012-11-30 22:28:37 -0800292 // Remove the "ndn." term and don't advance i.
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800293 terms.splice(i, 1);
294 }
295 else
296 ++i;
297 }
298
299 if (terms.length == 0)
300 return "";
301 else
302 return "?" + terms.join('&');
Jeff Thompson963d2da2012-12-02 23:31:22 -0800303}