blob: ed6ea7e64ffcb1fe415af6da6777344dde17e998 [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.
4 * This is the ccnx protocol handler for NDN.
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
18function CcnxProtocol() {
19}
20
21CcnxProtocol.prototype = {
Jeff Thompson9e6dff02012-11-04 09:20:47 -080022 scheme: "ccnx",
23 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 {
30 var uri = Cc["@mozilla.org/network/simple-uri;1"].createInstance(Ci.nsIURI);
31 uri.spec = aSpec;
32 return uri;
33 },
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070034
Jeff Thompson9e6dff02012-11-04 09:20:47 -080035 newChannel: function(aURI)
36 {
Jeff Thompson17a9da82012-11-12 01:11:01 -080037 var thisCcnxProtocol = this;
38
Jeff Thompson1eea6322012-11-23 16:56:18 -080039 try {
40 var spec = aURI.spec.trim();
41 var preHash = spec.split('#', 1)[0];
Jeff Thompson5fc9b672012-11-24 10:00:56 -080042 var hash = spec.substr(preHash.length).trim();
Jeff Thompson1eea6322012-11-23 16:56:18 -080043 var preSearch = preHash.split('?', 1)[0];
Jeff Thompson5fc9b672012-11-24 10:00:56 -080044 var search = preHash.substr(preSearch.length).trim();
Jeff Thompson1eea6322012-11-23 16:56:18 -080045 // Set nameString to the preSearch without the protocol.
46 var nameString = preSearch.trim();
47 if (nameString.indexOf(':') >= 0)
Jeff Thompson5fc9b672012-11-24 10:00:56 -080048 nameString = nameString.substr(nameString.indexOf(':') + 1).trim();
Jeff Thompsondf0a6f72012-10-21 15:58:58 -070049
Jeff Thompson5fc9b672012-11-24 10:00:56 -080050 var template = new Interest(new Name([]));
51 // Use the same default as NDN.expressInterest.
52 template.interestLifetime = 4200;
53 var searchWithoutCcnx = extractCcnxSearch(search, template);
54
Jeff Thompson1eea6322012-11-23 16:56:18 -080055 var requestContent = function(contentListener) {
Jeff Thompson9e6dff02012-11-04 09:20:47 -080056 var name = new Name(nameString);
Jeff Thompson10de4592012-10-21 23:54:18 -070057 // TODO: Strip off an ending implicit digest before checking the last component?
Jeff Thompson9e6dff02012-11-04 09:20:47 -080058 var uriEndsWithSegmentNumber = endsWithSegmentNumber(name);
Jeff Thompson10de4592012-10-21 23:54:18 -070059
Jeff Thompson17a9da82012-11-12 01:11:01 -080060 var ndn = new NDN({ host: "lioncub.metwi.ucla.edu", port: 9695,
61 // Use the same transport object each time.
62 getTransport: function() { return thisCcnxProtocol.transport; } });
Jeff Thompson5fc9b672012-11-24 10:00:56 -080063 ndn.expressInterest(name,
64 new ContentClosure(ndn, contentListener, uriEndsWithSegmentNumber,
65 aURI.originCharset, searchWithoutCcnx + hash),
66 template);
Jeff Thompson9e6dff02012-11-04 09:20:47 -080067 };
Jeff Thompson57d07382012-10-29 23:25:54 -070068
Jeff Thompson5fc9b672012-11-24 10:00:56 -080069 return new ContentChannel(aURI, requestContent);
Jeff Thompson9e6dff02012-11-04 09:20:47 -080070 } catch (ex) {
71 dump("CcnxProtocol.newChannel exception: " + ex + "\n");
72 }
73 },
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070074
Jeff Thompson9e6dff02012-11-04 09:20:47 -080075 classDescription: "ccnx Protocol Handler",
76 contractID: "@mozilla.org/network/protocol;1?name=" + "ccnx",
77 classID: Components.ID('{8122e660-1012-11e2-892e-0800200c9a66}'),
78 QueryInterface: XPCOMUtils.generateQI([Ci.nsIProtocolHandler])
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070079}
80
81if (XPCOMUtils.generateNSGetFactory)
Jeff Thompson9e6dff02012-11-04 09:20:47 -080082 var NSGetFactory = XPCOMUtils.generateNSGetFactory([CcnxProtocol]);
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070083else
Jeff Thompson9e6dff02012-11-04 09:20:47 -080084 var NSGetModule = XPCOMUtils.generateNSGetModule([CcnxProtocol]);
85
86/*
87 * Create a closure for calling expressInterest.
88 * contentListener is from the call to requestContent.
89 * uriEndsWithSegmentNumber is true if the URI passed to newChannel has a segment number
90 * (used to determine whether to request only that segment number and for updating the URL bar).
91 * uriOriginCharset is the charset of the URI passed to newChannel (used for making a new URI)
Jeff Thompson1eea6322012-11-23 16:56:18 -080092 * uriSearchAndHash is the search and hash part of the URI passed to newChannel, including the '?'
93 * and/or '#' but without the interest selector fields.
Jeff Thompson9e6dff02012-11-04 09:20:47 -080094 */
95var ContentClosure = function ContentClosure
Jeff Thompson1eea6322012-11-23 16:56:18 -080096 (ndn, contentListener, uriEndsWithSegmentNumber, uriOriginCharset, uriSearchAndHash) {
Jeff Thompson9e6dff02012-11-04 09:20:47 -080097 // Inherit from Closure.
98 Closure.call(this);
99
100 this.ndn = ndn;
101 this.contentListener = contentListener;
102 this.uriEndsWithSegmentNumber = uriEndsWithSegmentNumber;
103 this.uriOriginCharset = uriOriginCharset;
Jeff Thompson1eea6322012-11-23 16:56:18 -0800104 this.uriSearchAndHash = uriSearchAndHash;
105
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800106 this.firstReceivedSegmentNumber = null;
107 this.firstReceivedContentObject = null;
108}
109
110ContentClosure.prototype.upcall = function(kind, upcallInfo) {
111 if (!(kind == Closure.UPCALL_CONTENT ||
112 kind == Closure.UPCALL_CONTENT_UNVERIFIED))
113 // The upcall is not for us.
114 return Closure.RESULT_ERR;
115
116 var contentObject = upcallInfo.contentObject;
117 if (contentObject.content == null) {
118 dump("CcnxProtocol.ContentClosure: contentObject.content is null\n");
119 return Closure.RESULT_ERR;
120 }
121
122 // If !this.uriEndsWithSegmentNumber, we use the segmentNumber to load multiple segments.
123 var segmentNumber = null;
124 if (!this.uriEndsWithSegmentNumber && endsWithSegmentNumber(contentObject.name)) {
125 segmentNumber = DataUtils.bigEndianToUnsignedInt
126 (contentObject.name.components[contentObject.name.components.length - 1]);
127 if (this.firstReceivedSegmentNumber == null) {
128 // This is the first call.
129 this.firstReceivedSegmentNumber = segmentNumber;
130 if (segmentNumber != 0) {
131 // Special case: Save this content object for later and request segment zero.
132 this.firstReceivedContentObject = contentObject;
133 var componentsForZero = contentObject.name.components.slice
134 (0, contentObject.name.components.length - 1);
135 componentsForZero.push([0]);
136 this.ndn.expressInterest(new Name(componentsForZero), this);
137 return Closure.RESULT_OK;
138 }
139 }
140 }
141
142 if (this.uriEndsWithSegmentNumber || segmentNumber == null || segmentNumber == 0) {
143 // This is the first or only segment, so start.
144 // Get the URI from the ContentObject including the version.
145 var contentUriSpec;
146 if (!this.uriEndsWithSegmentNumber && endsWithSegmentNumber(contentObject.name)) {
147 var nameWithoutSegmentNumber = new Name
148 (contentObject.name.components.slice
149 (0, contentObject.name.components.length - 1));
150 contentUriSpec = "ccnx:" + nameWithoutSegmentNumber.to_uri();
151 }
152 else
153 contentUriSpec = "ccnx:" + contentObject.name.to_uri();
154
Jeff Thompson1eea6322012-11-23 16:56:18 -0800155 // Include the search and hash.
156 contentUriSpec += this.uriSearchAndHash;
157
Jeff Thompsone769c512012-11-04 17:25:07 -0800158 var contentTypeEtc = getNameContentTypeAndCharset(contentObject.name);
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800159 var ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
160 this.contentListener.onStart(contentTypeEtc.contentType, contentTypeEtc.contentCharset,
161 ioService.newURI(contentUriSpec, this.uriOriginCharset, null));
162 }
163
164 this.contentListener.onReceivedContent(DataUtils.toString(contentObject.content));
165
166 // Check for the special case if the saved content is for the next segment that we need.
167 if (this.firstReceivedContentObject != null &&
168 this.firstReceivedSegmentNumber == segmentNumber + 1) {
169 // Substitute the saved contentObject send its content and keep going.
170 contentObject = this.firstReceivedContentObject;
171 segmentNumber = segmentNumber + 1;
172 // Clear firstReceivedContentObject to save memory.
173 this.firstReceivedContentObject = null;
174
175 this.contentListener.onReceivedContent(DataUtils.toString(contentObject.content));
176 }
177
178 var finalSegmentNumber = null;
179 if (contentObject.signedInfo != null && contentObject.signedInfo.finalBlockID != null)
180 finalSegmentNumber = DataUtils.bigEndianToUnsignedInt(contentObject.signedInfo.finalBlockID);
181
182 if (!this.uriEndsWithSegmentNumber &&
183 segmentNumber != null &&
184 (finalSegmentNumber == null || segmentNumber != finalSegmentNumber)) {
185 // Make a name for the next segment and get it.
Jeff Thompsond0327ff2012-11-11 20:30:12 -0800186 var segmentNumberPlus1 = DataUtils.nonNegativeIntToBigEndian(segmentNumber + 1);
187 // Put a 0 byte in front.
188 var nextSegmentNumber = new Uint8Array(segmentNumberPlus1.length + 1);
189 nextSegmentNumber.set(segmentNumberPlus1, 1);
190
191 // TODO: When Name uses Uint8Array, we don't need a byte array.
192 var nextSegmentNumberByteArray = [];
193 for (var i = 0; i < nextSegmentNumber.length; ++i)
194 nextSegmentNumberByteArray.push(nextSegmentNumber[i]);
195
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800196 var components = contentObject.name.components.slice
197 (0, contentObject.name.components.length - 1);
Jeff Thompsond0327ff2012-11-11 20:30:12 -0800198 components.push(nextSegmentNumberByteArray);
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800199 this.ndn.expressInterest(new Name(components), this);
200 }
201 else
202 // Finished.
203 this.contentListener.onStop();
204
205 return Closure.RESULT_OK;
206};
Jeff Thompson1eea6322012-11-23 16:56:18 -0800207
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700208/*
Jeff Thompson25b06412012-10-21 20:07:57 -0700209 * Scan the name from the last component to the first (skipping special CCNx components)
210 * for a recognized file name extension, and return an object with properties contentType and charset.
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700211 */
Jeff Thompsone769c512012-11-04 17:25:07 -0800212function getNameContentTypeAndCharset(name) {
213 var filename = "";
Jeff Thompson25b06412012-10-21 20:07:57 -0700214 for (var i = name.components.length - 1; i >= 0; --i) {
215 var component = name.components[i];
216 if (component.length <= 0)
217 continue;
218
219 // Skip special components which just may have ".gif", etc.
220 if (component[0] == 0 || component[0] == 0xC0 || component[0] == 0xC1 ||
221 (component[0] >= 0xF5 && component[0] <= 0xFF))
222 continue;
223
Jeff Thompsone769c512012-11-04 17:25:07 -0800224 filename = DataUtils.toString(component).toLowerCase();
225 break;
Jeff Thompson25b06412012-10-21 20:07:57 -0700226 }
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700227
Jeff Thompsone769c512012-11-04 17:25:07 -0800228 return MimeTypes.getContentTypeAndCharset(filename);
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700229}
Jeff Thompson10de4592012-10-21 23:54:18 -0700230
231/*
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800232 * Return true if the last component in the name is a segment number..
Jeff Thompson10de4592012-10-21 23:54:18 -0700233 */
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800234function endsWithSegmentNumber(name) {
Jeff Thompson10de4592012-10-21 23:54:18 -0700235 return name.components != null && name.components.length >= 1 &&
236 name.components[name.components.length - 1].length >= 1 &&
237 name.components[name.components.length - 1][0] == 0;
Jeff Thompson5fc9b672012-11-24 10:00:56 -0800238}
239
240/*
241 * Find all search keys starting with "ccnx." and set the attribute in template.
242 * Return the search string including the starting "?" but with the "ccnx." keys removed,
243 * or return "" if there are no search terms left.
244 */
245function extractCcnxSearch(search, template) {
246 if (!(search.length >= 1 && search[0] == '?'))
247 return search;
248
249 var terms = search.substr(1).split('&');
250 var i = 0;
251 while (i < terms.length) {
252 var keyValue = terms[i].split('=');
253 var key = keyValue[0].trim();
254 if (key.substr(0, 5) == "ccnx.") {
255 if (keyValue.length >= 1) {
256 var value = keyValue[1].trim;
257 var nonNegativeInt = parseInt(value);
258
259 if (key == "ccnx.MinSuffixComponents" && nonNegativeInt >= 0)
260 template.minSuffixComponents = nonNegativeInt;
261 if (key == "ccnx.MaxSuffixComponents" && nonNegativeInt >= 0)
262 template.maxSuffixComponents = nonNegativeInt;
263 if (key == "ccnx.ChildSelector" && nonNegativeInt >= 0)
264 template.childSelector = nonNegativeInt;
265 if (key == "ccnx.AnswerOriginKind" && nonNegativeInt >= 0)
266 template.answerOriginKind = nonNegativeInt;
267 if (key == "ccnx.Scope" && nonNegativeInt >= 0)
268 template.scope = nonNegativeInt;
269 if (key == "ccnx.InterestLifetime" && nonNegativeInt >= 0)
270 template.interestLifetime = nonNegativeInt;
271 if (key == "ccnx.PublisherPublicKeyDigest" && nonNegativeInt >= 0)
272 template.publisherPublicKeyDigest = DataUtils.toNumbersFromString(unescape(value));
273 if (key == "ccnx.Nonce" && nonNegativeInt >= 0)
274 template.nonce = DataUtils.toNumbersFromString(unescape(value));
275 // TODO: handle Exclude.
276 }
277
278 // Remove the "ccnx." term and don't advance i.
279 terms.splice(i, 1);
280 }
281 else
282 ++i;
283 }
284
285 if (terms.length == 0)
286 return "";
287 else
288 return "?" + terms.join('&');
Jeff Thompson10de4592012-10-21 23:54:18 -0700289}