|
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/. */ |
|
5 |
|
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 */ |
|
14 |
|
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; |
|
23 |
|
24 /** we'll listen on this port for HTTP requests **/ |
|
25 const kPORT = 4444; |
|
26 |
|
27 function nsTestServ() { dump(">>> creating nsTestServ instance\n"); }; |
|
28 |
|
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; |
|
37 |
|
38 throw Components.results.NS_ERROR_NO_INTERFACE; |
|
39 }, |
|
40 |
|
41 observe: function(subject, topic, data) |
|
42 { |
|
43 dump(">>> observe [" + topic + "]\n"); |
|
44 this.startListening(); |
|
45 }, |
|
46 |
|
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"); |
|
51 |
|
52 var input = clientSocket.openInputStream(nsITransport.OPEN_BLOCKING, 0, 0); |
|
53 var output = clientSocket.openOutputStream(nsITransport.OPEN_BLOCKING, 0, 0); |
|
54 |
|
55 this.consumeInput(input); |
|
56 |
|
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"); |
|
62 |
|
63 input.close(); |
|
64 output.close(); |
|
65 }, |
|
66 |
|
67 onStopListening: function(serverSocket, status) |
|
68 { |
|
69 dump(">>> shutting down server socket\n"); |
|
70 }, |
|
71 |
|
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 }, |
|
80 |
|
81 consumeInput: function(input) |
|
82 { |
|
83 /* use nsIScriptableInputStream to consume all of the data on the stream */ |
|
84 |
|
85 var sin = Components.classes["@mozilla.org/scriptableinputstream;1"] |
|
86 .createInstance(nsIScriptableInputStream); |
|
87 sin.init(input); |
|
88 |
|
89 /* discard all data */ |
|
90 while (sin.available() > 0) |
|
91 sin.read(512); |
|
92 } |
|
93 } |
|
94 |
|
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 */ |
|
101 |
|
102 var servModule = new Object(); |
|
103 |
|
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); |
|
114 |
|
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 } |
|
124 |
|
125 servModule.getClassObject = |
|
126 function (compMgr, cid, iid) |
|
127 { |
|
128 if (!cid.equals(kTESTSERV_CID)) |
|
129 throw Components.results.NS_ERROR_NO_INTERFACE; |
|
130 |
|
131 if (!iid.equals(Components.interfaces.nsIFactory)) |
|
132 throw Components.results.NS_ERROR_NOT_IMPLEMENTED; |
|
133 |
|
134 return servFactory; |
|
135 } |
|
136 |
|
137 servModule.canUnload = |
|
138 function (compMgr) |
|
139 { |
|
140 dump(">>> unloading test serv.\n"); |
|
141 return true; |
|
142 } |
|
143 |
|
144 var servFactory = new Object(); |
|
145 |
|
146 servFactory.createInstance = |
|
147 function (outer, iid) |
|
148 { |
|
149 if (outer != null) |
|
150 throw Components.results.NS_ERROR_NO_AGGREGATION; |
|
151 |
|
152 if (!iid.equals(nsIObserver) && |
|
153 !iid.equals(nsISupports)) |
|
154 throw Components.results.NS_ERROR_NO_INTERFACE; |
|
155 |
|
156 return TestServ; |
|
157 } |
|
158 |
|
159 function NSGetModule(compMgr, fileSpec) |
|
160 { |
|
161 return servModule; |
|
162 } |
|
163 |
|
164 var TestServ = new nsTestServ(); |