1999-04-29 00:07:33 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
1999-11-06 06:40:37 +03:00
|
|
|
* The contents of this file are subject to the Netscape Public
|
|
|
|
* License Version 1.1 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.mozilla.org/NPL/
|
1999-04-29 00:07:33 +04:00
|
|
|
*
|
1999-11-06 06:40:37 +03:00
|
|
|
* Software distributed under the License is distributed on an "AS
|
|
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
1999-04-29 00:07:33 +04:00
|
|
|
*
|
1999-11-06 06:40:37 +03:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
1999-04-29 00:07:33 +04:00
|
|
|
* Communications Corporation. Portions created by Netscape are
|
1999-11-06 06:40:37 +03:00
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
2000-01-11 23:49:15 +03:00
|
|
|
* Pierre Phaneuf <pp@ludusdesign.com>
|
1999-04-29 00:07:33 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
1999-05-04 22:50:59 +04:00
|
|
|
#ifdef WIN32
|
1999-04-29 00:07:33 +04:00
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "plstr.h"
|
1999-05-06 02:41:44 +04:00
|
|
|
#include "nsIEventQueue.h"
|
1999-04-29 00:07:33 +04:00
|
|
|
|
1999-04-29 03:13:07 +04:00
|
|
|
#include "nsIProtocolConnection.h"
|
1999-04-29 00:07:33 +04:00
|
|
|
#include "nsIStreamListener.h"
|
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsIUrl.h"
|
|
|
|
#include "nsINetService.h"
|
|
|
|
#include "nsIComponentManager.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIEventQueueService.h"
|
2000-03-31 14:19:31 +04:00
|
|
|
#include "nsString.h"
|
1999-04-29 00:07:33 +04:00
|
|
|
|
|
|
|
|
|
|
|
int urlLoaded;
|
|
|
|
PRBool bTraceEnabled;
|
|
|
|
PRBool bLoadAsync;
|
|
|
|
|
|
|
|
//#include "nsIPostToServer.h"
|
|
|
|
#include "nsINetService.h"
|
|
|
|
|
|
|
|
// Define CIDs...
|
|
|
|
static NS_DEFINE_IID(kNetServiceCID, NS_NETSERVICE_CID);
|
|
|
|
static NS_DEFINE_IID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
|
1999-05-19 00:52:07 +04:00
|
|
|
static NS_DEFINE_IID(kEventQueueCID, NS_EVENTQUEUE_CID);
|
1999-04-29 00:07:33 +04:00
|
|
|
|
|
|
|
// Define IIDs...
|
|
|
|
static NS_DEFINE_IID(kIEventQueueServiceIID, NS_IEVENTQUEUESERVICE_IID);
|
|
|
|
//NS_DEFINE_IID(kIPostToServerIID, NS_IPOSTTOSERVER_IID);
|
|
|
|
|
|
|
|
|
|
|
|
class TestConsumer : public nsIStreamListener
|
|
|
|
{
|
|
|
|
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
TestConsumer();
|
|
|
|
|
|
|
|
// stream listener methods
|
1999-08-22 17:26:18 +04:00
|
|
|
NS_DECL_NSISTREAMLISTENER
|
1999-04-29 00:07:33 +04:00
|
|
|
|
|
|
|
// stream observer methods
|
1999-08-22 17:26:18 +04:00
|
|
|
NS_DECL_NSISTREAMOBSERVER
|
1999-04-29 00:07:33 +04:00
|
|
|
|
|
|
|
protected:
|
|
|
|
~TestConsumer();
|
|
|
|
nsIUrl* mUrl;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
TestConsumer::TestConsumer()
|
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_DEFINE_IID(kIStreamListenerIID, NS_ISTREAMLISTENER_IID);
|
|
|
|
NS_IMPL_ISUPPORTS(TestConsumer,kIStreamListenerIID);
|
|
|
|
|
|
|
|
|
|
|
|
TestConsumer::~TestConsumer()
|
|
|
|
{
|
|
|
|
if (bTraceEnabled) {
|
|
|
|
printf("\n+++ TestConsumer is being deleted...\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-06-09 00:57:32 +04:00
|
|
|
NS_IMETHODIMP TestConsumer::OnDataAvailable(nsISupports *context,
|
1999-06-29 00:37:10 +04:00
|
|
|
nsIInputStream* aIStream,
|
1999-06-09 00:57:32 +04:00
|
|
|
PRUint32 aSourceOffset,
|
|
|
|
PRUint32 aLength) {
|
1999-04-29 00:07:33 +04:00
|
|
|
PRUint32 len;
|
|
|
|
|
|
|
|
if (bTraceEnabled) {
|
|
|
|
printf("\n+++ TestConsumer::OnDataAvailable: URL: %p, %d bytes available...\n", mUrl, aLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
nsresult err;
|
|
|
|
char buffer[80];
|
|
|
|
PRUint32 i;
|
|
|
|
|
|
|
|
err = aIStream->Read(buffer, 80, &len);
|
|
|
|
if (err == NS_OK) {
|
|
|
|
for (i=0; i<len; i++) {
|
|
|
|
putchar(buffer[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (len > 0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1999-07-07 12:08:40 +04:00
|
|
|
NS_IMETHODIMP TestConsumer::OnStartRequest(nsIChannel* channel, nsISupports *context) {
|
1999-04-29 00:07:33 +04:00
|
|
|
if (bTraceEnabled) {
|
1999-07-01 23:30:20 +04:00
|
|
|
printf("\n+++ TestConsumer::OnStartRequest: URL: %p, Content type: %s\n", mUrl, "XXX some context");
|
1999-04-29 00:07:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1999-07-07 12:08:40 +04:00
|
|
|
NS_IMETHODIMP TestConsumer::OnStopRequest(nsISupports *context, nsIChannel* channel,
|
|
|
|
nsresult aStatus, nsIString *aMsg) {
|
1999-04-29 00:07:33 +04:00
|
|
|
if (bTraceEnabled) {
|
1999-07-01 23:30:20 +04:00
|
|
|
printf("\n+++ TestConsumer::OnStopRequest... URL: %p status: %d\n", mUrl, aStatus);
|
1999-04-29 00:07:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(aStatus)) {
|
|
|
|
char* url;
|
|
|
|
mUrl->ToNewCString(&url);
|
|
|
|
|
|
|
|
printf("Unable to load URL %s\n", url);
|
1999-07-28 08:05:10 +04:00
|
|
|
nsAllocator::Free(url);
|
1999-04-29 00:07:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
urlLoaded = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult ReadStreamSynchronously(nsIInputStream* aIn)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
char buffer[1024];
|
|
|
|
|
|
|
|
if (nsnull != aIn) {
|
|
|
|
PRUint32 len;
|
|
|
|
|
|
|
|
do {
|
|
|
|
PRUint32 i;
|
|
|
|
|
|
|
|
rv = aIn->Read(buffer, sizeof(buffer), &len);
|
|
|
|
for (i=0; i<len; i++) {
|
|
|
|
putchar(buffer[i]);
|
|
|
|
}
|
|
|
|
} while (len > 0);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
nsAutoString2 url_address;
|
|
|
|
char buf[256];
|
|
|
|
nsIStreamListener *pConsumer;
|
|
|
|
nsIEventQueueService* pEventQService;
|
|
|
|
nsIUrl *pURL;
|
|
|
|
nsresult result;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (argc < 2) {
|
|
|
|
printf("test: [-trace] [-sync] <URL>\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1999-06-02 15:30:05 +04:00
|
|
|
result = nsComponentManager::AutoRegister(nsIComponentManager::NS_Startup,
|
|
|
|
"components");
|
|
|
|
if (NS_FAILED(result)) return result;
|
1999-04-29 00:07:33 +04:00
|
|
|
|
|
|
|
// Create the Event Queue for this thread...
|
|
|
|
pEventQService = nsnull;
|
|
|
|
result = nsServiceManager::GetService(kEventQueueServiceCID,
|
|
|
|
kIEventQueueServiceIID,
|
|
|
|
(nsISupports **)&pEventQService);
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
// XXX: What if this fails?
|
|
|
|
result = pEventQService->CreateThreadEventQueue();
|
|
|
|
}
|
|
|
|
|
|
|
|
bTraceEnabled = PR_FALSE;
|
|
|
|
bLoadAsync = PR_TRUE;
|
|
|
|
|
|
|
|
for (i=1; i < argc; i++) {
|
|
|
|
// Turn on netlib tracing...
|
|
|
|
if (PL_strcasecmp(argv[i], "-trace") == 0) {
|
|
|
|
//NET_ToggleTrace();
|
|
|
|
bTraceEnabled = PR_TRUE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Turn on synchronous URL loading...
|
|
|
|
if (PL_strcasecmp(argv[i], "-sync") == 0) {
|
|
|
|
bLoadAsync = PR_FALSE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
urlLoaded = 0;
|
|
|
|
|
|
|
|
url_address = argv[i];
|
|
|
|
if (bTraceEnabled) {
|
|
|
|
url_address.ToCString(buf, 256);
|
|
|
|
printf("+++ loading URL: %s...\n", buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
pConsumer = new TestConsumer;
|
|
|
|
pConsumer->AddRef();
|
|
|
|
|
|
|
|
// Create the URL object...
|
|
|
|
pURL = NULL;
|
|
|
|
#if 1
|
1999-06-18 21:34:08 +04:00
|
|
|
NS_WITH_SERVICE(nsIIOService, service, kIOServiceCID, &result);
|
|
|
|
if (NS_FAILED(result)) return result;
|
|
|
|
|
|
|
|
nsIURI *uri = nsnull;
|
|
|
|
= service->NewURI(argv[i], nsnull, &uri);
|
|
|
|
if (NS_FAILED(result)) return result;
|
|
|
|
|
2000-01-11 23:49:15 +03:00
|
|
|
result = uri->QueryInterface(NS_GET_IID(nsIURL), (void**)&pURL);
|
1999-06-18 21:34:08 +04:00
|
|
|
NS_RELEASE(uri);
|
|
|
|
if (NS_FAILED(result)) return result;
|
1999-04-29 00:07:33 +04:00
|
|
|
if (NS_OK != result) {
|
|
|
|
if (bTraceEnabled) {
|
|
|
|
printf("NS_NewURL() failed...\n");
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
return 1;
|
|
|
|
#endif // 0
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
nsIPostToServer *pPoster;
|
|
|
|
result = pURL->QueryInterface(kIPostToServerIID, (void**)&pPoster);
|
|
|
|
if (result == NS_OK) {
|
|
|
|
pPoster->SendFile("foo.txt");
|
|
|
|
}
|
|
|
|
NS_IF_RELEASE(pPoster);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
// Start the URL load...
|
|
|
|
if (PR_TRUE == bLoadAsync) {
|
|
|
|
result = NS_OpenURL(pURL, pConsumer);
|
|
|
|
|
|
|
|
/* If the open failed, then do not drop into the message loop... */
|
|
|
|
if (NS_OK != result) {
|
|
|
|
char* url;
|
|
|
|
pURL->ToNewCString(&url);
|
|
|
|
|
|
|
|
printf("Unable to load URL %s\n", url);
|
1999-07-28 08:05:10 +04:00
|
|
|
nsAllocator::Free(url);
|
1999-04-29 00:07:33 +04:00
|
|
|
urlLoaded = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Load the URL synchronously...
|
|
|
|
else {
|
|
|
|
nsIInputStream *in;
|
|
|
|
|
|
|
|
result = NS_OpenURL(pURL, &in);
|
|
|
|
ReadStreamSynchronously(in);
|
|
|
|
NS_IF_RELEASE(in);
|
|
|
|
urlLoaded = 1;
|
|
|
|
}
|
|
|
|
#else
|
1999-05-19 00:52:07 +04:00
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsIEventQueue* eventQ = nsnull;
|
|
|
|
NS_WITH_SERVICE(nsIEventQueueService, eventQService, kEventQueueServiceCID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
1999-11-30 03:32:43 +03:00
|
|
|
rv = eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, &eventQ);
|
1999-05-19 00:52:07 +04:00
|
|
|
}
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
1999-04-29 03:13:07 +04:00
|
|
|
// Start the URL load...
|
|
|
|
nsIProtocolConnection* protoConn = nsnull;
|
|
|
|
result = NS_NewConnection(pURL,
|
|
|
|
// nsISupports* eventSink,
|
1999-05-19 00:52:07 +04:00
|
|
|
eventQ,
|
1999-04-29 03:13:07 +04:00
|
|
|
// nsIConnectionGroup* group,
|
|
|
|
nsnull,
|
|
|
|
// nsIProtocolConnection* *result
|
|
|
|
&protoConn);
|
|
|
|
|
|
|
|
if (NS_FAILED(result)) return 1;
|
|
|
|
|
1999-04-30 19:28:33 +04:00
|
|
|
protoConn->Open();
|
|
|
|
|
1999-04-29 00:07:33 +04:00
|
|
|
#endif // 0
|
|
|
|
|
|
|
|
|
|
|
|
// Enter the message pump to allow the URL load to proceed.
|
|
|
|
while ( !urlLoaded ) {
|
|
|
|
#ifdef XP_PC
|
|
|
|
MSG msg;
|
|
|
|
|
|
|
|
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
|
|
|
|
TranslateMessage(&msg);
|
|
|
|
DispatchMessage(&msg);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
pURL->Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nsnull != pEventQService) {
|
|
|
|
pEventQService->DestroyThreadEventQueue();
|
|
|
|
nsServiceManager::ReleaseService(kEventQueueServiceCID, pEventQService);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|