TestSocketIO.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  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 <stdio.h>
  6. #include <signal.h>
  7. #ifdef WIN32
  8. #include <windows.h>
  9. #endif
  10. #include "nspr.h"
  11. #include "nscore.h"
  12. #include "nsISocketTransportService.h"
  13. #include "nsIEventQueueService.h"
  14. #include "nsIServiceManager.h"
  15. #include "nsITransport.h"
  16. #include "nsIRequest.h"
  17. #include "nsIStreamProvider.h"
  18. #include "nsIStreamListener.h"
  19. #include "nsIPipe.h"
  20. #include "nsIOutputStream.h"
  21. #include "nsIInputStream.h"
  22. #include "nsCRT.h"
  23. #include "nsCOMPtr.h"
  24. #include "nsIByteArrayInputStream.h"
  25. static PRLogModuleInfo *gTestSocketIOLog;
  26. #define LOG(args) MOZ_LOG(gTestSocketIOLog, mozilla::LogLevel::Debug, args)
  27. static NS_DEFINE_CID(kSocketTransportServiceCID, NS_SOCKETTRANSPORTSERVICE_CID);
  28. static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
  29. static PRTime gElapsedTime;
  30. static int gKeepRunning = 1;
  31. static nsIEventQueue* gEventQ = nullptr;
  32. //
  33. //----------------------------------------------------------------------------
  34. // Test Listener
  35. //----------------------------------------------------------------------------
  36. //
  37. class TestListener : public nsIStreamListener
  38. {
  39. public:
  40. TestListener() {}
  41. virtual ~TestListener() {}
  42. NS_DECL_ISUPPORTS
  43. NS_DECL_NSIREQUESTOBSERVER
  44. NS_DECL_NSISTREAMLISTENER
  45. };
  46. NS_IMPL_ISUPPORTS(TestListener,
  47. nsIRequestObserver,
  48. nsIStreamListener);
  49. NS_IMETHODIMP
  50. TestListener::OnStartRequest(nsIRequest* request, nsISupports* context)
  51. {
  52. LOG(("TestListener::OnStartRequest\n"));
  53. return NS_OK;
  54. }
  55. NS_IMETHODIMP
  56. TestListener::OnDataAvailable(nsIRequest* request,
  57. nsISupports* context,
  58. nsIInputStream *aIStream,
  59. uint32_t aSourceOffset,
  60. uint32_t aLength)
  61. {
  62. LOG(("TestListener::OnDataAvailable [offset=%u length=%u]\n",
  63. aSourceOffset, aLength));
  64. char buf[1025];
  65. uint32_t amt;
  66. while (1) {
  67. aIStream->Read(buf, 1024, &amt);
  68. if (amt == 0)
  69. break;
  70. buf[amt] = '\0';
  71. puts(buf);
  72. }
  73. return NS_OK;
  74. }
  75. NS_IMETHODIMP
  76. TestListener::OnStopRequest(nsIRequest* request, nsISupports* context,
  77. nsresult aStatus)
  78. {
  79. LOG(("TestListener::OnStopRequest [aStatus=%x]\n", aStatus));
  80. gKeepRunning = 0;
  81. return NS_OK;
  82. }
  83. //
  84. //----------------------------------------------------------------------------
  85. // Test Provider
  86. //----------------------------------------------------------------------------
  87. //
  88. class TestProvider : public nsIStreamProvider
  89. {
  90. public:
  91. TestProvider(char *data);
  92. virtual ~TestProvider();
  93. NS_DECL_ISUPPORTS
  94. NS_DECL_NSIREQUESTOBSERVER
  95. NS_DECL_NSISTREAMPROVIDER
  96. protected:
  97. nsCOMPtr<nsIByteArrayInputStream> mData;
  98. };
  99. NS_IMPL_ISUPPORTS(TestProvider,
  100. nsIStreamProvider,
  101. nsIRequestObserver)
  102. TestProvider::TestProvider(char *data)
  103. {
  104. NS_NewByteArrayInputStream(getter_AddRefs(mData), data, strlen(data));
  105. LOG(("Constructing TestProvider [this=%p]\n", this));
  106. }
  107. TestProvider::~TestProvider()
  108. {
  109. LOG(("Destroying TestProvider [this=%p]\n", this));
  110. }
  111. NS_IMETHODIMP
  112. TestProvider::OnStartRequest(nsIRequest* request, nsISupports* context)
  113. {
  114. LOG(("TestProvider::OnStartRequest [this=%p]\n", this));
  115. return NS_OK;
  116. }
  117. NS_IMETHODIMP
  118. TestProvider::OnStopRequest(nsIRequest* request, nsISupports* context,
  119. nsresult aStatus)
  120. {
  121. LOG(("TestProvider::OnStopRequest [status=%x]\n", aStatus));
  122. nsCOMPtr<nsIStreamListener> listener = do_QueryInterface(new TestListener());
  123. if (NS_SUCCEEDED(aStatus)) {
  124. nsCOMPtr<nsITransportRequest> treq = do_QueryInterface(request);
  125. nsCOMPtr<nsITransport> transport;
  126. treq->GetTransport(getter_AddRefs(transport));
  127. if (transport) {
  128. nsCOMPtr<nsIRequest> readRequest;
  129. transport->AsyncRead(listener, nullptr, 0, 0, 0, getter_AddRefs(readRequest));
  130. }
  131. } else
  132. gKeepRunning = 0;
  133. return NS_OK;
  134. }
  135. NS_IMETHODIMP
  136. TestProvider::OnDataWritable(nsIRequest *request, nsISupports *context,
  137. nsIOutputStream *output, uint32_t offset, uint32_t count)
  138. {
  139. LOG(("TestProvider::OnDataWritable [offset=%u, count=%u]\n", offset, count));
  140. uint32_t writeCount;
  141. nsresult rv = output->WriteFrom(mData, count, &writeCount);
  142. // Zero bytes written on success indicates EOF
  143. if (NS_SUCCEEDED(rv) && (writeCount == 0))
  144. return NS_BASE_STREAM_CLOSED;
  145. return rv;
  146. }
  147. //
  148. //----------------------------------------------------------------------------
  149. // Synchronous IO
  150. //----------------------------------------------------------------------------
  151. //
  152. nsresult
  153. WriteRequest(nsIOutputStream *os, const char *request)
  154. {
  155. LOG(("WriteRequest [request=%s]\n", request));
  156. uint32_t n;
  157. return os->Write(request, strlen(request), &n);
  158. }
  159. nsresult
  160. ReadResponse(nsIInputStream *is)
  161. {
  162. uint32_t bytesRead;
  163. char buf[2048];
  164. do {
  165. is->Read(buf, sizeof(buf), &bytesRead);
  166. if (bytesRead > 0)
  167. fwrite(buf, 1, bytesRead, stdout);
  168. } while (bytesRead > 0);
  169. return NS_OK;
  170. }
  171. //
  172. //----------------------------------------------------------------------------
  173. // Startup...
  174. //----------------------------------------------------------------------------
  175. //
  176. void
  177. sighandler(int sig)
  178. {
  179. LOG(("got signal: %d\n", sig));
  180. NS_BREAK();
  181. }
  182. void
  183. usage(char **argv)
  184. {
  185. printf("usage: %s [-sync] <host> <path>\n", argv[0]);
  186. exit(1);
  187. }
  188. int
  189. main(int argc, char* argv[])
  190. {
  191. nsresult rv;
  192. signal(SIGSEGV, sighandler);
  193. gTestSocketIOLog = PR_NewLogModule("TestSocketIO");
  194. if (argc < 3)
  195. usage(argv);
  196. int i=0;
  197. bool sync = false;
  198. if (nsCRT::strcasecmp(argv[1], "-sync") == 0) {
  199. if (argc < 4)
  200. usage(argv);
  201. sync = true;
  202. i = 1;
  203. }
  204. char *hostName = argv[1+i];
  205. char *fileName = argv[2+i];
  206. int port = 80;
  207. // Create the Event Queue for this thread...
  208. nsCOMPtr<nsIEventQueueService> eventQService =
  209. do_GetService(kEventQueueServiceCID, &rv);
  210. if (NS_FAILED(rv)) {
  211. NS_WARNING("failed to create: event queue service!");
  212. return rv;
  213. }
  214. rv = eventQService->CreateMonitoredThreadEventQueue();
  215. if (NS_FAILED(rv)) {
  216. NS_WARNING("failed to create: thread event queue!");
  217. return rv;
  218. }
  219. eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
  220. // Create the Socket transport service...
  221. nsCOMPtr<nsISocketTransportService> sts =
  222. do_GetService(kSocketTransportServiceCID, &rv);
  223. if (NS_FAILED(rv)) {
  224. NS_WARNING("failed to create: socket transport service!");
  225. return rv;
  226. }
  227. char *buffer = PR_smprintf("GET %s HTTP/1.1" CRLF
  228. "host: %s" CRLF
  229. "user-agent: Mozilla/5.0 (X11; N; Linux 2.2.16-22smp i686; en-US; m18) Gecko/20001220" CRLF
  230. "accept: */*" CRLF
  231. "accept-language: en" CRLF
  232. "accept-encoding: gzip,deflate,compress,identity" CRLF
  233. "keep-alive: 300" CRLF
  234. "connection: keep-alive" CRLF
  235. CRLF,
  236. fileName, hostName);
  237. LOG(("Request [\n%s]\n", buffer));
  238. // Create the socket transport...
  239. nsCOMPtr<nsITransport> transport;
  240. rv = sts->CreateTransport(hostName, port, nullptr, 0, 0, getter_AddRefs(transport));
  241. if (NS_FAILED(rv)) {
  242. NS_WARNING("failed to create: socket transport!");
  243. return rv;
  244. }
  245. gElapsedTime = PR_Now();
  246. if (!sync) {
  247. nsCOMPtr<nsIRequest> request;
  248. rv = transport->AsyncWrite(new TestProvider(buffer), nullptr, 0, 0, 0, getter_AddRefs(request));
  249. if (NS_FAILED(rv)) {
  250. NS_WARNING("failed calling: AsyncWrite!");
  251. return rv;
  252. }
  253. // Enter the message pump to allow the URL load to proceed.
  254. while ( gKeepRunning ) {
  255. PLEvent *gEvent;
  256. gEventQ->WaitForEvent(&gEvent);
  257. gEventQ->HandleEvent(gEvent);
  258. }
  259. }
  260. else {
  261. // synchronous write
  262. {
  263. nsCOMPtr<nsIOutputStream> os;
  264. rv = transport->OpenOutputStream(0, 0, 0, getter_AddRefs(os));
  265. if (NS_FAILED(rv)) {
  266. LOG(("OpenOutputStream failed [rv=%x]\n", rv));
  267. return rv;
  268. }
  269. rv = WriteRequest(os, buffer);
  270. if (NS_FAILED(rv)) {
  271. LOG(("WriteRequest failed [rv=%x]\n", rv));
  272. return rv;
  273. }
  274. }
  275. // synchronous read
  276. {
  277. nsCOMPtr<nsIInputStream> is;
  278. rv = transport->OpenInputStream(0, 0, 0, getter_AddRefs(is));
  279. if (NS_FAILED(rv)) {
  280. LOG(("OpenInputStream failed [rv=%x]\n", rv));
  281. return rv;
  282. }
  283. rv = ReadResponse(is);
  284. if (NS_FAILED(rv)) {
  285. LOG(("ReadResponse failed [rv=%x]\n", rv));
  286. return rv;
  287. }
  288. }
  289. }
  290. PRTime endTime;
  291. endTime = PR_Now();
  292. LOG(("Elapsed time: %d\n", (int32_t)(endTime/1000UL - gElapsedTime/1000UL)));
  293. sts->Shutdown();
  294. return 0;
  295. }