blob: 290f91a01300916f1ab65ecacb42e979ae8143d4 [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 Thompson08ab3cd2012-10-08 02:56:20 -070019}
20
Jeff Thompsonbd829262012-11-30 22:28:37 -080021NdnProtocol.prototype = {
22 scheme: "ndn",
Jeff Thompson9e6dff02012-11-04 09:20:47 -080023 protocolFlags: nsIProtocolHandler.URI_NORELATIVE |
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070024 nsIProtocolHandler.URI_NOAUTH |
25 nsIProtocolHandler.URI_LOADABLE_BY_ANYONE,
Jeff Thompson17a9da82012-11-12 01:11:01 -080026 transport: new XpcomTransport(),
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070027
Jeff Thompson9e6dff02012-11-04 09:20:47 -080028 newURI: function(aSpec, aOriginCharset, aBaseURI)
29 {
Jeff Thompsond6b61e42012-11-24 12:10:35 -080030 // We have to trim now because nsIURI converts spaces to %20 and we can't trim in newChannel.
31 var spec = aSpec.trim();
32 var preSearch = spec.split('?', 1)[0];
33 var searchAndHash = spec.substr(preSearch.length).trim();
34
Jeff Thompson9e6dff02012-11-04 09:20:47 -080035 var uri = Cc["@mozilla.org/network/simple-uri;1"].createInstance(Ci.nsIURI);
Jeff Thompsond6b61e42012-11-24 12:10:35 -080036 uri.spec = preSearch.trim() + searchAndHash;
Jeff Thompson9e6dff02012-11-04 09:20:47 -080037 return uri;
38 },
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070039
Jeff Thompson9e6dff02012-11-04 09:20:47 -080040 newChannel: function(aURI)
41 {
Jeff Thompsonbd829262012-11-30 22:28:37 -080042 var thisNdnProtocol = this;
Jeff Thompson17a9da82012-11-12 01:11:01 -080043
Jeff Thompson1eea6322012-11-23 16:56:18 -080044 try {
Jeff Thompsond6b61e42012-11-24 12:10:35 -080045 // Decode manually since nsIURI doesn't have selectors for hash, etc.
Jeff Thompson1eea6322012-11-23 16:56:18 -080046 var spec = aURI.spec.trim();
47 var preHash = spec.split('#', 1)[0];
Jeff Thompson5fc9b672012-11-24 10:00:56 -080048 var hash = spec.substr(preHash.length).trim();
Jeff Thompson1eea6322012-11-23 16:56:18 -080049 var preSearch = preHash.split('?', 1)[0];
Jeff Thompson5fc9b672012-11-24 10:00:56 -080050 var search = preHash.substr(preSearch.length).trim();
Jeff Thompson1eea6322012-11-23 16:56:18 -080051 // Set nameString to the preSearch without the protocol.
52 var nameString = preSearch.trim();
53 if (nameString.indexOf(':') >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -080054 nameString = nameString.substr(nameString.indexOf(':') + 1).trim();
Jeff Thompsondf0a6f72012-10-21 15:58:58 -070055
Jeff Thompson5fc9b672012-11-24 10:00:56 -080056 var template = new Interest(new Name([]));
57 // Use the same default as NDN.expressInterest.
58 template.interestLifetime = 4200;
Jeff Thompsonbd829262012-11-30 22:28:37 -080059 var searchWithoutNdn = extractNdnSearch(search, template);
Jeff Thompson5fc9b672012-11-24 10:00:56 -080060
Jeff Thompson1eea6322012-11-23 16:56:18 -080061 var requestContent = function(contentListener) {
Jeff Thompson9e6dff02012-11-04 09:20:47 -080062 var name = new Name(nameString);
Jeff Thompson10de4592012-10-21 23:54:18 -070063 // TODO: Strip off an ending implicit digest before checking the last component?
Jeff Thompson9e6dff02012-11-04 09:20:47 -080064 var uriEndsWithSegmentNumber = endsWithSegmentNumber(name);
Jeff Thompson10de4592012-10-21 23:54:18 -070065
Jeff Thompson17a9da82012-11-12 01:11:01 -080066 var ndn = new NDN({ host: "lioncub.metwi.ucla.edu", port: 9695,
67 // Use the same transport object each time.
Jeff Thompsonbd829262012-11-30 22:28:37 -080068 getTransport: function() { return thisNdnProtocol.transport; } });
Jeff Thompson5fc9b672012-11-24 10:00:56 -080069 ndn.expressInterest(name,
70 new ContentClosure(ndn, contentListener, uriEndsWithSegmentNumber,
Jeff Thompsonbd829262012-11-30 22:28:37 -080071 aURI.originCharset, searchWithoutNdn + hash),
Jeff Thompson5fc9b672012-11-24 10:00:56 -080072 template);
Jeff Thompson9e6dff02012-11-04 09:20:47 -080073 };
Jeff Thompson57d07382012-10-29 23:25:54 -070074
Jeff Thompson5fc9b672012-11-24 10:00:56 -080075 return new ContentChannel(aURI, requestContent);
Jeff Thompson9e6dff02012-11-04 09:20:47 -080076 } catch (ex) {
Jeff Thompsonbd829262012-11-30 22:28:37 -080077 dump("NdnProtocol.newChannel exception: " + ex + "\n" + ex.stack);
Jeff Thompson9e6dff02012-11-04 09:20:47 -080078 }
79 },
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070080
Jeff Thompsonbd829262012-11-30 22:28:37 -080081 classDescription: "ndn Protocol Handler",
82 contractID: "@mozilla.org/network/protocol;1?name=" + "ndn",
Jeff Thompson9e6dff02012-11-04 09:20:47 -080083 classID: Components.ID('{8122e660-1012-11e2-892e-0800200c9a66}'),
84 QueryInterface: XPCOMUtils.generateQI([Ci.nsIProtocolHandler])
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070085}
86
87if (XPCOMUtils.generateNSGetFactory)
Jeff Thompsonbd829262012-11-30 22:28:37 -080088 var NSGetFactory = XPCOMUtils.generateNSGetFactory([NdnProtocol]);
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070089else
Jeff Thompsonbd829262012-11-30 22:28:37 -080090 var NSGetModule = XPCOMUtils.generateNSGetModule([NdnProtocol]);
Jeff Thompson9e6dff02012-11-04 09:20:47 -080091
92/*
93 * Create a closure for calling expressInterest.
94 * contentListener is from the call to requestContent.
95 * uriEndsWithSegmentNumber is true if the URI passed to newChannel has a segment number
96 * (used to determine whether to request only that segment number and for updating the URL bar).
97 * uriOriginCharset is the charset of the URI passed to newChannel (used for making a new URI)
Jeff Thompson1eea6322012-11-23 16:56:18 -080098 * uriSearchAndHash is the search and hash part of the URI passed to newChannel, including the '?'
99 * and/or '#' but without the interest selector fields.
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800100 */
101var ContentClosure = function ContentClosure
Jeff Thompson1eea6322012-11-23 16:56:18 -0800102 (ndn, contentListener, uriEndsWithSegmentNumber, uriOriginCharset, uriSearchAndHash) {
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800103 // Inherit from Closure.
104 Closure.call(this);
105
106 this.ndn = ndn;
107 this.contentListener = contentListener;
108 this.uriEndsWithSegmentNumber = uriEndsWithSegmentNumber;
109 this.uriOriginCharset = uriOriginCharset;
Jeff Thompson1eea6322012-11-23 16:56:18 -0800110 this.uriSearchAndHash = uriSearchAndHash;
111
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800112 this.firstReceivedSegmentNumber = null;
113 this.firstReceivedContentObject = null;
Jeff Thompson963d2da2012-12-02 23:31:22 -0800114 this.contentSha256 = null;
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800115}
116
117ContentClosure.prototype.upcall = function(kind, upcallInfo) {
118 if (!(kind == Closure.UPCALL_CONTENT ||
119 kind == Closure.UPCALL_CONTENT_UNVERIFIED))
120 // The upcall is not for us.
121 return Closure.RESULT_ERR;
122
123 var contentObject = upcallInfo.contentObject;
124 if (contentObject.content == null) {
Jeff Thompsonbd829262012-11-30 22:28:37 -0800125 dump("NdnProtocol.ContentClosure: contentObject.content is null\n");
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800126 return Closure.RESULT_ERR;
127 }
128
129 // If !this.uriEndsWithSegmentNumber, we use the segmentNumber to load multiple segments.
130 var segmentNumber = null;
131 if (!this.uriEndsWithSegmentNumber && endsWithSegmentNumber(contentObject.name)) {
132 segmentNumber = DataUtils.bigEndianToUnsignedInt
133 (contentObject.name.components[contentObject.name.components.length - 1]);
134 if (this.firstReceivedSegmentNumber == null) {
135 // This is the first call.
136 this.firstReceivedSegmentNumber = segmentNumber;
137 if (segmentNumber != 0) {
138 // Special case: Save this content object for later and request segment zero.
139 this.firstReceivedContentObject = contentObject;
140 var componentsForZero = contentObject.name.components.slice
141 (0, contentObject.name.components.length - 1);
142 componentsForZero.push([0]);
143 this.ndn.expressInterest(new Name(componentsForZero), this);
144 return Closure.RESULT_OK;
145 }
146 }
147 }
148
149 if (this.uriEndsWithSegmentNumber || segmentNumber == null || segmentNumber == 0) {
150 // This is the first or only segment, so start.
151 // Get the URI from the ContentObject including the version.
152 var contentUriSpec;
153 if (!this.uriEndsWithSegmentNumber && endsWithSegmentNumber(contentObject.name)) {
154 var nameWithoutSegmentNumber = new Name
Jeff Thompson963d2da2012-12-02 23:31:22 -0800155 (contentObject.name.components.slice(0, contentObject.name.components.length - 1));
Jeff Thompsonbd829262012-11-30 22:28:37 -0800156 contentUriSpec = "ndn:" + nameWithoutSegmentNumber.to_uri();
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800157 }
158 else
Jeff Thompsonbd829262012-11-30 22:28:37 -0800159 contentUriSpec = "ndn:" + contentObject.name.to_uri();
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800160
Jeff Thompson1eea6322012-11-23 16:56:18 -0800161 // Include the search and hash.
162 contentUriSpec += this.uriSearchAndHash;
163
Jeff Thompsone769c512012-11-04 17:25:07 -0800164 var contentTypeEtc = getNameContentTypeAndCharset(contentObject.name);
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800165 var ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
166 this.contentListener.onStart(contentTypeEtc.contentType, contentTypeEtc.contentCharset,
167 ioService.newURI(contentUriSpec, this.uriOriginCharset, null));
Jeff Thompson963d2da2012-12-02 23:31:22 -0800168
169 if (contentObject.name.getContentDigestValue() != null)
170 // We need to compute the content digest.
171 this.contentSha256 = new Sha256();
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800172 }
173
174 this.contentListener.onReceivedContent(DataUtils.toString(contentObject.content));
Jeff Thompson963d2da2012-12-02 23:31:22 -0800175 if (this.contentSha256 != null)
176 this.contentSha256.update(contentObject.content);
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800177
178 // Check for the special case if the saved content is for the next segment that we need.
179 if (this.firstReceivedContentObject != null &&
180 this.firstReceivedSegmentNumber == segmentNumber + 1) {
181 // Substitute the saved contentObject send its content and keep going.
182 contentObject = this.firstReceivedContentObject;
183 segmentNumber = segmentNumber + 1;
184 // Clear firstReceivedContentObject to save memory.
185 this.firstReceivedContentObject = null;
186
187 this.contentListener.onReceivedContent(DataUtils.toString(contentObject.content));
Jeff Thompson963d2da2012-12-02 23:31:22 -0800188 if (this.contentSha256 != null)
189 this.contentSha256.update(contentObject.content);
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800190 }
191
192 var finalSegmentNumber = null;
193 if (contentObject.signedInfo != null && contentObject.signedInfo.finalBlockID != null)
194 finalSegmentNumber = DataUtils.bigEndianToUnsignedInt(contentObject.signedInfo.finalBlockID);
195
196 if (!this.uriEndsWithSegmentNumber &&
197 segmentNumber != null &&
198 (finalSegmentNumber == null || segmentNumber != finalSegmentNumber)) {
199 // Make a name for the next segment and get it.
Jeff Thompsond0327ff2012-11-11 20:30:12 -0800200 var segmentNumberPlus1 = DataUtils.nonNegativeIntToBigEndian(segmentNumber + 1);
201 // Put a 0 byte in front.
202 var nextSegmentNumber = new Uint8Array(segmentNumberPlus1.length + 1);
203 nextSegmentNumber.set(segmentNumberPlus1, 1);
204
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800205 var components = contentObject.name.components.slice
206 (0, contentObject.name.components.length - 1);
Jeff Thompson5a5a2822012-11-24 22:01:20 -0800207 components.push(nextSegmentNumber);
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800208 this.ndn.expressInterest(new Name(components), this);
209 }
Jeff Thompson963d2da2012-12-02 23:31:22 -0800210 else {
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800211 // Finished.
Jeff Thompson963d2da2012-12-02 23:31:22 -0800212 this.contentListener.onStop();
213 if (this.contentSha256 != null) {
214 var nameContentDigest = contentObject.name.getContentDigestValue();
215 if (nameContentDigest != null &&
216 !DataUtils.arraysEqual(nameContentDigest, this.contentSha256.finalize()))
217 // TODO: How to show the user an error for invalid digest?
218 dump("Content does not match digest in name " + contentObject.name.to_uri());
219 }
220 }
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800221
222 return Closure.RESULT_OK;
223};
Jeff Thompson1eea6322012-11-23 16:56:18 -0800224
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700225/*
Jeff Thompsonbd829262012-11-30 22:28:37 -0800226 * Scan the name from the last component to the first (skipping special name components)
Jeff Thompson25b06412012-10-21 20:07:57 -0700227 * for a recognized file name extension, and return an object with properties contentType and charset.
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700228 */
Jeff Thompsone769c512012-11-04 17:25:07 -0800229function getNameContentTypeAndCharset(name) {
Jeff Thompson16a35f72012-11-25 08:07:33 -0800230 var iFileName = name.indexOfFileName();
231 if (iFileName < 0)
232 // Get the default mime type.
233 return MimeTypes.getContentTypeAndCharset("");
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700234
Jeff Thompson16a35f72012-11-25 08:07:33 -0800235 return MimeTypes.getContentTypeAndCharset
236 (DataUtils.toString(name.components[iFileName]).toLowerCase());
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700237}
Jeff Thompson10de4592012-10-21 23:54:18 -0700238
239/*
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800240 * Return true if the last component in the name is a segment number..
Jeff Thompson10de4592012-10-21 23:54:18 -0700241 */
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800242function endsWithSegmentNumber(name) {
Jeff Thompson10de4592012-10-21 23:54:18 -0700243 return name.components != null && name.components.length >= 1 &&
244 name.components[name.components.length - 1].length >= 1 &&
245 name.components[name.components.length - 1][0] == 0;
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800246}
247
248/*
Jeff Thompsonbd829262012-11-30 22:28:37 -0800249 * Find all search keys starting with "ndn." and set the attribute in template.
250 * Return the search string including the starting "?" but with the "ndn." keys removed,
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800251 * or return "" if there are no search terms left.
252 */
Jeff Thompsonbd829262012-11-30 22:28:37 -0800253function extractNdnSearch(search, template) {
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800254 if (!(search.length >= 1 && search[0] == '?'))
255 return search;
256
257 var terms = search.substr(1).split('&');
258 var i = 0;
259 while (i < terms.length) {
260 var keyValue = terms[i].split('=');
261 var key = keyValue[0].trim();
Jeff Thompsonbd829262012-11-30 22:28:37 -0800262 if (key.substr(0, 4) == "ndn.") {
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800263 if (keyValue.length >= 1) {
Jeff Thompson754652d2012-11-24 16:23:43 -0800264 var value = keyValue[1].trim();
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800265 var nonNegativeInt = parseInt(value);
266
Jeff Thompsonbd829262012-11-30 22:28:37 -0800267 if (key == "ndn.MinSuffixComponents" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800268 template.minSuffixComponents = nonNegativeInt;
Jeff Thompsonbd829262012-11-30 22:28:37 -0800269 if (key == "ndn.MaxSuffixComponents" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800270 template.maxSuffixComponents = nonNegativeInt;
Jeff Thompsonbd829262012-11-30 22:28:37 -0800271 if (key == "ndn.ChildSelector" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800272 template.childSelector = nonNegativeInt;
Jeff Thompsonbd829262012-11-30 22:28:37 -0800273 if (key == "ndn.AnswerOriginKind" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800274 template.answerOriginKind = nonNegativeInt;
Jeff Thompsonbd829262012-11-30 22:28:37 -0800275 if (key == "ndn.Scope" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800276 template.scope = nonNegativeInt;
Jeff Thompsonbd829262012-11-30 22:28:37 -0800277 if (key == "ndn.InterestLifetime" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800278 template.interestLifetime = nonNegativeInt;
Jeff Thompsonbd829262012-11-30 22:28:37 -0800279 if (key == "ndn.PublisherPublicKeyDigest" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800280 template.publisherPublicKeyDigest = DataUtils.toNumbersFromString(unescape(value));
Jeff Thompsonbd829262012-11-30 22:28:37 -0800281 if (key == "ndn.Nonce" && nonNegativeInt >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800282 template.nonce = DataUtils.toNumbersFromString(unescape(value));
283 // TODO: handle Exclude.
284 }
285
Jeff Thompsonbd829262012-11-30 22:28:37 -0800286 // Remove the "ndn." term and don't advance i.
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800287 terms.splice(i, 1);
288 }
289 else
290 ++i;
291 }
292
293 if (terms.length == 0)
294 return "";
295 else
296 return "?" + terms.join('&');
Jeff Thompson963d2da2012-12-02 23:31:22 -0800297}