blob: ad9bfaca5423602469dbd42293e7f5e0db3a5574 [file] [log] [blame]
Jeff Thompson08ab3cd2012-10-08 02:56:20 -07001/*
Jeff Thompson17a9da82012-11-12 01:11:01 -08002 * @author: Jeff Thompson
Jeff Thompson745026e2012-10-13 12:49:20 -07003 * See COPYING for copyright and distribution information.
Jeff Thompsonbd829262012-11-30 22:28:37 -08004 * This is the ndn protocol handler.
Jeff Thompson08ab3cd2012-10-08 02:56:20 -07005 * Protocol handling code derived from http://mike.kaply.com/2011/01/18/writing-a-firefox-protocol-handler/
6 */
7
8const Cc = Components.classes;
9const Ci = Components.interfaces;
10const Cr = Components.results;
11
12const nsIProtocolHandler = Ci.nsIProtocolHandler;
13
14Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
15Components.utils.import("chrome://modules/content/ndn-js.jsm");
16Components.utils.import("chrome://modules/content/ContentChannel.jsm");
Jeff Thompson6ad5c362012-12-27 17:57:02 -080017Components.utils.import("chrome://modules/content/NdnProtocolInfo.jsm");
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070018
Jeff Thompsonbd829262012-11-30 22:28:37 -080019function NdnProtocol() {
Jeff Thompson2a67a542012-12-16 17:48:13 -080020 // TODO: Remove host: null when null is the default.
21 this.ndn = new NDN({ getTransport: function() { return new XpcomTransport(); }, host: null });
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070022}
23
Jeff Thompsonbd829262012-11-30 22:28:37 -080024NdnProtocol.prototype = {
25 scheme: "ndn",
Jeff Thompson9e6dff02012-11-04 09:20:47 -080026 protocolFlags: nsIProtocolHandler.URI_NORELATIVE |
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070027 nsIProtocolHandler.URI_NOAUTH |
28 nsIProtocolHandler.URI_LOADABLE_BY_ANYONE,
29
Jeff Thompson9e6dff02012-11-04 09:20:47 -080030 newURI: function(aSpec, aOriginCharset, aBaseURI)
31 {
Jeff Thompson2cc54b42013-01-12 23:11:12 -080032 var uri = Cc["@mozilla.org/network/simple-uri;1"].createInstance(Ci.nsIURI);
33
Jeff Thompsond6b61e42012-11-24 12:10:35 -080034 // We have to trim now because nsIURI converts spaces to %20 and we can't trim in newChannel.
Jeff Thompson8107ec82013-01-12 21:53:27 -080035 var uriParts = splitUri(aSpec);
Jeff Thompson2cc54b42013-01-12 23:11:12 -080036 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.
41 var baseUriParts = splitUri(aBaseURI.spec);
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 Thompson9e6dff02012-11-04 09:20:47 -080065 return uri;
66 },
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070067
Jeff Thompson9e6dff02012-11-04 09:20:47 -080068 newChannel: function(aURI)
69 {
Jeff Thompsonbd829262012-11-30 22:28:37 -080070 var thisNdnProtocol = this;
Jeff Thompson17a9da82012-11-12 01:11:01 -080071
Jeff Thompson1eea6322012-11-23 16:56:18 -080072 try {
Jeff Thompson8107ec82013-01-12 21:53:27 -080073 var uriParts = splitUri(aURI.spec);
Jeff Thompsondf0a6f72012-10-21 15:58:58 -070074
Jeff Thompson5fc9b672012-11-24 10:00:56 -080075 var template = new Interest(new Name([]));
76 // Use the same default as NDN.expressInterest.
Jeff Thompson42806a12012-12-29 18:19:39 -080077 template.interestLifetime = 4000; // milliseconds
Jeff Thompson8107ec82013-01-12 21:53:27 -080078 var searchWithoutNdn = extractNdnSearch(uriParts.search, template);
Jeff Thompsone5a88282013-01-05 21:02:06 -080079
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 Thompson5fc9b672012-11-24 10:00:56 -080085
Jeff Thompson1eea6322012-11-23 16:56:18 -080086 var requestContent = function(contentListener) {
Jeff Thompson8107ec82013-01-12 21:53:27 -080087 var name = new Name(uriParts.name);
Jeff Thompson10de4592012-10-21 23:54:18 -070088 // TODO: Strip off an ending implicit digest before checking the last component?
Jeff Thompson9e6dff02012-11-04 09:20:47 -080089 var uriEndsWithSegmentNumber = endsWithSegmentNumber(name);
Jeff Thompson10de4592012-10-21 23:54:18 -070090
Jeff Thompson3d6ce942012-12-16 12:11:42 -080091 // Use the same NDN object each time.
92 thisNdnProtocol.ndn.expressInterest(name,
93 new ContentClosure(thisNdnProtocol.ndn, contentListener, uriEndsWithSegmentNumber,
Jeff Thompson8107ec82013-01-12 21:53:27 -080094 aURI.originCharset, searchWithoutNdn + uriParts.hash, segmentTemplate),
Jeff Thompson5fc9b672012-11-24 10:00:56 -080095 template);
Jeff Thompson9e6dff02012-11-04 09:20:47 -080096 };
Jeff Thompson57d07382012-10-29 23:25:54 -070097
Jeff Thompson5fc9b672012-11-24 10:00:56 -080098 return new ContentChannel(aURI, requestContent);
Jeff Thompson9e6dff02012-11-04 09:20:47 -080099 } catch (ex) {
Jeff Thompsonbd829262012-11-30 22:28:37 -0800100 dump("NdnProtocol.newChannel exception: " + ex + "\n" + ex.stack);
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800101 }
102 },
Jeff Thompson08ab3cd2012-10-08 02:56:20 -0700103
Jeff Thompsonbd829262012-11-30 22:28:37 -0800104 classDescription: "ndn Protocol Handler",
105 contractID: "@mozilla.org/network/protocol;1?name=" + "ndn",
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800106 classID: Components.ID('{8122e660-1012-11e2-892e-0800200c9a66}'),
107 QueryInterface: XPCOMUtils.generateQI([Ci.nsIProtocolHandler])
Jeff Thompson3d6ce942012-12-16 12:11:42 -0800108};
Jeff Thompson08ab3cd2012-10-08 02:56:20 -0700109
110if (XPCOMUtils.generateNSGetFactory)
Jeff Thompsonbd829262012-11-30 22:28:37 -0800111 var NSGetFactory = XPCOMUtils.generateNSGetFactory([NdnProtocol]);
Jeff Thompson08ab3cd2012-10-08 02:56:20 -0700112else
Jeff Thompsonbd829262012-11-30 22:28:37 -0800113 var NSGetModule = XPCOMUtils.generateNSGetModule([NdnProtocol]);
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800114
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).
120 * uriOriginCharset is the charset of the URI passed to newChannel (used for making a new URI)
Jeff Thompson1eea6322012-11-23 16:56:18 -0800121 * 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 Thompsone5a88282013-01-05 21:02:06 -0800123 * segmentTemplate is the template used in expressInterest to fetch further segments.
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800124 */
125var ContentClosure = function ContentClosure
Jeff Thompsone5a88282013-01-05 21:02:06 -0800126 (ndn, contentListener, uriEndsWithSegmentNumber, uriOriginCharset, uriSearchAndHash,
127 segmentTemplate) {
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800128 // Inherit from Closure.
129 Closure.call(this);
130
131 this.ndn = ndn;
132 this.contentListener = contentListener;
133 this.uriEndsWithSegmentNumber = uriEndsWithSegmentNumber;
134 this.uriOriginCharset = uriOriginCharset;
Jeff Thompson1eea6322012-11-23 16:56:18 -0800135 this.uriSearchAndHash = uriSearchAndHash;
Jeff Thompsone5a88282013-01-05 21:02:06 -0800136 this.segmentTemplate = segmentTemplate;
Jeff Thompson1eea6322012-11-23 16:56:18 -0800137
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800138 this.firstReceivedSegmentNumber = null;
139 this.firstReceivedContentObject = null;
Jeff Thompson963d2da2012-12-02 23:31:22 -0800140 this.contentSha256 = null;
Jeff Thompson3d6ce942012-12-16 12:11:42 -0800141};
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800142
143ContentClosure.prototype.upcall = function(kind, upcallInfo) {
144 if (!(kind == Closure.UPCALL_CONTENT ||
145 kind == Closure.UPCALL_CONTENT_UNVERIFIED))
146 // The upcall is not for us.
147 return Closure.RESULT_ERR;
148
149 var contentObject = upcallInfo.contentObject;
150 if (contentObject.content == null) {
Jeff Thompsonbd829262012-11-30 22:28:37 -0800151 dump("NdnProtocol.ContentClosure: contentObject.content is null\n");
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800152 return Closure.RESULT_ERR;
153 }
Jeff Thompson6ad5c362012-12-27 17:57:02 -0800154
155 // Now that we're connected, report the host and port.
156 setConnectedNdnHub(this.ndn.host, this.ndn.port);
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800157
158 // If !this.uriEndsWithSegmentNumber, we use the segmentNumber to load multiple segments.
159 var segmentNumber = null;
160 if (!this.uriEndsWithSegmentNumber && endsWithSegmentNumber(contentObject.name)) {
161 segmentNumber = DataUtils.bigEndianToUnsignedInt
162 (contentObject.name.components[contentObject.name.components.length - 1]);
163 if (this.firstReceivedSegmentNumber == null) {
164 // This is the first call.
165 this.firstReceivedSegmentNumber = segmentNumber;
166 if (segmentNumber != 0) {
167 // Special case: Save this content object for later and request segment zero.
168 this.firstReceivedContentObject = contentObject;
169 var componentsForZero = contentObject.name.components.slice
170 (0, contentObject.name.components.length - 1);
171 componentsForZero.push([0]);
Jeff Thompsone5a88282013-01-05 21:02:06 -0800172 this.ndn.expressInterest(new Name(componentsForZero), this, this.segmentTemplate);
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800173 return Closure.RESULT_OK;
174 }
175 }
176 }
177
178 if (this.uriEndsWithSegmentNumber || segmentNumber == null || segmentNumber == 0) {
179 // This is the first or only segment, so start.
180 // Get the URI from the ContentObject including the version.
181 var contentUriSpec;
182 if (!this.uriEndsWithSegmentNumber && endsWithSegmentNumber(contentObject.name)) {
183 var nameWithoutSegmentNumber = new Name
Jeff Thompson963d2da2012-12-02 23:31:22 -0800184 (contentObject.name.components.slice(0, contentObject.name.components.length - 1));
Jeff Thompsonbd829262012-11-30 22:28:37 -0800185 contentUriSpec = "ndn:" + nameWithoutSegmentNumber.to_uri();
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800186 }
187 else
Jeff Thompsonbd829262012-11-30 22:28:37 -0800188 contentUriSpec = "ndn:" + contentObject.name.to_uri();
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800189
Jeff Thompson1eea6322012-11-23 16:56:18 -0800190 // Include the search and hash.
191 contentUriSpec += this.uriSearchAndHash;
192
Jeff Thompsone769c512012-11-04 17:25:07 -0800193 var contentTypeEtc = getNameContentTypeAndCharset(contentObject.name);
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800194 var ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
195 this.contentListener.onStart(contentTypeEtc.contentType, contentTypeEtc.contentCharset,
196 ioService.newURI(contentUriSpec, this.uriOriginCharset, null));
Jeff Thompson963d2da2012-12-02 23:31:22 -0800197
Jeff Thompson869e8192012-12-16 12:18:24 -0800198 this.contentSha256 = new Sha256();
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800199 }
200
201 this.contentListener.onReceivedContent(DataUtils.toString(contentObject.content));
Jeff Thompson869e8192012-12-16 12:18:24 -0800202 this.contentSha256.update(contentObject.content);
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800203
204 // Check for the special case if the saved content is for the next segment that we need.
205 if (this.firstReceivedContentObject != null &&
206 this.firstReceivedSegmentNumber == segmentNumber + 1) {
207 // Substitute the saved contentObject send its content and keep going.
208 contentObject = this.firstReceivedContentObject;
209 segmentNumber = segmentNumber + 1;
210 // Clear firstReceivedContentObject to save memory.
211 this.firstReceivedContentObject = null;
212
213 this.contentListener.onReceivedContent(DataUtils.toString(contentObject.content));
Jeff Thompson869e8192012-12-16 12:18:24 -0800214 this.contentSha256.update(contentObject.content);
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800215 }
216
217 var finalSegmentNumber = null;
218 if (contentObject.signedInfo != null && contentObject.signedInfo.finalBlockID != null)
219 finalSegmentNumber = DataUtils.bigEndianToUnsignedInt(contentObject.signedInfo.finalBlockID);
220
221 if (!this.uriEndsWithSegmentNumber &&
222 segmentNumber != null &&
223 (finalSegmentNumber == null || segmentNumber != finalSegmentNumber)) {
224 // Make a name for the next segment and get it.
Jeff Thompsond0327ff2012-11-11 20:30:12 -0800225 var segmentNumberPlus1 = DataUtils.nonNegativeIntToBigEndian(segmentNumber + 1);
226 // Put a 0 byte in front.
227 var nextSegmentNumber = new Uint8Array(segmentNumberPlus1.length + 1);
228 nextSegmentNumber.set(segmentNumberPlus1, 1);
229
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800230 var components = contentObject.name.components.slice
231 (0, contentObject.name.components.length - 1);
Jeff Thompson5a5a2822012-11-24 22:01:20 -0800232 components.push(nextSegmentNumber);
Jeff Thompsone5a88282013-01-05 21:02:06 -0800233 this.ndn.expressInterest(new Name(components), this, this.segmentTemplate);
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800234 }
Jeff Thompson963d2da2012-12-02 23:31:22 -0800235 else {
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800236 // Finished.
Jeff Thompson963d2da2012-12-02 23:31:22 -0800237 this.contentListener.onStop();
Jeff Thompson869e8192012-12-16 12:18:24 -0800238 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());
Jeff Thompson963d2da2012-12-02 23:31:22 -0800243 }
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800244
245 return Closure.RESULT_OK;
246};
Jeff Thompson1eea6322012-11-23 16:56:18 -0800247
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700248/*
Jeff Thompsonbd829262012-11-30 22:28:37 -0800249 * Scan the name from the last component to the first (skipping special name components)
Jeff Thompson25b06412012-10-21 20:07:57 -0700250 * for a recognized file name extension, and return an object with properties contentType and charset.
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700251 */
Jeff Thompsone769c512012-11-04 17:25:07 -0800252function getNameContentTypeAndCharset(name) {
Jeff Thompson16a35f72012-11-25 08:07:33 -0800253 var iFileName = name.indexOfFileName();
254 if (iFileName < 0)
255 // Get the default mime type.
256 return MimeTypes.getContentTypeAndCharset("");
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700257
Jeff Thompson16a35f72012-11-25 08:07:33 -0800258 return MimeTypes.getContentTypeAndCharset
259 (DataUtils.toString(name.components[iFileName]).toLowerCase());
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700260}
Jeff Thompson10de4592012-10-21 23:54:18 -0700261
262/*
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800263 * Return true if the last component in the name is a segment number..
Jeff Thompson10de4592012-10-21 23:54:18 -0700264 */
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800265function endsWithSegmentNumber(name) {
Jeff Thompson10de4592012-10-21 23:54:18 -0700266 return name.components != null && name.components.length >= 1 &&
267 name.components[name.components.length - 1].length >= 1 &&
268 name.components[name.components.length - 1][0] == 0;
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800269}
270
271/*
Jeff Thompsonbd829262012-11-30 22:28:37 -0800272 * Find all search keys starting with "ndn." and set the attribute in template.
273 * Return the search string including the starting "?" but with the "ndn." keys removed,
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800274 * or return "" if there are no search terms left.
275 */
Jeff Thompsonbd829262012-11-30 22:28:37 -0800276function extractNdnSearch(search, template) {
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800277 if (!(search.length >= 1 && search[0] == '?'))
278 return search;
279
280 var terms = search.substr(1).split('&');
281 var i = 0;
282 while (i < terms.length) {
283 var keyValue = terms[i].split('=');
284 var key = keyValue[0].trim();
Jeff Thompsonbd829262012-11-30 22:28:37 -0800285 if (key.substr(0, 4) == "ndn.") {
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800286 if (keyValue.length >= 1) {
Jeff Thompson754652d2012-11-24 16:23:43 -0800287 var value = keyValue[1].trim();
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800288 var nonNegativeInt = parseInt(value);
289
Jeff Thompsonbd829262012-11-30 22:28:37 -0800290 if (key == "ndn.MinSuffixComponents" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800291 template.minSuffixComponents = nonNegativeInt;
Jeff Thompsonbd829262012-11-30 22:28:37 -0800292 if (key == "ndn.MaxSuffixComponents" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800293 template.maxSuffixComponents = nonNegativeInt;
Jeff Thompsonbd829262012-11-30 22:28:37 -0800294 if (key == "ndn.ChildSelector" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800295 template.childSelector = nonNegativeInt;
Jeff Thompsonbd829262012-11-30 22:28:37 -0800296 if (key == "ndn.AnswerOriginKind" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800297 template.answerOriginKind = nonNegativeInt;
Jeff Thompsonbd829262012-11-30 22:28:37 -0800298 if (key == "ndn.Scope" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800299 template.scope = nonNegativeInt;
Jeff Thompsonbd829262012-11-30 22:28:37 -0800300 if (key == "ndn.InterestLifetime" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800301 template.interestLifetime = nonNegativeInt;
Jeff Thompsonbd829262012-11-30 22:28:37 -0800302 if (key == "ndn.PublisherPublicKeyDigest" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800303 template.publisherPublicKeyDigest = DataUtils.toNumbersFromString(unescape(value));
Jeff Thompsonbd829262012-11-30 22:28:37 -0800304 if (key == "ndn.Nonce" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800305 template.nonce = DataUtils.toNumbersFromString(unescape(value));
306 // TODO: handle Exclude.
307 }
308
Jeff Thompsonbd829262012-11-30 22:28:37 -0800309 // Remove the "ndn." term and don't advance i.
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800310 terms.splice(i, 1);
311 }
312 else
313 ++i;
314 }
315
316 if (terms.length == 0)
317 return "";
318 else
319 return "?" + terms.join('&');
Jeff Thompson963d2da2012-12-02 23:31:22 -0800320}