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