michael@0: /* vim:set ts=2 sw=2 et: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * To use try out this JS server socket implementation, just copy this file michael@0: * into the "components" directory of a Mozilla build. Then load the URL michael@0: * http://localhost:4444/ in the browser. You should see a page get loaded michael@0: * that was served up by this component :-) michael@0: * michael@0: * This code requires Mozilla 1.6 or better. michael@0: */ michael@0: michael@0: const kTESTSERV_CONTRACTID = "@mozilla.org/network/test-serv;1"; michael@0: const kTESTSERV_CID = Components.ID("{a741fcd5-9695-42e8-a7f7-14f9a29f8200}"); michael@0: const nsISupports = Components.interfaces.nsISupports; michael@0: const nsIObserver = Components.interfaces.nsIObserver; michael@0: const nsIServerSocket = Components.interfaces.nsIServerSocket; michael@0: const nsIServerSocketListener = Components.interfaces.nsIServerSocketListener; michael@0: const nsITransport = Components.interfaces.nsITransport; michael@0: const nsIScriptableInputStream = Components.interfaces.nsIScriptableInputStream; michael@0: michael@0: /** we'll listen on this port for HTTP requests **/ michael@0: const kPORT = 4444; michael@0: michael@0: function nsTestServ() { dump(">>> creating nsTestServ instance\n"); }; michael@0: michael@0: nsTestServ.prototype = michael@0: { michael@0: QueryInterface: function(iid) michael@0: { michael@0: if (iid.equals(nsIObserver) || michael@0: iid.equals(nsIServerSocketListener) || michael@0: iid.equals(nsISupports)) michael@0: return this; michael@0: michael@0: throw Components.results.NS_ERROR_NO_INTERFACE; michael@0: }, michael@0: michael@0: observe: function(subject, topic, data) michael@0: { michael@0: dump(">>> observe [" + topic + "]\n"); michael@0: this.startListening(); michael@0: }, michael@0: michael@0: /* this function is called when we receive a new connection */ michael@0: onSocketAccepted: function(serverSocket, clientSocket) michael@0: { michael@0: dump(">>> accepted connection on "+clientSocket.host+":"+clientSocket.port+"\n"); michael@0: michael@0: var input = clientSocket.openInputStream(nsITransport.OPEN_BLOCKING, 0, 0); michael@0: var output = clientSocket.openOutputStream(nsITransport.OPEN_BLOCKING, 0, 0); michael@0: michael@0: this.consumeInput(input); michael@0: michael@0: const fixedResponse = michael@0: "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\nFooooopy!!\r\n"; michael@0: var response = fixedResponse + "\r\n" + new Date().toString() + "\r\n"; michael@0: var n = output.write(response, response.length); michael@0: dump(">>> wrote "+n+" bytes\n"); michael@0: michael@0: input.close(); michael@0: output.close(); michael@0: }, michael@0: michael@0: onStopListening: function(serverSocket, status) michael@0: { michael@0: dump(">>> shutting down server socket\n"); michael@0: }, michael@0: michael@0: startListening: function() michael@0: { michael@0: const SERVERSOCKET_CONTRACTID = "@mozilla.org/network/server-socket;1"; michael@0: var socket = Components.classes[SERVERSOCKET_CONTRACTID].createInstance(nsIServerSocket); michael@0: socket.init(kPORT, true /* loopback only */, 5); michael@0: dump(">>> listening on port "+socket.port+"\n"); michael@0: socket.asyncListen(this); michael@0: }, michael@0: michael@0: consumeInput: function(input) michael@0: { michael@0: /* use nsIScriptableInputStream to consume all of the data on the stream */ michael@0: michael@0: var sin = Components.classes["@mozilla.org/scriptableinputstream;1"] michael@0: .createInstance(nsIScriptableInputStream); michael@0: sin.init(input); michael@0: michael@0: /* discard all data */ michael@0: while (sin.available() > 0) michael@0: sin.read(512); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * JS XPCOM component registration goop: michael@0: * michael@0: * We set ourselves up to observe the xpcom-startup category. This provides michael@0: * us with a starting point. michael@0: */ michael@0: michael@0: var servModule = new Object(); michael@0: michael@0: servModule.registerSelf = michael@0: function (compMgr, fileSpec, location, type) michael@0: { michael@0: compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar); michael@0: compMgr.registerFactoryLocation(kTESTSERV_CID, michael@0: "nsTestServ", michael@0: kTESTSERV_CONTRACTID, michael@0: fileSpec, michael@0: location, michael@0: type); michael@0: michael@0: const CATMAN_CONTRACTID = "@mozilla.org/categorymanager;1"; michael@0: const nsICategoryManager = Components.interfaces.nsICategoryManager; michael@0: var catman = Components.classes[CATMAN_CONTRACTID].getService(nsICategoryManager); michael@0: catman.addCategoryEntry("xpcom-startup", michael@0: "TestServ", michael@0: kTESTSERV_CONTRACTID, michael@0: true, michael@0: true); michael@0: } michael@0: michael@0: servModule.getClassObject = michael@0: function (compMgr, cid, iid) michael@0: { michael@0: if (!cid.equals(kTESTSERV_CID)) michael@0: throw Components.results.NS_ERROR_NO_INTERFACE; michael@0: michael@0: if (!iid.equals(Components.interfaces.nsIFactory)) michael@0: throw Components.results.NS_ERROR_NOT_IMPLEMENTED; michael@0: michael@0: return servFactory; michael@0: } michael@0: michael@0: servModule.canUnload = michael@0: function (compMgr) michael@0: { michael@0: dump(">>> unloading test serv.\n"); michael@0: return true; michael@0: } michael@0: michael@0: var servFactory = new Object(); michael@0: michael@0: servFactory.createInstance = michael@0: function (outer, iid) michael@0: { michael@0: if (outer != null) michael@0: throw Components.results.NS_ERROR_NO_AGGREGATION; michael@0: michael@0: if (!iid.equals(nsIObserver) && michael@0: !iid.equals(nsISupports)) michael@0: throw Components.results.NS_ERROR_NO_INTERFACE; michael@0: michael@0: return TestServ; michael@0: } michael@0: michael@0: function NSGetModule(compMgr, fileSpec) michael@0: { michael@0: return servModule; michael@0: } michael@0: michael@0: var TestServ = new nsTestServ();