2001-04-19 23:43:43 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
1999-08-25 02:04:05 +04:00
|
|
|
*
|
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-08-25 02:04:05 +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-08-25 02:04:05 +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-08-25 02:04:05 +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):
|
1999-08-25 02:04:05 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIComponentManager.h"
|
|
|
|
#include "nsIStreamConverterService.h"
|
|
|
|
#include "nsIStreamConverter.h"
|
2001-03-08 03:41:53 +03:00
|
|
|
#include "nsICategoryManager.h"
|
1999-08-25 02:04:05 +04:00
|
|
|
#include "nsIFactory.h"
|
|
|
|
#include "nsIStringStream.h"
|
1999-10-30 00:50:06 +04:00
|
|
|
#include "nsCOMPtr.h"
|
1999-11-30 07:50:42 +03:00
|
|
|
#include "nsNetUtil.h"
|
|
|
|
|
1999-09-02 06:18:28 +04:00
|
|
|
#include "nspr.h"
|
|
|
|
|
1999-08-25 02:04:05 +04:00
|
|
|
#define ASYNC_TEST // undefine this if you want to test sycnronous conversion.
|
|
|
|
|
|
|
|
/////////////////////////////////
|
|
|
|
// Event pump setup
|
|
|
|
/////////////////////////////////
|
|
|
|
#include "nsIEventQueueService.h"
|
2001-04-19 23:43:43 +04:00
|
|
|
#ifdef XP_WIN
|
1999-08-25 02:04:05 +04:00
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
2000-02-29 18:40:42 +03:00
|
|
|
#ifdef XP_OS2
|
|
|
|
#include <os2.h>
|
|
|
|
#endif
|
|
|
|
|
1999-08-25 02:04:05 +04:00
|
|
|
static int gKeepRunning = 0;
|
|
|
|
static nsIEventQueue* gEventQ = nsnull;
|
|
|
|
/////////////////////////////////
|
|
|
|
// Event pump END
|
|
|
|
/////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////
|
|
|
|
// Test converters include
|
|
|
|
/////////////////////////////////
|
|
|
|
#include "Converters.h"
|
|
|
|
|
|
|
|
// CID setup
|
|
|
|
static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
|
|
|
|
static NS_DEFINE_CID(kStreamConverterServiceCID, NS_STREAMCONVERTERSERVICE_CID);
|
|
|
|
static NS_DEFINE_CID(kComponentManagerCID, NS_COMPONENTMANAGER_CID);
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// EndListener - This listener is the final one in the chain. It
|
|
|
|
// receives the fully converted data, although it doesn't do anything with
|
|
|
|
// the data.
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
class EndListener : public nsIStreamListener {
|
|
|
|
public:
|
|
|
|
// nsISupports declaration
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
EndListener() {NS_INIT_ISUPPORTS();};
|
|
|
|
|
|
|
|
// nsIStreamListener method
|
2001-02-21 23:38:08 +03:00
|
|
|
NS_IMETHOD OnDataAvailable(nsIRequest* request, nsISupports *ctxt, nsIInputStream *inStr,
|
1999-08-25 02:04:05 +04:00
|
|
|
PRUint32 sourceOffset, PRUint32 count)
|
|
|
|
{
|
1999-08-29 18:11:41 +04:00
|
|
|
nsresult rv;
|
|
|
|
PRUint32 read, len;
|
1999-09-10 02:05:05 +04:00
|
|
|
rv = inStr->Available(&len);
|
1999-08-29 18:11:41 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2000-06-03 13:46:12 +04:00
|
|
|
char *buffer = (char*)nsMemory::Alloc(len + 1);
|
1999-08-29 18:11:41 +04:00
|
|
|
if (!buffer) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
rv = inStr->Read(buffer, len, &read);
|
|
|
|
buffer[len] = '\0';
|
1999-09-10 02:05:05 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
1999-10-07 08:10:12 +04:00
|
|
|
printf("CONTEXT %p: Received %u bytes and the following data: \n %s\n\n", ctxt, read, buffer);
|
1999-09-10 02:05:05 +04:00
|
|
|
}
|
2000-06-03 13:46:12 +04:00
|
|
|
nsMemory::Free(buffer);
|
1999-08-29 18:11:41 +04:00
|
|
|
|
1999-08-25 02:04:05 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-04-10 10:01:08 +04:00
|
|
|
// nsIRequestObserver methods
|
2001-03-08 03:41:53 +03:00
|
|
|
NS_IMETHOD OnStartRequest(nsIRequest* request, nsISupports *ctxt) { return NS_OK; }
|
1999-08-25 02:04:05 +04:00
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
NS_IMETHOD OnStopRequest(nsIRequest* request, nsISupports *ctxt,
|
2001-04-10 10:01:08 +04:00
|
|
|
nsresult aStatus) { return NS_OK; }
|
1999-08-25 02:04:05 +04:00
|
|
|
};
|
|
|
|
|
2000-11-17 06:45:13 +03:00
|
|
|
NS_IMPL_ISUPPORTS1(EndListener, nsIStreamListener);
|
1999-08-25 02:04:05 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// EndListener END
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
nsresult SendData(const char * aData, nsIStreamListener* aListener, nsIRequest* request) {
|
2000-04-25 09:40:45 +04:00
|
|
|
nsString data;
|
|
|
|
data.AssignWithConversion(aData);
|
2001-03-08 03:41:53 +03:00
|
|
|
nsCOMPtr<nsISupports> dataSup;
|
|
|
|
nsresult rv = NS_NewStringInputStream(getter_AddRefs(dataSup), data);
|
2000-03-02 23:48:40 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2001-03-08 03:41:53 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> dataStream(do_QueryInterface(dataSup));
|
2001-02-21 23:38:08 +03:00
|
|
|
return aListener->OnDataAvailable(request, nsnull, dataStream, 0, -1);
|
2000-03-02 23:48:40 +03:00
|
|
|
}
|
2001-03-08 03:41:53 +03:00
|
|
|
#define SEND_DATA(x) SendData(x, converterListener, request)
|
2000-03-02 23:48:40 +03:00
|
|
|
|
1999-08-25 02:04:05 +04:00
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// Create the Event Queue for this thread...
|
2001-07-25 11:54:28 +04:00
|
|
|
nsCOMPtr<nsIEventQueueService> eventQService =
|
|
|
|
do_GetService(kEventQueueServiceCID, &rv);
|
1999-08-25 02:04:05 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = eventQService->CreateThreadEventQueue();
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
1999-11-30 03:32:43 +03:00
|
|
|
eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
|
1999-08-25 02:04:05 +04:00
|
|
|
|
|
|
|
rv = nsComponentManager::AutoRegister(nsIComponentManager::NS_Startup, NULL /* default */);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2001-03-08 03:41:53 +03:00
|
|
|
nsCOMPtr<nsICategoryManager> catman =
|
|
|
|
do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
nsXPIDLCString previous;
|
|
|
|
|
1999-08-25 02:04:05 +04:00
|
|
|
///////////////////////////////////////////
|
|
|
|
// BEGIN - Stream converter registration
|
2001-03-08 03:41:53 +03:00
|
|
|
// All stream converters must register with the ComponentManager
|
1999-08-25 02:04:05 +04:00
|
|
|
///////////////////////////////////////////
|
|
|
|
|
2001-03-08 03:41:53 +03:00
|
|
|
// these stream converters are just for testing. running this harness
|
|
|
|
// from the dist/bin dir will also pickup converters registered
|
|
|
|
// in other modules (necko converters for example).
|
|
|
|
|
|
|
|
PRUint32 converterListSize = 7;
|
|
|
|
char *converterList[] = {
|
|
|
|
"?from=a/foo&to=b/foo",
|
|
|
|
"?from=b/foo&to=c/foo",
|
|
|
|
"?from=b/foo&to=d/foo",
|
|
|
|
"?from=c/foo&to=d/foo",
|
|
|
|
"?from=d/foo&to=e/foo",
|
|
|
|
"?from=d/foo&to=f/foo",
|
|
|
|
"?from=t/foo&to=k/foo",
|
|
|
|
};
|
1999-08-25 02:04:05 +04:00
|
|
|
|
2001-03-08 03:41:53 +03:00
|
|
|
TestConverterFactory *convFactory = new TestConverterFactory(kTestConverterCID, "TestConverter", NS_ISTREAMCONVERTER_KEY);
|
|
|
|
nsCOMPtr<nsIFactory> convFactSup(do_QueryInterface(convFactory, &rv));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
PRUint32 count = 0;
|
|
|
|
while (count < converterListSize) {
|
|
|
|
// register the TestConverter with the component manager. One contractid registration
|
|
|
|
// per conversion pair (from - to pair).
|
|
|
|
nsCString contractID(NS_ISTREAMCONVERTER_KEY);
|
|
|
|
contractID.Append(converterList[count]);
|
|
|
|
rv = nsComponentManager::RegisterFactory(kTestConverterCID,
|
|
|
|
"TestConverter",
|
|
|
|
contractID.get(),
|
|
|
|
convFactSup,
|
|
|
|
PR_TRUE);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
rv = catman->AddCategoryEntry(NS_ISTREAMCONVERTER_KEY, converterList[count], "x",
|
|
|
|
PR_TRUE, PR_TRUE, getter_Copies(previous));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
count++;
|
|
|
|
}
|
1999-11-11 03:39:21 +03:00
|
|
|
|
2001-07-25 11:54:28 +04:00
|
|
|
nsCOMPtr<nsIStreamConverterService> StreamConvService =
|
|
|
|
do_GetService(kStreamConverterServiceCID, &rv);
|
1999-08-25 02:04:05 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2001-03-08 03:41:53 +03:00
|
|
|
// Define the *from* content type and *to* content-type for conversion.
|
2000-04-25 09:40:45 +04:00
|
|
|
nsString fromStr;
|
2001-03-08 03:41:53 +03:00
|
|
|
fromStr.AssignWithConversion("a/foo");
|
2000-04-25 09:40:45 +04:00
|
|
|
nsString toStr;
|
2001-03-08 03:41:53 +03:00
|
|
|
toStr.AssignWithConversion("c/foo");
|
2000-04-25 09:40:45 +04:00
|
|
|
|
1999-08-25 02:04:05 +04:00
|
|
|
#ifdef ASYNC_TEST
|
|
|
|
// ASYNCRONOUS conversion
|
|
|
|
|
1999-08-29 18:11:41 +04:00
|
|
|
|
2001-03-08 03:41:53 +03:00
|
|
|
// Build up a channel that represents the content we're
|
|
|
|
// starting the transaction with.
|
|
|
|
//
|
|
|
|
// sample multipart mixed content-type string:
|
|
|
|
// "multipart/x-mixed-replacE;boundary=thisrandomstring"
|
|
|
|
/*nsCOMPtr<nsIChannel> channel;
|
1999-11-11 03:39:21 +03:00
|
|
|
nsCOMPtr<nsIURI> dummyURI;
|
2001-03-08 03:41:53 +03:00
|
|
|
rv = NS_NewURI(getter_AddRefs(dummyURI), "http://meaningless");
|
1999-08-29 18:11:41 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2001-03-08 03:41:53 +03:00
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
rv = NS_NewInputStreamChannel(getter_AddRefs(channel),
|
2000-03-29 07:58:50 +04:00
|
|
|
dummyURI,
|
|
|
|
nsnull, // inStr
|
2001-03-08 03:41:53 +03:00
|
|
|
"text/plain", // content-type
|
2000-03-29 07:58:50 +04:00
|
|
|
-1); // XXX fix contentLength
|
1999-08-29 18:11:41 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2001-03-08 03:41:53 +03:00
|
|
|
nsCOMPtr<nsIRequest> request(do_QueryInterface(channel));*/
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRequest> request;
|
1999-08-29 18:11:41 +04:00
|
|
|
|
1999-08-25 02:04:05 +04:00
|
|
|
// setup a listener to receive the converted data. This guy is the end
|
|
|
|
// listener in the chain, he wants the fully converted (toType) data.
|
|
|
|
// An example of this listener in mozilla would be the DocLoader.
|
2001-03-08 03:41:53 +03:00
|
|
|
nsIStreamListener *dataReceiver = new EndListener();
|
|
|
|
NS_ADDREF(dataReceiver);
|
1999-08-25 02:04:05 +04:00
|
|
|
|
|
|
|
// setup a listener to push the data into. This listener sits inbetween the
|
|
|
|
// unconverted data of fromType, and the final listener in the chain (in this case
|
|
|
|
// the dataReceiver.
|
2001-03-08 03:41:53 +03:00
|
|
|
nsIStreamListener *converterListener = nsnull;
|
2001-06-30 15:02:25 +04:00
|
|
|
rv = StreamConvService->AsyncConvertData(fromStr.get(), toStr.get(),
|
2001-03-08 03:41:53 +03:00
|
|
|
dataReceiver, nsnull, &converterListener);
|
1999-08-25 02:04:05 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2001-03-08 03:41:53 +03:00
|
|
|
NS_RELEASE(dataReceiver);
|
1999-08-25 02:04:05 +04:00
|
|
|
|
|
|
|
// at this point we have a stream listener to push data to, and the one
|
|
|
|
// that will receive the converted data. Let's mimic On*() calls and get the conversion
|
|
|
|
// going. Typically these On*() calls would be made inside their respective wrappers On*()
|
|
|
|
// methods.
|
2001-03-08 03:41:53 +03:00
|
|
|
rv = converterListener->OnStartRequest(request, nsnull);
|
1999-08-29 18:11:41 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
|
2001-03-08 03:41:53 +03:00
|
|
|
rv = SEND_DATA("aaa");
|
1999-08-25 02:04:05 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2000-03-02 23:48:40 +03:00
|
|
|
|
2001-03-08 03:41:53 +03:00
|
|
|
rv = SEND_DATA("aaa");
|
2000-03-02 23:48:40 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// Finish the request.
|
2001-04-10 10:01:08 +04:00
|
|
|
rv = converterListener->OnStopRequest(request, nsnull, rv);
|
1999-08-29 18:11:41 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2001-03-08 03:41:53 +03:00
|
|
|
NS_RELEASE(converterListener);
|
|
|
|
|
1999-08-29 18:11:41 +04:00
|
|
|
|
1999-08-25 02:04:05 +04:00
|
|
|
#else
|
|
|
|
// SYNCRONOUS conversion
|
2001-03-08 03:41:53 +03:00
|
|
|
nsCOMPtr<nsIInputStream> convertedData;
|
2001-06-30 15:02:25 +04:00
|
|
|
rv = StreamConvService->Convert(inputData, fromStr.get(), toStr.get(),
|
2001-03-08 03:41:53 +03:00
|
|
|
nsnull, getter_AddRefs(convertedData));
|
1999-08-25 02:04:05 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2001-03-08 03:41:53 +03:00
|
|
|
#endif
|
1999-08-25 02:04:05 +04:00
|
|
|
|
|
|
|
// Enter the message pump to allow the URL load to proceed.
|
|
|
|
while ( gKeepRunning ) {
|
2001-04-19 23:43:43 +04:00
|
|
|
#ifdef XP_WIN
|
1999-08-25 02:04:05 +04:00
|
|
|
MSG msg;
|
|
|
|
|
|
|
|
if (GetMessage(&msg, NULL, 0, 0)) {
|
|
|
|
TranslateMessage(&msg);
|
|
|
|
DispatchMessage(&msg);
|
2001-04-19 23:43:43 +04:00
|
|
|
} else
|
|
|
|
gKeepRunning = PR_FALSE;
|
1999-08-25 02:04:05 +04:00
|
|
|
#else
|
|
|
|
#ifdef XP_MAC
|
2001-04-19 23:43:43 +04:00
|
|
|
/* Mac stuff is missing here! */
|
2000-02-29 18:40:42 +03:00
|
|
|
#else
|
|
|
|
#ifdef XP_OS2
|
2001-04-19 23:43:43 +04:00
|
|
|
QMSG qmsg;
|
2000-02-29 18:40:42 +03:00
|
|
|
|
2001-04-19 23:43:43 +04:00
|
|
|
if (WinGetMsg(0, &qmsg, 0, 0, 0))
|
|
|
|
WinDispatchMsg(0, &qmsg);
|
|
|
|
else
|
|
|
|
gKeepRunning = PR_FALSE;
|
|
|
|
#else
|
|
|
|
#ifdef XP_UNIX
|
|
|
|
PLEvent *gEvent;
|
|
|
|
rv = gEventQ->GetEvent(&gEvent);
|
|
|
|
rv = gEventQ->HandleEvent(gEvent);
|
|
|
|
/* gKeepRunning = PR_FALSE; */
|
1999-08-25 02:04:05 +04:00
|
|
|
#else
|
2001-04-19 23:43:43 +04:00
|
|
|
/* Other stuff is missing here! */
|
1999-08-25 02:04:05 +04:00
|
|
|
#endif /* XP_UNIX */
|
2000-02-29 18:40:42 +03:00
|
|
|
#endif /* XP_OS2 */
|
2001-04-19 23:43:43 +04:00
|
|
|
#endif /* XP_MAC */
|
|
|
|
#endif /* XP_WIN */
|
1999-08-25 02:04:05 +04:00
|
|
|
}
|
|
|
|
|
2001-03-08 03:41:53 +03:00
|
|
|
//return NS_ShutdownXPCOM(NULL);
|
2001-04-19 23:43:43 +04:00
|
|
|
return rv;
|
1999-08-25 02:04:05 +04:00
|
|
|
}
|