Back to index

lightning-sunbird  0.9+nobinonly
TestUpload.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "TestCommon.h"
00039 #ifdef WIN32 
00040 #include <windows.h>
00041 #endif
00042 
00043 #include "nsIComponentRegistrar.h"
00044 #include "nsIEventQueueService.h"
00045 #include "nsIIOService.h"
00046 #include "nsIServiceManager.h"
00047 #include "nsNetUtil.h"
00048 
00049 #include "nsIUploadChannel.h"
00050 
00051 static NS_DEFINE_CID(kEventQueueServiceCID,      NS_EVENTQUEUESERVICE_CID);
00052 static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID);
00053 
00054 #include "prlog.h"
00055 #if defined(PR_LOGGING)
00056 //
00057 // set NSPR_LOG_MODULES=Test:5
00058 //
00059 static PRLogModuleInfo *gTestLog = nsnull;
00060 #endif
00061 #define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args)
00062 
00063 static int gKeepRunning = 1;
00064 static nsIEventQueue* gEventQ = nsnull;
00065 
00066 //-----------------------------------------------------------------------------
00067 // InputTestConsumer
00068 //-----------------------------------------------------------------------------
00069 
00070 class InputTestConsumer : public nsIStreamListener
00071 {
00072 public:
00073 
00074   InputTestConsumer();
00075   virtual ~InputTestConsumer();
00076 
00077   NS_DECL_ISUPPORTS
00078   NS_DECL_NSIREQUESTOBSERVER
00079   NS_DECL_NSISTREAMLISTENER
00080 };
00081 
00082 InputTestConsumer::InputTestConsumer()
00083 {
00084 }
00085 
00086 InputTestConsumer::~InputTestConsumer()
00087 {
00088 }
00089 
00090 NS_IMPL_ISUPPORTS1(InputTestConsumer, nsIStreamListener)
00091 
00092 NS_IMETHODIMP
00093 InputTestConsumer::OnStartRequest(nsIRequest *request, nsISupports* context)
00094 {
00095   LOG(("InputTestConsumer::OnStartRequest\n"));
00096   return NS_OK;
00097 }
00098 
00099 NS_IMETHODIMP
00100 InputTestConsumer::OnDataAvailable(nsIRequest *request, 
00101                                    nsISupports* context,
00102                                    nsIInputStream *aIStream, 
00103                                    PRUint32 aSourceOffset,
00104                                    PRUint32 aLength)
00105 {
00106   char buf[1025];
00107   PRUint32 amt, size;
00108   nsresult rv;
00109 
00110   while (aLength) {
00111     size = PR_MIN(aLength, sizeof(buf));
00112     rv = aIStream->Read(buf, size, &amt);
00113     if (NS_FAILED(rv)) {
00114       NS_ASSERTION((NS_BASE_STREAM_WOULD_BLOCK != rv), 
00115                    "The stream should never block.");
00116       return rv;
00117     }
00118     aLength -= amt;
00119   }
00120   return NS_OK;
00121 }
00122 
00123 
00124 NS_IMETHODIMP
00125 InputTestConsumer::OnStopRequest(nsIRequest *request, nsISupports* context,
00126                                  nsresult aStatus)
00127 {
00128     LOG(("InputTestConsumer::OnStopRequest [status=%x]\n", aStatus));
00129     gKeepRunning = PR_FALSE;
00130     return NS_OK;
00131 }
00132 
00133 
00134 int
00135 main(int argc, char* argv[])
00136 {
00137     if (test_common_init(&argc, &argv) != 0)
00138         return -1;
00139 
00140     nsresult rv;
00141 
00142     if (argc < 2) {
00143         printf("usage: %s <url> <file-to-upload>\n", argv[0]);
00144         return -1;
00145     }
00146     char* uriSpec  = argv[1];
00147     char* fileName = argv[2];
00148 
00149 #if defined(PR_LOGGING) 
00150     gTestLog = PR_NewLogModule("Test");
00151 #endif
00152 
00153     {
00154         nsCOMPtr<nsIServiceManager> servMan;
00155         NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
00156         nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
00157         NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
00158         if (registrar)
00159             registrar->AutoRegister(nsnull);
00160 
00161         // Create the Event Queue for this thread...
00162         nsCOMPtr<nsIEventQueueService> eventQService =
00163                  do_GetService(kEventQueueServiceCID, &rv);
00164         if (NS_FAILED(rv)) return rv;
00165 
00166         eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
00167 
00168         nsCOMPtr<nsIIOService> ioService(do_GetService(kIOServiceCID, &rv));
00169         // first thing to do is create ourselves a stream that
00170         // is to be uploaded.
00171         nsCOMPtr<nsIInputStream> uploadStream;
00172         rv = NS_NewPostDataStream(getter_AddRefs(uploadStream),
00173                                   PR_TRUE,
00174                                   nsDependentCString(fileName), // XXX UTF-8
00175                                   0, ioService);
00176         if (NS_FAILED(rv)) return rv;
00177 
00178         // create our url.
00179         nsCOMPtr<nsIURI> uri;
00180         rv = NS_NewURI(getter_AddRefs(uri), uriSpec);
00181         if (NS_FAILED(rv)) return rv;
00182 
00183         nsCOMPtr<nsIChannel> channel;
00184         rv = ioService->NewChannelFromURI(uri, getter_AddRefs(channel));
00185         if (NS_FAILED(rv)) return rv;
00186        
00187         // QI and set the upload stream
00188         nsCOMPtr<nsIUploadChannel> uploadChannel(do_QueryInterface(channel));
00189         uploadChannel->SetUploadStream(uploadStream, EmptyCString(), -1);
00190 
00191         // create a dummy listener
00192         InputTestConsumer* listener;
00193 
00194         listener = new InputTestConsumer;
00195         if (!listener) {
00196             NS_ERROR("Failed to create a new stream listener!");
00197             return -1;
00198         }
00199         NS_ADDREF(listener);
00200 
00201         channel->AsyncOpen(listener, nsnull);
00202 
00203         while ( gKeepRunning ) {
00204             PLEvent *gEvent;
00205             gEventQ->WaitForEvent(&gEvent);
00206             gEventQ->HandleEvent(gEvent);
00207         }
00208     } // this scopes the nsCOMPtrs
00209     // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
00210     rv = NS_ShutdownXPCOM(nsnull);
00211     NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
00212 
00213     return 0;
00214 }
00215