blob: 474a1ccd6c4d5978a1f39ffbdbd2ca12ca91f708 [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.
Jeff Thompson6b3d81a2013-01-26 15:45:21 -080021 this.ndn = new NDN({ getTransport: function() { return new XpcomTransport(); },
Jeff Thompsona5668d52013-01-26 16:23:27 -080022 host: null, verify: false });
23// DEBUG host: "E.hub.ndn.ucla.edu", port: 9695, verify: false });
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070024}
25
Jeff Thompsonbd829262012-11-30 22:28:37 -080026NdnProtocol.prototype = {
27 scheme: "ndn",
Jeff Thompson9e6dff02012-11-04 09:20:47 -080028 protocolFlags: nsIProtocolHandler.URI_NORELATIVE |
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070029 nsIProtocolHandler.URI_NOAUTH |
30 nsIProtocolHandler.URI_LOADABLE_BY_ANYONE,
31
Jeff Thompson9e6dff02012-11-04 09:20:47 -080032 newURI: function(aSpec, aOriginCharset, aBaseURI)
33 {
Jeff Thompson2cc54b42013-01-12 23:11:12 -080034 var uri = Cc["@mozilla.org/network/simple-uri;1"].createInstance(Ci.nsIURI);
35
Jeff Thompsond6b61e42012-11-24 12:10:35 -080036 // We have to trim now because nsIURI converts spaces to %20 and we can't trim in newChannel.
Jeff Thompsonb2f91ea2013-01-13 15:59:26 -080037 var uriParts = NdnProtocolInfo.splitUri(aSpec);
Jeff Thompson2cc54b42013-01-12 23:11:12 -080038 if (aBaseURI == null || uriParts.name.length < 1 || uriParts.name[0] == '/')
39 // Just reconstruct the trimmed URI.
40 uri.spec = "ndn:" + uriParts.name + uriParts.search + uriParts.hash;
41 else {
42 // Make a URI relative to the base name up to the file name component.
Jeff Thompsonb2f91ea2013-01-13 15:59:26 -080043 var baseUriParts = NdnProtocolInfo.splitUri(aBaseURI.spec);
Jeff Thompson2cc54b42013-01-12 23:11:12 -080044 var baseName = new Name(baseUriParts.name);
45 var iFileName = baseName.indexOfFileName();
46
47 var relativeName = uriParts.name;
48 // Handle ../
49 while (true) {
50 if (relativeName.substr(0, 2) == "./")
51 relativeName = relativeName.substr(2);
52 else if (relativeName.substr(0, 3) == "../") {
53 relativeName = relativeName.substr(3);
54 if (iFileName > 0)
55 --iFileName;
56 }
57 else
58 break;
59 }
60
61 var prefixUri = "/";
62 if (iFileName > 0)
63 prefixUri = new Name(baseName.components.slice(0, iFileName)).to_uri() + "/";
64 uri.spec = "ndn:" + prefixUri + relativeName + uriParts.search + uriParts.hash;
65 }
66
Jeff Thompson9e6dff02012-11-04 09:20:47 -080067 return uri;
68 },
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070069
Jeff Thompson9e6dff02012-11-04 09:20:47 -080070 newChannel: function(aURI)
71 {
Jeff Thompsonbd829262012-11-30 22:28:37 -080072 var thisNdnProtocol = this;
Jeff Thompson17a9da82012-11-12 01:11:01 -080073
Jeff Thompson1eea6322012-11-23 16:56:18 -080074 try {
Jeff Thompsonb2f91ea2013-01-13 15:59:26 -080075 var uriParts = NdnProtocolInfo.splitUri(aURI.spec);
Jeff Thompsondf0a6f72012-10-21 15:58:58 -070076
Jeff Thompson5fc9b672012-11-24 10:00:56 -080077 var template = new Interest(new Name([]));
78 // Use the same default as NDN.expressInterest.
Jeff Thompson42806a12012-12-29 18:19:39 -080079 template.interestLifetime = 4000; // milliseconds
Jeff Thompson8107ec82013-01-12 21:53:27 -080080 var searchWithoutNdn = extractNdnSearch(uriParts.search, template);
Jeff Thompsone5a88282013-01-05 21:02:06 -080081
82 var segmentTemplate = new Interest(new Name([]));
83 // Only use the interest selectors which make sense for fetching further segments.
84 segmentTemplate.publisherPublicKeyDigest = template.publisherPublicKeyDigest;
85 segmentTemplate.scope = template.scope;
86 segmentTemplate.interestLifetime = template.interestLifetime;
Jeff Thompson5fc9b672012-11-24 10:00:56 -080087
Jeff Thompson1eea6322012-11-23 16:56:18 -080088 var requestContent = function(contentListener) {
Jeff Thompson8107ec82013-01-12 21:53:27 -080089 var name = new Name(uriParts.name);
Jeff Thompson10de4592012-10-21 23:54:18 -070090 // TODO: Strip off an ending implicit digest before checking the last component?
Jeff Thompson9e6dff02012-11-04 09:20:47 -080091 var uriEndsWithSegmentNumber = endsWithSegmentNumber(name);
Jeff Thompson10de4592012-10-21 23:54:18 -070092
Jeff Thompson3d6ce942012-12-16 12:11:42 -080093 // Use the same NDN object each time.
94 thisNdnProtocol.ndn.expressInterest(name,
95 new ContentClosure(thisNdnProtocol.ndn, contentListener, uriEndsWithSegmentNumber,
Jeff Thompson8107ec82013-01-12 21:53:27 -080096 aURI.originCharset, searchWithoutNdn + uriParts.hash, segmentTemplate),
Jeff Thompson5fc9b672012-11-24 10:00:56 -080097 template);
Jeff Thompson9e6dff02012-11-04 09:20:47 -080098 };
Jeff Thompson57d07382012-10-29 23:25:54 -070099
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800100 return new ContentChannel(aURI, requestContent);
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800101 } catch (ex) {
Jeff Thompsonbd829262012-11-30 22:28:37 -0800102 dump("NdnProtocol.newChannel exception: " + ex + "\n" + ex.stack);
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800103 }
104 },
Jeff Thompson08ab3cd2012-10-08 02:56:20 -0700105
Jeff Thompsonbd829262012-11-30 22:28:37 -0800106 classDescription: "ndn Protocol Handler",
107 contractID: "@mozilla.org/network/protocol;1?name=" + "ndn",
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800108 classID: Components.ID('{8122e660-1012-11e2-892e-0800200c9a66}'),
109 QueryInterface: XPCOMUtils.generateQI([Ci.nsIProtocolHandler])
Jeff Thompson3d6ce942012-12-16 12:11:42 -0800110};
Jeff Thompson08ab3cd2012-10-08 02:56:20 -0700111
112if (XPCOMUtils.generateNSGetFactory)
Jeff Thompsonbd829262012-11-30 22:28:37 -0800113 var NSGetFactory = XPCOMUtils.generateNSGetFactory([NdnProtocol]);
Jeff Thompson08ab3cd2012-10-08 02:56:20 -0700114else
Jeff Thompsonbd829262012-11-30 22:28:37 -0800115 var NSGetModule = XPCOMUtils.generateNSGetModule([NdnProtocol]);
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800116
117/*
118 * Create a closure for calling expressInterest.
119 * contentListener is from the call to requestContent.
120 * uriEndsWithSegmentNumber is true if the URI passed to newChannel has a segment number
121 * (used to determine whether to request only that segment number and for updating the URL bar).
122 * uriOriginCharset is the charset of the URI passed to newChannel (used for making a new URI)
Jeff Thompson1eea6322012-11-23 16:56:18 -0800123 * uriSearchAndHash is the search and hash part of the URI passed to newChannel, including the '?'
124 * and/or '#' but without the interest selector fields.
Jeff Thompsone5a88282013-01-05 21:02:06 -0800125 * segmentTemplate is the template used in expressInterest to fetch further segments.
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800126 */
127var ContentClosure = function ContentClosure
Jeff Thompsone5a88282013-01-05 21:02:06 -0800128 (ndn, contentListener, uriEndsWithSegmentNumber, uriOriginCharset, uriSearchAndHash,
129 segmentTemplate) {
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800130 // Inherit from Closure.
131 Closure.call(this);
132
133 this.ndn = ndn;
134 this.contentListener = contentListener;
135 this.uriEndsWithSegmentNumber = uriEndsWithSegmentNumber;
136 this.uriOriginCharset = uriOriginCharset;
Jeff Thompson1eea6322012-11-23 16:56:18 -0800137 this.uriSearchAndHash = uriSearchAndHash;
Jeff Thompsone5a88282013-01-05 21:02:06 -0800138 this.segmentTemplate = segmentTemplate;
Jeff Thompson1eea6322012-11-23 16:56:18 -0800139
Jeff Thompsonf6995b52013-01-23 21:21:16 -0800140 this.segmentStore = new SegmentStore();
Jeff Thompson1ac86ce2013-01-21 21:51:07 -0800141 this.contentSha256 = new Sha256();
Jeff Thompsonf6995b52013-01-23 21:21:16 -0800142 this.didRequestFinalSegment = false;
143 this.finalSegmentNumber = null;
Jeff Thompson3d6ce942012-12-16 12:11:42 -0800144};
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800145
146ContentClosure.prototype.upcall = function(kind, upcallInfo) {
Jeff Thompsonf6995b52013-01-23 21:21:16 -0800147 try {
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800148 if (!(kind == Closure.UPCALL_CONTENT ||
149 kind == Closure.UPCALL_CONTENT_UNVERIFIED))
150 // The upcall is not for us.
151 return Closure.RESULT_ERR;
152
153 var contentObject = upcallInfo.contentObject;
154 if (contentObject.content == null) {
Jeff Thompsonbd829262012-11-30 22:28:37 -0800155 dump("NdnProtocol.ContentClosure: contentObject.content is null\n");
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800156 return Closure.RESULT_ERR;
157 }
Jeff Thompson6ad5c362012-12-27 17:57:02 -0800158
159 // Now that we're connected, report the host and port.
Jeff Thompsonb2f91ea2013-01-13 15:59:26 -0800160 NdnProtocolInfo.setConnectedNdnHub(this.ndn.host, this.ndn.port);
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800161
162 // If !this.uriEndsWithSegmentNumber, we use the segmentNumber to load multiple segments.
Jeff Thompsonf6995b52013-01-23 21:21:16 -0800163 // If this.uriEndsWithSegmentNumber, then we leave segmentNumber null.
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800164 var segmentNumber = null;
165 if (!this.uriEndsWithSegmentNumber && endsWithSegmentNumber(contentObject.name)) {
166 segmentNumber = DataUtils.bigEndianToUnsignedInt
167 (contentObject.name.components[contentObject.name.components.length - 1]);
Jeff Thompsonf6995b52013-01-23 21:21:16 -0800168 this.segmentStore.storeContent(segmentNumber, contentObject);
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800169 }
170
Jeff Thompsonf6995b52013-01-23 21:21:16 -0800171 if (segmentNumber == null || segmentNumber == 0) {
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800172 // This is the first or only segment, so start.
173 // Get the URI from the ContentObject including the version.
174 var contentUriSpec;
175 if (!this.uriEndsWithSegmentNumber && endsWithSegmentNumber(contentObject.name)) {
176 var nameWithoutSegmentNumber = new Name
Jeff Thompson963d2da2012-12-02 23:31:22 -0800177 (contentObject.name.components.slice(0, contentObject.name.components.length - 1));
Jeff Thompsonbd829262012-11-30 22:28:37 -0800178 contentUriSpec = "ndn:" + nameWithoutSegmentNumber.to_uri();
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800179 }
180 else
Jeff Thompsonbd829262012-11-30 22:28:37 -0800181 contentUriSpec = "ndn:" + contentObject.name.to_uri();
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800182
Jeff Thompson1eea6322012-11-23 16:56:18 -0800183 // Include the search and hash.
184 contentUriSpec += this.uriSearchAndHash;
185
Jeff Thompsone769c512012-11-04 17:25:07 -0800186 var contentTypeEtc = getNameContentTypeAndCharset(contentObject.name);
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800187 var ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
188 this.contentListener.onStart(contentTypeEtc.contentType, contentTypeEtc.contentCharset,
189 ioService.newURI(contentUriSpec, this.uriOriginCharset, null));
190 }
191
Jeff Thompsonf6995b52013-01-23 21:21:16 -0800192 if (segmentNumber == null) {
193 // We are not doing segments, so just finish.
194 this.contentListener.onReceivedContent(DataUtils.toString(contentObject.content));
Jeff Thompson869e8192012-12-16 12:18:24 -0800195 this.contentSha256.update(contentObject.content);
Jeff Thompsonf6995b52013-01-23 21:21:16 -0800196 this.contentListener.onStop();
197
198 if (!this.uriEndsWithSegmentNumber) {
199 var nameContentDigest = contentObject.name.getContentDigestValue();
200 if (nameContentDigest != null &&
201 !DataUtils.arraysEqual(nameContentDigest, this.contentSha256.finalize()))
202 // TODO: How to show the user an error for invalid digest?
203 dump("Content does not match digest in name " + contentObject.name.to_uri());
204 }
205 return Closure.RESULT_OK;
206 }
207
208 if (contentObject.signedInfo != null && contentObject.signedInfo.finalBlockID != null)
209 this.finalSegmentNumber = DataUtils.bigEndianToUnsignedInt(contentObject.signedInfo.finalBlockID);
210
211 // The content was already put in the store. Retrieve as much as possible.
212 var entry;
213 while ((entry = this.segmentStore.maybeRetrieveNextEntry()) != null) {
214 segmentNumber = entry.key;
215 contentObject = entry.value;
216 this.contentListener.onReceivedContent(DataUtils.toString(contentObject.content));
217 this.contentSha256.update(contentObject.content);
218
219 if (this.finalSegmentNumber != null && segmentNumber == this.finalSegmentNumber) {
220 // Finished.
221 this.contentListener.onStop();
222 var nameContentDigest = contentObject.name.getContentDigestValue();
223 if (nameContentDigest != null &&
224 !DataUtils.arraysEqual(nameContentDigest, this.contentSha256.finalize()))
225 // TODO: How to show the user an error for invalid digest?
226 dump("Content does not match digest in name " + contentObject.name.to_uri());
227
228 return Closure.RESULT_OK;
229 }
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800230 }
231
Jeff Thompsonf6995b52013-01-23 21:21:16 -0800232 if (this.finalSegmentNumber == null && !this.didRequestFinalSegment) {
Jeff Thompsonb083c8e2013-01-23 21:27:32 -0800233 this.didRequestFinalSegment = true;
Jeff Thompsonf6995b52013-01-23 21:21:16 -0800234 // Try to determine the final segment now.
235 var components = contentObject.name.components.slice
236 (0, contentObject.name.components.length - 1);
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800237
Jeff Thompsonf6995b52013-01-23 21:21:16 -0800238 // Temporarily set the childSelector in the segmentTemplate.
239 this.segmentTemplate.childSelector = 1;
240 this.ndn.expressInterest(new Name(components), this, this.segmentTemplate);
241 this.segmentTemplate.childSelector = null;
242 }
243
244 // Request new segments.
245 var toRequest = this.segmentStore.requestSegmentNumbers(2);
246 for (var i = 0; i < toRequest.length; ++i) {
247 if (this.finalSegmentNumber != null && toRequest[i] > this.finalSegmentNumber)
248 continue;
249
250 // Make a name for the segment and get it.
251 var segmentNumberBigEndian = DataUtils.nonNegativeIntToBigEndian(toRequest[i]);
Jeff Thompsond0327ff2012-11-11 20:30:12 -0800252 // Put a 0 byte in front.
Jeff Thompsonf6995b52013-01-23 21:21:16 -0800253 var segmentNumberComponent = new Uint8Array(segmentNumberBigEndian.length + 1);
254 segmentNumberComponent.set(segmentNumberBigEndian, 1);
Jeff Thompsond0327ff2012-11-11 20:30:12 -0800255
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800256 var components = contentObject.name.components.slice
257 (0, contentObject.name.components.length - 1);
Jeff Thompsonf6995b52013-01-23 21:21:16 -0800258 components.push(segmentNumberComponent);
Jeff Thompsone5a88282013-01-05 21:02:06 -0800259 this.ndn.expressInterest(new Name(components), this, this.segmentTemplate);
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800260 }
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800261
262 return Closure.RESULT_OK;
Jeff Thompsonf6995b52013-01-23 21:21:16 -0800263 } catch (ex) {
264 dump("ContentClosure.upcall exception: " + ex + "\n" + ex.stack);
265 return Closure.RESULT_ERR;
266 }
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800267};
Jeff Thompsonf6995b52013-01-23 21:21:16 -0800268
269/*
270 * A SegmentStore stores segments until they are retrieved in order starting with segment 0.
271 */
272var SegmentStore = function SegmentStore() {
273 // Each entry is an object where the key is the segment number and value is null if
274 // the segment number is requested or the contentObject if received.
275 this.store = new SortedArray();
276 this.maxRetrievedSegmentNumber = -1;
277};
278
279SegmentStore.prototype.storeContent = function(segmentNumber, contentObject) {
280 // We don't expect to try to store a segment that has already been retrieved, but check anyway.
281 if (segmentNumber > this.maxRetrievedSegmentNumber)
282 this.store.set(segmentNumber, contentObject);
283};
284
285/*
286 * If the min segment number is this.maxRetrievedSegmentNumber + 1 and its value is not null,
287 * then delete from the store, return the entry with key and value, and update maxRetrievedSegmentNumber.
288 * Otherwise return null.
289 */
290SegmentStore.prototype.maybeRetrieveNextEntry = function() {
291 if (this.store.entries.length > 0 && this.store.entries[0].value != null &&
292 this.store.entries[0].key == this.maxRetrievedSegmentNumber + 1) {
293 var entry = this.store.entries[0];
294 this.store.removeAt(0);
295 ++this.maxRetrievedSegmentNumber;
296 return entry;
297 }
298 else
299 return null;
300};
301
302/*
303 * Return an array of the next segment numbers that need to be requested so that the total
304 * requested segments is totalRequestedSegments. If a segment store entry value is null, it is
305 * already requested and is not returned. If a segment number is returned, create a
306 * entry in the segment store with a null value.
307 */
308SegmentStore.prototype.requestSegmentNumbers = function(totalRequestedSegments) {
309 // First, count how many are already requested.
310 var nRequestedSegments = 0;
311 for (var i = 0; i < this.store.entries.length; ++i) {
312 if (this.store.entries[i].value == null) {
313 ++nRequestedSegments;
314 if (nRequestedSegments >= totalRequestedSegments)
315 // Already maxed out on requests.
316 return [];
317 }
318 }
319
320 var toRequest = [];
321 var nextSegmentNumber = this.maxRetrievedSegmentNumber + 1;
322 for (var i = 0; i < this.store.entries.length; ++i) {
323 var entry = this.store.entries[i];
324 // Fill in the gap before the segment number in the entry.
325 while (nextSegmentNumber < entry.key) {
326 toRequest.push(nextSegmentNumber);
327 ++nextSegmentNumber;
328 ++nRequestedSegments;
329 if (nRequestedSegments >= totalRequestedSegments)
330 break;
331 }
332 if (nRequestedSegments >= totalRequestedSegments)
333 break;
334
335 nextSegmentNumber = entry.key + 1;
336 }
337
338 // We already filled in the gaps for the segments in the store. Continue after the last.
339 while (nRequestedSegments < totalRequestedSegments) {
340 toRequest.push(nextSegmentNumber);
341 ++nextSegmentNumber;
342 ++nRequestedSegments;
343 }
344
345 // Mark the new segment numbers as requested.
346 for (var i = 0; i < toRequest.length; ++i)
347 this.store.set(toRequest[i], null);
348 return toRequest;
349}
350
351/*
352 * A SortedArray is an array of objects with key and value, where the key is an integer.
353 */
354var SortedArray = function SortedArray() {
355 this.entries = [];
356}
357
358SortedArray.prototype.sortEntries = function() {
359 this.entries.sort(function(a, b) { return a.key - b.key; });
360};
361
362SortedArray.prototype.indexOfKey = function(key) {
363 for (var i = 0; i < this.entries.length; ++i) {
364 if (this.entries[i].key == key)
365 return i;
366 }
367
368 return -1;
369}
370
371SortedArray.prototype.set = function(key, value) {
372 var i = this.indexOfKey(key);
373 if (i >= 0) {
374 this.entries[i].value = value;
375 return;
376 }
377
378 this.entries.push({ key: key, value: value});
379 this.sortEntries();
380}
381
382SortedArray.prototype.removeAt = function(index) {
383 this.entries.splice(index, 1);
384}
385
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700386/*
Jeff Thompsonbd829262012-11-30 22:28:37 -0800387 * Scan the name from the last component to the first (skipping special name components)
Jeff Thompson25b06412012-10-21 20:07:57 -0700388 * for a recognized file name extension, and return an object with properties contentType and charset.
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700389 */
Jeff Thompsone769c512012-11-04 17:25:07 -0800390function getNameContentTypeAndCharset(name) {
Jeff Thompson16a35f72012-11-25 08:07:33 -0800391 var iFileName = name.indexOfFileName();
392 if (iFileName < 0)
393 // Get the default mime type.
394 return MimeTypes.getContentTypeAndCharset("");
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700395
Jeff Thompson16a35f72012-11-25 08:07:33 -0800396 return MimeTypes.getContentTypeAndCharset
397 (DataUtils.toString(name.components[iFileName]).toLowerCase());
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700398}
Jeff Thompson10de4592012-10-21 23:54:18 -0700399
400/*
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800401 * Return true if the last component in the name is a segment number..
Jeff Thompson10de4592012-10-21 23:54:18 -0700402 */
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800403function endsWithSegmentNumber(name) {
Jeff Thompson10de4592012-10-21 23:54:18 -0700404 return name.components != null && name.components.length >= 1 &&
405 name.components[name.components.length - 1].length >= 1 &&
406 name.components[name.components.length - 1][0] == 0;
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800407}
408
409/*
Jeff Thompsonbd829262012-11-30 22:28:37 -0800410 * Find all search keys starting with "ndn." and set the attribute in template.
411 * Return the search string including the starting "?" but with the "ndn." keys removed,
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800412 * or return "" if there are no search terms left.
413 */
Jeff Thompsonbd829262012-11-30 22:28:37 -0800414function extractNdnSearch(search, template) {
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800415 if (!(search.length >= 1 && search[0] == '?'))
416 return search;
417
418 var terms = search.substr(1).split('&');
419 var i = 0;
420 while (i < terms.length) {
421 var keyValue = terms[i].split('=');
422 var key = keyValue[0].trim();
Jeff Thompsonbd829262012-11-30 22:28:37 -0800423 if (key.substr(0, 4) == "ndn.") {
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800424 if (keyValue.length >= 1) {
Jeff Thompson754652d2012-11-24 16:23:43 -0800425 var value = keyValue[1].trim();
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800426 var nonNegativeInt = parseInt(value);
427
Jeff Thompsonbd829262012-11-30 22:28:37 -0800428 if (key == "ndn.MinSuffixComponents" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800429 template.minSuffixComponents = nonNegativeInt;
Jeff Thompsonbd829262012-11-30 22:28:37 -0800430 if (key == "ndn.MaxSuffixComponents" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800431 template.maxSuffixComponents = nonNegativeInt;
Jeff Thompsonbd829262012-11-30 22:28:37 -0800432 if (key == "ndn.ChildSelector" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800433 template.childSelector = nonNegativeInt;
Jeff Thompsonbd829262012-11-30 22:28:37 -0800434 if (key == "ndn.AnswerOriginKind" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800435 template.answerOriginKind = nonNegativeInt;
Jeff Thompsonbd829262012-11-30 22:28:37 -0800436 if (key == "ndn.Scope" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800437 template.scope = nonNegativeInt;
Jeff Thompsonbd829262012-11-30 22:28:37 -0800438 if (key == "ndn.InterestLifetime" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800439 template.interestLifetime = nonNegativeInt;
Jeff Thompsonbd829262012-11-30 22:28:37 -0800440 if (key == "ndn.PublisherPublicKeyDigest" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800441 template.publisherPublicKeyDigest = DataUtils.toNumbersFromString(unescape(value));
Jeff Thompsonbd829262012-11-30 22:28:37 -0800442 if (key == "ndn.Nonce" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800443 template.nonce = DataUtils.toNumbersFromString(unescape(value));
444 // TODO: handle Exclude.
445 }
446
Jeff Thompsonbd829262012-11-30 22:28:37 -0800447 // Remove the "ndn." term and don't advance i.
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800448 terms.splice(i, 1);
449 }
450 else
451 ++i;
452 }
453
454 if (terms.length == 0)
455 return "";
456 else
457 return "?" + terms.join('&');
Jeff Thompson963d2da2012-12-02 23:31:22 -0800458}