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