Back to index

lightning-sunbird  0.9+nobinonly
TestSyncHTTP.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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 <nsCOMPtr.h>
00039 #include <nsString.h>
00040 #include <nsIURI.h>
00041 #include <nsIChannel.h>
00042 #include <nsIHTTPChannel.h>
00043 #include <nsIInputStream.h>
00044 #include <nsNetUtil.h>
00045 
00046 /*
00047  * Test synchronous HTTP.
00048  */
00049 
00050 #define RETURN_IF_FAILED(rv, what) \
00051     PR_BEGIN_MACRO \
00052     if (NS_FAILED(rv)) { \
00053         printf(what ": failed - %08x\n", rv); \
00054         return -1; \
00055     } \
00056     PR_END_MACRO
00057 
00058 struct TestContext {
00059     nsCOMPtr<nsIURI> uri;
00060     nsCOMPtr<nsIChannel> channel;
00061     nsCOMPtr<nsIInputStream> inputStream;
00062     PRTime t1, t2;
00063     PRUint32 bytesRead, totalRead;
00064 
00065     TestContext()
00066         : t1(0), t2(0), bytesRead(0), totalRead(0)
00067         { printf("TestContext [this=%p]\n", (void*)this); }
00068    ~TestContext()
00069         { printf("~TestContext [this=%p]\n", (void*)this); }
00070 };
00071 
00072 int
00073 main(int argc, char **argv)
00074 {
00075     nsresult rv;
00076     TestContext *c;
00077     int i, nc=0, npending=0;
00078     char buf[256];
00079 
00080     if (argc < 2) {
00081         printf("Usage: TestSyncHTTP <url-list>\n");
00082         return -1;
00083     }
00084 
00085     c = new TestContext[argc-1];
00086 
00087     for (i=0; i<(argc-1); ++i, ++nc) {
00088         rv = NS_NewURI(getter_AddRefs(c[i].uri), argv[i+1]);
00089         RETURN_IF_FAILED(rv, "NS_NewURI");
00090 
00091         rv = NS_OpenURI(getter_AddRefs(c[i].channel), c[i].uri, nsnull, nsnull);
00092         RETURN_IF_FAILED(rv, "NS_OpenURI");
00093 
00094         nsCOMPtr<nsIHTTPChannel> httpChannel = do_QueryInterface(c[i].channel);
00095         if (httpChannel)
00096             httpChannel->SetOpenHasEventQueue(PR_FALSE);
00097 
00098         // initialize these fields for reading
00099         c[i].bytesRead = 1;
00100         c[i].totalRead = 0;
00101     }
00102 
00103     for (i=0; i<nc; ++i) {
00104         c[i].t1 = PR_Now();
00105 
00106         rv = c[i].channel->Open(getter_AddRefs(c[i].inputStream));
00107         RETURN_IF_FAILED(rv, "nsIChannel::OpenInputStream");
00108     }
00109 
00110     npending = nc;
00111     while (npending) {
00112         for (i=0; i<nc; ++i) {
00113             //
00114             // read the response content...
00115             //
00116             if (c[i].bytesRead > 0) {
00117                 rv = c[i].inputStream->Read(buf, sizeof buf, &c[i].bytesRead);
00118                 RETURN_IF_FAILED(rv, "nsIInputStream::Read");
00119                 c[i].totalRead += c[i].bytesRead;
00120 
00121                 if (c[i].bytesRead == 0) {
00122                     c[i].t2 = PR_Now();
00123                     printf("finished GET of: %s\n", argv[i+1]);
00124                     printf("total read: %u bytes\n", c[i].totalRead);
00125                     printf("total read time: %0.3f\n",
00126                             ((double) (c[i].t2 - c[i].t1))/1000000.0);
00127                     npending--;
00128                 }
00129             }
00130         }
00131     }
00132 
00133     delete[] c;
00134 
00135     NS_ShutdownXPCOM(nsnull);
00136     return 0;
00137 }