1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/test/TestServ.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,164 @@ 1.4 +/* vim:set ts=2 sw=2 et: */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/* 1.10 + * To use try out this JS server socket implementation, just copy this file 1.11 + * into the "components" directory of a Mozilla build. Then load the URL 1.12 + * http://localhost:4444/ in the browser. You should see a page get loaded 1.13 + * that was served up by this component :-) 1.14 + * 1.15 + * This code requires Mozilla 1.6 or better. 1.16 + */ 1.17 + 1.18 +const kTESTSERV_CONTRACTID = "@mozilla.org/network/test-serv;1"; 1.19 +const kTESTSERV_CID = Components.ID("{a741fcd5-9695-42e8-a7f7-14f9a29f8200}"); 1.20 +const nsISupports = Components.interfaces.nsISupports; 1.21 +const nsIObserver = Components.interfaces.nsIObserver; 1.22 +const nsIServerSocket = Components.interfaces.nsIServerSocket; 1.23 +const nsIServerSocketListener = Components.interfaces.nsIServerSocketListener; 1.24 +const nsITransport = Components.interfaces.nsITransport; 1.25 +const nsIScriptableInputStream = Components.interfaces.nsIScriptableInputStream; 1.26 + 1.27 +/** we'll listen on this port for HTTP requests **/ 1.28 +const kPORT = 4444; 1.29 + 1.30 +function nsTestServ() { dump(">>> creating nsTestServ instance\n"); }; 1.31 + 1.32 +nsTestServ.prototype = 1.33 +{ 1.34 + QueryInterface: function(iid) 1.35 + { 1.36 + if (iid.equals(nsIObserver) || 1.37 + iid.equals(nsIServerSocketListener) || 1.38 + iid.equals(nsISupports)) 1.39 + return this; 1.40 + 1.41 + throw Components.results.NS_ERROR_NO_INTERFACE; 1.42 + }, 1.43 + 1.44 + observe: function(subject, topic, data) 1.45 + { 1.46 + dump(">>> observe [" + topic + "]\n"); 1.47 + this.startListening(); 1.48 + }, 1.49 + 1.50 + /* this function is called when we receive a new connection */ 1.51 + onSocketAccepted: function(serverSocket, clientSocket) 1.52 + { 1.53 + dump(">>> accepted connection on "+clientSocket.host+":"+clientSocket.port+"\n"); 1.54 + 1.55 + var input = clientSocket.openInputStream(nsITransport.OPEN_BLOCKING, 0, 0); 1.56 + var output = clientSocket.openOutputStream(nsITransport.OPEN_BLOCKING, 0, 0); 1.57 + 1.58 + this.consumeInput(input); 1.59 + 1.60 + const fixedResponse = 1.61 + "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\nFooooopy!!\r\n"; 1.62 + var response = fixedResponse + "\r\n" + new Date().toString() + "\r\n"; 1.63 + var n = output.write(response, response.length); 1.64 + dump(">>> wrote "+n+" bytes\n"); 1.65 + 1.66 + input.close(); 1.67 + output.close(); 1.68 + }, 1.69 + 1.70 + onStopListening: function(serverSocket, status) 1.71 + { 1.72 + dump(">>> shutting down server socket\n"); 1.73 + }, 1.74 + 1.75 + startListening: function() 1.76 + { 1.77 + const SERVERSOCKET_CONTRACTID = "@mozilla.org/network/server-socket;1"; 1.78 + var socket = Components.classes[SERVERSOCKET_CONTRACTID].createInstance(nsIServerSocket); 1.79 + socket.init(kPORT, true /* loopback only */, 5); 1.80 + dump(">>> listening on port "+socket.port+"\n"); 1.81 + socket.asyncListen(this); 1.82 + }, 1.83 + 1.84 + consumeInput: function(input) 1.85 + { 1.86 + /* use nsIScriptableInputStream to consume all of the data on the stream */ 1.87 + 1.88 + var sin = Components.classes["@mozilla.org/scriptableinputstream;1"] 1.89 + .createInstance(nsIScriptableInputStream); 1.90 + sin.init(input); 1.91 + 1.92 + /* discard all data */ 1.93 + while (sin.available() > 0) 1.94 + sin.read(512); 1.95 + } 1.96 +} 1.97 + 1.98 +/** 1.99 + * JS XPCOM component registration goop: 1.100 + * 1.101 + * We set ourselves up to observe the xpcom-startup category. This provides 1.102 + * us with a starting point. 1.103 + */ 1.104 + 1.105 +var servModule = new Object(); 1.106 + 1.107 +servModule.registerSelf = 1.108 +function (compMgr, fileSpec, location, type) 1.109 +{ 1.110 + compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar); 1.111 + compMgr.registerFactoryLocation(kTESTSERV_CID, 1.112 + "nsTestServ", 1.113 + kTESTSERV_CONTRACTID, 1.114 + fileSpec, 1.115 + location, 1.116 + type); 1.117 + 1.118 + const CATMAN_CONTRACTID = "@mozilla.org/categorymanager;1"; 1.119 + const nsICategoryManager = Components.interfaces.nsICategoryManager; 1.120 + var catman = Components.classes[CATMAN_CONTRACTID].getService(nsICategoryManager); 1.121 + catman.addCategoryEntry("xpcom-startup", 1.122 + "TestServ", 1.123 + kTESTSERV_CONTRACTID, 1.124 + true, 1.125 + true); 1.126 +} 1.127 + 1.128 +servModule.getClassObject = 1.129 +function (compMgr, cid, iid) 1.130 +{ 1.131 + if (!cid.equals(kTESTSERV_CID)) 1.132 + throw Components.results.NS_ERROR_NO_INTERFACE; 1.133 + 1.134 + if (!iid.equals(Components.interfaces.nsIFactory)) 1.135 + throw Components.results.NS_ERROR_NOT_IMPLEMENTED; 1.136 + 1.137 + return servFactory; 1.138 +} 1.139 + 1.140 +servModule.canUnload = 1.141 +function (compMgr) 1.142 +{ 1.143 + dump(">>> unloading test serv.\n"); 1.144 + return true; 1.145 +} 1.146 + 1.147 +var servFactory = new Object(); 1.148 + 1.149 +servFactory.createInstance = 1.150 +function (outer, iid) 1.151 +{ 1.152 + if (outer != null) 1.153 + throw Components.results.NS_ERROR_NO_AGGREGATION; 1.154 + 1.155 + if (!iid.equals(nsIObserver) && 1.156 + !iid.equals(nsISupports)) 1.157 + throw Components.results.NS_ERROR_NO_INTERFACE; 1.158 + 1.159 + return TestServ; 1.160 +} 1.161 + 1.162 +function NSGetModule(compMgr, fileSpec) 1.163 +{ 1.164 + return servModule; 1.165 +} 1.166 + 1.167 +var TestServ = new nsTestServ();