Bug 346849: Add a "Save Image as..." entry to the context menu for <canvas>, Original...
[mozilla-central.git] / netwerk / test / TestServ.js
blobd5c0aff2cd099666f1303b9dd3d6dc2e58c6ff41
1 /* vim:set ts=2 sw=2 et: */
2 /* ***** BEGIN LICENSE BLOCK *****
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
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/
9  *
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
13  * License.
14  *
15  * The Original Code is Mozilla.
16  *
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.
20  *
21  * Contributor(s):
22  *   Darin Fisher <darin@meer.net>
23  *
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.
35  *
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 :-)
43  *
44  * This code requires Mozilla 1.6 or better.
45  */
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 **/
57 const kPORT = 4444;
59 function nsTestServ() { dump(">>> creating nsTestServ instance\n"); };
61 nsTestServ.prototype =
63   QueryInterface: function(iid)
64   {
65     if (iid.equals(nsIObserver) ||
66         iid.equals(nsIServerSocketListener) ||
67         iid.equals(nsISupports))
68       return this;
70     Components.returnCode = Components.results.NS_ERROR_NO_INTERFACE;
71     return null;
72   },
74   observe: function(subject, topic, data)
75   {
76     dump(">>> observe [" + topic + "]\n");
77     this.startListening();
78   },
80   /* this function is called when we receive a new connection */
81   onSocketAccepted: function(serverSocket, clientSocket)
82   {
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);
90     const fixedResponse =
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");
96     input.close();
97     output.close();
98   },
100   onStopListening: function(serverSocket, status)
101   {
102     dump(">>> shutting down server socket\n");
103   },
105   startListening: function()
106   {
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);
112   },
114   consumeInput: function(input)
115   {
116     /* use nsIScriptableInputStream to consume all of the data on the stream */
118     var sin = Components.classes["@mozilla.org/scriptableinputstream;1"]
119                         .createInstance(nsIScriptableInputStream);
120     sin.init(input);
122     /* discard all data */
123     while (sin.available() > 0)
124       sin.read(512);
125   }
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.
133  */
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,
142                                   "nsTestServ",
143                                   kTESTSERV_CONTRACTID,
144                                   fileSpec,
145                                   location,
146                                   type);
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",
152                           "TestServ",
153                           kTESTSERV_CONTRACTID,
154                           true,
155                           true);
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;
167   return servFactory;
170 servModule.canUnload =
171 function (compMgr)
173   dump(">>> unloading test serv.\n");
174   return true;
177 var servFactory = new Object();
179 servFactory.createInstance =
180 function (outer, iid)
182   if (outer != null)
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;
189   return TestServ;
192 function NSGetModule(compMgr, fileSpec)
194   return servModule;
197 var TestServ = new nsTestServ();