michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "TestCommon.h" michael@0: #include michael@0: #ifdef WIN32 michael@0: #include michael@0: #endif michael@0: michael@0: #include "nsIComponentRegistrar.h" michael@0: #include "nsIIOService.h" michael@0: #include "nsIServiceManager.h" michael@0: #include "nsNetUtil.h" michael@0: michael@0: #include "nsIUploadChannel.h" michael@0: michael@0: static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID); michael@0: michael@0: #include "prlog.h" michael@0: #if defined(PR_LOGGING) michael@0: // michael@0: // set NSPR_LOG_MODULES=Test:5 michael@0: // michael@0: static PRLogModuleInfo *gTestLog = nullptr; michael@0: #endif michael@0: #define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args) michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: // InputTestConsumer michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: class InputTestConsumer : public nsIStreamListener michael@0: { michael@0: public: michael@0: michael@0: InputTestConsumer(); michael@0: virtual ~InputTestConsumer(); michael@0: michael@0: NS_DECL_ISUPPORTS michael@0: NS_DECL_NSIREQUESTOBSERVER michael@0: NS_DECL_NSISTREAMLISTENER michael@0: }; michael@0: michael@0: InputTestConsumer::InputTestConsumer() michael@0: { michael@0: } michael@0: michael@0: InputTestConsumer::~InputTestConsumer() michael@0: { michael@0: } michael@0: michael@0: NS_IMPL_ISUPPORTS(InputTestConsumer, michael@0: nsIStreamListener, michael@0: nsIRequestObserver) michael@0: michael@0: NS_IMETHODIMP michael@0: InputTestConsumer::OnStartRequest(nsIRequest *request, nsISupports* context) michael@0: { michael@0: LOG(("InputTestConsumer::OnStartRequest\n")); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: InputTestConsumer::OnDataAvailable(nsIRequest *request, michael@0: nsISupports* context, michael@0: nsIInputStream *aIStream, michael@0: uint64_t aSourceOffset, michael@0: uint32_t aLength) michael@0: { michael@0: char buf[1025]; michael@0: uint32_t amt, size; michael@0: nsresult rv; michael@0: michael@0: while (aLength) { michael@0: size = std::min(aLength, sizeof(buf)); michael@0: rv = aIStream->Read(buf, size, &amt); michael@0: if (NS_FAILED(rv)) { michael@0: NS_ASSERTION((NS_BASE_STREAM_WOULD_BLOCK != rv), michael@0: "The stream should never block."); michael@0: return rv; michael@0: } michael@0: aLength -= amt; michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP michael@0: InputTestConsumer::OnStopRequest(nsIRequest *request, nsISupports* context, michael@0: nsresult aStatus) michael@0: { michael@0: LOG(("InputTestConsumer::OnStopRequest [status=%x]\n", aStatus)); michael@0: QuitPumpingEvents(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: int michael@0: main(int argc, char* argv[]) michael@0: { michael@0: if (test_common_init(&argc, &argv) != 0) michael@0: return -1; michael@0: michael@0: nsresult rv; michael@0: michael@0: if (argc < 2) { michael@0: printf("usage: %s \n", argv[0]); michael@0: return -1; michael@0: } michael@0: char* uriSpec = argv[1]; michael@0: char* fileName = argv[2]; michael@0: michael@0: #if defined(PR_LOGGING) michael@0: gTestLog = PR_NewLogModule("Test"); michael@0: #endif michael@0: michael@0: { michael@0: nsCOMPtr servMan; michael@0: NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr); michael@0: michael@0: // first thing to do is create ourselves a stream that michael@0: // is to be uploaded. michael@0: nsCOMPtr uploadStream; michael@0: rv = NS_NewPostDataStream(getter_AddRefs(uploadStream), michael@0: true, michael@0: nsDependentCString(fileName)); // XXX UTF-8 michael@0: if (NS_FAILED(rv)) return -1; michael@0: michael@0: nsCOMPtr ioService(do_GetService(kIOServiceCID, &rv)); michael@0: michael@0: // create our url. michael@0: nsCOMPtr uri; michael@0: rv = NS_NewURI(getter_AddRefs(uri), uriSpec); michael@0: if (NS_FAILED(rv)) return -1; michael@0: michael@0: nsCOMPtr channel; michael@0: rv = ioService->NewChannelFromURI(uri, getter_AddRefs(channel)); michael@0: if (NS_FAILED(rv)) return -1; michael@0: michael@0: // QI and set the upload stream michael@0: nsCOMPtr uploadChannel(do_QueryInterface(channel)); michael@0: uploadChannel->SetUploadStream(uploadStream, EmptyCString(), -1); michael@0: michael@0: // create a dummy listener michael@0: InputTestConsumer* listener; michael@0: michael@0: listener = new InputTestConsumer; michael@0: if (!listener) { michael@0: NS_ERROR("Failed to create a new stream listener!"); michael@0: return -1; michael@0: } michael@0: NS_ADDREF(listener); michael@0: michael@0: channel->AsyncOpen(listener, nullptr); michael@0: michael@0: PumpEvents(); michael@0: } // this scopes the nsCOMPtrs michael@0: // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM michael@0: rv = NS_ShutdownXPCOM(nullptr); michael@0: NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed"); michael@0: michael@0: return 0; michael@0: } michael@0: