Introduction to Mozilla’s Necko.

This short example shows how one can download a file from an FTP server using mozilla’s Necko networking library(s). It is done by constructing the neccesary XPCOM Components and interfaces. First we need to get the FTP Component:

  1. var ftpcomponent = Components.classes[";1?name=ftp"].createInstance();

Then we QueryInterface() the nsIProtocolHandler. This interface provides protocol specific enhangements to the basic network functions newURI() and newChannel(). The nsIURI and nsIChannel interfaces returned by these functions are not very different. An nsIURI interface is an very descriptive platform independent way to determine a uniform resource identificator - a comprehensive descriptor for a resource. An nsIChannel interface uses the information of an nsIURI and constructs a connection (= channel) between the client and the remote resource.

  1. // Get the protocol handler.
  2. ftpcomponent.QueryInterface(Components.interfaces.nsIProtocolHandler);
  3. // Create an URI and a Channel.
  4. var nsIURI = ftpcomponent.newURI(url,null,null);
  5. var nsIChannel = ftpcomponent.newChannel(nsIURI);

Streams are low-level data containers. They can be used to transport, distribute, convert etc… the data they contain. The nsIChannel open() method creates a synchronised connection to the data and returns an nsIInputStream. If the action should be done asynchronously, use the AsyncOpen() method instead.

nsIInputStreams are not entirely scriptable, so we need to initialise the stream into a scriptable stream. XPCOM provides an interface called nsIScriptableInputStream which is implemented by the;1 component. The returned stream ‘aStream’ is the one we will be using.

  2. var stream =;
  3. var aStream = Components.classes[";1"].createInstance(Components.interfaces.nsIScriptableInputStream);
  4. aStream.init(stream);

Now we have to read the contents of the stream into a buffer (eg: a javascript String object).

  1. var buf = new String;
  2. buf =;

An nsIInputStream has a method called read() which is not scriptable. Therefore we have the use the method read() from the nsIScriptableInputStream. Although these methods look the same there is an important differance. They have a different construction. The nsIInputStream method:

PRUint32 read( charPtr* bufPRUint32 count);
charPtr* buf PRUint32 count ;

The nsIScriptableInputStream method:
char* read( PRUint32 count);
PRUint32 count ;

Where the first one takes the buffer as a parameter the second simply returns it.

Now we close the stream and return the buffer.

  1. aStream.close();
  2. return buf;

Full code listing:

  2. function getFileFromFTP(url) {
  3.    // Get the ftp component.
  4.    var ftpcomponent = Components.classes[";1?name=ftp"].createInstance();
  6.   // Get the protocol handler.
  7.   ftpcomponent.QueryInterface(Components.interfaces.nsIProtocolHandler);
  9.   // Create an URI and a Channel.
  10.   var nsIURI = ftpcomponent.newURI(url,null,null);
  11.   var nsIChannel = ftpcomponent.newChannel(nsIURI);
  13.   // Create the stream (using scriptable_stream).
  14.   var stream =;
  15.   var aStream = Components.classes[";1"]
  16.             .createInstance(Components.interfaces.nsIScriptableInputStream);
  17.   aStream.init(stream);
  19.   // Read the stream contents into a buffer.
  20.   var buf = new String;
  21.   buf =;
  23.   // Close the stream.
  24.   aStream.close();
  26.   // Return the buffer.
  27.   return buf;
  28. }

1 comment to Introduction to Mozilla’s Necko.

  • [...] loaded externally. Flex compiles ok, but runningToni Epple Quick Tip 8: Layer & Factory MethodsIntroduction to Mozilla’s Necko. | Polemian vzwThis short example shows how one can download a file from an FTP server using mozilla’s Necko [...]

Leave a Reply




You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>