gecko-dev/network/module/tests/nettest.cpp

312 строки
7.2 KiB
C++
Исходник Обычный вид История

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
1998-06-02 05:27:17 +04:00
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#include <stdio.h>
#include <assert.h>
#ifdef XP_PC
#include <windows.h>
#endif
#include "plstr.h"
#include "plevent.h"
1998-06-02 05:27:17 +04:00
#include "nsIStreamListener.h"
#include "nsIInputStream.h"
#include "nsIURL.h"
#include "nsINetService.h"
#include "nsRepository.h"
1998-06-02 05:27:17 +04:00
#include "nsString.h"
1998-06-02 05:27:17 +04:00
int urlLoaded;
PRBool bTraceEnabled;
PRBool bLoadAsync;
1998-06-02 05:27:17 +04:00
#include "nsIPostToServer.h"
#include "nsINetService.h"
#ifdef XP_PC
#define NETLIB_DLL "netlib.dll"
#else
#ifdef XP_MAC
#include "nsMacRepository.h"
#else
#define NETLIB_DLL "libnetlib.so"
#endif
#endif
static NS_DEFINE_IID(kNetServiceCID, NS_NETSERVICE_CID);
1998-06-02 05:27:17 +04:00
NS_DEFINE_IID(kIPostToServerIID, NS_IPOSTTOSERVER_IID);
#ifdef XP_UNIX
extern "C" char *fe_GetConfigDir(void) {
printf("XXX: return /tmp for fe_GetConfigDir\n");
return strdup("/tmp");
}
#endif /* XP_UNIX */
1998-06-02 05:27:17 +04:00
/* XXX: Don't include net.h... */
extern "C" {
extern void NET_ToggleTrace();
};
class TestConsumer : public nsIStreamListener
{
public:
NS_DECL_ISUPPORTS
TestConsumer();
NS_IMETHOD GetBindInfo(nsIURL* aURL, nsStreamBindingInfo* info);
NS_IMETHOD OnProgress(nsIURL* aURL, PRUint32 Progress, PRUint32 ProgressMax);
NS_IMETHOD OnStatus(nsIURL* aURL, const PRUnichar* aMsg);
NS_IMETHOD OnStartBinding(nsIURL* aURL, const char *aContentType);
NS_IMETHOD OnDataAvailable(nsIURL* aURL, nsIInputStream *pIStream, PRUint32 length);
NS_IMETHOD OnStopBinding(nsIURL* aURL, nsresult status, const PRUnichar* aMsg);
1998-06-02 05:27:17 +04:00
protected:
~TestConsumer();
};
TestConsumer::TestConsumer()
{
NS_INIT_REFCNT();
}
1998-07-01 15:24:10 +04:00
NS_DEFINE_IID(kIStreamListenerIID, NS_ISTREAMLISTENER_IID);
NS_IMPL_ISUPPORTS(TestConsumer,kIStreamListenerIID);
1998-06-02 05:27:17 +04:00
TestConsumer::~TestConsumer()
{
if (bTraceEnabled) {
printf("\n+++ TestConsumer is being deleted...\n");
1998-06-02 05:27:17 +04:00
}
}
NS_IMETHODIMP TestConsumer::GetBindInfo(nsIURL* aURL, nsStreamBindingInfo* info)
1998-06-02 05:27:17 +04:00
{
if (bTraceEnabled) {
printf("\n+++ TestConsumer::GetBindInfo: URL: %p\n", aURL);
1998-06-02 05:27:17 +04:00
}
return 0;
}
NS_IMETHODIMP TestConsumer::OnProgress(nsIURL* aURL, PRUint32 Progress,
PRUint32 ProgressMax)
1998-06-02 05:27:17 +04:00
{
if (bTraceEnabled) {
printf("\n+++ TestConsumer::OnProgress: URL: %p - %d of total %d\n", aURL, Progress, ProgressMax);
}
return 0;
}
NS_IMETHODIMP TestConsumer::OnStatus(nsIURL* aURL, const PRUnichar* aMsg)
{
if (bTraceEnabled) {
printf("\n+++ TestConsumer::OnStatus: ");
nsString str(aMsg);
char* c = str.ToNewCString();
fputs(c, stdout);
free(c);
fputs("\n", stdout);
1998-06-02 05:27:17 +04:00
}
return 0;
}
NS_IMETHODIMP TestConsumer::OnStartBinding(nsIURL* aURL, const char *aContentType)
1998-06-02 05:27:17 +04:00
{
if (bTraceEnabled) {
printf("\n+++ TestConsumer::OnStartBinding: URL: %p, Content type: %s\n", aURL, aContentType);
1998-06-02 05:27:17 +04:00
}
return 0;
}
NS_IMETHODIMP TestConsumer::OnDataAvailable(nsIURL* aURL, nsIInputStream *pIStream, PRUint32 length)
1998-06-02 05:27:17 +04:00
{
PRUint32 len;
1998-06-02 05:27:17 +04:00
if (bTraceEnabled) {
printf("\n+++ TestConsumer::OnDataAvailable: URL: %p, %d bytes available...\n", aURL, length);
1998-06-02 05:27:17 +04:00
}
do {
1998-07-24 00:43:08 +04:00
nsresult err;
1998-06-02 05:27:17 +04:00
char buffer[80];
PRUint32 i;
1998-06-02 05:27:17 +04:00
1998-07-24 00:43:08 +04:00
err = pIStream->Read(buffer, 0, 80, &len);
if (err == NS_OK) {
for (i=0; i<len; i++) {
putchar(buffer[i]);
}
1998-06-02 05:27:17 +04:00
}
} while (len > 0);
return 0;
}
NS_IMETHODIMP TestConsumer::OnStopBinding(nsIURL* aURL, nsresult status, const PRUnichar* aMsg)
1998-06-02 05:27:17 +04:00
{
if (bTraceEnabled) {
printf("\n+++ TestConsumer::OnStopBinding... URL: %p status: %d\n", aURL, status);
1998-06-02 05:27:17 +04:00
}
/* The document has been loaded, so drop out of the message pump... */
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, 0, sizeof(buffer), &len);
for (i=0; i<len; i++) {
putchar(buffer[i]);
}
} while (len > 0);
}
return NS_OK;
}
1998-06-02 05:27:17 +04:00
int main(int argc, char **argv)
{
#ifdef XP_PC
MSG msg;
#endif
nsString url_address;
char buf[256];
nsIStreamListener *pConsumer;
nsIURL *pURL;
nsresult result;
int i;
1998-06-02 05:27:17 +04:00
if (argc < 2) {
printf("test: [-trace] [-sync] <URL>\n");
1998-06-02 05:27:17 +04:00
return 0;
}
PL_InitializeEventsLib("");
nsRepository::RegisterFactory(kNetServiceCID, NETLIB_DLL, PR_FALSE, PR_FALSE);
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;
}
1998-06-02 05:27:17 +04:00
urlLoaded = 0;
1998-06-02 05:27:17 +04:00
url_address = argv[i];
if (bTraceEnabled) {
url_address.ToCString(buf, 256);
printf("+++ loading URL: %s...\n", buf);
}
1998-06-02 05:27:17 +04:00
pConsumer = new TestConsumer;
pConsumer->AddRef();
// Create the URL object...
pURL = NULL;
result = NS_NewURL(&pURL, url_address);
if (NS_OK != result) {
if (bTraceEnabled) {
printf("NS_NewURL() failed...\n");
}
return 1;
}
1998-06-02 05:27:17 +04:00
#if 0
nsIPostToServer *pPoster;
result = pURL->QueryInterface(kIPostToServerIID, (void**)&pPoster);
if (result == NS_OK) {
pPoster->SendFile("foo.txt");
}
NS_IF_RELEASE(pPoster);
1998-06-02 05:27:17 +04:00
#endif
// 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) {
urlLoaded = 1;
}
}
// Load the URL synchronously...
else {
nsIInputStream *in;
result = NS_OpenURL(pURL, &in);
ReadStreamSynchronously(in);
NS_IF_RELEASE(in);
urlLoaded = 1;
1998-06-02 05:27:17 +04:00
}
// Enter the message pump to allow the URL load to proceed.
while ( !urlLoaded ) {
#ifdef XP_PC
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
1998-06-02 05:27:17 +04:00
#endif
}
pURL->Release();
1998-06-02 05:27:17 +04:00
}
1998-06-02 05:27:17 +04:00
return 0;
}