Back to index

lightning-sunbird  0.9+nobinonly
TestHttp.cpp
Go to the documentation of this file.
00001 #include "nsNetUtil.h"
00002 #include "nsIEventQueueService.h"
00003 #include "nsIServiceManager.h"
00004 #include "nsIComponentRegistrar.h"
00005 #include "nsIInterfaceRequestor.h"
00006 #include "nsIInterfaceRequestorUtils.h"
00007 #include "nsIProgressEventSink.h"
00008 
00009 #define RETURN_IF_FAILED(rv, step) \
00010     PR_BEGIN_MACRO \
00011     if (NS_FAILED(rv)) { \
00012         printf(">>> %s failed: rv=%x\n", step, rv); \
00013         return rv;\
00014     } \
00015     PR_END_MACRO
00016 
00017 static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
00018 static nsIEventQueue* gEventQ = nsnull;
00019 static PRBool gKeepRunning = PR_TRUE;
00020 
00021 //-----------------------------------------------------------------------------
00022 // nsIStreamListener implementation
00023 //-----------------------------------------------------------------------------
00024 
00025 class MyListener : public nsIStreamListener
00026 {
00027 public:
00028     NS_DECL_ISUPPORTS
00029     NS_DECL_NSIREQUESTOBSERVER
00030     NS_DECL_NSISTREAMLISTENER
00031 
00032     MyListener() { }
00033     virtual ~MyListener() {}
00034 };
00035 
00036 NS_IMPL_ISUPPORTS2(MyListener,
00037                    nsIRequestObserver,
00038                    nsIStreamListener)
00039 
00040 NS_IMETHODIMP
00041 MyListener::OnStartRequest(nsIRequest *req, nsISupports *ctxt)
00042 {
00043     printf(">>> OnStartRequest\n");
00044     return NS_OK;
00045 }
00046 
00047 NS_IMETHODIMP
00048 MyListener::OnStopRequest(nsIRequest *req, nsISupports *ctxt, nsresult status)
00049 {
00050     printf(">>> OnStopRequest status=%x\n", status);
00051     gKeepRunning = PR_FALSE;
00052     return NS_OK;
00053 }
00054 
00055 NS_IMETHODIMP
00056 MyListener::OnDataAvailable(nsIRequest *req, nsISupports *ctxt,
00057                             nsIInputStream *stream,
00058                             PRUint32 offset, PRUint32 count)
00059 {
00060     printf(">>> OnDataAvailable [count=%u]\n", count);
00061 
00062     char buf[256];
00063     nsresult rv;
00064     PRUint32 bytesRead=0;
00065 
00066     while (count) {
00067         PRUint32 amount = PR_MIN(count, sizeof(buf));
00068 
00069         rv = stream->Read(buf, amount, &bytesRead);
00070         if (NS_FAILED(rv)) {
00071             printf(">>> stream->Read failed with rv=%x\n", rv);
00072             return rv;
00073         }
00074 
00075         fwrite(buf, 1, bytesRead, stdout);
00076 
00077         count -= bytesRead;
00078     }
00079     return NS_OK;
00080 }
00081 
00082 //-----------------------------------------------------------------------------
00083 // NotificationCallbacks implementation
00084 //-----------------------------------------------------------------------------
00085 
00086 class MyNotifications : public nsIInterfaceRequestor
00087                       , public nsIProgressEventSink
00088 {
00089 public:
00090     NS_DECL_ISUPPORTS
00091     NS_DECL_NSIINTERFACEREQUESTOR
00092     NS_DECL_NSIPROGRESSEVENTSINK
00093 
00094     MyNotifications() { }
00095     virtual ~MyNotifications() {}
00096 };
00097 
00098 NS_IMPL_THREADSAFE_ISUPPORTS2(MyNotifications,
00099                               nsIInterfaceRequestor,
00100                               nsIProgressEventSink)
00101 
00102 NS_IMETHODIMP
00103 MyNotifications::GetInterface(const nsIID &iid, void **result)
00104 {
00105     return QueryInterface(iid, result);
00106 }
00107 
00108 NS_IMETHODIMP
00109 MyNotifications::OnStatus(nsIRequest *req, nsISupports *ctx,
00110                           nsresult status, const PRUnichar *statusText)
00111 {
00112     printf("status: %x\n", status);
00113     return NS_OK;
00114 }
00115 
00116 NS_IMETHODIMP
00117 MyNotifications::OnProgress(nsIRequest *req, nsISupports *ctx,
00118                             PRUint64 progress, PRUint64 progressMax)
00119 {
00120     printf("progress: %llu/%llu\n", progress, progressMax);
00121     return NS_OK;
00122 }
00123 
00124 //-----------------------------------------------------------------------------
00125 // main, etc..
00126 //-----------------------------------------------------------------------------
00127 
00128 
00129 int main(int argc, char **argv)
00130 {
00131     nsresult rv;
00132 
00133     if (argc == 1) {
00134         printf("usage: TestHttp <url>\n");
00135         return -1;
00136     }
00137     {
00138         nsCOMPtr<nsIServiceManager> servMan;
00139         NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
00140         nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
00141         NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
00142         if (registrar)
00143             registrar->AutoRegister(nsnull);
00144 
00145         // Create the Event Queue for this thread...
00146         nsCOMPtr<nsIEventQueueService> eqs =
00147                  do_GetService(kEventQueueServiceCID, &rv);
00148         RETURN_IF_FAILED(rv, "do_GetService(EventQueueService)");
00149 
00150         rv = eqs->CreateMonitoredThreadEventQueue();
00151         RETURN_IF_FAILED(rv, "CreateMonitoredThreadEventQueue");
00152 
00153         rv = eqs->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
00154         RETURN_IF_FAILED(rv, "GetThreadEventQueue");
00155 
00156         nsCOMPtr<nsIURI> uri;
00157         nsCOMPtr<nsIChannel> chan;
00158         nsCOMPtr<nsIStreamListener> listener = new MyListener();
00159         nsCOMPtr<nsIInterfaceRequestor> callbacks = new MyNotifications();
00160 
00161         rv = NS_NewURI(getter_AddRefs(uri), argv[1]);
00162         RETURN_IF_FAILED(rv, "NS_NewURI");
00163 
00164         rv = NS_NewChannel(getter_AddRefs(chan), uri, nsnull, nsnull, callbacks);
00165         RETURN_IF_FAILED(rv, "NS_OpenURI");
00166 
00167         rv = chan->AsyncOpen(listener, nsnull);
00168         RETURN_IF_FAILED(rv, "AsyncOpen");
00169 
00170         while (gKeepRunning)
00171             gEventQ->ProcessPendingEvents();
00172 
00173         printf(">>> done\n");
00174     } // this scopes the nsCOMPtrs
00175     // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
00176     rv = NS_ShutdownXPCOM(nsnull);
00177     NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
00178     return 0;
00179 }