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
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.
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 ***** */
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"
57 #include "nsIOutputStream.h"
58 #include "nsIInputStream.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)
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 //----------------------------------------------------------------------------
80 //----------------------------------------------------------------------------
83 class TestListener
: public nsIStreamListener
87 virtual ~TestListener() {}
90 NS_DECL_NSIREQUESTOBSERVER
91 NS_DECL_NSISTREAMLISTENER
94 NS_IMPL_ISUPPORTS2(TestListener
,
99 TestListener::OnStartRequest(nsIRequest
* request
, nsISupports
* context
)
101 LOG(("TestListener::OnStartRequest\n"));
106 TestListener::OnDataAvailable(nsIRequest
* request
,
107 nsISupports
* context
,
108 nsIInputStream
*aIStream
,
109 PRUint32 aSourceOffset
,
112 LOG(("TestListener::OnDataAvailable [offset=%u length=%u]\n",
113 aSourceOffset
, aLength
));
117 aIStream
->Read(buf
, 1024, &amt
);
127 TestListener::OnStopRequest(nsIRequest
* request
, nsISupports
* context
,
130 LOG(("TestListener::OnStopRequest [aStatus=%x]\n", aStatus
));
136 //----------------------------------------------------------------------------
138 //----------------------------------------------------------------------------
141 class TestProvider
: public nsIStreamProvider
144 TestProvider(char *data
);
145 virtual ~TestProvider();
148 NS_DECL_NSIREQUESTOBSERVER
149 NS_DECL_NSISTREAMPROVIDER
152 nsCOMPtr
<nsIByteArrayInputStream
> mData
;
155 NS_IMPL_ISUPPORTS2(TestProvider
,
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));
171 TestProvider::OnStartRequest(nsIRequest
* request
, nsISupports
* context
)
173 LOG(("TestProvider::OnStartRequest [this=%x]\n", this));
178 TestProvider::OnStopRequest(nsIRequest
* request
, nsISupports
* context
,
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
));
190 nsCOMPtr
<nsIRequest
> readRequest
;
191 transport
->AsyncRead(listener
, nsnull
, 0, 0, 0, getter_AddRefs(readRequest
));
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
));
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
;
213 //----------------------------------------------------------------------------
215 //----------------------------------------------------------------------------
218 WriteRequest(nsIOutputStream
*os
, const char *request
)
220 LOG(("WriteRequest [request=%s]\n", request
));
222 return os
->Write(request
, strlen(request
), &n
);
226 ReadResponse(nsIInputStream
*is
)
231 is
->Read(buf
, sizeof(buf
), &bytesRead
);
233 fwrite(buf
, 1, bytesRead
, stdout
);
234 } while (bytesRead
> 0);
239 //----------------------------------------------------------------------------
241 //----------------------------------------------------------------------------
247 LOG(("got signal: %d\n", sig
));
254 printf("usage: %s [-sync] <host> <path>\n", argv
[0]);
259 main(int argc
, char* argv
[])
263 signal(SIGSEGV
, sighandler
);
265 #if defined(PR_LOGGING)
266 gTestSocketIOLog
= PR_NewLogModule("TestSocketIO");
273 PRBool sync
= PR_FALSE
;
274 if (nsCRT::strcasecmp(argv
[1], "-sync") == 0) {
281 char *hostName
= argv
[1+i
];
282 char *fileName
= argv
[2+i
];
285 // Create the Event Queue for this thread...
286 nsCOMPtr
<nsIEventQueueService
> eventQService
=
287 do_GetService(kEventQueueServiceCID
, &rv
);
289 NS_WARNING("failed to create: event queue service!");
293 rv
= eventQService
->CreateMonitoredThreadEventQueue();
295 NS_WARNING("failed to create: thread event queue!");
299 eventQService
->GetThreadEventQueue(NS_CURRENT_THREAD
, &gEventQ
);
301 // Create the Socket transport service...
302 nsCOMPtr
<nsISocketTransportService
> sts
=
303 do_GetService(kSocketTransportServiceCID
, &rv
);
305 NS_WARNING("failed to create: socket transport service!");
309 char *buffer
= PR_smprintf("GET %s HTTP/1.1" CRLF
311 "user-agent: Mozilla/5.0 (X11; N; Linux 2.2.16-22smp i686; en-US; m18) Gecko/20001220" CRLF
313 "accept-language: en" CRLF
314 "accept-encoding: gzip,deflate,compress,identity" CRLF
315 "keep-alive: 300" CRLF
316 "connection: keep-alive" CRLF
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
));
325 NS_WARNING("failed to create: socket transport!");
329 gElapsedTime
= PR_Now();
332 nsCOMPtr
<nsIRequest
> request
;
333 rv
= transport
->AsyncWrite(new TestProvider(buffer
), nsnull
, 0, 0, 0, getter_AddRefs(request
));
335 NS_WARNING("failed calling: AsyncWrite!");
339 // Enter the message pump to allow the URL load to proceed.
340 while ( gKeepRunning
) {
342 gEventQ
->WaitForEvent(&gEvent
);
343 gEventQ
->HandleEvent(gEvent
);
349 nsCOMPtr
<nsIOutputStream
> os
;
350 rv
= transport
->OpenOutputStream(0, 0, 0, getter_AddRefs(os
));
352 LOG(("OpenOutputStream failed [rv=%x]\n", rv
));
355 rv
= WriteRequest(os
, buffer
);
357 LOG(("WriteRequest failed [rv=%x]\n", rv
));
363 nsCOMPtr
<nsIInputStream
> is
;
364 rv
= transport
->OpenInputStream(0, 0, 0, getter_AddRefs(is
));
366 LOG(("OpenInputStream failed [rv=%x]\n", rv
));
369 rv
= ReadResponse(is
);
371 LOG(("ReadResponse failed [rv=%x]\n", rv
));
379 LOG(("Elapsed time: %d\n", (PRInt32
)(endTime
/1000UL - gElapsedTime
/1000UL)));