1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is Nvu.
16 * The Initial Developer of the Original Code is
18 * Portions created by the Initial Developer are Copyright (C) 2003-2005
19 * the Initial Developer. All Rights Reserved.
22 * Daniel Glazman (glazman@disruptive-innovations.com), original author
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
39 const IOSERVICE_CTRID = "@mozilla.org/network/io-service;1";
40 const cnsIIOService = Components.interfaces.nsIIOService;
41 const SIS_CTRID = "@mozilla.org/scriptableinputstream;1"
42 const cnsIScriptableInputStream = Components.interfaces.nsIScriptableInputStream;
43 const cnsIChannel = Components.interfaces.nsIChannel;
44 const cnsIInputStream = Components.interfaces.nsIInputStream;
45 const cnsIRequest = Components.interfaces.nsIRequest;
46 const cnsIDirectoryListing = Components.interfaces.nsIDirectoryListing;
51 function _getChannelForURL (url)
53 var serv = Components.classes[IOSERVICE_CTRID].getService(cnsIIOService);
57 return serv.newChannel(url, "UTF-8", null);
61 function StreamListener(channel, url, observer)
63 this.channel = channel;
66 this.observer = observer;
69 StreamListener.prototype.onStartRequest =
70 function (request, context)
74 StreamListener.prototype.onStopRequest =
75 function (request, context, status)
78 this.channel.cancel(0x804b0002); // NS_BINDING_ABORTED
81 typeof this.observer.onComplete == "function")
82 this.observer.onComplete (this.data, this.url, status);
85 StreamListener.prototype.onDataAvailable =
86 function (request, context, inStr, sourceOffset, count)
88 // dump ("onDataAvailable(): " + count + "\n");
89 // sometimes the inStr changes between onDataAvailable calls, so we
92 Components.classes[SIS_CTRID].createInstance(cnsIScriptableInputStream);
94 var str = sis.read(count);*/
95 var sis = Components.classes['@mozilla.org/binaryinputstream;1']
96 .createInstance(Components.interfaces.nsIBinaryInputStream);
97 sis.setInputStream(inStr);
98 var str = sis.readBytes(count);
105 function loadURLAsync (url, observer)
107 var chan = _getChannelForURL (url);
108 chan.loadFlags |= cnsIRequest.LOAD_BYPASS_CACHE;
110 chan.contentType = "application/http-index-format";
111 gCurrentChannel = chan;
113 return chan.asyncOpen (new StreamListener (chan, url, observer), null);
116 function newFileInputStream(file, buffered)
118 const PR_RDONLY = 0x1;
120 Components.classes["@mozilla.org/network/file-input-stream;1"].
121 createInstance(Components.interfaces.nsIFileInputStream);
122 stream.init(file, PR_RDONLY, 0, 0);
127 Components.classes["@mozilla.org/network/buffered-input-stream;1"].
128 createInstance(Components.interfaces.nsIBufferedInputStream);
129 buffer.init(stream, 4096);
133 function StoreURLAsync (aURLSpec, aFile, observer)
135 var chan = _getChannelForURL (aURLSpec);
136 chan.loadFlags |= cnsIRequest.LOAD_BYPASS_CACHE;
138 // chan.contentType = "application/http-index-format";
139 gCurrentChannel = chan;
141 var uploadstream = newFileInputStream(aFile, true);
143 chan.QueryInterface(Components.interfaces.nsIUploadChannel);
144 chan.setUploadStream(uploadstream, "", aFile.fileSize);
146 return chan.asyncOpen (new StreamListener (chan, aURLSpec, observer), null);
149 function loadURL(url, caller)
151 function onComplete(data, url, status)
153 if (status == Components.results.NS_OK)
155 if (caller.parseData)
156 caller.parseData(data, caller.rqData);
157 if (caller.endCallback)
158 caller.endCallback(caller.rqData);
160 else if (caller.errorCallback)
161 caller.errorCallback(url, status);
164 loadURLAsync (url, { onComplete: onComplete });
167 /*******************************/
169 function deleteURLAsync (url, treeitem)
171 var chan = _getChannelForURL (url);
172 chan.loadFlags |= cnsIRequest.LOAD_BYPASS_CACHE;
174 chan instanceof Components.interfaces.nsIFTPChannel;
175 var p = new progressListener(chan, FTP_DEL, url, null, treeitem)
176 chan.notificationCallbacks = p.QueryInterface(Components.interfaces.nsIInterfaceRequestor) ;
180 gCurrentChannel = chan;
182 return chan.asyncOpen (new StreamListener (chan, url, null), null);
185 function removeDirURLAsync (url, treeitem)
187 var chan = _getChannelForURL (url);
188 chan.loadFlags |= cnsIRequest.LOAD_BYPASS_CACHE;
190 chan instanceof Components.interfaces.nsIFTPChannel;
191 var p = new progressListener(chan, FTP_RMDIR, url, null, treeitem)
192 chan.notificationCallbacks = p.QueryInterface(Components.interfaces.nsIInterfaceRequestor) ;
193 chan.removeDirectory();
196 gCurrentChannel = chan;
198 return chan.asyncOpen (new StreamListener (chan, url, null), null);
201 function createDirURLAsync (url, aDirName, aRequestData, aEnableNotifs)
203 var chan = _getChannelForURL (url);
204 chan.loadFlags |= cnsIRequest.LOAD_BYPASS_CACHE;
206 chan instanceof Components.interfaces.nsIFTPChannel;
209 var p = new progressListener(chan, FTP_MKDIR, url, aDirName, aRequestData)
210 chan.notificationCallbacks = p.QueryInterface(Components.interfaces.nsIInterfaceRequestor);
212 chan.createDirectory();
215 gCurrentChannel = chan;
217 return chan.asyncOpen (new StreamListener (chan, url, null), null);
220 function renameURLAsync(url, aNewURI, aRequestData)
222 var chan = _getChannelForURL(url);
223 chan.loadFlags |= cnsIRequest.LOAD_BYPASS_CACHE;
225 chan instanceof Components.interfaces.nsIFTPChannel;
226 var p = new progressListener(chan, FTP_RENAME, url, aNewURI, aRequestData)
227 chan.notificationCallbacks = p.QueryInterface(Components.interfaces.nsIInterfaceRequestor) ;
228 chan.renameTo(aNewURI);
231 gCurrentChannel = chan;
233 return chan.asyncOpen (new StreamListener (chan, url, null), null);
236 /**************************************/
238 function DropFtpConnection()
241 gCurrentChannel.cancel(0x804b0002); // NS_BINDING_ABORTED
244 function ForgetAboutLastFtpRequest()
246 gCurrentChannel = null;