Wed, 31 Dec 2014 06:55:46 +0100
Added tag TORBROWSER_REPLICA for changeset 6474c204b198
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/. */
6 /*
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 :-)
11 *
12 * This code requires Mozilla 1.6 or better.
13 */
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 **/
25 const kPORT = 4444;
27 function nsTestServ() { dump(">>> creating nsTestServ instance\n"); };
29 nsTestServ.prototype =
30 {
31 QueryInterface: function(iid)
32 {
33 if (iid.equals(nsIObserver) ||
34 iid.equals(nsIServerSocketListener) ||
35 iid.equals(nsISupports))
36 return this;
38 throw Components.results.NS_ERROR_NO_INTERFACE;
39 },
41 observe: function(subject, topic, data)
42 {
43 dump(">>> observe [" + topic + "]\n");
44 this.startListening();
45 },
47 /* this function is called when we receive a new connection */
48 onSocketAccepted: function(serverSocket, clientSocket)
49 {
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);
57 const fixedResponse =
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");
63 input.close();
64 output.close();
65 },
67 onStopListening: function(serverSocket, status)
68 {
69 dump(">>> shutting down server socket\n");
70 },
72 startListening: function()
73 {
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);
79 },
81 consumeInput: function(input)
82 {
83 /* use nsIScriptableInputStream to consume all of the data on the stream */
85 var sin = Components.classes["@mozilla.org/scriptableinputstream;1"]
86 .createInstance(nsIScriptableInputStream);
87 sin.init(input);
89 /* discard all data */
90 while (sin.available() > 0)
91 sin.read(512);
92 }
93 }
95 /**
96 * JS XPCOM component registration goop:
97 *
98 * We set ourselves up to observe the xpcom-startup category. This provides
99 * us with a starting point.
100 */
102 var servModule = new Object();
104 servModule.registerSelf =
105 function (compMgr, fileSpec, location, type)
106 {
107 compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
108 compMgr.registerFactoryLocation(kTESTSERV_CID,
109 "nsTestServ",
110 kTESTSERV_CONTRACTID,
111 fileSpec,
112 location,
113 type);
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",
119 "TestServ",
120 kTESTSERV_CONTRACTID,
121 true,
122 true);
123 }
125 servModule.getClassObject =
126 function (compMgr, cid, iid)
127 {
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;
134 return servFactory;
135 }
137 servModule.canUnload =
138 function (compMgr)
139 {
140 dump(">>> unloading test serv.\n");
141 return true;
142 }
144 var servFactory = new Object();
146 servFactory.createInstance =
147 function (outer, iid)
148 {
149 if (outer != null)
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;
156 return TestServ;
157 }
159 function NSGetModule(compMgr, fileSpec)
160 {
161 return servModule;
162 }
164 var TestServ = new nsTestServ();