michael@0: /* vim:set ts=4 sw=4 et cindent: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "TestCommon.h" michael@0: #include michael@0: #include "nsIServiceManager.h" michael@0: #include "nsIServerSocket.h" michael@0: #include "nsISocketTransport.h" michael@0: #include "nsNetUtil.h" michael@0: #include "nsStringAPI.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "prlog.h" michael@0: michael@0: #if defined(PR_LOGGING) michael@0: // michael@0: // set NSPR_LOG_MODULES=Test:5 michael@0: // michael@0: static PRLogModuleInfo *gTestLog = nullptr; michael@0: #endif michael@0: #define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args) michael@0: michael@0: class MySocketListener : public nsIServerSocketListener michael@0: { michael@0: public: michael@0: NS_DECL_THREADSAFE_ISUPPORTS michael@0: NS_DECL_NSISERVERSOCKETLISTENER michael@0: michael@0: MySocketListener() {} michael@0: virtual ~MySocketListener() {} michael@0: }; michael@0: michael@0: NS_IMPL_ISUPPORTS(MySocketListener, nsIServerSocketListener) michael@0: michael@0: NS_IMETHODIMP michael@0: MySocketListener::OnSocketAccepted(nsIServerSocket *serv, michael@0: nsISocketTransport *trans) michael@0: { michael@0: LOG(("MySocketListener::OnSocketAccepted [serv=%p trans=%p]\n", serv, trans)); michael@0: michael@0: nsAutoCString host; michael@0: int32_t port; michael@0: michael@0: trans->GetHost(host); michael@0: trans->GetPort(&port); michael@0: michael@0: LOG((" -> %s:%d\n", host.get(), port)); michael@0: michael@0: nsCOMPtr input; michael@0: nsCOMPtr output; michael@0: nsresult rv; michael@0: michael@0: rv = trans->OpenInputStream(nsITransport::OPEN_BLOCKING, 0, 0, getter_AddRefs(input)); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: rv = trans->OpenOutputStream(nsITransport::OPEN_BLOCKING, 0, 0, getter_AddRefs(output)); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: char buf[256]; michael@0: uint32_t n; michael@0: michael@0: rv = input->Read(buf, sizeof(buf), &n); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: const char response[] = "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\nFooooopy!!\r\n"; michael@0: rv = output->Write(response, sizeof(response) - 1, &n); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: input->Close(); michael@0: output->Close(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: MySocketListener::OnStopListening(nsIServerSocket *serv, nsresult status) michael@0: { michael@0: LOG(("MySocketListener::OnStopListening [serv=%p status=%x]\n", serv, status)); michael@0: QuitPumpingEvents(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: static nsresult michael@0: MakeServer(int32_t port) michael@0: { michael@0: nsresult rv; michael@0: nsCOMPtr serv = do_CreateInstance(NS_SERVERSOCKET_CONTRACTID, &rv); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: rv = serv->Init(port, true, 5); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: rv = serv->GetPort(&port); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: LOG((" listening on port %d\n", port)); michael@0: michael@0: rv = serv->AsyncListen(new MySocketListener()); michael@0: return rv; michael@0: } michael@0: michael@0: int michael@0: main(int argc, char* argv[]) michael@0: { michael@0: if (test_common_init(&argc, &argv) != 0) michael@0: return -1; michael@0: michael@0: nsresult rv= (nsresult)-1; michael@0: if (argc < 2) { michael@0: printf("usage: %s \n", argv[0]); michael@0: return -1; michael@0: } michael@0: michael@0: #if defined(PR_LOGGING) michael@0: gTestLog = PR_NewLogModule("Test"); michael@0: #endif michael@0: michael@0: /* michael@0: * The following code only deals with XPCOM registration stuff. and setting michael@0: * up the event queues. Copied from TestSocketIO.cpp michael@0: */ michael@0: michael@0: rv = NS_InitXPCOM2(nullptr, nullptr, nullptr); michael@0: if (NS_FAILED(rv)) return -1; michael@0: michael@0: { michael@0: rv = MakeServer(atoi(argv[1])); michael@0: if (NS_FAILED(rv)) { michael@0: LOG(("MakeServer failed [rv=%x]\n", rv)); michael@0: return -1; michael@0: } michael@0: michael@0: // Enter the message pump to allow the URL load to proceed. michael@0: PumpEvents(); michael@0: } // this scopes the nsCOMPtrs michael@0: // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM michael@0: NS_ShutdownXPCOM(nullptr); michael@0: return 0; michael@0: }