"Automated configuration bump, release for firefox 2.0.0.9rc1"
[mozilla-central.git] / netwerk / test / TestServ.cpp
blobdbb4e7a06a05265f3d65fd1f0e62b8610c25b91b
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
13 * License.
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.
21 * Contributor(s):
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"
39 #include <stdlib.h>
40 #include "nsIServiceManager.h"
41 #include "nsIServerSocket.h"
42 #include "nsISocketTransport.h"
43 #include "nsNetUtil.h"
44 #include "nsStringAPI.h"
45 #include "nsCOMPtr.h"
46 #include "prlog.h"
48 #if defined(PR_LOGGING)
50 // set NSPR_LOG_MODULES=Test:5
52 static PRLogModuleInfo *gTestLog = nsnull;
53 #endif
54 #define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args)
56 class MySocketListener : public nsIServerSocketListener
58 public:
59 NS_DECL_ISUPPORTS
60 NS_DECL_NSISERVERSOCKETLISTENER
62 MySocketListener() {}
63 virtual ~MySocketListener() {}
66 NS_IMPL_THREADSAFE_ISUPPORTS1(MySocketListener, nsIServerSocketListener)
68 NS_IMETHODIMP
69 MySocketListener::OnSocketAccepted(nsIServerSocket *serv,
70 nsISocketTransport *trans)
72 LOG(("MySocketListener::OnSocketAccepted [serv=%p trans=%p]\n", serv, trans));
74 nsCAutoString host;
75 PRInt32 port;
77 trans->GetHost(host);
78 trans->GetPort(&port);
80 LOG((" -> %s:%d\n", host.get(), port));
82 nsCOMPtr<nsIInputStream> input;
83 nsCOMPtr<nsIOutputStream> output;
84 nsresult rv;
86 rv = trans->OpenInputStream(nsITransport::OPEN_BLOCKING, 0, 0, getter_AddRefs(input));
87 if (NS_FAILED(rv))
88 return rv;
90 rv = trans->OpenOutputStream(nsITransport::OPEN_BLOCKING, 0, 0, getter_AddRefs(output));
91 if (NS_FAILED(rv))
92 return rv;
94 char buf[256];
95 PRUint32 n;
97 rv = input->Read(buf, sizeof(buf), &n);
98 if (NS_FAILED(rv))
99 return rv;
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);
103 if (NS_FAILED(rv))
104 return rv;
106 input->Close();
107 output->Close();
108 return NS_OK;
111 NS_IMETHODIMP
112 MySocketListener::OnStopListening(nsIServerSocket *serv, nsresult status)
114 LOG(("MySocketListener::OnStopListening [serv=%p status=%x]\n", serv, status));
115 QuitPumpingEvents();
116 return NS_OK;
119 static nsresult
120 MakeServer(PRInt32 port)
122 nsresult rv;
123 nsCOMPtr<nsIServerSocket> serv = do_CreateInstance(NS_SERVERSOCKET_CONTRACTID, &rv);
124 if (NS_FAILED(rv))
125 return rv;
127 rv = serv->Init(port, PR_TRUE, 5);
128 if (NS_FAILED(rv))
129 return rv;
131 rv = serv->GetPort(&port);
132 if (NS_FAILED(rv))
133 return rv;
134 LOG((" listening on port %d\n", port));
136 rv = serv->AsyncListen(new MySocketListener());
137 return rv;
141 main(int argc, char* argv[])
143 if (test_common_init(&argc, &argv) != 0)
144 return -1;
146 nsresult rv= (nsresult)-1;
147 if (argc < 2) {
148 printf("usage: %s <port>\n", argv[0]);
149 return -1;
152 #if defined(PR_LOGGING)
153 gTestLog = PR_NewLogModule("Test");
154 #endif
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]));
166 if (NS_FAILED(rv)) {
167 LOG(("MakeServer failed [rv=%x]\n", rv));
168 return -1;
171 // Enter the message pump to allow the URL load to proceed.
172 PumpEvents();
173 } // this scopes the nsCOMPtrs
174 // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
175 NS_ShutdownXPCOM(nsnull);
176 return rv;