TestServ.js 4.9 KB

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