Bug 460926 A11y hierachy is broken on Ubuntu 8.10 (GNOME 2.24), r=Evan.Yan sr=roc
[wine-gecko.git] / netwerk / test / TestSocketIO.cpp
blob7183c2bc3ecf1d66ed35504bd9abe505d60ec67f
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
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 ***** */
37 #include <stdio.h>
38 #include <signal.h>
40 #ifdef WIN32
41 #include <windows.h>
42 #endif
43 #ifdef OS2
44 #include <os2.h>
45 #endif
47 #include "nspr.h"
48 #include "nscore.h"
49 #include "nsISocketTransportService.h"
50 #include "nsIEventQueueService.h"
51 #include "nsIServiceManager.h"
52 #include "nsITransport.h"
53 #include "nsIRequest.h"
54 #include "nsIStreamProvider.h"
55 #include "nsIStreamListener.h"
56 #include "nsIPipe.h"
57 #include "nsIOutputStream.h"
58 #include "nsIInputStream.h"
59 #include "nsCRT.h"
60 #include "nsCOMPtr.h"
61 #include "nsIByteArrayInputStream.h"
63 #if defined(PR_LOGGING)
64 static PRLogModuleInfo *gTestSocketIOLog;
65 #define LOG(args) PR_LOG(gTestSocketIOLog, PR_LOG_DEBUG, args)
66 #else
67 #define LOG(args)
68 #endif
70 static NS_DEFINE_CID(kSocketTransportServiceCID, NS_SOCKETTRANSPORTSERVICE_CID);
71 static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
73 static PRTime gElapsedTime;
74 static int gKeepRunning = 1;
75 static nsIEventQueue* gEventQ = nsnull;
78 //----------------------------------------------------------------------------
79 // Test Listener
80 //----------------------------------------------------------------------------
83 class TestListener : public nsIStreamListener
85 public:
86 TestListener() {}
87 virtual ~TestListener() {}
89 NS_DECL_ISUPPORTS
90 NS_DECL_NSIREQUESTOBSERVER
91 NS_DECL_NSISTREAMLISTENER
94 NS_IMPL_ISUPPORTS2(TestListener,
95 nsIRequestObserver,
96 nsIStreamListener);
98 NS_IMETHODIMP
99 TestListener::OnStartRequest(nsIRequest* request, nsISupports* context)
101 LOG(("TestListener::OnStartRequest\n"));
102 return NS_OK;
105 NS_IMETHODIMP
106 TestListener::OnDataAvailable(nsIRequest* request,
107 nsISupports* context,
108 nsIInputStream *aIStream,
109 PRUint32 aSourceOffset,
110 PRUint32 aLength)
112 LOG(("TestListener::OnDataAvailable [offset=%u length=%u]\n",
113 aSourceOffset, aLength));
114 char buf[1025];
115 PRUint32 amt;
116 while (1) {
117 aIStream->Read(buf, 1024, &amt);
118 if (amt == 0)
119 break;
120 buf[amt] = '\0';
121 puts(buf);
123 return NS_OK;
126 NS_IMETHODIMP
127 TestListener::OnStopRequest(nsIRequest* request, nsISupports* context,
128 nsresult aStatus)
130 LOG(("TestListener::OnStopRequest [aStatus=%x]\n", aStatus));
131 gKeepRunning = 0;
132 return NS_OK;
136 //----------------------------------------------------------------------------
137 // Test Provider
138 //----------------------------------------------------------------------------
141 class TestProvider : public nsIStreamProvider
143 public:
144 TestProvider(char *data);
145 virtual ~TestProvider();
147 NS_DECL_ISUPPORTS
148 NS_DECL_NSIREQUESTOBSERVER
149 NS_DECL_NSISTREAMPROVIDER
151 protected:
152 nsCOMPtr<nsIByteArrayInputStream> mData;
155 NS_IMPL_ISUPPORTS2(TestProvider,
156 nsIStreamProvider,
157 nsIRequestObserver)
159 TestProvider::TestProvider(char *data)
161 NS_NewByteArrayInputStream(getter_AddRefs(mData), data, strlen(data));
162 LOG(("Constructing TestProvider [this=%x]\n", this));
165 TestProvider::~TestProvider()
167 LOG(("Destroying TestProvider [this=%x]\n", this));
170 NS_IMETHODIMP
171 TestProvider::OnStartRequest(nsIRequest* request, nsISupports* context)
173 LOG(("TestProvider::OnStartRequest [this=%x]\n", this));
174 return NS_OK;
177 NS_IMETHODIMP
178 TestProvider::OnStopRequest(nsIRequest* request, nsISupports* context,
179 nsresult aStatus)
181 LOG(("TestProvider::OnStopRequest [status=%x]\n", aStatus));
183 nsCOMPtr<nsIStreamListener> listener = do_QueryInterface(new TestListener());
185 if (NS_SUCCEEDED(aStatus)) {
186 nsCOMPtr<nsITransportRequest> treq = do_QueryInterface(request);
187 nsCOMPtr<nsITransport> transport;
188 treq->GetTransport(getter_AddRefs(transport));
189 if (transport) {
190 nsCOMPtr<nsIRequest> readRequest;
191 transport->AsyncRead(listener, nsnull, 0, 0, 0, getter_AddRefs(readRequest));
193 } else
194 gKeepRunning = 0;
196 return NS_OK;
199 NS_IMETHODIMP
200 TestProvider::OnDataWritable(nsIRequest *request, nsISupports *context,
201 nsIOutputStream *output, PRUint32 offset, PRUint32 count)
203 LOG(("TestProvider::OnDataWritable [offset=%u, count=%u]\n", offset, count));
204 PRUint32 writeCount;
205 nsresult rv = output->WriteFrom(mData, count, &writeCount);
206 // Zero bytes written on success indicates EOF
207 if (NS_SUCCEEDED(rv) && (writeCount == 0))
208 return NS_BASE_STREAM_CLOSED;
209 return rv;
213 //----------------------------------------------------------------------------
214 // Synchronous IO
215 //----------------------------------------------------------------------------
217 nsresult
218 WriteRequest(nsIOutputStream *os, const char *request)
220 LOG(("WriteRequest [request=%s]\n", request));
221 PRUint32 n;
222 return os->Write(request, strlen(request), &n);
225 nsresult
226 ReadResponse(nsIInputStream *is)
228 PRUint32 bytesRead;
229 char buf[2048];
230 do {
231 is->Read(buf, sizeof(buf), &bytesRead);
232 if (bytesRead > 0)
233 fwrite(buf, 1, bytesRead, stdout);
234 } while (bytesRead > 0);
235 return NS_OK;
239 //----------------------------------------------------------------------------
240 // Startup...
241 //----------------------------------------------------------------------------
244 void
245 sighandler(int sig)
247 LOG(("got signal: %d\n", sig));
248 NS_BREAK();
251 void
252 usage(char **argv)
254 printf("usage: %s [-sync] <host> <path>\n", argv[0]);
255 exit(1);
259 main(int argc, char* argv[])
261 nsresult rv;
263 signal(SIGSEGV, sighandler);
265 #if defined(PR_LOGGING)
266 gTestSocketIOLog = PR_NewLogModule("TestSocketIO");
267 #endif
269 if (argc < 3)
270 usage(argv);
272 PRIntn i=0;
273 PRBool sync = PR_FALSE;
274 if (nsCRT::strcasecmp(argv[1], "-sync") == 0) {
275 if (argc < 4)
276 usage(argv);
277 sync = PR_TRUE;
278 i = 1;
281 char *hostName = argv[1+i];
282 char *fileName = argv[2+i];
283 int port = 80;
285 // Create the Event Queue for this thread...
286 nsCOMPtr<nsIEventQueueService> eventQService =
287 do_GetService(kEventQueueServiceCID, &rv);
288 if (NS_FAILED(rv)) {
289 NS_WARNING("failed to create: event queue service!");
290 return rv;
293 rv = eventQService->CreateMonitoredThreadEventQueue();
294 if (NS_FAILED(rv)) {
295 NS_WARNING("failed to create: thread event queue!");
296 return rv;
299 eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
301 // Create the Socket transport service...
302 nsCOMPtr<nsISocketTransportService> sts =
303 do_GetService(kSocketTransportServiceCID, &rv);
304 if (NS_FAILED(rv)) {
305 NS_WARNING("failed to create: socket transport service!");
306 return rv;
309 char *buffer = PR_smprintf("GET %s HTTP/1.1" CRLF
310 "host: %s" CRLF
311 "user-agent: Mozilla/5.0 (X11; N; Linux 2.2.16-22smp i686; en-US; m18) Gecko/20001220" CRLF
312 "accept: */*" CRLF
313 "accept-language: en" CRLF
314 "accept-encoding: gzip,deflate,compress,identity" CRLF
315 "keep-alive: 300" CRLF
316 "connection: keep-alive" CRLF
317 CRLF,
318 fileName, hostName);
319 LOG(("Request [\n%s]\n", buffer));
321 // Create the socket transport...
322 nsCOMPtr<nsITransport> transport;
323 rv = sts->CreateTransport(hostName, port, nsnull, 0, 0, getter_AddRefs(transport));
324 if (NS_FAILED(rv)) {
325 NS_WARNING("failed to create: socket transport!");
326 return rv;
329 gElapsedTime = PR_Now();
331 if (!sync) {
332 nsCOMPtr<nsIRequest> request;
333 rv = transport->AsyncWrite(new TestProvider(buffer), nsnull, 0, 0, 0, getter_AddRefs(request));
334 if (NS_FAILED(rv)) {
335 NS_WARNING("failed calling: AsyncWrite!");
336 return rv;
339 // Enter the message pump to allow the URL load to proceed.
340 while ( gKeepRunning ) {
341 PLEvent *gEvent;
342 gEventQ->WaitForEvent(&gEvent);
343 gEventQ->HandleEvent(gEvent);
346 else {
347 // synchronous write
349 nsCOMPtr<nsIOutputStream> os;
350 rv = transport->OpenOutputStream(0, 0, 0, getter_AddRefs(os));
351 if (NS_FAILED(rv)) {
352 LOG(("OpenOutputStream failed [rv=%x]\n", rv));
353 return rv;
355 rv = WriteRequest(os, buffer);
356 if (NS_FAILED(rv)) {
357 LOG(("WriteRequest failed [rv=%x]\n", rv));
358 return rv;
361 // synchronous read
363 nsCOMPtr<nsIInputStream> is;
364 rv = transport->OpenInputStream(0, 0, 0, getter_AddRefs(is));
365 if (NS_FAILED(rv)) {
366 LOG(("OpenInputStream failed [rv=%x]\n", rv));
367 return rv;
369 rv = ReadResponse(is);
370 if (NS_FAILED(rv)) {
371 LOG(("ReadResponse failed [rv=%x]\n", rv));
372 return rv;
377 PRTime endTime;
378 endTime = PR_Now();
379 LOG(("Elapsed time: %d\n", (PRInt32)(endTime/1000UL - gElapsedTime/1000UL)));
381 sts->Shutdown();
382 return 0;