Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* vim:set ts=2 sw=2 et: */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | /* |
michael@0 | 7 | * To use try out this JS server socket implementation, just copy this file |
michael@0 | 8 | * into the "components" directory of a Mozilla build. Then load the URL |
michael@0 | 9 | * http://localhost:4444/ in the browser. You should see a page get loaded |
michael@0 | 10 | * that was served up by this component :-) |
michael@0 | 11 | * |
michael@0 | 12 | * This code requires Mozilla 1.6 or better. |
michael@0 | 13 | */ |
michael@0 | 14 | |
michael@0 | 15 | const kTESTSERV_CONTRACTID = "@mozilla.org/network/test-serv;1"; |
michael@0 | 16 | const kTESTSERV_CID = Components.ID("{a741fcd5-9695-42e8-a7f7-14f9a29f8200}"); |
michael@0 | 17 | const nsISupports = Components.interfaces.nsISupports; |
michael@0 | 18 | const nsIObserver = Components.interfaces.nsIObserver; |
michael@0 | 19 | const nsIServerSocket = Components.interfaces.nsIServerSocket; |
michael@0 | 20 | const nsIServerSocketListener = Components.interfaces.nsIServerSocketListener; |
michael@0 | 21 | const nsITransport = Components.interfaces.nsITransport; |
michael@0 | 22 | const nsIScriptableInputStream = Components.interfaces.nsIScriptableInputStream; |
michael@0 | 23 | |
michael@0 | 24 | /** we'll listen on this port for HTTP requests **/ |
michael@0 | 25 | const kPORT = 4444; |
michael@0 | 26 | |
michael@0 | 27 | function nsTestServ() { dump(">>> creating nsTestServ instance\n"); }; |
michael@0 | 28 | |
michael@0 | 29 | nsTestServ.prototype = |
michael@0 | 30 | { |
michael@0 | 31 | QueryInterface: function(iid) |
michael@0 | 32 | { |
michael@0 | 33 | if (iid.equals(nsIObserver) || |
michael@0 | 34 | iid.equals(nsIServerSocketListener) || |
michael@0 | 35 | iid.equals(nsISupports)) |
michael@0 | 36 | return this; |
michael@0 | 37 | |
michael@0 | 38 | throw Components.results.NS_ERROR_NO_INTERFACE; |
michael@0 | 39 | }, |
michael@0 | 40 | |
michael@0 | 41 | observe: function(subject, topic, data) |
michael@0 | 42 | { |
michael@0 | 43 | dump(">>> observe [" + topic + "]\n"); |
michael@0 | 44 | this.startListening(); |
michael@0 | 45 | }, |
michael@0 | 46 | |
michael@0 | 47 | /* this function is called when we receive a new connection */ |
michael@0 | 48 | onSocketAccepted: function(serverSocket, clientSocket) |
michael@0 | 49 | { |
michael@0 | 50 | dump(">>> accepted connection on "+clientSocket.host+":"+clientSocket.port+"\n"); |
michael@0 | 51 | |
michael@0 | 52 | var input = clientSocket.openInputStream(nsITransport.OPEN_BLOCKING, 0, 0); |
michael@0 | 53 | var output = clientSocket.openOutputStream(nsITransport.OPEN_BLOCKING, 0, 0); |
michael@0 | 54 | |
michael@0 | 55 | this.consumeInput(input); |
michael@0 | 56 | |
michael@0 | 57 | const fixedResponse = |
michael@0 | 58 | "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\nFooooopy!!\r\n"; |
michael@0 | 59 | var response = fixedResponse + "\r\n" + new Date().toString() + "\r\n"; |
michael@0 | 60 | var n = output.write(response, response.length); |
michael@0 | 61 | dump(">>> wrote "+n+" bytes\n"); |
michael@0 | 62 | |
michael@0 | 63 | input.close(); |
michael@0 | 64 | output.close(); |
michael@0 | 65 | }, |
michael@0 | 66 | |
michael@0 | 67 | onStopListening: function(serverSocket, status) |
michael@0 | 68 | { |
michael@0 | 69 | dump(">>> shutting down server socket\n"); |
michael@0 | 70 | }, |
michael@0 | 71 | |
michael@0 | 72 | startListening: function() |
michael@0 | 73 | { |
michael@0 | 74 | const SERVERSOCKET_CONTRACTID = "@mozilla.org/network/server-socket;1"; |
michael@0 | 75 | var socket = Components.classes[SERVERSOCKET_CONTRACTID].createInstance(nsIServerSocket); |
michael@0 | 76 | socket.init(kPORT, true /* loopback only */, 5); |
michael@0 | 77 | dump(">>> listening on port "+socket.port+"\n"); |
michael@0 | 78 | socket.asyncListen(this); |
michael@0 | 79 | }, |
michael@0 | 80 | |
michael@0 | 81 | consumeInput: function(input) |
michael@0 | 82 | { |
michael@0 | 83 | /* use nsIScriptableInputStream to consume all of the data on the stream */ |
michael@0 | 84 | |
michael@0 | 85 | var sin = Components.classes["@mozilla.org/scriptableinputstream;1"] |
michael@0 | 86 | .createInstance(nsIScriptableInputStream); |
michael@0 | 87 | sin.init(input); |
michael@0 | 88 | |
michael@0 | 89 | /* discard all data */ |
michael@0 | 90 | while (sin.available() > 0) |
michael@0 | 91 | sin.read(512); |
michael@0 | 92 | } |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | /** |
michael@0 | 96 | * JS XPCOM component registration goop: |
michael@0 | 97 | * |
michael@0 | 98 | * We set ourselves up to observe the xpcom-startup category. This provides |
michael@0 | 99 | * us with a starting point. |
michael@0 | 100 | */ |
michael@0 | 101 | |
michael@0 | 102 | var servModule = new Object(); |
michael@0 | 103 | |
michael@0 | 104 | servModule.registerSelf = |
michael@0 | 105 | function (compMgr, fileSpec, location, type) |
michael@0 | 106 | { |
michael@0 | 107 | compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar); |
michael@0 | 108 | compMgr.registerFactoryLocation(kTESTSERV_CID, |
michael@0 | 109 | "nsTestServ", |
michael@0 | 110 | kTESTSERV_CONTRACTID, |
michael@0 | 111 | fileSpec, |
michael@0 | 112 | location, |
michael@0 | 113 | type); |
michael@0 | 114 | |
michael@0 | 115 | const CATMAN_CONTRACTID = "@mozilla.org/categorymanager;1"; |
michael@0 | 116 | const nsICategoryManager = Components.interfaces.nsICategoryManager; |
michael@0 | 117 | var catman = Components.classes[CATMAN_CONTRACTID].getService(nsICategoryManager); |
michael@0 | 118 | catman.addCategoryEntry("xpcom-startup", |
michael@0 | 119 | "TestServ", |
michael@0 | 120 | kTESTSERV_CONTRACTID, |
michael@0 | 121 | true, |
michael@0 | 122 | true); |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | servModule.getClassObject = |
michael@0 | 126 | function (compMgr, cid, iid) |
michael@0 | 127 | { |
michael@0 | 128 | if (!cid.equals(kTESTSERV_CID)) |
michael@0 | 129 | throw Components.results.NS_ERROR_NO_INTERFACE; |
michael@0 | 130 | |
michael@0 | 131 | if (!iid.equals(Components.interfaces.nsIFactory)) |
michael@0 | 132 | throw Components.results.NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 133 | |
michael@0 | 134 | return servFactory; |
michael@0 | 135 | } |
michael@0 | 136 | |
michael@0 | 137 | servModule.canUnload = |
michael@0 | 138 | function (compMgr) |
michael@0 | 139 | { |
michael@0 | 140 | dump(">>> unloading test serv.\n"); |
michael@0 | 141 | return true; |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | var servFactory = new Object(); |
michael@0 | 145 | |
michael@0 | 146 | servFactory.createInstance = |
michael@0 | 147 | function (outer, iid) |
michael@0 | 148 | { |
michael@0 | 149 | if (outer != null) |
michael@0 | 150 | throw Components.results.NS_ERROR_NO_AGGREGATION; |
michael@0 | 151 | |
michael@0 | 152 | if (!iid.equals(nsIObserver) && |
michael@0 | 153 | !iid.equals(nsISupports)) |
michael@0 | 154 | throw Components.results.NS_ERROR_NO_INTERFACE; |
michael@0 | 155 | |
michael@0 | 156 | return TestServ; |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | function NSGetModule(compMgr, fileSpec) |
michael@0 | 160 | { |
michael@0 | 161 | return servModule; |
michael@0 | 162 | } |
michael@0 | 163 | |
michael@0 | 164 | var TestServ = new nsTestServ(); |