Back to index

lightning-sunbird  0.9+nobinonly
TestLibxpnet.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; 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 Communicator client code, released
00016  * March 31, 1998.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 1998
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Samir Gehani <sgehani@netscape.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #include <stdio.h>
00041 #include <stdlib.h>
00042 #include <string.h>
00043 #include <time.h>
00044 
00045 #ifdef macintosh
00046 #include <console.h>
00047 #endif
00048 
00049 #include "nsFTPConn.h"
00050 #include "nsHTTPConn.h"
00051 
00052 const int kProxySrvrLen = 1024;
00053 const char kHTTP[8] = "http://";
00054 const char kFTP[7] = "ftp://";
00055 const char kLoclFile[7] = "zzzFTP";
00056 static int sTotalSize = 0;
00057 
00058 int
00059 ProgressCB(int aBytesSoFar, int aTotalFinalSize)
00060 {
00061     static int bTotalSizeFound = FALSE;
00062     
00063     if (!bTotalSizeFound && aTotalFinalSize > 0)
00064     {
00065         sTotalSize = aTotalFinalSize;
00066         bTotalSizeFound = TRUE;
00067     }
00068     
00069     printf(".");
00070     return 0;
00071 }
00072 
00073 void
00074 spew(char *funcName, int rv)
00075 {
00076     printf("%s returned %d\n", funcName, rv);
00077 }
00078 
00079 void
00080 usage(char *prog)
00081 {
00082     fprintf(stderr, "usage: %s <URL>\n\t\t[<ProxyServer> ", prog);
00083     fprintf(stderr, "<ProxyPort> [<ProxyUserName> <ProxyPassword>]]\n");
00084     fprintf(stderr, "\t\t[-r<ResumePos> or -rg]\n");
00085     
00086 #ifdef macintosh
00087     int fin = getchar();
00088 #endif
00089 }
00090 
00091 int
00092 main(int argc, char **argv)
00093 {
00094     char *proxyUser = 0, *proxyPswd = 0;
00095     char proxyURL[kProxySrvrLen];
00096     int rv = 0, resPos = 0;
00097     time_t startTime, endTime;
00098     double dlTime = 0;  /* download time */
00099     float dlRate = 0;   /* download rate */
00100     int bResumeOrGet = 0;
00101     
00102 #ifdef macintosh
00103     argc = ccommand(&argv);
00104 #endif
00105 
00106     if (argc < 2)
00107     {
00108         usage(argv[0]);
00109         exit(1);
00110     }
00111 
00112     /* get resume pos if -r arg passed in */
00113     for (int i = 1; i < argc; ++i)
00114     {
00115         /* resume or get */
00116         if (strncmp(argv[i], "-rg", 3) == 0)
00117         {
00118             bResumeOrGet = 1;
00119         }
00120 
00121         /* resume from pos */
00122         else if (strncmp(argv[i], "-r", 2) == 0)
00123         {
00124             resPos = atoi(argv[i] + 2);
00125             printf("resPos = %d\n", resPos);
00126 
00127             break;
00128         }        
00129     }
00130 
00131     /* has a proxy server been specified? */
00132     if (argc >= 4)
00133     {
00134         memset(proxyURL, 0, kProxySrvrLen);
00135         sprintf(proxyURL, "http://%s:%s", argv[2], argv[3]);
00136 
00137         if (argc >=6)
00138         {
00139             proxyUser = argv[4];
00140             proxyPswd = argv[5];
00141         }
00142 
00143         nsHTTPConn *conn = new nsHTTPConn(proxyURL);
00144         conn->SetProxyInfo(argv[1], proxyUser, proxyPswd);
00145         printf("Proxy URL: %s\n", argv[1]);
00146         if (proxyUser && proxyPswd)
00147         {
00148             printf("Proxy User: %s\n", proxyUser);
00149             printf("Proxy Pswd: %s\n", proxyPswd);
00150         }
00151 
00152         rv = conn->Open();
00153         spew("nsHTTPConn::Open", rv);
00154 
00155         startTime = time(NULL);
00156         if (bResumeOrGet)
00157         {
00158             rv = conn->ResumeOrGet(ProgressCB, NULL); // use leaf from URL
00159         }
00160         else
00161         {
00162             rv = conn->Get(ProgressCB, NULL, resPos); // use leaf from URL
00163         }
00164         endTime = time(NULL);
00165         printf("\n"); // newline after progress completes
00166         spew("nsHTTPConn::Get", rv);
00167     
00168         rv = conn->Close();
00169         spew("nsHTTPConn::Close", rv);
00170     }
00171     else
00172     {
00173         /* is this an HTTP URL? */
00174         if (strncmp(argv[1], kHTTP, strlen(kHTTP)) == 0)
00175         {
00176             nsHTTPConn *conn = new nsHTTPConn(argv[1]);
00177             
00178             rv = conn->Open();
00179             spew("nsHTTPConn::Open", rv);
00180 
00181             startTime = time(NULL);
00182             if (bResumeOrGet)
00183             {
00184                 rv = conn->ResumeOrGet(ProgressCB, NULL);
00185             }
00186             else
00187             {
00188                 rv = conn->Get(ProgressCB, NULL, resPos);
00189             }
00190             endTime = time(NULL);
00191             printf("\n"); // newline after progress completes
00192             spew("nsHTTPConn::Get", rv);
00193 
00194             rv = conn->Close();
00195             spew("nsHTTPConn::Close", rv);
00196         }
00197 
00198         /* or is this an FTP URL? */
00199         else if (strncmp(argv[1], kFTP, strlen(kFTP)) == 0)
00200         {
00201             char *host = 0, *path = 0, *file = (char*) kLoclFile;
00202             int port = 21;
00203 
00204             rv = nsHTTPConn::ParseURL(kFTP, argv[1], &host, &port, &path);
00205             spew("nsHTTPConn::ParseURL", rv);
00206 
00207             nsFTPConn *conn = new nsFTPConn(host);
00208 
00209             rv = conn->Open();
00210             spew("nsFTPConn::Open", rv);
00211 
00212             if (strrchr(path, '/') != (path + strlen(path)))
00213                 file = strrchr(path, '/') + 1; // set to leaf name
00214             startTime = time(NULL);
00215             if (bResumeOrGet)
00216             {
00217                 rv = conn->ResumeOrGet(path, file, nsFTPConn::BINARY, 1, 
00218                      ProgressCB);
00219             }
00220             else
00221             {
00222                 rv = conn->Get(path, file, nsFTPConn::BINARY, resPos, 1, 
00223                      ProgressCB);
00224             }
00225             endTime = time(NULL);
00226             printf("\n"); // newline after progress completes
00227             spew("nsFTPConn::Get", rv);
00228 
00229             rv = conn->Close();
00230             spew("nsFTPConn::Close", rv);
00231 
00232             if (host)
00233                 free(host);
00234             if (path)
00235                 free(path);
00236         }
00237 
00238         /* or we don't understand the args */
00239         else
00240         {
00241             fprintf(stderr, "Like, uhm, dude!  I don't get you.  ");
00242             fprintf(stderr, "See usage...\n");
00243             usage(argv[0]);
00244             return 1;
00245         }
00246     }
00247     
00248     /* compute rate */
00249     if (sTotalSize > 0)
00250     {
00251         dlTime = difftime(endTime, startTime);
00252         if (dlTime > 0)
00253             dlRate = sTotalSize/dlTime;
00254     }
00255     printf("Download rate = %f\tTotal size = %d\tTotal time = %f\n", 
00256             dlRate, sTotalSize, dlTime);
00257 
00258 #ifdef macintosh
00259     int fin = getchar();
00260 #endif
00261 
00262     return 0;
00263 }