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.
|
Jeff Thompson | 6b3d81a | 2013-01-26 15:45:21 -0800 | [diff] [blame] | 21 | this.ndn = new NDN({ getTransport: function() { return new XpcomTransport(); },
|
Jeff Thompson | a5668d5 | 2013-01-26 16:23:27 -0800 | [diff] [blame] | 22 | host: null, verify: false });
|
| 23 | // DEBUG host: "E.hub.ndn.ucla.edu", port: 9695, verify: false });
|
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 24 | }
|
| 25 |
|
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 26 | NdnProtocol.prototype = {
|
| 27 | scheme: "ndn",
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 28 | protocolFlags: nsIProtocolHandler.URI_NORELATIVE |
|
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 29 | nsIProtocolHandler.URI_NOAUTH |
|
| 30 | nsIProtocolHandler.URI_LOADABLE_BY_ANYONE,
|
| 31 |
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 32 | newURI: function(aSpec, aOriginCharset, aBaseURI)
|
| 33 | {
|
Jeff Thompson | 2cc54b4 | 2013-01-12 23:11:12 -0800 | [diff] [blame] | 34 | var uri = Cc["@mozilla.org/network/simple-uri;1"].createInstance(Ci.nsIURI);
|
| 35 |
|
Jeff Thompson | d6b61e4 | 2012-11-24 12:10:35 -0800 | [diff] [blame] | 36 | // We have to trim now because nsIURI converts spaces to %20 and we can't trim in newChannel.
|
Jeff Thompson | b2f91ea | 2013-01-13 15:59:26 -0800 | [diff] [blame] | 37 | var uriParts = NdnProtocolInfo.splitUri(aSpec);
|
Jeff Thompson | 2cc54b4 | 2013-01-12 23:11:12 -0800 | [diff] [blame] | 38 | 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 Thompson | b2f91ea | 2013-01-13 15:59:26 -0800 | [diff] [blame] | 43 | var baseUriParts = NdnProtocolInfo.splitUri(aBaseURI.spec);
|
Jeff Thompson | 2cc54b4 | 2013-01-12 23:11:12 -0800 | [diff] [blame] | 44 | 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 Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 67 | return uri;
|
| 68 | },
|
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 69 |
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 70 | newChannel: function(aURI)
|
| 71 | {
|
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 72 | var thisNdnProtocol = this;
|
Jeff Thompson | 17a9da8 | 2012-11-12 01:11:01 -0800 | [diff] [blame] | 73 |
|
Jeff Thompson | 1eea632 | 2012-11-23 16:56:18 -0800 | [diff] [blame] | 74 | try {
|
Jeff Thompson | b2f91ea | 2013-01-13 15:59:26 -0800 | [diff] [blame] | 75 | var uriParts = NdnProtocolInfo.splitUri(aURI.spec);
|
Jeff Thompson | df0a6f7 | 2012-10-21 15:58:58 -0700 | [diff] [blame] | 76 |
|
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 77 | var template = new Interest(new Name([]));
|
| 78 | // Use the same default as NDN.expressInterest.
|
Jeff Thompson | 42806a1 | 2012-12-29 18:19:39 -0800 | [diff] [blame] | 79 | template.interestLifetime = 4000; // milliseconds
|
Jeff Thompson | 8107ec8 | 2013-01-12 21:53:27 -0800 | [diff] [blame] | 80 | var searchWithoutNdn = extractNdnSearch(uriParts.search, template);
|
Jeff Thompson | e5a8828 | 2013-01-05 21:02:06 -0800 | [diff] [blame] | 81 |
|
| 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 Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 87 |
|
Jeff Thompson | 1eea632 | 2012-11-23 16:56:18 -0800 | [diff] [blame] | 88 | var requestContent = function(contentListener) {
|
Jeff Thompson | 8107ec8 | 2013-01-12 21:53:27 -0800 | [diff] [blame] | 89 | var name = new Name(uriParts.name);
|
Jeff Thompson | 10de459 | 2012-10-21 23:54:18 -0700 | [diff] [blame] | 90 | // TODO: Strip off an ending implicit digest before checking the last component?
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 91 | var uriEndsWithSegmentNumber = endsWithSegmentNumber(name);
|
Jeff Thompson | 10de459 | 2012-10-21 23:54:18 -0700 | [diff] [blame] | 92 |
|
Jeff Thompson | 3d6ce94 | 2012-12-16 12:11:42 -0800 | [diff] [blame] | 93 | // Use the same NDN object each time.
|
| 94 | thisNdnProtocol.ndn.expressInterest(name,
|
| 95 | new ContentClosure(thisNdnProtocol.ndn, contentListener, uriEndsWithSegmentNumber,
|
Jeff Thompson | 8107ec8 | 2013-01-12 21:53:27 -0800 | [diff] [blame] | 96 | aURI.originCharset, searchWithoutNdn + uriParts.hash, segmentTemplate),
|
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 97 | template);
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 98 | };
|
Jeff Thompson | 57d0738 | 2012-10-29 23:25:54 -0700 | [diff] [blame] | 99 |
|
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 100 | return new ContentChannel(aURI, requestContent);
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 101 | } catch (ex) {
|
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 102 | dump("NdnProtocol.newChannel exception: " + ex + "\n" + ex.stack);
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 103 | }
|
| 104 | },
|
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 105 |
|
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 106 | classDescription: "ndn Protocol Handler",
|
| 107 | contractID: "@mozilla.org/network/protocol;1?name=" + "ndn",
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 108 | classID: Components.ID('{8122e660-1012-11e2-892e-0800200c9a66}'),
|
| 109 | QueryInterface: XPCOMUtils.generateQI([Ci.nsIProtocolHandler])
|
Jeff Thompson | 3d6ce94 | 2012-12-16 12:11:42 -0800 | [diff] [blame] | 110 | };
|
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 111 |
|
| 112 | if (XPCOMUtils.generateNSGetFactory)
|
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 113 | var NSGetFactory = XPCOMUtils.generateNSGetFactory([NdnProtocol]);
|
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 114 | else
|
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 115 | var NSGetModule = XPCOMUtils.generateNSGetModule([NdnProtocol]);
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 116 |
|
| 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 Thompson | 1eea632 | 2012-11-23 16:56:18 -0800 | [diff] [blame] | 123 | * 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 Thompson | e5a8828 | 2013-01-05 21:02:06 -0800 | [diff] [blame] | 125 | * segmentTemplate is the template used in expressInterest to fetch further segments.
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 126 | */
|
| 127 | var ContentClosure = function ContentClosure
|
Jeff Thompson | e5a8828 | 2013-01-05 21:02:06 -0800 | [diff] [blame] | 128 | (ndn, contentListener, uriEndsWithSegmentNumber, uriOriginCharset, uriSearchAndHash,
|
| 129 | segmentTemplate) {
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 130 | // 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 Thompson | 1eea632 | 2012-11-23 16:56:18 -0800 | [diff] [blame] | 137 | this.uriSearchAndHash = uriSearchAndHash;
|
Jeff Thompson | e5a8828 | 2013-01-05 21:02:06 -0800 | [diff] [blame] | 138 | this.segmentTemplate = segmentTemplate;
|
Jeff Thompson | 1eea632 | 2012-11-23 16:56:18 -0800 | [diff] [blame] | 139 |
|
Jeff Thompson | f6995b5 | 2013-01-23 21:21:16 -0800 | [diff] [blame] | 140 | this.segmentStore = new SegmentStore();
|
Jeff Thompson | 1ac86ce | 2013-01-21 21:51:07 -0800 | [diff] [blame] | 141 | this.contentSha256 = new Sha256();
|
Jeff Thompson | f6995b5 | 2013-01-23 21:21:16 -0800 | [diff] [blame] | 142 | this.didRequestFinalSegment = false;
|
| 143 | this.finalSegmentNumber = null;
|
Jeff Thompson | 3d6ce94 | 2012-12-16 12:11:42 -0800 | [diff] [blame] | 144 | };
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 145 |
|
| 146 | ContentClosure.prototype.upcall = function(kind, upcallInfo) {
|
Jeff Thompson | f6995b5 | 2013-01-23 21:21:16 -0800 | [diff] [blame] | 147 | try {
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 148 | 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 Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 155 | dump("NdnProtocol.ContentClosure: contentObject.content is null\n");
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 156 | return Closure.RESULT_ERR;
|
| 157 | }
|
Jeff Thompson | 6ad5c36 | 2012-12-27 17:57:02 -0800 | [diff] [blame] | 158 |
|
| 159 | // Now that we're connected, report the host and port.
|
Jeff Thompson | b2f91ea | 2013-01-13 15:59:26 -0800 | [diff] [blame] | 160 | NdnProtocolInfo.setConnectedNdnHub(this.ndn.host, this.ndn.port);
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 161 |
|
| 162 | // If !this.uriEndsWithSegmentNumber, we use the segmentNumber to load multiple segments.
|
Jeff Thompson | f6995b5 | 2013-01-23 21:21:16 -0800 | [diff] [blame] | 163 | // If this.uriEndsWithSegmentNumber, then we leave segmentNumber null.
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 164 | var segmentNumber = null;
|
| 165 | if (!this.uriEndsWithSegmentNumber && endsWithSegmentNumber(contentObject.name)) {
|
| 166 | segmentNumber = DataUtils.bigEndianToUnsignedInt
|
| 167 | (contentObject.name.components[contentObject.name.components.length - 1]);
|
Jeff Thompson | f6995b5 | 2013-01-23 21:21:16 -0800 | [diff] [blame] | 168 | this.segmentStore.storeContent(segmentNumber, contentObject);
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 169 | }
|
| 170 |
|
Jeff Thompson | f6995b5 | 2013-01-23 21:21:16 -0800 | [diff] [blame] | 171 | if (segmentNumber == null || segmentNumber == 0) {
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 172 | // 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 Thompson | 963d2da | 2012-12-02 23:31:22 -0800 | [diff] [blame] | 177 | (contentObject.name.components.slice(0, contentObject.name.components.length - 1));
|
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 178 | contentUriSpec = "ndn:" + nameWithoutSegmentNumber.to_uri();
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 179 | }
|
| 180 | else
|
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 181 | contentUriSpec = "ndn:" + contentObject.name.to_uri();
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 182 |
|
Jeff Thompson | 1eea632 | 2012-11-23 16:56:18 -0800 | [diff] [blame] | 183 | // Include the search and hash.
|
| 184 | contentUriSpec += this.uriSearchAndHash;
|
| 185 |
|
Jeff Thompson | e769c51 | 2012-11-04 17:25:07 -0800 | [diff] [blame] | 186 | var contentTypeEtc = getNameContentTypeAndCharset(contentObject.name);
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 187 | 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 Thompson | f6995b5 | 2013-01-23 21:21:16 -0800 | [diff] [blame] | 192 | if (segmentNumber == null) {
|
| 193 | // We are not doing segments, so just finish.
|
| 194 | this.contentListener.onReceivedContent(DataUtils.toString(contentObject.content));
|
Jeff Thompson | 869e819 | 2012-12-16 12:18:24 -0800 | [diff] [blame] | 195 | this.contentSha256.update(contentObject.content);
|
Jeff Thompson | f6995b5 | 2013-01-23 21:21:16 -0800 | [diff] [blame] | 196 | 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 Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 230 | }
|
| 231 |
|
Jeff Thompson | f6995b5 | 2013-01-23 21:21:16 -0800 | [diff] [blame] | 232 | if (this.finalSegmentNumber == null && !this.didRequestFinalSegment) {
|
Jeff Thompson | b083c8e | 2013-01-23 21:27:32 -0800 | [diff] [blame] | 233 | this.didRequestFinalSegment = true;
|
Jeff Thompson | f6995b5 | 2013-01-23 21:21:16 -0800 | [diff] [blame] | 234 | // Try to determine the final segment now.
|
| 235 | var components = contentObject.name.components.slice
|
| 236 | (0, contentObject.name.components.length - 1);
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 237 |
|
Jeff Thompson | f6995b5 | 2013-01-23 21:21:16 -0800 | [diff] [blame] | 238 | // 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 Thompson | d0327ff | 2012-11-11 20:30:12 -0800 | [diff] [blame] | 252 | // Put a 0 byte in front.
|
Jeff Thompson | f6995b5 | 2013-01-23 21:21:16 -0800 | [diff] [blame] | 253 | var segmentNumberComponent = new Uint8Array(segmentNumberBigEndian.length + 1);
|
| 254 | segmentNumberComponent.set(segmentNumberBigEndian, 1);
|
Jeff Thompson | d0327ff | 2012-11-11 20:30:12 -0800 | [diff] [blame] | 255 |
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 256 | var components = contentObject.name.components.slice
|
| 257 | (0, contentObject.name.components.length - 1);
|
Jeff Thompson | f6995b5 | 2013-01-23 21:21:16 -0800 | [diff] [blame] | 258 | components.push(segmentNumberComponent);
|
Jeff Thompson | e5a8828 | 2013-01-05 21:02:06 -0800 | [diff] [blame] | 259 | this.ndn.expressInterest(new Name(components), this, this.segmentTemplate);
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 260 | }
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 261 |
|
| 262 | return Closure.RESULT_OK;
|
Jeff Thompson | f6995b5 | 2013-01-23 21:21:16 -0800 | [diff] [blame] | 263 | } catch (ex) {
|
| 264 | dump("ContentClosure.upcall exception: " + ex + "\n" + ex.stack);
|
| 265 | return Closure.RESULT_ERR;
|
| 266 | }
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 267 | };
|
Jeff Thompson | f6995b5 | 2013-01-23 21:21:16 -0800 | [diff] [blame] | 268 |
|
| 269 | /*
|
| 270 | * A SegmentStore stores segments until they are retrieved in order starting with segment 0.
|
| 271 | */
|
| 272 | var 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 |
|
| 279 | SegmentStore.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 | */
|
| 290 | SegmentStore.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 | */
|
| 308 | SegmentStore.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 | */
|
| 354 | var SortedArray = function SortedArray() {
|
| 355 | this.entries = [];
|
| 356 | }
|
| 357 |
|
| 358 | SortedArray.prototype.sortEntries = function() {
|
| 359 | this.entries.sort(function(a, b) { return a.key - b.key; });
|
| 360 | };
|
| 361 |
|
| 362 | SortedArray.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 |
|
| 371 | SortedArray.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 |
|
| 382 | SortedArray.prototype.removeAt = function(index) {
|
| 383 | this.entries.splice(index, 1);
|
| 384 | }
|
| 385 |
|
Jeff Thompson | df0a6f7 | 2012-10-21 15:58:58 -0700 | [diff] [blame] | 386 | /*
|
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 387 | * 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] | 388 | * 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] | 389 | */
|
Jeff Thompson | e769c51 | 2012-11-04 17:25:07 -0800 | [diff] [blame] | 390 | function getNameContentTypeAndCharset(name) {
|
Jeff Thompson | 16a35f7 | 2012-11-25 08:07:33 -0800 | [diff] [blame] | 391 | var iFileName = name.indexOfFileName();
|
| 392 | if (iFileName < 0)
|
| 393 | // Get the default mime type.
|
| 394 | return MimeTypes.getContentTypeAndCharset("");
|
Jeff Thompson | df0a6f7 | 2012-10-21 15:58:58 -0700 | [diff] [blame] | 395 |
|
Jeff Thompson | 16a35f7 | 2012-11-25 08:07:33 -0800 | [diff] [blame] | 396 | return MimeTypes.getContentTypeAndCharset
|
| 397 | (DataUtils.toString(name.components[iFileName]).toLowerCase());
|
Jeff Thompson | df0a6f7 | 2012-10-21 15:58:58 -0700 | [diff] [blame] | 398 | }
|
Jeff Thompson | 10de459 | 2012-10-21 23:54:18 -0700 | [diff] [blame] | 399 |
|
| 400 | /*
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 401 | * 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] | 402 | */
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 403 | function endsWithSegmentNumber(name) {
|
Jeff Thompson | 10de459 | 2012-10-21 23:54:18 -0700 | [diff] [blame] | 404 | 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 Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 407 | }
|
| 408 |
|
| 409 | /*
|
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 410 | * 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 Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 412 | * or return "" if there are no search terms left.
|
| 413 | */
|
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 414 | function extractNdnSearch(search, template) {
|
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 415 | 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 Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 423 | if (key.substr(0, 4) == "ndn.") {
|
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 424 | if (keyValue.length >= 1) {
|
Jeff Thompson | 754652d | 2012-11-24 16:23:43 -0800 | [diff] [blame] | 425 | var value = keyValue[1].trim();
|
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 426 | var nonNegativeInt = parseInt(value);
|
| 427 |
|
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 428 | if (key == "ndn.MinSuffixComponents" && nonNegativeInt >= 0)
|
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 429 | template.minSuffixComponents = nonNegativeInt;
|
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 430 | if (key == "ndn.MaxSuffixComponents" && nonNegativeInt >= 0)
|
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 431 | template.maxSuffixComponents = nonNegativeInt;
|
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 432 | if (key == "ndn.ChildSelector" && nonNegativeInt >= 0)
|
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 433 | template.childSelector = nonNegativeInt;
|
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 434 | if (key == "ndn.AnswerOriginKind" && nonNegativeInt >= 0)
|
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 435 | template.answerOriginKind = nonNegativeInt;
|
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 436 | if (key == "ndn.Scope" && nonNegativeInt >= 0)
|
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 437 | template.scope = nonNegativeInt;
|
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 438 | if (key == "ndn.InterestLifetime" && nonNegativeInt >= 0)
|
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 439 | template.interestLifetime = nonNegativeInt;
|
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 440 | if (key == "ndn.PublisherPublicKeyDigest" && nonNegativeInt >= 0)
|
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 441 | template.publisherPublicKeyDigest = DataUtils.toNumbersFromString(unescape(value));
|
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 442 | if (key == "ndn.Nonce" && nonNegativeInt >= 0)
|
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 443 | template.nonce = DataUtils.toNumbersFromString(unescape(value));
|
| 444 | // TODO: handle Exclude.
|
| 445 | }
|
| 446 |
|
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 447 | // Remove the "ndn." term and don't advance i.
|
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 448 | 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 Thompson | 963d2da | 2012-12-02 23:31:22 -0800 | [diff] [blame] | 458 | }
|