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