TestServ.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* vim:set ts=4 sw=4 et cindent: */
  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. #include "TestCommon.h"
  6. #include <stdlib.h>
  7. #include "nsIServiceManager.h"
  8. #include "nsIServerSocket.h"
  9. #include "nsISocketTransport.h"
  10. #include "nsIInputStream.h"
  11. #include "nsIOutputStream.h"
  12. #include "nsNetCID.h"
  13. #include "nsComponentManagerUtils.h"
  14. #include "nsStringAPI.h"
  15. #include "nsCOMPtr.h"
  16. #include "NetwerkTestLogging.h"
  17. //
  18. // set NSPR_LOG_MODULES=Test:5
  19. //
  20. static PRLogModuleInfo *gTestLog = nullptr;
  21. #define LOG(args) MOZ_LOG(gTestLog, mozilla::LogLevel::Debug, args)
  22. class MySocketListener : public nsIServerSocketListener
  23. {
  24. protected:
  25. virtual ~MySocketListener() = default;
  26. public:
  27. NS_DECL_THREADSAFE_ISUPPORTS
  28. NS_DECL_NSISERVERSOCKETLISTENER
  29. MySocketListener() {}
  30. };
  31. NS_IMPL_ISUPPORTS(MySocketListener, nsIServerSocketListener)
  32. NS_IMETHODIMP
  33. MySocketListener::OnSocketAccepted(nsIServerSocket *serv,
  34. nsISocketTransport *trans)
  35. {
  36. LOG(("MySocketListener::OnSocketAccepted [serv=%p trans=%p]\n", serv, trans));
  37. nsAutoCString host;
  38. int32_t port;
  39. trans->GetHost(host);
  40. trans->GetPort(&port);
  41. LOG((" -> %s:%d\n", host.get(), port));
  42. nsCOMPtr<nsIInputStream> input;
  43. nsCOMPtr<nsIOutputStream> output;
  44. nsresult rv;
  45. rv = trans->OpenInputStream(nsITransport::OPEN_BLOCKING, 0, 0, getter_AddRefs(input));
  46. if (NS_FAILED(rv))
  47. return rv;
  48. rv = trans->OpenOutputStream(nsITransport::OPEN_BLOCKING, 0, 0, getter_AddRefs(output));
  49. if (NS_FAILED(rv))
  50. return rv;
  51. char buf[256];
  52. uint32_t n;
  53. rv = input->Read(buf, sizeof(buf), &n);
  54. if (NS_FAILED(rv))
  55. return rv;
  56. const char response[] = "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\nFooooopy!!\r\n";
  57. rv = output->Write(response, sizeof(response) - 1, &n);
  58. if (NS_FAILED(rv))
  59. return rv;
  60. input->Close();
  61. output->Close();
  62. return NS_OK;
  63. }
  64. NS_IMETHODIMP
  65. MySocketListener::OnStopListening(nsIServerSocket *serv, nsresult status)
  66. {
  67. LOG(("MySocketListener::OnStopListening [serv=%p status=%x]\n", serv, status));
  68. QuitPumpingEvents();
  69. return NS_OK;
  70. }
  71. static nsresult
  72. MakeServer(int32_t port)
  73. {
  74. nsresult rv;
  75. nsCOMPtr<nsIServerSocket> serv = do_CreateInstance(NS_SERVERSOCKET_CONTRACTID, &rv);
  76. if (NS_FAILED(rv))
  77. return rv;
  78. rv = serv->Init(port, true, 5);
  79. if (NS_FAILED(rv))
  80. return rv;
  81. rv = serv->GetPort(&port);
  82. if (NS_FAILED(rv))
  83. return rv;
  84. LOG((" listening on port %d\n", port));
  85. rv = serv->AsyncListen(new MySocketListener());
  86. return rv;
  87. }
  88. int
  89. main(int argc, char* argv[])
  90. {
  91. if (test_common_init(&argc, &argv) != 0)
  92. return -1;
  93. nsresult rv= (nsresult)-1;
  94. if (argc < 2) {
  95. printf("usage: %s <port>\n", argv[0]);
  96. return -1;
  97. }
  98. gTestLog = PR_NewLogModule("Test");
  99. /*
  100. * The following code only deals with XPCOM registration stuff. and setting
  101. * up the event queues. Copied from TestSocketIO.cpp
  102. */
  103. rv = NS_InitXPCOM2(nullptr, nullptr, nullptr);
  104. if (NS_FAILED(rv)) return -1;
  105. {
  106. rv = MakeServer(atoi(argv[1]));
  107. if (NS_FAILED(rv)) {
  108. LOG(("MakeServer failed [rv=%x]\n", rv));
  109. return -1;
  110. }
  111. // Enter the message pump to allow the URL load to proceed.
  112. PumpEvents();
  113. } // this scopes the nsCOMPtrs
  114. // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
  115. NS_ShutdownXPCOM(nullptr);
  116. return 0;
  117. }