blob: f5d526dd47f88d00982b09ade88a9ac5329852e3 [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 Thompson9e6dff02012-11-04 09:20:47 -080039 try {
Jeff Thompsond4617ff2012-10-25 20:40:53 -070040 var trimmedSpec = aURI.spec.trim();
Jeff Thompsondf0a6f72012-10-21 15:58:58 -070041
Jeff Thompson6576bbb2012-10-28 22:20:02 -070042 var contentChannel;
Jeff Thompson9e6dff02012-11-04 09:20:47 -080043 var requestContent = function(contentListener) {
Jeff Thompson25b06412012-10-21 20:07:57 -070044 // Set nameString to the URI without the protocol.
Jeff Thompsond4617ff2012-10-25 20:40:53 -070045 var nameString = trimmedSpec;
Jeff Thompson25b06412012-10-21 20:07:57 -070046 var colonIndex = nameString.indexOf(':');
47 if (colonIndex >= 0)
Jeff Thompsond4617ff2012-10-25 20:40:53 -070048 nameString = nameString.substr
49 (colonIndex + 1, nameString.length - colonIndex - 1).trim();
Jeff Thompson25b06412012-10-21 20:07:57 -070050
Jeff Thompson9e6dff02012-11-04 09:20:47 -080051 var name = new Name(nameString);
Jeff Thompson10de4592012-10-21 23:54:18 -070052 // TODO: Strip off an ending implicit digest before checking the last component?
Jeff Thompson9e6dff02012-11-04 09:20:47 -080053 var uriEndsWithSegmentNumber = endsWithSegmentNumber(name);
Jeff Thompson10de4592012-10-21 23:54:18 -070054
Jeff Thompson17a9da82012-11-12 01:11:01 -080055 var ndn = new NDN({ host: "lioncub.metwi.ucla.edu", port: 9695,
56 // Use the same transport object each time.
57 getTransport: function() { return thisCcnxProtocol.transport; } });
Jeff Thompson9e6dff02012-11-04 09:20:47 -080058 ndn.expressInterest(name, new ContentClosure
59 (ndn, contentListener, uriEndsWithSegmentNumber, aURI.originCharset));
60 };
Jeff Thompson57d07382012-10-29 23:25:54 -070061
Jeff Thompson9e6dff02012-11-04 09:20:47 -080062 contentChannel = new ContentChannel(aURI, requestContent);
Jeff Thompson6576bbb2012-10-28 22:20:02 -070063 return contentChannel;
Jeff Thompson9e6dff02012-11-04 09:20:47 -080064 } catch (ex) {
65 dump("CcnxProtocol.newChannel exception: " + ex + "\n");
66 }
67 },
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070068
Jeff Thompson9e6dff02012-11-04 09:20:47 -080069 classDescription: "ccnx Protocol Handler",
70 contractID: "@mozilla.org/network/protocol;1?name=" + "ccnx",
71 classID: Components.ID('{8122e660-1012-11e2-892e-0800200c9a66}'),
72 QueryInterface: XPCOMUtils.generateQI([Ci.nsIProtocolHandler])
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070073}
74
75if (XPCOMUtils.generateNSGetFactory)
Jeff Thompson9e6dff02012-11-04 09:20:47 -080076 var NSGetFactory = XPCOMUtils.generateNSGetFactory([CcnxProtocol]);
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070077else
Jeff Thompson9e6dff02012-11-04 09:20:47 -080078 var NSGetModule = XPCOMUtils.generateNSGetModule([CcnxProtocol]);
79
80/*
81 * Create a closure for calling expressInterest.
82 * contentListener is from the call to requestContent.
83 * uriEndsWithSegmentNumber is true if the URI passed to newChannel has a segment number
84 * (used to determine whether to request only that segment number and for updating the URL bar).
85 * uriOriginCharset is the charset of the URI passed to newChannel (used for making a new URI)
86 */
87var ContentClosure = function ContentClosure
88 (ndn, contentListener, uriEndsWithSegmentNumber, uriOriginCharset) {
89 // Inherit from Closure.
90 Closure.call(this);
91
92 this.ndn = ndn;
93 this.contentListener = contentListener;
94 this.uriEndsWithSegmentNumber = uriEndsWithSegmentNumber;
95 this.uriOriginCharset = uriOriginCharset;
96 this.firstReceivedSegmentNumber = null;
97 this.firstReceivedContentObject = null;
98}
99
100ContentClosure.prototype.upcall = function(kind, upcallInfo) {
101 if (!(kind == Closure.UPCALL_CONTENT ||
102 kind == Closure.UPCALL_CONTENT_UNVERIFIED))
103 // The upcall is not for us.
104 return Closure.RESULT_ERR;
105
106 var contentObject = upcallInfo.contentObject;
107 if (contentObject.content == null) {
108 dump("CcnxProtocol.ContentClosure: contentObject.content is null\n");
109 return Closure.RESULT_ERR;
110 }
111
112 // If !this.uriEndsWithSegmentNumber, we use the segmentNumber to load multiple segments.
113 var segmentNumber = null;
114 if (!this.uriEndsWithSegmentNumber && endsWithSegmentNumber(contentObject.name)) {
115 segmentNumber = DataUtils.bigEndianToUnsignedInt
116 (contentObject.name.components[contentObject.name.components.length - 1]);
117 if (this.firstReceivedSegmentNumber == null) {
118 // This is the first call.
119 this.firstReceivedSegmentNumber = segmentNumber;
120 if (segmentNumber != 0) {
121 // Special case: Save this content object for later and request segment zero.
122 this.firstReceivedContentObject = contentObject;
123 var componentsForZero = contentObject.name.components.slice
124 (0, contentObject.name.components.length - 1);
125 componentsForZero.push([0]);
126 this.ndn.expressInterest(new Name(componentsForZero), this);
127 return Closure.RESULT_OK;
128 }
129 }
130 }
131
132 if (this.uriEndsWithSegmentNumber || segmentNumber == null || segmentNumber == 0) {
133 // This is the first or only segment, so start.
134 // Get the URI from the ContentObject including the version.
135 var contentUriSpec;
136 if (!this.uriEndsWithSegmentNumber && endsWithSegmentNumber(contentObject.name)) {
137 var nameWithoutSegmentNumber = new Name
138 (contentObject.name.components.slice
139 (0, contentObject.name.components.length - 1));
140 contentUriSpec = "ccnx:" + nameWithoutSegmentNumber.to_uri();
141 }
142 else
143 contentUriSpec = "ccnx:" + contentObject.name.to_uri();
144
Jeff Thompsone769c512012-11-04 17:25:07 -0800145 var contentTypeEtc = getNameContentTypeAndCharset(contentObject.name);
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800146 var ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
147 this.contentListener.onStart(contentTypeEtc.contentType, contentTypeEtc.contentCharset,
148 ioService.newURI(contentUriSpec, this.uriOriginCharset, null));
149 }
150
151 this.contentListener.onReceivedContent(DataUtils.toString(contentObject.content));
152
153 // Check for the special case if the saved content is for the next segment that we need.
154 if (this.firstReceivedContentObject != null &&
155 this.firstReceivedSegmentNumber == segmentNumber + 1) {
156 // Substitute the saved contentObject send its content and keep going.
157 contentObject = this.firstReceivedContentObject;
158 segmentNumber = segmentNumber + 1;
159 // Clear firstReceivedContentObject to save memory.
160 this.firstReceivedContentObject = null;
161
162 this.contentListener.onReceivedContent(DataUtils.toString(contentObject.content));
163 }
164
165 var finalSegmentNumber = null;
166 if (contentObject.signedInfo != null && contentObject.signedInfo.finalBlockID != null)
167 finalSegmentNumber = DataUtils.bigEndianToUnsignedInt(contentObject.signedInfo.finalBlockID);
168
169 if (!this.uriEndsWithSegmentNumber &&
170 segmentNumber != null &&
171 (finalSegmentNumber == null || segmentNumber != finalSegmentNumber)) {
172 // Make a name for the next segment and get it.
Jeff Thompsond0327ff2012-11-11 20:30:12 -0800173 var segmentNumberPlus1 = DataUtils.nonNegativeIntToBigEndian(segmentNumber + 1);
174 // Put a 0 byte in front.
175 var nextSegmentNumber = new Uint8Array(segmentNumberPlus1.length + 1);
176 nextSegmentNumber.set(segmentNumberPlus1, 1);
177
178 // TODO: When Name uses Uint8Array, we don't need a byte array.
179 var nextSegmentNumberByteArray = [];
180 for (var i = 0; i < nextSegmentNumber.length; ++i)
181 nextSegmentNumberByteArray.push(nextSegmentNumber[i]);
182
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800183 var components = contentObject.name.components.slice
184 (0, contentObject.name.components.length - 1);
Jeff Thompsond0327ff2012-11-11 20:30:12 -0800185 components.push(nextSegmentNumberByteArray);
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800186 this.ndn.expressInterest(new Name(components), this);
187 }
188 else
189 // Finished.
190 this.contentListener.onStop();
191
192 return Closure.RESULT_OK;
193};
194
Jeff Thompson08ab3cd2012-10-08 02:56:20 -0700195
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700196/*
Jeff Thompson25b06412012-10-21 20:07:57 -0700197 * Scan the name from the last component to the first (skipping special CCNx components)
198 * for a recognized file name extension, and return an object with properties contentType and charset.
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700199 */
Jeff Thompsone769c512012-11-04 17:25:07 -0800200function getNameContentTypeAndCharset(name) {
201 var filename = "";
Jeff Thompson25b06412012-10-21 20:07:57 -0700202 for (var i = name.components.length - 1; i >= 0; --i) {
203 var component = name.components[i];
204 if (component.length <= 0)
205 continue;
206
207 // Skip special components which just may have ".gif", etc.
208 if (component[0] == 0 || component[0] == 0xC0 || component[0] == 0xC1 ||
209 (component[0] >= 0xF5 && component[0] <= 0xFF))
210 continue;
211
Jeff Thompsone769c512012-11-04 17:25:07 -0800212 filename = DataUtils.toString(component).toLowerCase();
213 break;
Jeff Thompson25b06412012-10-21 20:07:57 -0700214 }
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700215
Jeff Thompsone769c512012-11-04 17:25:07 -0800216 return MimeTypes.getContentTypeAndCharset(filename);
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700217}
Jeff Thompson10de4592012-10-21 23:54:18 -0700218
219/*
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800220 * Return true if the last component in the name is a segment number..
Jeff Thompson10de4592012-10-21 23:54:18 -0700221 */
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800222function endsWithSegmentNumber(name) {
Jeff Thompson10de4592012-10-21 23:54:18 -0700223 return name.components != null && name.components.length >= 1 &&
224 name.components[name.components.length - 1].length >= 1 &&
225 name.components[name.components.length - 1][0] == 0;
226}