netwerk/test/TestServ.cpp

Wed, 31 Dec 2014 06:55:46 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:46 +0100
changeset 1
ca08bd8f51b2
permissions
-rw-r--r--

Added tag TORBROWSER_REPLICA for changeset 6474c204b198

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

mercurial