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 | 6b3d81a | 2013-01-26 15:45:21 -0800 | [diff] [blame] | 20 | this.ndn = new NDN({ getTransport: function() { return new XpcomTransport(); },
|
Jeff Thompson | 49f3788 | 2013-01-26 19:08:11 -0800 | [diff] [blame] | 21 | verify: false });
|
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 | 2cc54b4 | 2013-01-12 23:11:12 -0800 | [diff] [blame] | 32 | var uri = Cc["@mozilla.org/network/simple-uri;1"].createInstance(Ci.nsIURI);
|
| 33 |
|
Jeff Thompson | d6b61e4 | 2012-11-24 12:10:35 -0800 | [diff] [blame] | 34 | // 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] | 35 | var uriParts = NdnProtocolInfo.splitUri(aSpec);
|
Jeff Thompson | 2cc54b4 | 2013-01-12 23:11:12 -0800 | [diff] [blame] | 36 | if (aBaseURI == null || uriParts.name.length < 1 || uriParts.name[0] == '/')
|
| 37 | // Just reconstruct the trimmed URI.
|
| 38 | uri.spec = "ndn:" + uriParts.name + uriParts.search + uriParts.hash;
|
| 39 | else {
|
| 40 | // 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] | 41 | var baseUriParts = NdnProtocolInfo.splitUri(aBaseURI.spec);
|
Jeff Thompson | 2cc54b4 | 2013-01-12 23:11:12 -0800 | [diff] [blame] | 42 | var baseName = new Name(baseUriParts.name);
|
| 43 | var iFileName = baseName.indexOfFileName();
|
| 44 |
|
| 45 | var relativeName = uriParts.name;
|
| 46 | // Handle ../
|
| 47 | while (true) {
|
| 48 | if (relativeName.substr(0, 2) == "./")
|
| 49 | relativeName = relativeName.substr(2);
|
| 50 | else if (relativeName.substr(0, 3) == "../") {
|
| 51 | relativeName = relativeName.substr(3);
|
| 52 | if (iFileName > 0)
|
| 53 | --iFileName;
|
| 54 | }
|
| 55 | else
|
| 56 | break;
|
| 57 | }
|
| 58 |
|
| 59 | var prefixUri = "/";
|
| 60 | if (iFileName > 0)
|
| 61 | prefixUri = new Name(baseName.components.slice(0, iFileName)).to_uri() + "/";
|
| 62 | uri.spec = "ndn:" + prefixUri + relativeName + uriParts.search + uriParts.hash;
|
| 63 | }
|
| 64 |
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 65 | return uri;
|
| 66 | },
|
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 67 |
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 68 | newChannel: function(aURI)
|
| 69 | {
|
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 70 | var thisNdnProtocol = this;
|
Jeff Thompson | 17a9da8 | 2012-11-12 01:11:01 -0800 | [diff] [blame] | 71 |
|
Jeff Thompson | 1eea632 | 2012-11-23 16:56:18 -0800 | [diff] [blame] | 72 | try {
|
Jeff Thompson | b2f91ea | 2013-01-13 15:59:26 -0800 | [diff] [blame] | 73 | var uriParts = NdnProtocolInfo.splitUri(aURI.spec);
|
Jeff Thompson | df0a6f7 | 2012-10-21 15:58:58 -0700 | [diff] [blame] | 74 |
|
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 75 | var template = new Interest(new Name([]));
|
| 76 | // Use the same default as NDN.expressInterest.
|
Jeff Thompson | 42806a1 | 2012-12-29 18:19:39 -0800 | [diff] [blame] | 77 | template.interestLifetime = 4000; // milliseconds
|
Jeff Thompson | 8107ec8 | 2013-01-12 21:53:27 -0800 | [diff] [blame] | 78 | var searchWithoutNdn = extractNdnSearch(uriParts.search, template);
|
Jeff Thompson | e5a8828 | 2013-01-05 21:02:06 -0800 | [diff] [blame] | 79 |
|
| 80 | var segmentTemplate = new Interest(new Name([]));
|
| 81 | // Only use the interest selectors which make sense for fetching further segments.
|
| 82 | segmentTemplate.publisherPublicKeyDigest = template.publisherPublicKeyDigest;
|
| 83 | segmentTemplate.scope = template.scope;
|
| 84 | segmentTemplate.interestLifetime = template.interestLifetime;
|
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 85 |
|
Jeff Thompson | 1eea632 | 2012-11-23 16:56:18 -0800 | [diff] [blame] | 86 | var requestContent = function(contentListener) {
|
Jeff Thompson | 8107ec8 | 2013-01-12 21:53:27 -0800 | [diff] [blame] | 87 | var name = new Name(uriParts.name);
|
Jeff Thompson | 10de459 | 2012-10-21 23:54:18 -0700 | [diff] [blame] | 88 | // TODO: Strip off an ending implicit digest before checking the last component?
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 89 | var uriEndsWithSegmentNumber = endsWithSegmentNumber(name);
|
Jeff Thompson | 10de459 | 2012-10-21 23:54:18 -0700 | [diff] [blame] | 90 |
|
Jeff Thompson | 3d6ce94 | 2012-12-16 12:11:42 -0800 | [diff] [blame] | 91 | // Use the same NDN object each time.
|
| 92 | thisNdnProtocol.ndn.expressInterest(name,
|
| 93 | new ContentClosure(thisNdnProtocol.ndn, contentListener, uriEndsWithSegmentNumber,
|
Jeff Thompson | 3663c67 | 2013-02-04 23:22:11 -0800 | [diff] [blame] | 94 | aURI, searchWithoutNdn + uriParts.hash, segmentTemplate),
|
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 95 | template);
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 96 | };
|
Jeff Thompson | 57d0738 | 2012-10-29 23:25:54 -0700 | [diff] [blame] | 97 |
|
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 98 | return new ContentChannel(aURI, requestContent);
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 99 | } catch (ex) {
|
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 100 | dump("NdnProtocol.newChannel exception: " + ex + "\n" + ex.stack);
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 101 | }
|
| 102 | },
|
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 103 |
|
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 104 | classDescription: "ndn Protocol Handler",
|
| 105 | contractID: "@mozilla.org/network/protocol;1?name=" + "ndn",
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 106 | classID: Components.ID('{8122e660-1012-11e2-892e-0800200c9a66}'),
|
| 107 | QueryInterface: XPCOMUtils.generateQI([Ci.nsIProtocolHandler])
|
Jeff Thompson | 3d6ce94 | 2012-12-16 12:11:42 -0800 | [diff] [blame] | 108 | };
|
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 109 |
|
| 110 | if (XPCOMUtils.generateNSGetFactory)
|
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 111 | var NSGetFactory = XPCOMUtils.generateNSGetFactory([NdnProtocol]);
|
Jeff Thompson | 08ab3cd | 2012-10-08 02:56:20 -0700 | [diff] [blame] | 112 | else
|
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 113 | var NSGetModule = XPCOMUtils.generateNSGetModule([NdnProtocol]);
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 114 |
|
| 115 | /*
|
| 116 | * Create a closure for calling expressInterest.
|
| 117 | * contentListener is from the call to requestContent.
|
| 118 | * uriEndsWithSegmentNumber is true if the URI passed to newChannel has a segment number
|
| 119 | * (used to determine whether to request only that segment number and for updating the URL bar).
|
Jeff Thompson | 3663c67 | 2013-02-04 23:22:11 -0800 | [diff] [blame] | 120 | * aURI is the URI passed to newChannel.
|
Jeff Thompson | 1eea632 | 2012-11-23 16:56:18 -0800 | [diff] [blame] | 121 | * uriSearchAndHash is the search and hash part of the URI passed to newChannel, including the '?'
|
| 122 | * and/or '#' but without the interest selector fields.
|
Jeff Thompson | e5a8828 | 2013-01-05 21:02:06 -0800 | [diff] [blame] | 123 | * segmentTemplate is the template used in expressInterest to fetch further segments.
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 124 | */
|
| 125 | var ContentClosure = function ContentClosure
|
Jeff Thompson | 3663c67 | 2013-02-04 23:22:11 -0800 | [diff] [blame] | 126 | (ndn, contentListener, uriEndsWithSegmentNumber, aURI, uriSearchAndHash,
|
Jeff Thompson | e5a8828 | 2013-01-05 21:02:06 -0800 | [diff] [blame] | 127 | segmentTemplate) {
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 128 | // Inherit from Closure.
|
| 129 | Closure.call(this);
|
| 130 |
|
| 131 | this.ndn = ndn;
|
| 132 | this.contentListener = contentListener;
|
| 133 | this.uriEndsWithSegmentNumber = uriEndsWithSegmentNumber;
|
Jeff Thompson | 3663c67 | 2013-02-04 23:22:11 -0800 | [diff] [blame] | 134 | this.aURI = aURI;
|
Jeff Thompson | 1eea632 | 2012-11-23 16:56:18 -0800 | [diff] [blame] | 135 | this.uriSearchAndHash = uriSearchAndHash;
|
Jeff Thompson | e5a8828 | 2013-01-05 21:02:06 -0800 | [diff] [blame] | 136 | this.segmentTemplate = segmentTemplate;
|
Jeff Thompson | 1eea632 | 2012-11-23 16:56:18 -0800 | [diff] [blame] | 137 |
|
Jeff Thompson | f6995b5 | 2013-01-23 21:21:16 -0800 | [diff] [blame] | 138 | this.segmentStore = new SegmentStore();
|
Jeff Thompson | 1ac86ce | 2013-01-21 21:51:07 -0800 | [diff] [blame] | 139 | this.contentSha256 = new Sha256();
|
Jeff Thompson | f6995b5 | 2013-01-23 21:21:16 -0800 | [diff] [blame] | 140 | this.didRequestFinalSegment = false;
|
| 141 | this.finalSegmentNumber = null;
|
Jeff Thompson | 3d6ce94 | 2012-12-16 12:11:42 -0800 | [diff] [blame] | 142 | };
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 143 |
|
| 144 | ContentClosure.prototype.upcall = function(kind, upcallInfo) {
|
Jeff Thompson | f6995b5 | 2013-01-23 21:21:16 -0800 | [diff] [blame] | 145 | try {
|
Jeff Thompson | 3663c67 | 2013-02-04 23:22:11 -0800 | [diff] [blame] | 146 | if (this.contentListener.done)
|
| 147 | // We are getting unexpected extra results.
|
| 148 | return Closure.RESULT_ERR;
|
| 149 |
|
| 150 | if (kind == Closure.UPCALL_INTEREST_TIMED_OUT) {
|
| 151 | if (this.segmentStore.store.entries.length == 0) {
|
| 152 | // We have not received any segments yet, so assume the URI can't be fetched.
|
| 153 | this.contentListener.onStart("text/plain", "utf-8", this.aURI);
|
| 154 | this.contentListener.onReceivedContent
|
| 155 | ("Interest timeout after " + upcallInfo.interest.interestLifetime + " milliseconds.");
|
| 156 | this.contentListener.onStop();
|
| 157 | return Closure.RESULT_OK;
|
| 158 | }
|
| 159 | else
|
| 160 | // TODO: re-express the interest a few times.
|
| 161 | return Closure.RESULT_ERR;
|
| 162 | }
|
| 163 |
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 164 | if (!(kind == Closure.UPCALL_CONTENT ||
|
| 165 | kind == Closure.UPCALL_CONTENT_UNVERIFIED))
|
| 166 | // The upcall is not for us.
|
| 167 | return Closure.RESULT_ERR;
|
| 168 |
|
| 169 | var contentObject = upcallInfo.contentObject;
|
| 170 | if (contentObject.content == null) {
|
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 171 | dump("NdnProtocol.ContentClosure: contentObject.content is null\n");
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 172 | return Closure.RESULT_ERR;
|
| 173 | }
|
Jeff Thompson | 6ad5c36 | 2012-12-27 17:57:02 -0800 | [diff] [blame] | 174 |
|
| 175 | // Now that we're connected, report the host and port.
|
Jeff Thompson | b2f91ea | 2013-01-13 15:59:26 -0800 | [diff] [blame] | 176 | NdnProtocolInfo.setConnectedNdnHub(this.ndn.host, this.ndn.port);
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 177 |
|
| 178 | // If !this.uriEndsWithSegmentNumber, we use the segmentNumber to load multiple segments.
|
Jeff Thompson | f6995b5 | 2013-01-23 21:21:16 -0800 | [diff] [blame] | 179 | // If this.uriEndsWithSegmentNumber, then we leave segmentNumber null.
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 180 | var segmentNumber = null;
|
| 181 | if (!this.uriEndsWithSegmentNumber && endsWithSegmentNumber(contentObject.name)) {
|
| 182 | segmentNumber = DataUtils.bigEndianToUnsignedInt
|
| 183 | (contentObject.name.components[contentObject.name.components.length - 1]);
|
Jeff Thompson | f6995b5 | 2013-01-23 21:21:16 -0800 | [diff] [blame] | 184 | this.segmentStore.storeContent(segmentNumber, contentObject);
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 185 | }
|
| 186 |
|
Jeff Thompson | f6995b5 | 2013-01-23 21:21:16 -0800 | [diff] [blame] | 187 | if (segmentNumber == null || segmentNumber == 0) {
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 188 | // This is the first or only segment, so start.
|
| 189 | // Get the URI from the ContentObject including the version.
|
| 190 | var contentUriSpec;
|
| 191 | if (!this.uriEndsWithSegmentNumber && endsWithSegmentNumber(contentObject.name)) {
|
| 192 | var nameWithoutSegmentNumber = new Name
|
Jeff Thompson | 963d2da | 2012-12-02 23:31:22 -0800 | [diff] [blame] | 193 | (contentObject.name.components.slice(0, contentObject.name.components.length - 1));
|
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 194 | contentUriSpec = "ndn:" + nameWithoutSegmentNumber.to_uri();
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 195 | }
|
| 196 | else
|
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 197 | contentUriSpec = "ndn:" + contentObject.name.to_uri();
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 198 |
|
Jeff Thompson | 1eea632 | 2012-11-23 16:56:18 -0800 | [diff] [blame] | 199 | // Include the search and hash.
|
| 200 | contentUriSpec += this.uriSearchAndHash;
|
| 201 |
|
Jeff Thompson | e769c51 | 2012-11-04 17:25:07 -0800 | [diff] [blame] | 202 | var contentTypeEtc = getNameContentTypeAndCharset(contentObject.name);
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 203 | var ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
|
| 204 | this.contentListener.onStart(contentTypeEtc.contentType, contentTypeEtc.contentCharset,
|
Jeff Thompson | 3663c67 | 2013-02-04 23:22:11 -0800 | [diff] [blame] | 205 | ioService.newURI(contentUriSpec, this.aURI.originCharset, null));
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 206 | }
|
| 207 |
|
Jeff Thompson | f6995b5 | 2013-01-23 21:21:16 -0800 | [diff] [blame] | 208 | if (segmentNumber == null) {
|
| 209 | // We are not doing segments, so just finish.
|
| 210 | this.contentListener.onReceivedContent(DataUtils.toString(contentObject.content));
|
Jeff Thompson | 869e819 | 2012-12-16 12:18:24 -0800 | [diff] [blame] | 211 | this.contentSha256.update(contentObject.content);
|
Jeff Thompson | f6995b5 | 2013-01-23 21:21:16 -0800 | [diff] [blame] | 212 | this.contentListener.onStop();
|
| 213 |
|
| 214 | if (!this.uriEndsWithSegmentNumber) {
|
| 215 | var nameContentDigest = contentObject.name.getContentDigestValue();
|
| 216 | if (nameContentDigest != null &&
|
| 217 | !DataUtils.arraysEqual(nameContentDigest, this.contentSha256.finalize()))
|
| 218 | // TODO: How to show the user an error for invalid digest?
|
| 219 | dump("Content does not match digest in name " + contentObject.name.to_uri());
|
| 220 | }
|
| 221 | return Closure.RESULT_OK;
|
| 222 | }
|
| 223 |
|
| 224 | if (contentObject.signedInfo != null && contentObject.signedInfo.finalBlockID != null)
|
| 225 | this.finalSegmentNumber = DataUtils.bigEndianToUnsignedInt(contentObject.signedInfo.finalBlockID);
|
| 226 |
|
| 227 | // The content was already put in the store. Retrieve as much as possible.
|
| 228 | var entry;
|
| 229 | while ((entry = this.segmentStore.maybeRetrieveNextEntry()) != null) {
|
| 230 | segmentNumber = entry.key;
|
| 231 | contentObject = entry.value;
|
| 232 | this.contentListener.onReceivedContent(DataUtils.toString(contentObject.content));
|
| 233 | this.contentSha256.update(contentObject.content);
|
| 234 |
|
| 235 | if (this.finalSegmentNumber != null && segmentNumber == this.finalSegmentNumber) {
|
| 236 | // Finished.
|
| 237 | this.contentListener.onStop();
|
| 238 | var nameContentDigest = contentObject.name.getContentDigestValue();
|
| 239 | if (nameContentDigest != null &&
|
| 240 | !DataUtils.arraysEqual(nameContentDigest, this.contentSha256.finalize()))
|
| 241 | // TODO: How to show the user an error for invalid digest?
|
| 242 | dump("Content does not match digest in name " + contentObject.name.to_uri());
|
| 243 |
|
| 244 | return Closure.RESULT_OK;
|
| 245 | }
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 246 | }
|
| 247 |
|
Jeff Thompson | f6995b5 | 2013-01-23 21:21:16 -0800 | [diff] [blame] | 248 | if (this.finalSegmentNumber == null && !this.didRequestFinalSegment) {
|
Jeff Thompson | b083c8e | 2013-01-23 21:27:32 -0800 | [diff] [blame] | 249 | this.didRequestFinalSegment = true;
|
Jeff Thompson | f6995b5 | 2013-01-23 21:21:16 -0800 | [diff] [blame] | 250 | // Try to determine the final segment now.
|
| 251 | var components = contentObject.name.components.slice
|
| 252 | (0, contentObject.name.components.length - 1);
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 253 |
|
Jeff Thompson | f6995b5 | 2013-01-23 21:21:16 -0800 | [diff] [blame] | 254 | // Temporarily set the childSelector in the segmentTemplate.
|
| 255 | this.segmentTemplate.childSelector = 1;
|
| 256 | this.ndn.expressInterest(new Name(components), this, this.segmentTemplate);
|
| 257 | this.segmentTemplate.childSelector = null;
|
| 258 | }
|
| 259 |
|
| 260 | // Request new segments.
|
| 261 | var toRequest = this.segmentStore.requestSegmentNumbers(2);
|
| 262 | for (var i = 0; i < toRequest.length; ++i) {
|
| 263 | if (this.finalSegmentNumber != null && toRequest[i] > this.finalSegmentNumber)
|
| 264 | continue;
|
| 265 |
|
| 266 | // Make a name for the segment and get it.
|
| 267 | var segmentNumberBigEndian = DataUtils.nonNegativeIntToBigEndian(toRequest[i]);
|
Jeff Thompson | d0327ff | 2012-11-11 20:30:12 -0800 | [diff] [blame] | 268 | // Put a 0 byte in front.
|
Jeff Thompson | f6995b5 | 2013-01-23 21:21:16 -0800 | [diff] [blame] | 269 | var segmentNumberComponent = new Uint8Array(segmentNumberBigEndian.length + 1);
|
| 270 | segmentNumberComponent.set(segmentNumberBigEndian, 1);
|
Jeff Thompson | d0327ff | 2012-11-11 20:30:12 -0800 | [diff] [blame] | 271 |
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 272 | var components = contentObject.name.components.slice
|
| 273 | (0, contentObject.name.components.length - 1);
|
Jeff Thompson | f6995b5 | 2013-01-23 21:21:16 -0800 | [diff] [blame] | 274 | components.push(segmentNumberComponent);
|
Jeff Thompson | e5a8828 | 2013-01-05 21:02:06 -0800 | [diff] [blame] | 275 | this.ndn.expressInterest(new Name(components), this, this.segmentTemplate);
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 276 | }
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 277 |
|
| 278 | return Closure.RESULT_OK;
|
Jeff Thompson | f6995b5 | 2013-01-23 21:21:16 -0800 | [diff] [blame] | 279 | } catch (ex) {
|
| 280 | dump("ContentClosure.upcall exception: " + ex + "\n" + ex.stack);
|
| 281 | return Closure.RESULT_ERR;
|
| 282 | }
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 283 | };
|
Jeff Thompson | f6995b5 | 2013-01-23 21:21:16 -0800 | [diff] [blame] | 284 |
|
| 285 | /*
|
| 286 | * A SegmentStore stores segments until they are retrieved in order starting with segment 0.
|
| 287 | */
|
| 288 | var SegmentStore = function SegmentStore() {
|
| 289 | // Each entry is an object where the key is the segment number and value is null if
|
| 290 | // the segment number is requested or the contentObject if received.
|
| 291 | this.store = new SortedArray();
|
| 292 | this.maxRetrievedSegmentNumber = -1;
|
| 293 | };
|
| 294 |
|
| 295 | SegmentStore.prototype.storeContent = function(segmentNumber, contentObject) {
|
| 296 | // We don't expect to try to store a segment that has already been retrieved, but check anyway.
|
| 297 | if (segmentNumber > this.maxRetrievedSegmentNumber)
|
| 298 | this.store.set(segmentNumber, contentObject);
|
| 299 | };
|
| 300 |
|
| 301 | /*
|
| 302 | * If the min segment number is this.maxRetrievedSegmentNumber + 1 and its value is not null,
|
| 303 | * then delete from the store, return the entry with key and value, and update maxRetrievedSegmentNumber.
|
| 304 | * Otherwise return null.
|
| 305 | */
|
| 306 | SegmentStore.prototype.maybeRetrieveNextEntry = function() {
|
| 307 | if (this.store.entries.length > 0 && this.store.entries[0].value != null &&
|
| 308 | this.store.entries[0].key == this.maxRetrievedSegmentNumber + 1) {
|
| 309 | var entry = this.store.entries[0];
|
| 310 | this.store.removeAt(0);
|
| 311 | ++this.maxRetrievedSegmentNumber;
|
| 312 | return entry;
|
| 313 | }
|
| 314 | else
|
| 315 | return null;
|
| 316 | };
|
| 317 |
|
| 318 | /*
|
| 319 | * Return an array of the next segment numbers that need to be requested so that the total
|
| 320 | * requested segments is totalRequestedSegments. If a segment store entry value is null, it is
|
| 321 | * already requested and is not returned. If a segment number is returned, create a
|
| 322 | * entry in the segment store with a null value.
|
| 323 | */
|
| 324 | SegmentStore.prototype.requestSegmentNumbers = function(totalRequestedSegments) {
|
| 325 | // First, count how many are already requested.
|
| 326 | var nRequestedSegments = 0;
|
| 327 | for (var i = 0; i < this.store.entries.length; ++i) {
|
| 328 | if (this.store.entries[i].value == null) {
|
| 329 | ++nRequestedSegments;
|
| 330 | if (nRequestedSegments >= totalRequestedSegments)
|
| 331 | // Already maxed out on requests.
|
| 332 | return [];
|
| 333 | }
|
| 334 | }
|
| 335 |
|
| 336 | var toRequest = [];
|
| 337 | var nextSegmentNumber = this.maxRetrievedSegmentNumber + 1;
|
| 338 | for (var i = 0; i < this.store.entries.length; ++i) {
|
| 339 | var entry = this.store.entries[i];
|
| 340 | // Fill in the gap before the segment number in the entry.
|
| 341 | while (nextSegmentNumber < entry.key) {
|
| 342 | toRequest.push(nextSegmentNumber);
|
| 343 | ++nextSegmentNumber;
|
| 344 | ++nRequestedSegments;
|
| 345 | if (nRequestedSegments >= totalRequestedSegments)
|
| 346 | break;
|
| 347 | }
|
| 348 | if (nRequestedSegments >= totalRequestedSegments)
|
| 349 | break;
|
| 350 |
|
| 351 | nextSegmentNumber = entry.key + 1;
|
| 352 | }
|
| 353 |
|
| 354 | // We already filled in the gaps for the segments in the store. Continue after the last.
|
| 355 | while (nRequestedSegments < totalRequestedSegments) {
|
| 356 | toRequest.push(nextSegmentNumber);
|
| 357 | ++nextSegmentNumber;
|
| 358 | ++nRequestedSegments;
|
| 359 | }
|
| 360 |
|
| 361 | // Mark the new segment numbers as requested.
|
| 362 | for (var i = 0; i < toRequest.length; ++i)
|
| 363 | this.store.set(toRequest[i], null);
|
| 364 | return toRequest;
|
| 365 | }
|
| 366 |
|
| 367 | /*
|
| 368 | * A SortedArray is an array of objects with key and value, where the key is an integer.
|
| 369 | */
|
| 370 | var SortedArray = function SortedArray() {
|
| 371 | this.entries = [];
|
| 372 | }
|
| 373 |
|
| 374 | SortedArray.prototype.sortEntries = function() {
|
| 375 | this.entries.sort(function(a, b) { return a.key - b.key; });
|
| 376 | };
|
| 377 |
|
| 378 | SortedArray.prototype.indexOfKey = function(key) {
|
| 379 | for (var i = 0; i < this.entries.length; ++i) {
|
| 380 | if (this.entries[i].key == key)
|
| 381 | return i;
|
| 382 | }
|
| 383 |
|
| 384 | return -1;
|
| 385 | }
|
| 386 |
|
| 387 | SortedArray.prototype.set = function(key, value) {
|
| 388 | var i = this.indexOfKey(key);
|
| 389 | if (i >= 0) {
|
| 390 | this.entries[i].value = value;
|
| 391 | return;
|
| 392 | }
|
| 393 |
|
| 394 | this.entries.push({ key: key, value: value});
|
| 395 | this.sortEntries();
|
| 396 | }
|
| 397 |
|
| 398 | SortedArray.prototype.removeAt = function(index) {
|
| 399 | this.entries.splice(index, 1);
|
| 400 | }
|
| 401 |
|
Jeff Thompson | df0a6f7 | 2012-10-21 15:58:58 -0700 | [diff] [blame] | 402 | /*
|
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 403 | * 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] | 404 | * 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] | 405 | */
|
Jeff Thompson | e769c51 | 2012-11-04 17:25:07 -0800 | [diff] [blame] | 406 | function getNameContentTypeAndCharset(name) {
|
Jeff Thompson | 16a35f7 | 2012-11-25 08:07:33 -0800 | [diff] [blame] | 407 | var iFileName = name.indexOfFileName();
|
| 408 | if (iFileName < 0)
|
| 409 | // Get the default mime type.
|
| 410 | return MimeTypes.getContentTypeAndCharset("");
|
Jeff Thompson | df0a6f7 | 2012-10-21 15:58:58 -0700 | [diff] [blame] | 411 |
|
Jeff Thompson | 16a35f7 | 2012-11-25 08:07:33 -0800 | [diff] [blame] | 412 | return MimeTypes.getContentTypeAndCharset
|
| 413 | (DataUtils.toString(name.components[iFileName]).toLowerCase());
|
Jeff Thompson | df0a6f7 | 2012-10-21 15:58:58 -0700 | [diff] [blame] | 414 | }
|
Jeff Thompson | 10de459 | 2012-10-21 23:54:18 -0700 | [diff] [blame] | 415 |
|
| 416 | /*
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 417 | * 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] | 418 | */
|
Jeff Thompson | 9e6dff0 | 2012-11-04 09:20:47 -0800 | [diff] [blame] | 419 | function endsWithSegmentNumber(name) {
|
Jeff Thompson | 10de459 | 2012-10-21 23:54:18 -0700 | [diff] [blame] | 420 | return name.components != null && name.components.length >= 1 &&
|
| 421 | name.components[name.components.length - 1].length >= 1 &&
|
| 422 | name.components[name.components.length - 1][0] == 0;
|
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 423 | }
|
| 424 |
|
| 425 | /*
|
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 426 | * Find all search keys starting with "ndn." and set the attribute in template.
|
| 427 | * 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] | 428 | * or return "" if there are no search terms left.
|
| 429 | */
|
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 430 | function extractNdnSearch(search, template) {
|
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 431 | if (!(search.length >= 1 && search[0] == '?'))
|
| 432 | return search;
|
| 433 |
|
| 434 | var terms = search.substr(1).split('&');
|
| 435 | var i = 0;
|
| 436 | while (i < terms.length) {
|
| 437 | var keyValue = terms[i].split('=');
|
| 438 | var key = keyValue[0].trim();
|
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 439 | if (key.substr(0, 4) == "ndn.") {
|
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 440 | if (keyValue.length >= 1) {
|
Jeff Thompson | 754652d | 2012-11-24 16:23:43 -0800 | [diff] [blame] | 441 | var value = keyValue[1].trim();
|
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 442 | var nonNegativeInt = parseInt(value);
|
| 443 |
|
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 444 | if (key == "ndn.MinSuffixComponents" && nonNegativeInt >= 0)
|
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 445 | template.minSuffixComponents = nonNegativeInt;
|
Jeff Thompson | 6ac75d2 | 2013-02-04 22:41:34 -0800 | [diff] [blame] | 446 | else if (key == "ndn.MaxSuffixComponents" && nonNegativeInt >= 0)
|
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 447 | template.maxSuffixComponents = nonNegativeInt;
|
Jeff Thompson | 6ac75d2 | 2013-02-04 22:41:34 -0800 | [diff] [blame] | 448 | else if (key == "ndn.ChildSelector" && nonNegativeInt >= 0)
|
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 449 | template.childSelector = nonNegativeInt;
|
Jeff Thompson | 6ac75d2 | 2013-02-04 22:41:34 -0800 | [diff] [blame] | 450 | else if (key == "ndn.AnswerOriginKind" && nonNegativeInt >= 0)
|
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 451 | template.answerOriginKind = nonNegativeInt;
|
Jeff Thompson | 6ac75d2 | 2013-02-04 22:41:34 -0800 | [diff] [blame] | 452 | else if (key == "ndn.Scope" && nonNegativeInt >= 0)
|
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 453 | template.scope = nonNegativeInt;
|
Jeff Thompson | 6ac75d2 | 2013-02-04 22:41:34 -0800 | [diff] [blame] | 454 | else if (key == "ndn.InterestLifetime" && nonNegativeInt >= 0)
|
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 455 | template.interestLifetime = nonNegativeInt;
|
Jeff Thompson | 6ac75d2 | 2013-02-04 22:41:34 -0800 | [diff] [blame] | 456 | else if (key == "ndn.PublisherPublicKeyDigest")
|
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 457 | template.publisherPublicKeyDigest = DataUtils.toNumbersFromString(unescape(value));
|
Jeff Thompson | 6ac75d2 | 2013-02-04 22:41:34 -0800 | [diff] [blame] | 458 | else if (key == "ndn.Nonce")
|
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 459 | template.nonce = DataUtils.toNumbersFromString(unescape(value));
|
Jeff Thompson | 6ac75d2 | 2013-02-04 22:41:34 -0800 | [diff] [blame] | 460 | else if (key == "ndn.Exclude")
|
| 461 | template.exclude = parseExclude(value);
|
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 462 | }
|
| 463 |
|
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 464 | // Remove the "ndn." term and don't advance i.
|
Jeff Thompson | 5fc9b67 | 2012-11-24 10:00:56 -0800 | [diff] [blame] | 465 | terms.splice(i, 1);
|
| 466 | }
|
| 467 | else
|
| 468 | ++i;
|
| 469 | }
|
| 470 |
|
| 471 | if (terms.length == 0)
|
| 472 | return "";
|
| 473 | else
|
| 474 | return "?" + terms.join('&');
|
Jeff Thompson | 963d2da | 2012-12-02 23:31:22 -0800 | [diff] [blame] | 475 | }
|
Jeff Thompson | 6ac75d2 | 2013-02-04 22:41:34 -0800 | [diff] [blame] | 476 |
|
| 477 | /*
|
| 478 | * Parse the comma-separated list of exclude components and return an Exclude.
|
| 479 | */
|
| 480 | function parseExclude(value) {
|
| 481 | var excludeValues = [];
|
| 482 |
|
| 483 | var splitValue = value.split(',');
|
| 484 | for (var i = 0; i < splitValue.length; ++i) {
|
| 485 | var element = splitValue[i].trim();
|
| 486 | if (element == "*")
|
| 487 | excludeValues.push("*")
|
| 488 | else
|
| 489 | excludeValues.push(Name.fromEscapedString(element));
|
| 490 | }
|
| 491 |
|
| 492 | return new Exclude(excludeValues);
|
| 493 | }
|