1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "TestCommon.h"
12 #include "nsIComponentRegistrar.h"
13 #include "nsIIOService.h"
14 #include "nsIServiceManager.h"
15 #include "nsNetUtil.h"
17 #include "nsIUploadChannel.h"
19 static NS_DEFINE_CID(kIOServiceCID
, NS_IOSERVICE_CID
);
22 #if defined(PR_LOGGING)
24 // set NSPR_LOG_MODULES=Test:5
26 static PRLogModuleInfo
*gTestLog
= nullptr;
28 #define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args)
30 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
34 class InputTestConsumer
: public nsIStreamListener
36 virtual ~InputTestConsumer();
43 NS_DECL_NSIREQUESTOBSERVER
44 NS_DECL_NSISTREAMLISTENER
47 InputTestConsumer::InputTestConsumer()
51 InputTestConsumer::~InputTestConsumer()
55 NS_IMPL_ISUPPORTS(InputTestConsumer
,
60 InputTestConsumer::OnStartRequest(nsIRequest
*request
, nsISupports
* context
)
62 LOG(("InputTestConsumer::OnStartRequest\n"));
67 InputTestConsumer::OnDataAvailable(nsIRequest
*request
,
69 nsIInputStream
*aIStream
,
70 uint64_t aSourceOffset
,
78 size
= std::min
<uint32_t>(aLength
, sizeof(buf
));
79 rv
= aIStream
->Read(buf
, size
, &amt
);
81 NS_ASSERTION((NS_BASE_STREAM_WOULD_BLOCK
!= rv
),
82 "The stream should never block.");
92 InputTestConsumer::OnStopRequest(nsIRequest
*request
, nsISupports
* context
,
95 LOG(("InputTestConsumer::OnStopRequest [status=%x]\n", aStatus
));
102 main(int argc
, char* argv
[])
104 if (test_common_init(&argc
, &argv
) != 0)
110 printf("usage: %s <url> <file-to-upload>\n", argv
[0]);
113 char* uriSpec
= argv
[1];
114 char* fileName
= argv
[2];
116 #if defined(PR_LOGGING)
117 gTestLog
= PR_NewLogModule("Test");
121 nsCOMPtr
<nsIServiceManager
> servMan
;
122 NS_InitXPCOM2(getter_AddRefs(servMan
), nullptr, nullptr);
124 // first thing to do is create ourselves a stream that
125 // is to be uploaded.
126 nsCOMPtr
<nsIInputStream
> uploadStream
;
127 rv
= NS_NewPostDataStream(getter_AddRefs(uploadStream
),
129 nsDependentCString(fileName
)); // XXX UTF-8
130 if (NS_FAILED(rv
)) return -1;
132 nsCOMPtr
<nsIIOService
> ioService(do_GetService(kIOServiceCID
, &rv
));
135 nsCOMPtr
<nsIURI
> uri
;
136 rv
= NS_NewURI(getter_AddRefs(uri
), uriSpec
);
137 if (NS_FAILED(rv
)) return -1;
139 nsCOMPtr
<nsIChannel
> channel
;
140 rv
= ioService
->NewChannelFromURI(uri
, getter_AddRefs(channel
));
141 if (NS_FAILED(rv
)) return -1;
143 // QI and set the upload stream
144 nsCOMPtr
<nsIUploadChannel
> uploadChannel(do_QueryInterface(channel
));
145 uploadChannel
->SetUploadStream(uploadStream
, EmptyCString(), -1);
147 // create a dummy listener
148 InputTestConsumer
* listener
;
150 listener
= new InputTestConsumer
;
152 NS_ERROR("Failed to create a new stream listener!");
157 channel
->AsyncOpen(listener
, nullptr);
160 } // this scopes the nsCOMPtrs
161 // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
162 rv
= NS_ShutdownXPCOM(nullptr);
163 NS_ASSERTION(NS_SUCCEEDED(rv
), "NS_ShutdownXPCOM failed");