blob: 3276db9d707c073a1ea69bd6e074d9a6c2715768 [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(); },
22 host: null, verify: false });
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070023}
24
Jeff Thompsonbd829262012-11-30 22:28:37 -080025NdnProtocol.prototype = {
26 scheme: "ndn",
Jeff Thompson9e6dff02012-11-04 09:20:47 -080027 protocolFlags: nsIProtocolHandler.URI_NORELATIVE |
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070028 nsIProtocolHandler.URI_NOAUTH |
29 nsIProtocolHandler.URI_LOADABLE_BY_ANYONE,
30
Jeff Thompson9e6dff02012-11-04 09:20:47 -080031 newURI: function(aSpec, aOriginCharset, aBaseURI)
32 {
Jeff Thompson2cc54b42013-01-12 23:11:12 -080033 var uri = Cc["@mozilla.org/network/simple-uri;1"].createInstance(Ci.nsIURI);
34
Jeff Thompsond6b61e42012-11-24 12:10:35 -080035 // 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 -080036 var uriParts = NdnProtocolInfo.splitUri(aSpec);
Jeff Thompson2cc54b42013-01-12 23:11:12 -080037 if (aBaseURI == null || uriParts.name.length < 1 || uriParts.name[0] == '/')
38 // Just reconstruct the trimmed URI.
39 uri.spec = "ndn:" + uriParts.name + uriParts.search + uriParts.hash;
40 else {
41 // Make a URI relative to the base name up to the file name component.
Jeff Thompsonb2f91ea2013-01-13 15:59:26 -080042 var baseUriParts = NdnProtocolInfo.splitUri(aBaseURI.spec);
Jeff Thompson2cc54b42013-01-12 23:11:12 -080043 var baseName = new Name(baseUriParts.name);
44 var iFileName = baseName.indexOfFileName();
45
46 var relativeName = uriParts.name;
47 // Handle ../
48 while (true) {
49 if (relativeName.substr(0, 2) == "./")
50 relativeName = relativeName.substr(2);
51 else if (relativeName.substr(0, 3) == "../") {
52 relativeName = relativeName.substr(3);
53 if (iFileName > 0)
54 --iFileName;
55 }
56 else
57 break;
58 }
59
60 var prefixUri = "/";
61 if (iFileName > 0)
62 prefixUri = new Name(baseName.components.slice(0, iFileName)).to_uri() + "/";
63 uri.spec = "ndn:" + prefixUri + relativeName + uriParts.search + uriParts.hash;
64 }
65
Jeff Thompson9e6dff02012-11-04 09:20:47 -080066 return uri;
67 },
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070068
Jeff Thompson9e6dff02012-11-04 09:20:47 -080069 newChannel: function(aURI)
70 {
Jeff Thompsonbd829262012-11-30 22:28:37 -080071 var thisNdnProtocol = this;
Jeff Thompson17a9da82012-11-12 01:11:01 -080072
Jeff Thompson1eea6322012-11-23 16:56:18 -080073 try {
Jeff Thompsonb2f91ea2013-01-13 15:59:26 -080074 var uriParts = NdnProtocolInfo.splitUri(aURI.spec);
Jeff Thompsondf0a6f72012-10-21 15:58:58 -070075
Jeff Thompson5fc9b672012-11-24 10:00:56 -080076 var template = new Interest(new Name([]));
77 // Use the same default as NDN.expressInterest.
Jeff Thompson42806a12012-12-29 18:19:39 -080078 template.interestLifetime = 4000; // milliseconds
Jeff Thompson8107ec82013-01-12 21:53:27 -080079 var searchWithoutNdn = extractNdnSearch(uriParts.search, template);
Jeff Thompsone5a88282013-01-05 21:02:06 -080080
81 var segmentTemplate = new Interest(new Name([]));
82 // Only use the interest selectors which make sense for fetching further segments.
83 segmentTemplate.publisherPublicKeyDigest = template.publisherPublicKeyDigest;
84 segmentTemplate.scope = template.scope;
85 segmentTemplate.interestLifetime = template.interestLifetime;
Jeff Thompson5fc9b672012-11-24 10:00:56 -080086
Jeff Thompson1eea6322012-11-23 16:56:18 -080087 var requestContent = function(contentListener) {
Jeff Thompson8107ec82013-01-12 21:53:27 -080088 var name = new Name(uriParts.name);
Jeff Thompson10de4592012-10-21 23:54:18 -070089 // TODO: Strip off an ending implicit digest before checking the last component?
Jeff Thompson9e6dff02012-11-04 09:20:47 -080090 var uriEndsWithSegmentNumber = endsWithSegmentNumber(name);
Jeff Thompson10de4592012-10-21 23:54:18 -070091
Jeff Thompson3d6ce942012-12-16 12:11:42 -080092 // Use the same NDN object each time.
93 thisNdnProtocol.ndn.expressInterest(name,
94 new ContentClosure(thisNdnProtocol.ndn, contentListener, uriEndsWithSegmentNumber,
Jeff Thompson8107ec82013-01-12 21:53:27 -080095 aURI.originCharset, searchWithoutNdn + uriParts.hash, segmentTemplate),
Jeff Thompson5fc9b672012-11-24 10:00:56 -080096 template);
Jeff Thompson9e6dff02012-11-04 09:20:47 -080097 };
Jeff Thompson57d07382012-10-29 23:25:54 -070098
Jeff Thompson5fc9b672012-11-24 10:00:56 -080099 return new ContentChannel(aURI, requestContent);
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800100 } catch (ex) {
Jeff Thompsonbd829262012-11-30 22:28:37 -0800101 dump("NdnProtocol.newChannel exception: " + ex + "\n" + ex.stack);
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800102 }
103 },
Jeff Thompson08ab3cd2012-10-08 02:56:20 -0700104
Jeff Thompsonbd829262012-11-30 22:28:37 -0800105 classDescription: "ndn Protocol Handler",
106 contractID: "@mozilla.org/network/protocol;1?name=" + "ndn",
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800107 classID: Components.ID('{8122e660-1012-11e2-892e-0800200c9a66}'),
108 QueryInterface: XPCOMUtils.generateQI([Ci.nsIProtocolHandler])
Jeff Thompson3d6ce942012-12-16 12:11:42 -0800109};
Jeff Thompson08ab3cd2012-10-08 02:56:20 -0700110
111if (XPCOMUtils.generateNSGetFactory)
Jeff Thompsonbd829262012-11-30 22:28:37 -0800112 var NSGetFactory = XPCOMUtils.generateNSGetFactory([NdnProtocol]);
Jeff Thompson08ab3cd2012-10-08 02:56:20 -0700113else
Jeff Thompsonbd829262012-11-30 22:28:37 -0800114 var NSGetModule = XPCOMUtils.generateNSGetModule([NdnProtocol]);
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800115
116/*
117 * Create a closure for calling expressInterest.
118 * contentListener is from the call to requestContent.
119 * uriEndsWithSegmentNumber is true if the URI passed to newChannel has a segment number
120 * (used to determine whether to request only that segment number and for updating the URL bar).
121 * uriOriginCharset is the charset of the URI passed to newChannel (used for making a new URI)
Jeff Thompson1eea6322012-11-23 16:56:18 -0800122 * uriSearchAndHash is the search and hash part of the URI passed to newChannel, including the '?'
123 * and/or '#' but without the interest selector fields.
Jeff Thompsone5a88282013-01-05 21:02:06 -0800124 * segmentTemplate is the template used in expressInterest to fetch further segments.
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800125 */
126var ContentClosure = function ContentClosure
Jeff Thompsone5a88282013-01-05 21:02:06 -0800127 (ndn, contentListener, uriEndsWithSegmentNumber, uriOriginCharset, uriSearchAndHash,
128 segmentTemplate) {
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800129 // Inherit from Closure.
130 Closure.call(this);
131
132 this.ndn = ndn;
133 this.contentListener = contentListener;
134 this.uriEndsWithSegmentNumber = uriEndsWithSegmentNumber;
135 this.uriOriginCharset = uriOriginCharset;
Jeff Thompson1eea6322012-11-23 16:56:18 -0800136 this.uriSearchAndHash = uriSearchAndHash;
Jeff Thompsone5a88282013-01-05 21:02:06 -0800137 this.segmentTemplate = segmentTemplate;
Jeff Thompson1eea6322012-11-23 16:56:18 -0800138
Jeff Thompsonf6995b52013-01-23 21:21:16 -0800139 this.segmentStore = new SegmentStore();
Jeff Thompson1ac86ce2013-01-21 21:51:07 -0800140 this.contentSha256 = new Sha256();
Jeff Thompsonf6995b52013-01-23 21:21:16 -0800141 this.didRequestFinalSegment = false;
142 this.finalSegmentNumber = null;
Jeff Thompson3d6ce942012-12-16 12:11:42 -0800143};
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800144
145ContentClosure.prototype.upcall = function(kind, upcallInfo) {
Jeff Thompsonf6995b52013-01-23 21:21:16 -0800146 try {
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800147 if (!(kind == Closure.UPCALL_CONTENT ||
148 kind == Closure.UPCALL_CONTENT_UNVERIFIED))
149 // The upcall is not for us.
150 return Closure.RESULT_ERR;
151
152 var contentObject = upcallInfo.contentObject;
153 if (contentObject.content == null) {
Jeff Thompsonbd829262012-11-30 22:28:37 -0800154 dump("NdnProtocol.ContentClosure: contentObject.content is null\n");
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800155 return Closure.RESULT_ERR;
156 }
Jeff Thompson6ad5c362012-12-27 17:57:02 -0800157
158 // Now that we're connected, report the host and port.
Jeff Thompsonb2f91ea2013-01-13 15:59:26 -0800159 NdnProtocolInfo.setConnectedNdnHub(this.ndn.host, this.ndn.port);
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800160
161 // If !this.uriEndsWithSegmentNumber, we use the segmentNumber to load multiple segments.
Jeff Thompsonf6995b52013-01-23 21:21:16 -0800162 // If this.uriEndsWithSegmentNumber, then we leave segmentNumber null.
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800163 var segmentNumber = null;
164 if (!this.uriEndsWithSegmentNumber && endsWithSegmentNumber(contentObject.name)) {
165 segmentNumber = DataUtils.bigEndianToUnsignedInt
166 (contentObject.name.components[contentObject.name.components.length - 1]);
Jeff Thompsonf6995b52013-01-23 21:21:16 -0800167 this.segmentStore.storeContent(segmentNumber, contentObject);
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800168 }
169
Jeff Thompsonf6995b52013-01-23 21:21:16 -0800170 if (segmentNumber == null || segmentNumber == 0) {
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800171 // This is the first or only segment, so start.
172 // Get the URI from the ContentObject including the version.
173 var contentUriSpec;
174 if (!this.uriEndsWithSegmentNumber && endsWithSegmentNumber(contentObject.name)) {
175 var nameWithoutSegmentNumber = new Name
Jeff Thompson963d2da2012-12-02 23:31:22 -0800176 (contentObject.name.components.slice(0, contentObject.name.components.length - 1));
Jeff Thompsonbd829262012-11-30 22:28:37 -0800177 contentUriSpec = "ndn:" + nameWithoutSegmentNumber.to_uri();
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800178 }
179 else
Jeff Thompsonbd829262012-11-30 22:28:37 -0800180 contentUriSpec = "ndn:" + contentObject.name.to_uri();
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800181
Jeff Thompson1eea6322012-11-23 16:56:18 -0800182 // Include the search and hash.
183 contentUriSpec += this.uriSearchAndHash;
184
Jeff Thompsone769c512012-11-04 17:25:07 -0800185 var contentTypeEtc = getNameContentTypeAndCharset(contentObject.name);
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800186 var ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
187 this.contentListener.onStart(contentTypeEtc.contentType, contentTypeEtc.contentCharset,
188 ioService.newURI(contentUriSpec, this.uriOriginCharset, null));
189 }
190
Jeff Thompsonf6995b52013-01-23 21:21:16 -0800191 if (segmentNumber == null) {
192 // We are not doing segments, so just finish.
193 this.contentListener.onReceivedContent(DataUtils.toString(contentObject.content));
Jeff Thompson869e8192012-12-16 12:18:24 -0800194 this.contentSha256.update(contentObject.content);
Jeff Thompsonf6995b52013-01-23 21:21:16 -0800195 this.contentListener.onStop();
196
197 if (!this.uriEndsWithSegmentNumber) {
198 var nameContentDigest = contentObject.name.getContentDigestValue();
199 if (nameContentDigest != null &&
200 !DataUtils.arraysEqual(nameContentDigest, this.contentSha256.finalize()))
201 // TODO: How to show the user an error for invalid digest?
202 dump("Content does not match digest in name " + contentObject.name.to_uri());
203 }
204 return Closure.RESULT_OK;
205 }
206
207 if (contentObject.signedInfo != null && contentObject.signedInfo.finalBlockID != null)
208 this.finalSegmentNumber = DataUtils.bigEndianToUnsignedInt(contentObject.signedInfo.finalBlockID);
209
210 // The content was already put in the store. Retrieve as much as possible.
211 var entry;
212 while ((entry = this.segmentStore.maybeRetrieveNextEntry()) != null) {
213 segmentNumber = entry.key;
214 contentObject = entry.value;
215 this.contentListener.onReceivedContent(DataUtils.toString(contentObject.content));
216 this.contentSha256.update(contentObject.content);
217
218 if (this.finalSegmentNumber != null && segmentNumber == this.finalSegmentNumber) {
219 // Finished.
220 this.contentListener.onStop();
221 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());
226
227 return Closure.RESULT_OK;
228 }
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800229 }
230
Jeff Thompsonf6995b52013-01-23 21:21:16 -0800231 if (this.finalSegmentNumber == null && !this.didRequestFinalSegment) {
Jeff Thompsonb083c8e2013-01-23 21:27:32 -0800232 this.didRequestFinalSegment = true;
Jeff Thompsonf6995b52013-01-23 21:21:16 -0800233 // Try to determine the final segment now.
234 var components = contentObject.name.components.slice
235 (0, contentObject.name.components.length - 1);
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800236
Jeff Thompsonf6995b52013-01-23 21:21:16 -0800237 // Temporarily set the childSelector in the segmentTemplate.
238 this.segmentTemplate.childSelector = 1;
239 this.ndn.expressInterest(new Name(components), this, this.segmentTemplate);
240 this.segmentTemplate.childSelector = null;
241 }
242
243 // Request new segments.
244 var toRequest = this.segmentStore.requestSegmentNumbers(2);
245 for (var i = 0; i < toRequest.length; ++i) {
246 if (this.finalSegmentNumber != null && toRequest[i] > this.finalSegmentNumber)
247 continue;
248
249 // Make a name for the segment and get it.
250 var segmentNumberBigEndian = DataUtils.nonNegativeIntToBigEndian(toRequest[i]);
Jeff Thompsond0327ff2012-11-11 20:30:12 -0800251 // Put a 0 byte in front.
Jeff Thompsonf6995b52013-01-23 21:21:16 -0800252 var segmentNumberComponent = new Uint8Array(segmentNumberBigEndian.length + 1);
253 segmentNumberComponent.set(segmentNumberBigEndian, 1);
Jeff Thompsond0327ff2012-11-11 20:30:12 -0800254
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800255 var components = contentObject.name.components.slice
256 (0, contentObject.name.components.length - 1);
Jeff Thompsonf6995b52013-01-23 21:21:16 -0800257 components.push(segmentNumberComponent);
Jeff Thompsone5a88282013-01-05 21:02:06 -0800258 this.ndn.expressInterest(new Name(components), this, this.segmentTemplate);
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800259 }
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800260
261 return Closure.RESULT_OK;
Jeff Thompsonf6995b52013-01-23 21:21:16 -0800262 } catch (ex) {
263 dump("ContentClosure.upcall exception: " + ex + "\n" + ex.stack);
264 return Closure.RESULT_ERR;
265 }
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800266};
Jeff Thompsonf6995b52013-01-23 21:21:16 -0800267
268/*
269 * A SegmentStore stores segments until they are retrieved in order starting with segment 0.
270 */
271var SegmentStore = function SegmentStore() {
272 // Each entry is an object where the key is the segment number and value is null if
273 // the segment number is requested or the contentObject if received.
274 this.store = new SortedArray();
275 this.maxRetrievedSegmentNumber = -1;
276};
277
278SegmentStore.prototype.storeContent = function(segmentNumber, contentObject) {
279 // We don't expect to try to store a segment that has already been retrieved, but check anyway.
280 if (segmentNumber > this.maxRetrievedSegmentNumber)
281 this.store.set(segmentNumber, contentObject);
282};
283
284/*
285 * If the min segment number is this.maxRetrievedSegmentNumber + 1 and its value is not null,
286 * then delete from the store, return the entry with key and value, and update maxRetrievedSegmentNumber.
287 * Otherwise return null.
288 */
289SegmentStore.prototype.maybeRetrieveNextEntry = function() {
290 if (this.store.entries.length > 0 && this.store.entries[0].value != null &&
291 this.store.entries[0].key == this.maxRetrievedSegmentNumber + 1) {
292 var entry = this.store.entries[0];
293 this.store.removeAt(0);
294 ++this.maxRetrievedSegmentNumber;
295 return entry;
296 }
297 else
298 return null;
299};
300
301/*
302 * Return an array of the next segment numbers that need to be requested so that the total
303 * requested segments is totalRequestedSegments. If a segment store entry value is null, it is
304 * already requested and is not returned. If a segment number is returned, create a
305 * entry in the segment store with a null value.
306 */
307SegmentStore.prototype.requestSegmentNumbers = function(totalRequestedSegments) {
308 // First, count how many are already requested.
309 var nRequestedSegments = 0;
310 for (var i = 0; i < this.store.entries.length; ++i) {
311 if (this.store.entries[i].value == null) {
312 ++nRequestedSegments;
313 if (nRequestedSegments >= totalRequestedSegments)
314 // Already maxed out on requests.
315 return [];
316 }
317 }
318
319 var toRequest = [];
320 var nextSegmentNumber = this.maxRetrievedSegmentNumber + 1;
321 for (var i = 0; i < this.store.entries.length; ++i) {
322 var entry = this.store.entries[i];
323 // Fill in the gap before the segment number in the entry.
324 while (nextSegmentNumber < entry.key) {
325 toRequest.push(nextSegmentNumber);
326 ++nextSegmentNumber;
327 ++nRequestedSegments;
328 if (nRequestedSegments >= totalRequestedSegments)
329 break;
330 }
331 if (nRequestedSegments >= totalRequestedSegments)
332 break;
333
334 nextSegmentNumber = entry.key + 1;
335 }
336
337 // We already filled in the gaps for the segments in the store. Continue after the last.
338 while (nRequestedSegments < totalRequestedSegments) {
339 toRequest.push(nextSegmentNumber);
340 ++nextSegmentNumber;
341 ++nRequestedSegments;
342 }
343
344 // Mark the new segment numbers as requested.
345 for (var i = 0; i < toRequest.length; ++i)
346 this.store.set(toRequest[i], null);
347 return toRequest;
348}
349
350/*
351 * A SortedArray is an array of objects with key and value, where the key is an integer.
352 */
353var SortedArray = function SortedArray() {
354 this.entries = [];
355}
356
357SortedArray.prototype.sortEntries = function() {
358 this.entries.sort(function(a, b) { return a.key - b.key; });
359};
360
361SortedArray.prototype.indexOfKey = function(key) {
362 for (var i = 0; i < this.entries.length; ++i) {
363 if (this.entries[i].key == key)
364 return i;
365 }
366
367 return -1;
368}
369
370SortedArray.prototype.set = function(key, value) {
371 var i = this.indexOfKey(key);
372 if (i >= 0) {
373 this.entries[i].value = value;
374 return;
375 }
376
377 this.entries.push({ key: key, value: value});
378 this.sortEntries();
379}
380
381SortedArray.prototype.removeAt = function(index) {
382 this.entries.splice(index, 1);
383}
384
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700385/*
Jeff Thompsonbd829262012-11-30 22:28:37 -0800386 * Scan the name from the last component to the first (skipping special name components)
Jeff Thompson25b06412012-10-21 20:07:57 -0700387 * for a recognized file name extension, and return an object with properties contentType and charset.
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700388 */
Jeff Thompsone769c512012-11-04 17:25:07 -0800389function getNameContentTypeAndCharset(name) {
Jeff Thompson16a35f72012-11-25 08:07:33 -0800390 var iFileName = name.indexOfFileName();
391 if (iFileName < 0)
392 // Get the default mime type.
393 return MimeTypes.getContentTypeAndCharset("");
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700394
Jeff Thompson16a35f72012-11-25 08:07:33 -0800395 return MimeTypes.getContentTypeAndCharset
396 (DataUtils.toString(name.components[iFileName]).toLowerCase());
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700397}
Jeff Thompson10de4592012-10-21 23:54:18 -0700398
399/*
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800400 * Return true if the last component in the name is a segment number..
Jeff Thompson10de4592012-10-21 23:54:18 -0700401 */
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800402function endsWithSegmentNumber(name) {
Jeff Thompson10de4592012-10-21 23:54:18 -0700403 return name.components != null && name.components.length >= 1 &&
404 name.components[name.components.length - 1].length >= 1 &&
405 name.components[name.components.length - 1][0] == 0;
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800406}
407
408/*
Jeff Thompsonbd829262012-11-30 22:28:37 -0800409 * Find all search keys starting with "ndn." and set the attribute in template.
410 * Return the search string including the starting "?" but with the "ndn." keys removed,
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800411 * or return "" if there are no search terms left.
412 */
Jeff Thompsonbd829262012-11-30 22:28:37 -0800413function extractNdnSearch(search, template) {
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800414 if (!(search.length >= 1 && search[0] == '?'))
415 return search;
416
417 var terms = search.substr(1).split('&');
418 var i = 0;
419 while (i < terms.length) {
420 var keyValue = terms[i].split('=');
421 var key = keyValue[0].trim();
Jeff Thompsonbd829262012-11-30 22:28:37 -0800422 if (key.substr(0, 4) == "ndn.") {
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800423 if (keyValue.length >= 1) {
Jeff Thompson754652d2012-11-24 16:23:43 -0800424 var value = keyValue[1].trim();
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800425 var nonNegativeInt = parseInt(value);
426
Jeff Thompsonbd829262012-11-30 22:28:37 -0800427 if (key == "ndn.MinSuffixComponents" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800428 template.minSuffixComponents = nonNegativeInt;
Jeff Thompsonbd829262012-11-30 22:28:37 -0800429 if (key == "ndn.MaxSuffixComponents" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800430 template.maxSuffixComponents = nonNegativeInt;
Jeff Thompsonbd829262012-11-30 22:28:37 -0800431 if (key == "ndn.ChildSelector" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800432 template.childSelector = nonNegativeInt;
Jeff Thompsonbd829262012-11-30 22:28:37 -0800433 if (key == "ndn.AnswerOriginKind" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800434 template.answerOriginKind = nonNegativeInt;
Jeff Thompsonbd829262012-11-30 22:28:37 -0800435 if (key == "ndn.Scope" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800436 template.scope = nonNegativeInt;
Jeff Thompsonbd829262012-11-30 22:28:37 -0800437 if (key == "ndn.InterestLifetime" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800438 template.interestLifetime = nonNegativeInt;
Jeff Thompsonbd829262012-11-30 22:28:37 -0800439 if (key == "ndn.PublisherPublicKeyDigest" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800440 template.publisherPublicKeyDigest = DataUtils.toNumbersFromString(unescape(value));
Jeff Thompsonbd829262012-11-30 22:28:37 -0800441 if (key == "ndn.Nonce" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800442 template.nonce = DataUtils.toNumbersFromString(unescape(value));
443 // TODO: handle Exclude.
444 }
445
Jeff Thompsonbd829262012-11-30 22:28:37 -0800446 // Remove the "ndn." term and don't advance i.
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800447 terms.splice(i, 1);
448 }
449 else
450 ++i;
451 }
452
453 if (terms.length == 0)
454 return "";
455 else
456 return "?" + terms.join('&');
Jeff Thompson963d2da2012-12-02 23:31:22 -0800457}