1 /* vim:set ts=2 sw=2 et: */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is Mozilla.
17 * The Initial Developer of the Original Code is IBM Corporation.
18 * Portions created by IBM Corporation are Copyright (C) 2004
19 * IBM Corporation. All Rights Reserved.
22 * Darin Fisher <darin@meer.net>
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
39 * To use try out this JS server socket implementation, just copy this file
40 * into the "components" directory of a Mozilla build. Then load the URL
41 * http://localhost:4444/ in the browser. You should see a page get loaded
42 * that was served up by this component :-)
44 * This code requires Mozilla 1.6 or better.
47 const kTESTSERV_CONTRACTID
= "@mozilla.org/network/test-serv;1";
48 const kTESTSERV_CID
= Components
.ID("{a741fcd5-9695-42e8-a7f7-14f9a29f8200}");
49 const nsISupports
= Components
.interfaces
.nsISupports
;
50 const nsIObserver
= Components
.interfaces
.nsIObserver
;
51 const nsIServerSocket
= Components
.interfaces
.nsIServerSocket
;
52 const nsIServerSocketListener
= Components
.interfaces
.nsIServerSocketListener
;
53 const nsITransport
= Components
.interfaces
.nsITransport
;
54 const nsIScriptableInputStream
= Components
.interfaces
.nsIScriptableInputStream
;
56 /** we'll listen on this port for HTTP requests **/
59 function nsTestServ() { dump(">>> creating nsTestServ instance\n"); };
61 nsTestServ
.prototype =
63 QueryInterface: function(iid
)
65 if (iid
.equals(nsIObserver
) ||
66 iid
.equals(nsIServerSocketListener
) ||
67 iid
.equals(nsISupports
))
70 Components
.returnCode
= Components
.results
.NS_ERROR_NO_INTERFACE
;
74 observe: function(subject
, topic
, data
)
76 dump(">>> observe [" + topic
+ "]\n");
77 this.startListening();
80 /* this function is called when we receive a new connection */
81 onSocketAccepted: function(serverSocket
, clientSocket
)
83 dump(">>> accepted connection on "+clientSocket
.host
+":"+clientSocket
.port
+"\n");
85 var input
= clientSocket
.openInputStream(nsITransport
.OPEN_BLOCKING
, 0, 0);
86 var output
= clientSocket
.openOutputStream(nsITransport
.OPEN_BLOCKING
, 0, 0);
88 this.consumeInput(input
);
91 "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\nFooooopy!!\r\n";
92 var response
= fixedResponse
+ "\r\n" + new Date().toString() + "\r\n";
93 var n
= output
.write(response
, response
.length
);
94 dump(">>> wrote "+n
+" bytes\n");
100 onStopListening: function(serverSocket
, status
)
102 dump(">>> shutting down server socket\n");
105 startListening: function()
107 const SERVERSOCKET_CONTRACTID
= "@mozilla.org/network/server-socket;1";
108 var socket
= Components
.classes
[SERVERSOCKET_CONTRACTID
].createInstance(nsIServerSocket
);
109 socket
.init(kPORT
, true /* loopback only */, 5);
110 dump(">>> listening on port "+socket
.port
+"\n");
111 socket
.asyncListen(this);
114 consumeInput: function(input
)
116 /* use nsIScriptableInputStream to consume all of the data on the stream */
118 var sin
= Components
.classes
["@mozilla.org/scriptableinputstream;1"]
119 .createInstance(nsIScriptableInputStream
);
122 /* discard all data */
123 while (sin
.available() > 0)
129 * JS XPCOM component registration goop:
131 * We set ourselves up to observe the xpcom-startup category. This provides
132 * us with a starting point.
135 var servModule
= new Object();
137 servModule
.registerSelf
=
138 function (compMgr
, fileSpec
, location
, type
)
140 compMgr
= compMgr
.QueryInterface(Components
.interfaces
.nsIComponentRegistrar
);
141 compMgr
.registerFactoryLocation(kTESTSERV_CID
,
143 kTESTSERV_CONTRACTID
,
148 const CATMAN_CONTRACTID
= "@mozilla.org/categorymanager;1";
149 const nsICategoryManager
= Components
.interfaces
.nsICategoryManager
;
150 var catman
= Components
.classes
[CATMAN_CONTRACTID
].getService(nsICategoryManager
);
151 catman
.addCategoryEntry("xpcom-startup",
153 kTESTSERV_CONTRACTID
,
158 servModule
.getClassObject
=
159 function (compMgr
, cid
, iid
)
161 if (!cid
.equals(kTESTSERV_CID
))
162 throw Components
.results
.NS_ERROR_NO_INTERFACE
;
164 if (!iid
.equals(Components
.interfaces
.nsIFactory
))
165 throw Components
.results
.NS_ERROR_NOT_IMPLEMENTED
;
170 servModule
.canUnload
=
173 dump(">>> unloading test serv.\n");
177 var servFactory
= new Object();
179 servFactory
.createInstance
=
180 function (outer
, iid
)
183 throw Components
.results
.NS_ERROR_NO_AGGREGATION
;
185 if (!iid
.equals(nsIObserver
) &&
186 !iid
.equals(nsISupports
))
187 throw Components
.results
.NS_ERROR_NO_INTERFACE
;
192 function NSGetModule(compMgr
, fileSpec
)
197 var TestServ
= new nsTestServ();