blob: afd493bf0e55855e418fe9af0d8fa0a06b3df4de [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];
42 var hash = spec.substr(preHash.length, spec.length).trim();
43 var preSearch = preHash.split('?', 1)[0];
44 var search = preHash.substr(preSearch.length, spec.length).trim();
45 // Set nameString to the preSearch without the protocol.
46 var nameString = preSearch.trim();
47 if (nameString.indexOf(':') >= 0)
48 nameString = nameString.substr(nameString.indexOf(':') + 1, spec.length).trim();
Jeff Thompsondf0a6f72012-10-21 15:58:58 -070049
Jeff Thompson6576bbb2012-10-28 22:20:02 -070050 var contentChannel;
Jeff Thompson1eea6322012-11-23 16:56:18 -080051 var requestContent = function(contentListener) {
Jeff Thompson9e6dff02012-11-04 09:20:47 -080052 var name = new Name(nameString);
Jeff Thompson10de4592012-10-21 23:54:18 -070053 // TODO: Strip off an ending implicit digest before checking the last component?
Jeff Thompson9e6dff02012-11-04 09:20:47 -080054 var uriEndsWithSegmentNumber = endsWithSegmentNumber(name);
Jeff Thompson10de4592012-10-21 23:54:18 -070055
Jeff Thompson17a9da82012-11-12 01:11:01 -080056 var ndn = new NDN({ host: "lioncub.metwi.ucla.edu", port: 9695,
57 // Use the same transport object each time.
58 getTransport: function() { return thisCcnxProtocol.transport; } });
Jeff Thompson9e6dff02012-11-04 09:20:47 -080059 ndn.expressInterest(name, new ContentClosure
Jeff Thompson1eea6322012-11-23 16:56:18 -080060 (ndn, contentListener, uriEndsWithSegmentNumber, aURI.originCharset,
61 search + hash));
Jeff Thompson9e6dff02012-11-04 09:20:47 -080062 };
Jeff Thompson57d07382012-10-29 23:25:54 -070063
Jeff Thompson9e6dff02012-11-04 09:20:47 -080064 contentChannel = new ContentChannel(aURI, requestContent);
Jeff Thompson6576bbb2012-10-28 22:20:02 -070065 return contentChannel;
Jeff Thompson9e6dff02012-11-04 09:20:47 -080066 } catch (ex) {
67 dump("CcnxProtocol.newChannel exception: " + ex + "\n");
68 }
69 },
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070070
Jeff Thompson9e6dff02012-11-04 09:20:47 -080071 classDescription: "ccnx Protocol Handler",
72 contractID: "@mozilla.org/network/protocol;1?name=" + "ccnx",
73 classID: Components.ID('{8122e660-1012-11e2-892e-0800200c9a66}'),
74 QueryInterface: XPCOMUtils.generateQI([Ci.nsIProtocolHandler])
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070075}
76
77if (XPCOMUtils.generateNSGetFactory)
Jeff Thompson9e6dff02012-11-04 09:20:47 -080078 var NSGetFactory = XPCOMUtils.generateNSGetFactory([CcnxProtocol]);
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070079else
Jeff Thompson9e6dff02012-11-04 09:20:47 -080080 var NSGetModule = XPCOMUtils.generateNSGetModule([CcnxProtocol]);
81
82/*
83 * Create a closure for calling expressInterest.
84 * contentListener is from the call to requestContent.
85 * uriEndsWithSegmentNumber is true if the URI passed to newChannel has a segment number
86 * (used to determine whether to request only that segment number and for updating the URL bar).
87 * uriOriginCharset is the charset of the URI passed to newChannel (used for making a new URI)
Jeff Thompson1eea6322012-11-23 16:56:18 -080088 * uriSearchAndHash is the search and hash part of the URI passed to newChannel, including the '?'
89 * and/or '#' but without the interest selector fields.
Jeff Thompson9e6dff02012-11-04 09:20:47 -080090 */
91var ContentClosure = function ContentClosure
Jeff Thompson1eea6322012-11-23 16:56:18 -080092 (ndn, contentListener, uriEndsWithSegmentNumber, uriOriginCharset, uriSearchAndHash) {
Jeff Thompson9e6dff02012-11-04 09:20:47 -080093 // Inherit from Closure.
94 Closure.call(this);
95
96 this.ndn = ndn;
97 this.contentListener = contentListener;
98 this.uriEndsWithSegmentNumber = uriEndsWithSegmentNumber;
99 this.uriOriginCharset = uriOriginCharset;
Jeff Thompson1eea6322012-11-23 16:56:18 -0800100 this.uriSearchAndHash = uriSearchAndHash;
101
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800102 this.firstReceivedSegmentNumber = null;
103 this.firstReceivedContentObject = null;
104}
105
106ContentClosure.prototype.upcall = function(kind, upcallInfo) {
107 if (!(kind == Closure.UPCALL_CONTENT ||
108 kind == Closure.UPCALL_CONTENT_UNVERIFIED))
109 // The upcall is not for us.
110 return Closure.RESULT_ERR;
111
112 var contentObject = upcallInfo.contentObject;
113 if (contentObject.content == null) {
114 dump("CcnxProtocol.ContentClosure: contentObject.content is null\n");
115 return Closure.RESULT_ERR;
116 }
117
118 // If !this.uriEndsWithSegmentNumber, we use the segmentNumber to load multiple segments.
119 var segmentNumber = null;
120 if (!this.uriEndsWithSegmentNumber && endsWithSegmentNumber(contentObject.name)) {
121 segmentNumber = DataUtils.bigEndianToUnsignedInt
122 (contentObject.name.components[contentObject.name.components.length - 1]);
123 if (this.firstReceivedSegmentNumber == null) {
124 // This is the first call.
125 this.firstReceivedSegmentNumber = segmentNumber;
126 if (segmentNumber != 0) {
127 // Special case: Save this content object for later and request segment zero.
128 this.firstReceivedContentObject = contentObject;
129 var componentsForZero = contentObject.name.components.slice
130 (0, contentObject.name.components.length - 1);
131 componentsForZero.push([0]);
132 this.ndn.expressInterest(new Name(componentsForZero), this);
133 return Closure.RESULT_OK;
134 }
135 }
136 }
137
138 if (this.uriEndsWithSegmentNumber || segmentNumber == null || segmentNumber == 0) {
139 // This is the first or only segment, so start.
140 // Get the URI from the ContentObject including the version.
141 var contentUriSpec;
142 if (!this.uriEndsWithSegmentNumber && endsWithSegmentNumber(contentObject.name)) {
143 var nameWithoutSegmentNumber = new Name
144 (contentObject.name.components.slice
145 (0, contentObject.name.components.length - 1));
146 contentUriSpec = "ccnx:" + nameWithoutSegmentNumber.to_uri();
147 }
148 else
149 contentUriSpec = "ccnx:" + contentObject.name.to_uri();
150
Jeff Thompson1eea6322012-11-23 16:56:18 -0800151 // Include the search and hash.
152 contentUriSpec += this.uriSearchAndHash;
153
Jeff Thompsone769c512012-11-04 17:25:07 -0800154 var contentTypeEtc = getNameContentTypeAndCharset(contentObject.name);
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800155 var ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
156 this.contentListener.onStart(contentTypeEtc.contentType, contentTypeEtc.contentCharset,
157 ioService.newURI(contentUriSpec, this.uriOriginCharset, null));
158 }
159
160 this.contentListener.onReceivedContent(DataUtils.toString(contentObject.content));
161
162 // Check for the special case if the saved content is for the next segment that we need.
163 if (this.firstReceivedContentObject != null &&
164 this.firstReceivedSegmentNumber == segmentNumber + 1) {
165 // Substitute the saved contentObject send its content and keep going.
166 contentObject = this.firstReceivedContentObject;
167 segmentNumber = segmentNumber + 1;
168 // Clear firstReceivedContentObject to save memory.
169 this.firstReceivedContentObject = null;
170
171 this.contentListener.onReceivedContent(DataUtils.toString(contentObject.content));
172 }
173
174 var finalSegmentNumber = null;
175 if (contentObject.signedInfo != null && contentObject.signedInfo.finalBlockID != null)
176 finalSegmentNumber = DataUtils.bigEndianToUnsignedInt(contentObject.signedInfo.finalBlockID);
177
178 if (!this.uriEndsWithSegmentNumber &&
179 segmentNumber != null &&
180 (finalSegmentNumber == null || segmentNumber != finalSegmentNumber)) {
181 // Make a name for the next segment and get it.
Jeff Thompsond0327ff2012-11-11 20:30:12 -0800182 var segmentNumberPlus1 = DataUtils.nonNegativeIntToBigEndian(segmentNumber + 1);
183 // Put a 0 byte in front.
184 var nextSegmentNumber = new Uint8Array(segmentNumberPlus1.length + 1);
185 nextSegmentNumber.set(segmentNumberPlus1, 1);
186
187 // TODO: When Name uses Uint8Array, we don't need a byte array.
188 var nextSegmentNumberByteArray = [];
189 for (var i = 0; i < nextSegmentNumber.length; ++i)
190 nextSegmentNumberByteArray.push(nextSegmentNumber[i]);
191
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800192 var components = contentObject.name.components.slice
193 (0, contentObject.name.components.length - 1);
Jeff Thompsond0327ff2012-11-11 20:30:12 -0800194 components.push(nextSegmentNumberByteArray);
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800195 this.ndn.expressInterest(new Name(components), this);
196 }
197 else
198 // Finished.
199 this.contentListener.onStop();
200
201 return Closure.RESULT_OK;
202};
Jeff Thompson1eea6322012-11-23 16:56:18 -0800203
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700204/*
Jeff Thompson25b06412012-10-21 20:07:57 -0700205 * Scan the name from the last component to the first (skipping special CCNx components)
206 * for a recognized file name extension, and return an object with properties contentType and charset.
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700207 */
Jeff Thompsone769c512012-11-04 17:25:07 -0800208function getNameContentTypeAndCharset(name) {
209 var filename = "";
Jeff Thompson25b06412012-10-21 20:07:57 -0700210 for (var i = name.components.length - 1; i >= 0; --i) {
211 var component = name.components[i];
212 if (component.length <= 0)
213 continue;
214
215 // Skip special components which just may have ".gif", etc.
216 if (component[0] == 0 || component[0] == 0xC0 || component[0] == 0xC1 ||
217 (component[0] >= 0xF5 && component[0] <= 0xFF))
218 continue;
219
Jeff Thompsone769c512012-11-04 17:25:07 -0800220 filename = DataUtils.toString(component).toLowerCase();
221 break;
Jeff Thompson25b06412012-10-21 20:07:57 -0700222 }
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700223
Jeff Thompsone769c512012-11-04 17:25:07 -0800224 return MimeTypes.getContentTypeAndCharset(filename);
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700225}
Jeff Thompson10de4592012-10-21 23:54:18 -0700226
227/*
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800228 * Return true if the last component in the name is a segment number..
Jeff Thompson10de4592012-10-21 23:54:18 -0700229 */
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800230function endsWithSegmentNumber(name) {
Jeff Thompson10de4592012-10-21 23:54:18 -0700231 return name.components != null && name.components.length >= 1 &&
232 name.components[name.components.length - 1].length >= 1 &&
233 name.components[name.components.length - 1][0] == 0;
234}