1 /* vim:set ts=4 sw=4 et cindent: */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is Mozilla.
17 * The Initial Developer of the Original Code is IBM Corporation.
18 * Portions created by IBM Corporation are Copyright (C) 2003
19 * IBM Corporation. All Rights Reserved.
22 * Darin Fisher <darin@meer.net>
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "TestCommon.h"
40 #include "nsIServiceManager.h"
41 #include "nsIServerSocket.h"
42 #include "nsISocketTransport.h"
43 #include "nsNetUtil.h"
44 #include "nsStringAPI.h"
48 #if defined(PR_LOGGING)
50 // set NSPR_LOG_MODULES=Test:5
52 static PRLogModuleInfo
*gTestLog
= nsnull
;
54 #define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args)
56 class MySocketListener
: public nsIServerSocketListener
60 NS_DECL_NSISERVERSOCKETLISTENER
63 virtual ~MySocketListener() {}
66 NS_IMPL_THREADSAFE_ISUPPORTS1(MySocketListener
, nsIServerSocketListener
)
69 MySocketListener::OnSocketAccepted(nsIServerSocket
*serv
,
70 nsISocketTransport
*trans
)
72 LOG(("MySocketListener::OnSocketAccepted [serv=%p trans=%p]\n", serv
, trans
));
78 trans
->GetPort(&port
);
80 LOG((" -> %s:%d\n", host
.get(), port
));
82 nsCOMPtr
<nsIInputStream
> input
;
83 nsCOMPtr
<nsIOutputStream
> output
;
86 rv
= trans
->OpenInputStream(nsITransport::OPEN_BLOCKING
, 0, 0, getter_AddRefs(input
));
90 rv
= trans
->OpenOutputStream(nsITransport::OPEN_BLOCKING
, 0, 0, getter_AddRefs(output
));
97 rv
= input
->Read(buf
, sizeof(buf
), &n
);
101 const char response
[] = "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\nFooooopy!!\r\n";
102 rv
= output
->Write(response
, sizeof(response
) - 1, &n
);
112 MySocketListener::OnStopListening(nsIServerSocket
*serv
, nsresult status
)
114 LOG(("MySocketListener::OnStopListening [serv=%p status=%x]\n", serv
, status
));
120 MakeServer(PRInt32 port
)
123 nsCOMPtr
<nsIServerSocket
> serv
= do_CreateInstance(NS_SERVERSOCKET_CONTRACTID
, &rv
);
127 rv
= serv
->Init(port
, PR_TRUE
, 5);
131 rv
= serv
->GetPort(&port
);
134 LOG((" listening on port %d\n", port
));
136 rv
= serv
->AsyncListen(new MySocketListener());
141 main(int argc
, char* argv
[])
143 if (test_common_init(&argc
, &argv
) != 0)
146 nsresult rv
= (nsresult
)-1;
148 printf("usage: %s <port>\n", argv
[0]);
152 #if defined(PR_LOGGING)
153 gTestLog
= PR_NewLogModule("Test");
157 * The following code only deals with XPCOM registration stuff. and setting
158 * up the event queues. Copied from TestSocketIO.cpp
161 rv
= NS_InitXPCOM2(nsnull
, nsnull
, nsnull
);
162 if (NS_FAILED(rv
)) return rv
;
165 rv
= MakeServer(atoi(argv
[1]));
167 LOG(("MakeServer failed [rv=%x]\n", rv
));
171 // Enter the message pump to allow the URL load to proceed.
173 } // this scopes the nsCOMPtrs
174 // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
175 NS_ShutdownXPCOM(nsnull
);