blob: 407c21f0c9910ef484eb69776e17f32a1e221cf5 [file] [log] [blame]
Jeff Thompson08ab3cd2012-10-08 02:56:20 -07001/*
2 * @author: ucla-cs
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,
26
Jeff Thompson9e6dff02012-11-04 09:20:47 -080027 newURI: function(aSpec, aOriginCharset, aBaseURI)
28 {
29 var uri = Cc["@mozilla.org/network/simple-uri;1"].createInstance(Ci.nsIURI);
30 uri.spec = aSpec;
31 return uri;
32 },
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070033
Jeff Thompson9e6dff02012-11-04 09:20:47 -080034 newChannel: function(aURI)
35 {
36 try {
Jeff Thompsond4617ff2012-10-25 20:40:53 -070037 var trimmedSpec = aURI.spec.trim();
Jeff Thompsondf0a6f72012-10-21 15:58:58 -070038
Jeff Thompson6576bbb2012-10-28 22:20:02 -070039 var contentChannel;
Jeff Thompson9e6dff02012-11-04 09:20:47 -080040 var requestContent = function(contentListener) {
Jeff Thompson25b06412012-10-21 20:07:57 -070041 // Set nameString to the URI without the protocol.
Jeff Thompsond4617ff2012-10-25 20:40:53 -070042 var nameString = trimmedSpec;
Jeff Thompson25b06412012-10-21 20:07:57 -070043 var colonIndex = nameString.indexOf(':');
44 if (colonIndex >= 0)
Jeff Thompsond4617ff2012-10-25 20:40:53 -070045 nameString = nameString.substr
46 (colonIndex + 1, nameString.length - colonIndex - 1).trim();
Jeff Thompson25b06412012-10-21 20:07:57 -070047
Jeff Thompson9e6dff02012-11-04 09:20:47 -080048 var name = new Name(nameString);
Jeff Thompson10de4592012-10-21 23:54:18 -070049 // TODO: Strip off an ending implicit digest before checking the last component?
Jeff Thompson9e6dff02012-11-04 09:20:47 -080050 var uriEndsWithSegmentNumber = endsWithSegmentNumber(name);
Jeff Thompson10de4592012-10-21 23:54:18 -070051
Jeff Thompson9e6dff02012-11-04 09:20:47 -080052 var ndn = new NDN("lioncub.metwi.ucla.edu");
53 ndn.expressInterest(name, new ContentClosure
54 (ndn, contentListener, uriEndsWithSegmentNumber, aURI.originCharset));
55 };
Jeff Thompson57d07382012-10-29 23:25:54 -070056
Jeff Thompson9e6dff02012-11-04 09:20:47 -080057 contentChannel = new ContentChannel(aURI, requestContent);
Jeff Thompson6576bbb2012-10-28 22:20:02 -070058 return contentChannel;
Jeff Thompson9e6dff02012-11-04 09:20:47 -080059 } catch (ex) {
60 dump("CcnxProtocol.newChannel exception: " + ex + "\n");
61 }
62 },
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070063
Jeff Thompson9e6dff02012-11-04 09:20:47 -080064 classDescription: "ccnx Protocol Handler",
65 contractID: "@mozilla.org/network/protocol;1?name=" + "ccnx",
66 classID: Components.ID('{8122e660-1012-11e2-892e-0800200c9a66}'),
67 QueryInterface: XPCOMUtils.generateQI([Ci.nsIProtocolHandler])
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070068}
69
70if (XPCOMUtils.generateNSGetFactory)
Jeff Thompson9e6dff02012-11-04 09:20:47 -080071 var NSGetFactory = XPCOMUtils.generateNSGetFactory([CcnxProtocol]);
Jeff Thompson08ab3cd2012-10-08 02:56:20 -070072else
Jeff Thompson9e6dff02012-11-04 09:20:47 -080073 var NSGetModule = XPCOMUtils.generateNSGetModule([CcnxProtocol]);
74
75/*
76 * Create a closure for calling expressInterest.
77 * contentListener is from the call to requestContent.
78 * uriEndsWithSegmentNumber is true if the URI passed to newChannel has a segment number
79 * (used to determine whether to request only that segment number and for updating the URL bar).
80 * uriOriginCharset is the charset of the URI passed to newChannel (used for making a new URI)
81 */
82var ContentClosure = function ContentClosure
83 (ndn, contentListener, uriEndsWithSegmentNumber, uriOriginCharset) {
84 // Inherit from Closure.
85 Closure.call(this);
86
87 this.ndn = ndn;
88 this.contentListener = contentListener;
89 this.uriEndsWithSegmentNumber = uriEndsWithSegmentNumber;
90 this.uriOriginCharset = uriOriginCharset;
91 this.firstReceivedSegmentNumber = null;
92 this.firstReceivedContentObject = null;
93}
94
95ContentClosure.prototype.upcall = function(kind, upcallInfo) {
96 if (!(kind == Closure.UPCALL_CONTENT ||
97 kind == Closure.UPCALL_CONTENT_UNVERIFIED))
98 // The upcall is not for us.
99 return Closure.RESULT_ERR;
100
101 var contentObject = upcallInfo.contentObject;
102 if (contentObject.content == null) {
103 dump("CcnxProtocol.ContentClosure: contentObject.content is null\n");
104 return Closure.RESULT_ERR;
105 }
106
107 // If !this.uriEndsWithSegmentNumber, we use the segmentNumber to load multiple segments.
108 var segmentNumber = null;
109 if (!this.uriEndsWithSegmentNumber && endsWithSegmentNumber(contentObject.name)) {
110 segmentNumber = DataUtils.bigEndianToUnsignedInt
111 (contentObject.name.components[contentObject.name.components.length - 1]);
112 if (this.firstReceivedSegmentNumber == null) {
113 // This is the first call.
114 this.firstReceivedSegmentNumber = segmentNumber;
115 if (segmentNumber != 0) {
116 // Special case: Save this content object for later and request segment zero.
117 this.firstReceivedContentObject = contentObject;
118 var componentsForZero = contentObject.name.components.slice
119 (0, contentObject.name.components.length - 1);
120 componentsForZero.push([0]);
121 this.ndn.expressInterest(new Name(componentsForZero), this);
122 return Closure.RESULT_OK;
123 }
124 }
125 }
126
127 if (this.uriEndsWithSegmentNumber || segmentNumber == null || segmentNumber == 0) {
128 // This is the first or only segment, so start.
129 // Get the URI from the ContentObject including the version.
130 var contentUriSpec;
131 if (!this.uriEndsWithSegmentNumber && endsWithSegmentNumber(contentObject.name)) {
132 var nameWithoutSegmentNumber = new Name
133 (contentObject.name.components.slice
134 (0, contentObject.name.components.length - 1));
135 contentUriSpec = "ccnx:" + nameWithoutSegmentNumber.to_uri();
136 }
137 else
138 contentUriSpec = "ccnx:" + contentObject.name.to_uri();
139
Jeff Thompsone769c512012-11-04 17:25:07 -0800140 var contentTypeEtc = getNameContentTypeAndCharset(contentObject.name);
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800141 var ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
142 this.contentListener.onStart(contentTypeEtc.contentType, contentTypeEtc.contentCharset,
143 ioService.newURI(contentUriSpec, this.uriOriginCharset, null));
144 }
145
146 this.contentListener.onReceivedContent(DataUtils.toString(contentObject.content));
147
148 // Check for the special case if the saved content is for the next segment that we need.
149 if (this.firstReceivedContentObject != null &&
150 this.firstReceivedSegmentNumber == segmentNumber + 1) {
151 // Substitute the saved contentObject send its content and keep going.
152 contentObject = this.firstReceivedContentObject;
153 segmentNumber = segmentNumber + 1;
154 // Clear firstReceivedContentObject to save memory.
155 this.firstReceivedContentObject = null;
156
157 this.contentListener.onReceivedContent(DataUtils.toString(contentObject.content));
158 }
159
160 var finalSegmentNumber = null;
161 if (contentObject.signedInfo != null && contentObject.signedInfo.finalBlockID != null)
162 finalSegmentNumber = DataUtils.bigEndianToUnsignedInt(contentObject.signedInfo.finalBlockID);
163
164 if (!this.uriEndsWithSegmentNumber &&
165 segmentNumber != null &&
166 (finalSegmentNumber == null || segmentNumber != finalSegmentNumber)) {
167 // Make a name for the next segment and get it.
168 var nextSegmentNumber = DataUtils.nonNegativeIntToBigEndian(segmentNumber + 1);
169 nextSegmentNumber.unshift(0);
170 var components = contentObject.name.components.slice
171 (0, contentObject.name.components.length - 1);
172 components.push(nextSegmentNumber);
173 this.ndn.expressInterest(new Name(components), this);
174 }
175 else
176 // Finished.
177 this.contentListener.onStop();
178
179 return Closure.RESULT_OK;
180};
181
Jeff Thompson08ab3cd2012-10-08 02:56:20 -0700182
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700183/*
Jeff Thompson25b06412012-10-21 20:07:57 -0700184 * Scan the name from the last component to the first (skipping special CCNx components)
185 * for a recognized file name extension, and return an object with properties contentType and charset.
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700186 */
Jeff Thompsone769c512012-11-04 17:25:07 -0800187function getNameContentTypeAndCharset(name) {
188 var filename = "";
Jeff Thompson25b06412012-10-21 20:07:57 -0700189 for (var i = name.components.length - 1; i >= 0; --i) {
190 var component = name.components[i];
191 if (component.length <= 0)
192 continue;
193
194 // Skip special components which just may have ".gif", etc.
195 if (component[0] == 0 || component[0] == 0xC0 || component[0] == 0xC1 ||
196 (component[0] >= 0xF5 && component[0] <= 0xFF))
197 continue;
198
Jeff Thompsone769c512012-11-04 17:25:07 -0800199 filename = DataUtils.toString(component).toLowerCase();
200 break;
Jeff Thompson25b06412012-10-21 20:07:57 -0700201 }
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700202
Jeff Thompsone769c512012-11-04 17:25:07 -0800203 return MimeTypes.getContentTypeAndCharset(filename);
Jeff Thompsondf0a6f72012-10-21 15:58:58 -0700204}
Jeff Thompson10de4592012-10-21 23:54:18 -0700205
206/*
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800207 * Return true if the last component in the name is a segment number..
Jeff Thompson10de4592012-10-21 23:54:18 -0700208 */
Jeff Thompson9e6dff02012-11-04 09:20:47 -0800209function endsWithSegmentNumber(name) {
Jeff Thompson10de4592012-10-21 23:54:18 -0700210 return name.components != null && name.components.length >= 1 &&
211 name.components[name.components.length - 1].length >= 1 &&
212 name.components[name.components.length - 1][0] == 0;
213}