1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "TestCommon.h"
43 #include "nsIComponentRegistrar.h"
44 #include "nsIIOService.h"
45 #include "nsIServiceManager.h"
46 #include "nsNetUtil.h"
48 #include "nsIUploadChannel.h"
50 static NS_DEFINE_CID(kIOServiceCID
, NS_IOSERVICE_CID
);
53 #if defined(PR_LOGGING)
55 // set NSPR_LOG_MODULES=Test:5
57 static PRLogModuleInfo
*gTestLog
= nsnull
;
59 #define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args)
61 //-----------------------------------------------------------------------------
63 //-----------------------------------------------------------------------------
65 class InputTestConsumer
: public nsIStreamListener
70 virtual ~InputTestConsumer();
73 NS_DECL_NSIREQUESTOBSERVER
74 NS_DECL_NSISTREAMLISTENER
77 InputTestConsumer::InputTestConsumer()
81 InputTestConsumer::~InputTestConsumer()
85 NS_IMPL_ISUPPORTS2(InputTestConsumer
,
90 InputTestConsumer::OnStartRequest(nsIRequest
*request
, nsISupports
* context
)
92 LOG(("InputTestConsumer::OnStartRequest\n"));
97 InputTestConsumer::OnDataAvailable(nsIRequest
*request
,
99 nsIInputStream
*aIStream
,
100 PRUint32 aSourceOffset
,
108 size
= PR_MIN(aLength
, sizeof(buf
));
109 rv
= aIStream
->Read(buf
, size
, &amt
);
111 NS_ASSERTION((NS_BASE_STREAM_WOULD_BLOCK
!= rv
),
112 "The stream should never block.");
122 InputTestConsumer::OnStopRequest(nsIRequest
*request
, nsISupports
* context
,
125 LOG(("InputTestConsumer::OnStopRequest [status=%x]\n", aStatus
));
132 main(int argc
, char* argv
[])
134 if (test_common_init(&argc
, &argv
) != 0)
140 printf("usage: %s <url> <file-to-upload>\n", argv
[0]);
143 char* uriSpec
= argv
[1];
144 char* fileName
= argv
[2];
146 #if defined(PR_LOGGING)
147 gTestLog
= PR_NewLogModule("Test");
151 nsCOMPtr
<nsIServiceManager
> servMan
;
152 NS_InitXPCOM2(getter_AddRefs(servMan
), nsnull
, nsnull
);
153 nsCOMPtr
<nsIComponentRegistrar
> registrar
= do_QueryInterface(servMan
);
154 NS_ASSERTION(registrar
, "Null nsIComponentRegistrar");
156 registrar
->AutoRegister(nsnull
);
158 nsCOMPtr
<nsIIOService
> ioService(do_GetService(kIOServiceCID
, &rv
));
159 // first thing to do is create ourselves a stream that
160 // is to be uploaded.
161 nsCOMPtr
<nsIInputStream
> uploadStream
;
162 rv
= NS_NewPostDataStream(getter_AddRefs(uploadStream
),
164 nsDependentCString(fileName
), // XXX UTF-8
166 if (NS_FAILED(rv
)) return rv
;
169 nsCOMPtr
<nsIURI
> uri
;
170 rv
= NS_NewURI(getter_AddRefs(uri
), uriSpec
);
171 if (NS_FAILED(rv
)) return rv
;
173 nsCOMPtr
<nsIChannel
> channel
;
174 rv
= ioService
->NewChannelFromURI(uri
, getter_AddRefs(channel
));
175 if (NS_FAILED(rv
)) return rv
;
177 // QI and set the upload stream
178 nsCOMPtr
<nsIUploadChannel
> uploadChannel(do_QueryInterface(channel
));
179 uploadChannel
->SetUploadStream(uploadStream
, EmptyCString(), -1);
181 // create a dummy listener
182 InputTestConsumer
* listener
;
184 listener
= new InputTestConsumer
;
186 NS_ERROR("Failed to create a new stream listener!");
191 channel
->AsyncOpen(listener
, nsnull
);
194 } // this scopes the nsCOMPtrs
195 // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
196 rv
= NS_ShutdownXPCOM(nsnull
);
197 NS_ASSERTION(NS_SUCCEEDED(rv
), "NS_ShutdownXPCOM failed");