netwerk/test/TestUpload.cpp

Wed, 31 Dec 2014 06:55:46 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:46 +0100
changeset 1
ca08bd8f51b2
permissions
-rw-r--r--

Added tag TORBROWSER_REPLICA for changeset 6474c204b198

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "TestCommon.h"
michael@0 7 #include <algorithm>
michael@0 8 #ifdef WIN32
michael@0 9 #include <windows.h>
michael@0 10 #endif
michael@0 11
michael@0 12 #include "nsIComponentRegistrar.h"
michael@0 13 #include "nsIIOService.h"
michael@0 14 #include "nsIServiceManager.h"
michael@0 15 #include "nsNetUtil.h"
michael@0 16
michael@0 17 #include "nsIUploadChannel.h"
michael@0 18
michael@0 19 static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID);
michael@0 20
michael@0 21 #include "prlog.h"
michael@0 22 #if defined(PR_LOGGING)
michael@0 23 //
michael@0 24 // set NSPR_LOG_MODULES=Test:5
michael@0 25 //
michael@0 26 static PRLogModuleInfo *gTestLog = nullptr;
michael@0 27 #endif
michael@0 28 #define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args)
michael@0 29
michael@0 30 //-----------------------------------------------------------------------------
michael@0 31 // InputTestConsumer
michael@0 32 //-----------------------------------------------------------------------------
michael@0 33
michael@0 34 class InputTestConsumer : public nsIStreamListener
michael@0 35 {
michael@0 36 public:
michael@0 37
michael@0 38 InputTestConsumer();
michael@0 39 virtual ~InputTestConsumer();
michael@0 40
michael@0 41 NS_DECL_ISUPPORTS
michael@0 42 NS_DECL_NSIREQUESTOBSERVER
michael@0 43 NS_DECL_NSISTREAMLISTENER
michael@0 44 };
michael@0 45
michael@0 46 InputTestConsumer::InputTestConsumer()
michael@0 47 {
michael@0 48 }
michael@0 49
michael@0 50 InputTestConsumer::~InputTestConsumer()
michael@0 51 {
michael@0 52 }
michael@0 53
michael@0 54 NS_IMPL_ISUPPORTS(InputTestConsumer,
michael@0 55 nsIStreamListener,
michael@0 56 nsIRequestObserver)
michael@0 57
michael@0 58 NS_IMETHODIMP
michael@0 59 InputTestConsumer::OnStartRequest(nsIRequest *request, nsISupports* context)
michael@0 60 {
michael@0 61 LOG(("InputTestConsumer::OnStartRequest\n"));
michael@0 62 return NS_OK;
michael@0 63 }
michael@0 64
michael@0 65 NS_IMETHODIMP
michael@0 66 InputTestConsumer::OnDataAvailable(nsIRequest *request,
michael@0 67 nsISupports* context,
michael@0 68 nsIInputStream *aIStream,
michael@0 69 uint64_t aSourceOffset,
michael@0 70 uint32_t aLength)
michael@0 71 {
michael@0 72 char buf[1025];
michael@0 73 uint32_t amt, size;
michael@0 74 nsresult rv;
michael@0 75
michael@0 76 while (aLength) {
michael@0 77 size = std::min<uint32_t>(aLength, sizeof(buf));
michael@0 78 rv = aIStream->Read(buf, size, &amt);
michael@0 79 if (NS_FAILED(rv)) {
michael@0 80 NS_ASSERTION((NS_BASE_STREAM_WOULD_BLOCK != rv),
michael@0 81 "The stream should never block.");
michael@0 82 return rv;
michael@0 83 }
michael@0 84 aLength -= amt;
michael@0 85 }
michael@0 86 return NS_OK;
michael@0 87 }
michael@0 88
michael@0 89
michael@0 90 NS_IMETHODIMP
michael@0 91 InputTestConsumer::OnStopRequest(nsIRequest *request, nsISupports* context,
michael@0 92 nsresult aStatus)
michael@0 93 {
michael@0 94 LOG(("InputTestConsumer::OnStopRequest [status=%x]\n", aStatus));
michael@0 95 QuitPumpingEvents();
michael@0 96 return NS_OK;
michael@0 97 }
michael@0 98
michael@0 99
michael@0 100 int
michael@0 101 main(int argc, char* argv[])
michael@0 102 {
michael@0 103 if (test_common_init(&argc, &argv) != 0)
michael@0 104 return -1;
michael@0 105
michael@0 106 nsresult rv;
michael@0 107
michael@0 108 if (argc < 2) {
michael@0 109 printf("usage: %s <url> <file-to-upload>\n", argv[0]);
michael@0 110 return -1;
michael@0 111 }
michael@0 112 char* uriSpec = argv[1];
michael@0 113 char* fileName = argv[2];
michael@0 114
michael@0 115 #if defined(PR_LOGGING)
michael@0 116 gTestLog = PR_NewLogModule("Test");
michael@0 117 #endif
michael@0 118
michael@0 119 {
michael@0 120 nsCOMPtr<nsIServiceManager> servMan;
michael@0 121 NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
michael@0 122
michael@0 123 // first thing to do is create ourselves a stream that
michael@0 124 // is to be uploaded.
michael@0 125 nsCOMPtr<nsIInputStream> uploadStream;
michael@0 126 rv = NS_NewPostDataStream(getter_AddRefs(uploadStream),
michael@0 127 true,
michael@0 128 nsDependentCString(fileName)); // XXX UTF-8
michael@0 129 if (NS_FAILED(rv)) return -1;
michael@0 130
michael@0 131 nsCOMPtr<nsIIOService> ioService(do_GetService(kIOServiceCID, &rv));
michael@0 132
michael@0 133 // create our url.
michael@0 134 nsCOMPtr<nsIURI> uri;
michael@0 135 rv = NS_NewURI(getter_AddRefs(uri), uriSpec);
michael@0 136 if (NS_FAILED(rv)) return -1;
michael@0 137
michael@0 138 nsCOMPtr<nsIChannel> channel;
michael@0 139 rv = ioService->NewChannelFromURI(uri, getter_AddRefs(channel));
michael@0 140 if (NS_FAILED(rv)) return -1;
michael@0 141
michael@0 142 // QI and set the upload stream
michael@0 143 nsCOMPtr<nsIUploadChannel> uploadChannel(do_QueryInterface(channel));
michael@0 144 uploadChannel->SetUploadStream(uploadStream, EmptyCString(), -1);
michael@0 145
michael@0 146 // create a dummy listener
michael@0 147 InputTestConsumer* listener;
michael@0 148
michael@0 149 listener = new InputTestConsumer;
michael@0 150 if (!listener) {
michael@0 151 NS_ERROR("Failed to create a new stream listener!");
michael@0 152 return -1;
michael@0 153 }
michael@0 154 NS_ADDREF(listener);
michael@0 155
michael@0 156 channel->AsyncOpen(listener, nullptr);
michael@0 157
michael@0 158 PumpEvents();
michael@0 159 } // this scopes the nsCOMPtrs
michael@0 160 // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
michael@0 161 rv = NS_ShutdownXPCOM(nullptr);
michael@0 162 NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
michael@0 163
michael@0 164 return 0;
michael@0 165 }
michael@0 166

mercurial