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"
|
2015-07-06 05:55:00 +00:00
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsIOutputStream.h"
|
|
|
|
#include "nsNetCID.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
2005-11-08 19:23:00 +00:00
|
|
|
#include "nsStringAPI.h"
|
2003-11-15 00:13:59 +00:00
|
|
|
#include "nsCOMPtr.h"
|
2016-01-05 21:05:19 +00:00
|
|
|
#include "NetwerkTestLogging.h"
|
2003-11-15 00:13:59 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// set NSPR_LOG_MODULES=Test:5
|
|
|
|
//
|
2012-07-30 14:20:58 +00:00
|
|
|
static PRLogModuleInfo *gTestLog = nullptr;
|
2015-06-03 22:25:57 +00:00
|
|
|
#define LOG(args) MOZ_LOG(gTestLog, mozilla::LogLevel::Debug, args)
|
2003-11-15 00:13:59 +00:00
|
|
|
|
|
|
|
class MySocketListener : public nsIServerSocketListener
|
|
|
|
{
|
2014-06-24 16:36:44 +00:00
|
|
|
protected:
|
|
|
|
virtual ~MySocketListener() {}
|
|
|
|
|
2003-11-15 00:13:59 +00:00
|
|
|
public:
|
2013-07-19 02:24:13 +00:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2003-11-15 00:13:59 +00:00
|
|
|
NS_DECL_NSISERVERSOCKETLISTENER
|
|
|
|
|
|
|
|
MySocketListener() {}
|
|
|
|
};
|
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(MySocketListener, nsIServerSocketListener)
|
2003-11-15 00:13:59 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MySocketListener::OnSocketAccepted(nsIServerSocket *serv,
|
|
|
|
nsISocketTransport *trans)
|
|
|
|
{
|
|
|
|
LOG(("MySocketListener::OnSocketAccepted [serv=%p trans=%p]\n", serv, trans));
|
|
|
|
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
gTestLog = PR_NewLogModule("Test");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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);
|
2012-08-07 17:17:27 +00:00
|
|
|
if (NS_FAILED(rv)) return -1;
|
2003-11-15 00:13:59 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
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);
|
2012-08-07 17:17:27 +00:00
|
|
|
return 0;
|
2003-11-15 00:13:59 +00:00
|
|
|
}
|