blob: a9525741be12137a6f23691e32485bbe547181ff [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");
17
Jeff Thompsonbd829262012-11-30 22:28:37 -080018function NdnProtocol() {
Jeff Thompson3d6ce942012-12-16 12:11:42 -080019 this.ndn = new NDN({ getTransport: function() { return new XpcomTransport(); } });
20 this.ndn.host = null; // TODO: Remove this when null is the default.
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070021}
22
Jeff Thompsonbd829262012-11-30 22:28:37 -080023NdnProtocol.prototype = {
24 scheme: "ndn",
Jeff Thompson9e6dff02012-11-04 09:20:47 -080025 protocolFlags: nsIProtocolHandler.URI_NORELATIVE |
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070026 nsIProtocolHandler.URI_NOAUTH |
27 nsIProtocolHandler.URI_LOADABLE_BY_ANYONE,
28
Jeff Thompson9e6dff02012-11-04 09:20:47 -080029 newURI: function(aSpec, aOriginCharset, aBaseURI)
30 {
Jeff Thompsond6b61e42012-11-24 12:10:35 -080031 // We have to trim now because nsIURI converts spaces to %20 and we can't trim in newChannel.
32 var spec = aSpec.trim();
33 var preSearch = spec.split('?', 1)[0];
34 var searchAndHash = spec.substr(preSearch.length).trim();
35
Jeff Thompson9e6dff02012-11-04 09:20:47 -080036 var uri = Cc["@mozilla.org/network/simple-uri;1"].createInstance(Ci.nsIURI);
Jeff Thompsond6b61e42012-11-24 12:10:35 -080037 uri.spec = preSearch.trim() + searchAndHash;
Jeff Thompson9e6dff02012-11-04 09:20:47 -080038 return uri;
39 },
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070040
Jeff Thompson9e6dff02012-11-04 09:20:47 -080041 newChannel: function(aURI)
42 {
Jeff Thompsonbd829262012-11-30 22:28:37 -080043 var thisNdnProtocol = this;
Jeff Thompson17a9da82012-11-12 01:11:01 -080044
Jeff Thompson1eea6322012-11-23 16:56:18 -080045 try {
Jeff Thompsond6b61e42012-11-24 12:10:35 -080046 // Decode manually since nsIURI doesn't have selectors for hash, etc.
Jeff Thompson1eea6322012-11-23 16:56:18 -080047 var spec = aURI.spec.trim();
48 var preHash = spec.split('#', 1)[0];
Jeff Thompson5fc9b672012-11-24 10:00:56 -080049 var hash = spec.substr(preHash.length).trim();
Jeff Thompson1eea6322012-11-23 16:56:18 -080050 var preSearch = preHash.split('?', 1)[0];
Jeff Thompson5fc9b672012-11-24 10:00:56 -080051 var search = preHash.substr(preSearch.length).trim();
Jeff Thompson1eea6322012-11-23 16:56:18 -080052 // Set nameString to the preSearch without the protocol.
53 var nameString = preSearch.trim();
54 if (nameString.indexOf(':') >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -080055 nameString = nameString.substr(nameString.indexOf(':') + 1).trim();
Jeff Thompsondf0a6f72012-10-21 15:58:58 -070056
Jeff Thompson5fc9b672012-11-24 10:00:56 -080057 var template = new Interest(new Name([]));
58 // Use the same default as NDN.expressInterest.
Jeff Thompson3d6ce942012-12-16 12:11:42 -080059 template.interestLifetime = 4.0;
Jeff Thompsonbd829262012-11-30 22:28:37 -080060 var searchWithoutNdn = extractNdnSearch(search, template);
Jeff Thompson5fc9b672012-11-24 10:00:56 -080061
Jeff Thompson1eea6322012-11-23 16:56:18 -080062 var requestContent = function(contentListener) {
Jeff Thompson9e6dff02012-11-04 09:20:47 -080063 var name = new Name(nameString);
Jeff Thompson10de4592012-10-21 23:54:18 -070064 // TODO: Strip off an ending implicit digest before checking the last component?
Jeff Thompson9e6dff02012-11-04 09:20:47 -080065 var uriEndsWithSegmentNumber = endsWithSegmentNumber(name);
Jeff Thompson10de4592012-10-21 23:54:18 -070066
Jeff Thompson3d6ce942012-12-16 12:11:42 -080067 // Use the same NDN object each time.
68 thisNdnProtocol.ndn.expressInterest(name,
69 new ContentClosure(thisNdnProtocol.ndn, contentListener, uriEndsWithSegmentNumber,
Jeff Thompsonbd829262012-11-30 22:28:37 -080070 aURI.originCharset, searchWithoutNdn + hash),
Jeff Thompson5fc9b672012-11-24 10:00:56 -080071 template);
Jeff Thompson9e6dff02012-11-04 09:20:47 -080072 };
Jeff Thompson57d07382012-10-29 23:25:54 -070073
Jeff Thompson5fc9b672012-11-24 10:00:56 -080074 return new ContentChannel(aURI, requestContent);
Jeff Thompson9e6dff02012-11-04 09:20:47 -080075 } catch (ex) {
Jeff Thompsonbd829262012-11-30 22:28:37 -080076 dump("NdnProtocol.newChannel exception: " + ex + "\n" + ex.stack);
Jeff Thompson9e6dff02012-11-04 09:20:47 -080077 }
78 },
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070079
Jeff Thompsonbd829262012-11-30 22:28:37 -080080 classDescription: "ndn Protocol Handler",
81 contractID: "@mozilla.org/network/protocol;1?name=" + "ndn",
Jeff Thompson9e6dff02012-11-04 09:20:47 -080082 classID: Components.ID('{8122e660-1012-11e2-892e-0800200c9a66}'),
83 QueryInterface: XPCOMUtils.generateQI([Ci.nsIProtocolHandler])
Jeff Thompson3d6ce942012-12-16 12:11:42 -080084};
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070085
86if (XPCOMUtils.generateNSGetFactory)
Jeff Thompsonbd829262012-11-30 22:28:37 -080087 var NSGetFactory = XPCOMUtils.generateNSGetFactory([NdnProtocol]);
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070088else
Jeff Thompsonbd829262012-11-30 22:28:37 -080089 var NSGetModule = XPCOMUtils.generateNSGetModule([NdnProtocol]);
Jeff Thompson9e6dff02012-11-04 09:20:47 -080090
91/*
92 * Create a closure for calling expressInterest.
93 * contentListener is from the call to requestContent.
94 * uriEndsWithSegmentNumber is true if the URI passed to newChannel has a segment number
95 * (used to determine whether to request only that segment number and for updating the URL bar).
96 * uriOriginCharset is the charset of the URI passed to newChannel (used for making a new URI)
Jeff Thompson1eea6322012-11-23 16:56:18 -080097 * uriSearchAndHash is the search and hash part of the URI passed to newChannel, including the '?'
98 * and/or '#' but without the interest selector fields.
Jeff Thompson9e6dff02012-11-04 09:20:47 -080099 */
100var ContentClosure = function ContentClosure
Jeff Thompson1eea6322012-11-23 16:56:18 -0800101 (ndn, contentListener, uriEndsWithSegmentNumber, uriOriginCharset, uriSearchAndHash) {
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800102 // Inherit from Closure.
103 Closure.call(this);
104
105 this.ndn = ndn;
106 this.contentListener = contentListener;
107 this.uriEndsWithSegmentNumber = uriEndsWithSegmentNumber;
108 this.uriOriginCharset = uriOriginCharset;
Jeff Thompson1eea6322012-11-23 16:56:18 -0800109 this.uriSearchAndHash = uriSearchAndHash;
110
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800111 this.firstReceivedSegmentNumber = null;
112 this.firstReceivedContentObject = null;
Jeff Thompson963d2da2012-12-02 23:31:22 -0800113 this.contentSha256 = null;
Jeff Thompson3d6ce942012-12-16 12:11:42 -0800114};
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800115
116ContentClosure.prototype.upcall = function(kind, upcallInfo) {
117 if (!(kind == Closure.UPCALL_CONTENT ||
118 kind == Closure.UPCALL_CONTENT_UNVERIFIED))
119 // The upcall is not for us.
120 return Closure.RESULT_ERR;
121
122 var contentObject = upcallInfo.contentObject;
123 if (contentObject.content == null) {
Jeff Thompsonbd829262012-11-30 22:28:37 -0800124 dump("NdnProtocol.ContentClosure: contentObject.content is null\n");
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800125 return Closure.RESULT_ERR;
126 }
127
128 // If !this.uriEndsWithSegmentNumber, we use the segmentNumber to load multiple segments.
129 var segmentNumber = null;
130 if (!this.uriEndsWithSegmentNumber && endsWithSegmentNumber(contentObject.name)) {
131 segmentNumber = DataUtils.bigEndianToUnsignedInt
132 (contentObject.name.components[contentObject.name.components.length - 1]);
133 if (this.firstReceivedSegmentNumber == null) {
134 // This is the first call.
135 this.firstReceivedSegmentNumber = segmentNumber;
136 if (segmentNumber != 0) {
137 // Special case: Save this content object for later and request segment zero.
138 this.firstReceivedContentObject = contentObject;
139 var componentsForZero = contentObject.name.components.slice
140 (0, contentObject.name.components.length - 1);
141 componentsForZero.push([0]);
142 this.ndn.expressInterest(new Name(componentsForZero), this);
143 return Closure.RESULT_OK;
144 }
145 }
146 }
147
148 if (this.uriEndsWithSegmentNumber || segmentNumber == null || segmentNumber == 0) {
149 // This is the first or only segment, so start.
150 // Get the URI from the ContentObject including the version.
151 var contentUriSpec;
152 if (!this.uriEndsWithSegmentNumber && endsWithSegmentNumber(contentObject.name)) {
153 var nameWithoutSegmentNumber = new Name
Jeff Thompson963d2da2012-12-02 23:31:22 -0800154 (contentObject.name.components.slice(0, contentObject.name.components.length - 1));
Jeff Thompsonbd829262012-11-30 22:28:37 -0800155 contentUriSpec = "ndn:" + nameWithoutSegmentNumber.to_uri();
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800156 }
157 else
Jeff Thompsonbd829262012-11-30 22:28:37 -0800158 contentUriSpec = "ndn:" + contentObject.name.to_uri();
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800159
Jeff Thompson1eea6322012-11-23 16:56:18 -0800160 // Include the search and hash.
161 contentUriSpec += this.uriSearchAndHash;
162
Jeff Thompsone769c512012-11-04 17:25:07 -0800163 var contentTypeEtc = getNameContentTypeAndCharset(contentObject.name);
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800164 var ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
165 this.contentListener.onStart(contentTypeEtc.contentType, contentTypeEtc.contentCharset,
166 ioService.newURI(contentUriSpec, this.uriOriginCharset, null));
Jeff Thompson963d2da2012-12-02 23:31:22 -0800167
Jeff Thompson869e8192012-12-16 12:18:24 -0800168 this.contentSha256 = new Sha256();
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800169 }
170
171 this.contentListener.onReceivedContent(DataUtils.toString(contentObject.content));
Jeff Thompson869e8192012-12-16 12:18:24 -0800172 this.contentSha256.update(contentObject.content);
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800173
174 // Check for the special case if the saved content is for the next segment that we need.
175 if (this.firstReceivedContentObject != null &&
176 this.firstReceivedSegmentNumber == segmentNumber + 1) {
177 // Substitute the saved contentObject send its content and keep going.
178 contentObject = this.firstReceivedContentObject;
179 segmentNumber = segmentNumber + 1;
180 // Clear firstReceivedContentObject to save memory.
181 this.firstReceivedContentObject = null;
182
183 this.contentListener.onReceivedContent(DataUtils.toString(contentObject.content));
Jeff Thompson869e8192012-12-16 12:18:24 -0800184 this.contentSha256.update(contentObject.content);
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800185 }
186
187 var finalSegmentNumber = null;
188 if (contentObject.signedInfo != null && contentObject.signedInfo.finalBlockID != null)
189 finalSegmentNumber = DataUtils.bigEndianToUnsignedInt(contentObject.signedInfo.finalBlockID);
190
191 if (!this.uriEndsWithSegmentNumber &&
192 segmentNumber != null &&
193 (finalSegmentNumber == null || segmentNumber != finalSegmentNumber)) {
194 // Make a name for the next segment and get it.
Jeff Thompsond0327ff2012-11-11 20:30:12 -0800195 var segmentNumberPlus1 = DataUtils.nonNegativeIntToBigEndian(segmentNumber + 1);
196 // Put a 0 byte in front.
197 var nextSegmentNumber = new Uint8Array(segmentNumberPlus1.length + 1);
198 nextSegmentNumber.set(segmentNumberPlus1, 1);
199
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800200 var components = contentObject.name.components.slice
201 (0, contentObject.name.components.length - 1);
Jeff Thompson5a5a2822012-11-24 22:01:20 -0800202 components.push(nextSegmentNumber);
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800203 this.ndn.expressInterest(new Name(components), this);
204 }
Jeff Thompson963d2da2012-12-02 23:31:22 -0800205 else {
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800206 // Finished.
Jeff Thompson963d2da2012-12-02 23:31:22 -0800207 this.contentListener.onStop();
Jeff Thompson869e8192012-12-16 12:18:24 -0800208 var nameContentDigest = contentObject.name.getContentDigestValue();
209 if (nameContentDigest != null &&
210 !DataUtils.arraysEqual(nameContentDigest, this.contentSha256.finalize()))
211 // TODO: How to show the user an error for invalid digest?
212 dump("Content does not match digest in name " + contentObject.name.to_uri());
Jeff Thompson963d2da2012-12-02 23:31:22 -0800213 }
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800214
215 return Closure.RESULT_OK;
216};
Jeff Thompson1eea6322012-11-23 16:56:18 -0800217
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700218/*
Jeff Thompsonbd829262012-11-30 22:28:37 -0800219 * Scan the name from the last component to the first (skipping special name components)
Jeff Thompson25b06412012-10-21 20:07:57 -0700220 * for a recognized file name extension, and return an object with properties contentType and charset.
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700221 */
Jeff Thompsone769c512012-11-04 17:25:07 -0800222function getNameContentTypeAndCharset(name) {
Jeff Thompson16a35f72012-11-25 08:07:33 -0800223 var iFileName = name.indexOfFileName();
224 if (iFileName < 0)
225 // Get the default mime type.
226 return MimeTypes.getContentTypeAndCharset("");
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700227
Jeff Thompson16a35f72012-11-25 08:07:33 -0800228 return MimeTypes.getContentTypeAndCharset
229 (DataUtils.toString(name.components[iFileName]).toLowerCase());
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700230}
Jeff Thompson10de4592012-10-21 23:54:18 -0700231
232/*
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800233 * Return true if the last component in the name is a segment number..
Jeff Thompson10de4592012-10-21 23:54:18 -0700234 */
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800235function endsWithSegmentNumber(name) {
Jeff Thompson10de4592012-10-21 23:54:18 -0700236 return name.components != null && name.components.length >= 1 &&
237 name.components[name.components.length - 1].length >= 1 &&
238 name.components[name.components.length - 1][0] == 0;
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800239}
240
241/*
Jeff Thompsonbd829262012-11-30 22:28:37 -0800242 * Find all search keys starting with "ndn." and set the attribute in template.
243 * Return the search string including the starting "?" but with the "ndn." keys removed,
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800244 * or return "" if there are no search terms left.
245 */
Jeff Thompsonbd829262012-11-30 22:28:37 -0800246function extractNdnSearch(search, template) {
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800247 if (!(search.length >= 1 && search[0] == '?'))
248 return search;
249
250 var terms = search.substr(1).split('&');
251 var i = 0;
252 while (i < terms.length) {
253 var keyValue = terms[i].split('=');
254 var key = keyValue[0].trim();
Jeff Thompsonbd829262012-11-30 22:28:37 -0800255 if (key.substr(0, 4) == "ndn.") {
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800256 if (keyValue.length >= 1) {
Jeff Thompson754652d2012-11-24 16:23:43 -0800257 var value = keyValue[1].trim();
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800258 var nonNegativeInt = parseInt(value);
259
Jeff Thompsonbd829262012-11-30 22:28:37 -0800260 if (key == "ndn.MinSuffixComponents" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800261 template.minSuffixComponents = nonNegativeInt;
Jeff Thompsonbd829262012-11-30 22:28:37 -0800262 if (key == "ndn.MaxSuffixComponents" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800263 template.maxSuffixComponents = nonNegativeInt;
Jeff Thompsonbd829262012-11-30 22:28:37 -0800264 if (key == "ndn.ChildSelector" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800265 template.childSelector = nonNegativeInt;
Jeff Thompsonbd829262012-11-30 22:28:37 -0800266 if (key == "ndn.AnswerOriginKind" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800267 template.answerOriginKind = nonNegativeInt;
Jeff Thompsonbd829262012-11-30 22:28:37 -0800268 if (key == "ndn.Scope" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800269 template.scope = nonNegativeInt;
Jeff Thompsonbd829262012-11-30 22:28:37 -0800270 if (key == "ndn.InterestLifetime" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800271 template.interestLifetime = nonNegativeInt;
Jeff Thompsonbd829262012-11-30 22:28:37 -0800272 if (key == "ndn.PublisherPublicKeyDigest" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800273 template.publisherPublicKeyDigest = DataUtils.toNumbersFromString(unescape(value));
Jeff Thompsonbd829262012-11-30 22:28:37 -0800274 if (key == "ndn.Nonce" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800275 template.nonce = DataUtils.toNumbersFromString(unescape(value));
276 // TODO: handle Exclude.
277 }
278
Jeff Thompsonbd829262012-11-30 22:28:37 -0800279 // Remove the "ndn." term and don't advance i.
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800280 terms.splice(i, 1);
281 }
282 else
283 ++i;
284 }
285
286 if (terms.length == 0)
287 return "";
288 else
289 return "?" + terms.join('&');
Jeff Thompson963d2da2012-12-02 23:31:22 -0800290}