1 /* vim:set ts=2 sw=2 et: */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 * To use try out this JS server socket implementation, just copy this file
8 * into the "components" directory of a Mozilla build. Then load the URL
9 * http://localhost:4444/ in the browser. You should see a page get loaded
10 * that was served up by this component :-)
12 * This code requires Mozilla 1.6 or better.
15 const kTESTSERV_CONTRACTID = "@mozilla.org/network/test-serv;1";
16 const kTESTSERV_CID = Components.ID("{a741fcd5-9695-42e8-a7f7-14f9a29f8200}");
17 const nsISupports = Components.interfaces.nsISupports;
18 const nsIObserver = Components.interfaces.nsIObserver;
19 const nsIServerSocket = Components.interfaces.nsIServerSocket;
20 const nsIServerSocketListener = Components.interfaces.nsIServerSocketListener;
21 const nsITransport = Components.interfaces.nsITransport;
22 const nsIScriptableInputStream = Components.interfaces.nsIScriptableInputStream;
24 /** we'll listen on this port for HTTP requests **/
27 function nsTestServ() { dump(">>> creating nsTestServ instance\n"); };
29 nsTestServ.prototype =
31 QueryInterface: function(iid)
33 if (iid.equals(nsIObserver) ||
34 iid.equals(nsIServerSocketListener) ||
35 iid.equals(nsISupports))
38 throw Components.results.NS_ERROR_NO_INTERFACE;
41 observe: function(subject, topic, data)
43 dump(">>> observe [" + topic + "]\n");
44 this.startListening();
47 /* this function is called when we receive a new connection */
48 onSocketAccepted: function(serverSocket, clientSocket)
50 dump(">>> accepted connection on "+clientSocket.host+":"+clientSocket.port+"\n");
52 var input = clientSocket.openInputStream(nsITransport.OPEN_BLOCKING, 0, 0);
53 var output = clientSocket.openOutputStream(nsITransport.OPEN_BLOCKING, 0, 0);
55 this.consumeInput(input);
58 "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\nFooooopy!!\r\n";
59 var response = fixedResponse + "\r\n" + new Date().toString() + "\r\n";
60 var n = output.write(response, response.length);
61 dump(">>> wrote "+n+" bytes\n");
67 onStopListening: function(serverSocket, status)
69 dump(">>> shutting down server socket\n");
72 startListening: function()
74 const SERVERSOCKET_CONTRACTID = "@mozilla.org/network/server-socket;1";
75 var socket = Components.classes[SERVERSOCKET_CONTRACTID].createInstance(nsIServerSocket);
76 socket.init(kPORT, true /* loopback only */, 5);
77 dump(">>> listening on port "+socket.port+"\n");
78 socket.asyncListen(this);
81 consumeInput: function(input)
83 /* use nsIScriptableInputStream to consume all of the data on the stream */
85 var sin = Components.classes["@mozilla.org/scriptableinputstream;1"]
86 .createInstance(nsIScriptableInputStream);
89 /* discard all data */
90 while (sin.available() > 0)
96 * JS XPCOM component registration goop:
98 * We set ourselves up to observe the xpcom-startup category. This provides
99 * us with a starting point.
102 var servModule = new Object();
104 servModule.registerSelf =
105 function (compMgr, fileSpec, location, type)
107 compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
108 compMgr.registerFactoryLocation(kTESTSERV_CID,
110 kTESTSERV_CONTRACTID,
115 const CATMAN_CONTRACTID = "@mozilla.org/categorymanager;1";
116 const nsICategoryManager = Components.interfaces.nsICategoryManager;
117 var catman = Components.classes[CATMAN_CONTRACTID].getService(nsICategoryManager);
118 catman.addCategoryEntry("xpcom-startup",
120 kTESTSERV_CONTRACTID,
125 servModule.getClassObject =
126 function (compMgr, cid, iid)
128 if (!cid.equals(kTESTSERV_CID))
129 throw Components.results.NS_ERROR_NO_INTERFACE;
131 if (!iid.equals(Components.interfaces.nsIFactory))
132 throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
137 servModule.canUnload =
140 dump(">>> unloading test serv.\n");
144 var servFactory = new Object();
146 servFactory.createInstance =
147 function (outer, iid)
150 throw Components.results.NS_ERROR_NO_AGGREGATION;
152 if (!iid.equals(nsIObserver) &&
153 !iid.equals(nsISupports))
154 throw Components.results.NS_ERROR_NO_INTERFACE;
159 function NSGetModule(compMgr, fileSpec)
164 var TestServ = new nsTestServ();