home *** CD-ROM | disk | FTP | other *** search
/ BUG 14 / BUGCD1998_05.ISO / internet / tcp4u / tcp4u330.exe / tcp4u.330 / Samples / Http1 / http_get.c next >
C/C++ Source or Header  |  1998-03-05  |  4KB  |  130 lines

  1. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  2. /* Test for Http4u                                                           */
  3. /* This sample app gets all URLs specified into a file which is passed as    */ 
  4. /* parameter. It can be compiled as a Windows 3.1x quickwin application.     */
  5. /* However, callback code is rather nasty.                                   */
  6. /*                                                                           */
  7. /* Please read file ../build.txt before compiling this sample                */
  8. /*                                                                           */
  9. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  10.  
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <ctype.h>
  14.  
  15. #include <tcp4u.h>
  16. #include <http4u.h>
  17.  
  18. #ifndef TRUE
  19. #   define TRUE (1==1)
  20. #endif
  21.  
  22. /* print help */
  23. void Usage (void)
  24. {
  25.   printf ("Usage: http_get [-d] <file>\n");
  26.   printf ("This sample app gets all URLs specifed into file\n");
  27.   exit(0);
  28. } /* Usage */
  29.  
  30.  
  31. BOOL CALLBACK MyCbk(long lBytesTransferred, long lTotalBytes, long lUserValue,
  32.                     LPCSTR sDummy, int nDummy)
  33. {
  34. #if defined (_WINDOWS) && !defined (WIN32)
  35.    /* special printf format and casts for Windows 3.1x. */
  36.    /* Does not work with the DLL  (womeone knows why ?) */
  37.     printf ("%8ld/%8ld\t%Fs\r", lBytesTransferred, lTotalBytes, 
  38.             lUserValue==0 ? (LPCSTR) "NULL" :  (LPCSTR)lUserValue);  
  39. #else
  40.     printf ("%8d/%8d\t%s\r", (int) lBytesTransferred, (int) lTotalBytes, 
  41.             lUserValue==0 ? "NULL" :  (LPSTR) lUserValue);  
  42. #endif      
  43.     fflush (stdout);
  44. return TRUE;
  45. } /* MyCbk */
  46.  
  47.  
  48. /* Process one URL */
  49. int ProcessLine (const char *szURL, const char *szProxyURL)
  50. {
  51. int   Rc;
  52. char *p;
  53. char  szResponse[1024]; 
  54. LPCSTR Prox;
  55.  
  56.   /* for MSVC 16 bit: automatic cast will not work -> GPF */
  57.   Prox =  (szProxyURL==NULL) ?NULL : szProxyURL;
  58.  
  59.   p = strrchr (szURL, '/');  if (p==NULL)  return HTTP4U_BAD_URL;
  60.   p++;
  61.   szResponse [0] = 0;
  62.   Rc=HttpGetFileEx(szURL,                    /* URL to be retrieved     */
  63.                    Prox,                     /* the proxy  to be used   */
  64.                    (*p==0 ? "Main.htm" : p), /* local file              */
  65.                    NULL,                     /* forget headers          */
  66.                    MyCbk,                    /* to be called            */
  67.                    (long) (LPSTR)  szURL,     /* to be passed to MyCbk  */
  68.                    szResponse, sizeof szResponse, /* errors rfom server */
  69.                    NULL, 0);
  70.   printf ("                                             ");
  71.   printf ("                                            \r");
  72.   printf ("%s: %s\n", szURL, szResponse[0]==0 ?  Http4uErrorString(Rc) :   szResponse);
  73. return Rc;
  74. } /* ProcessLine */
  75.  
  76.  
  77. /* main function */
  78. int main (int argc, char *argv[])
  79. {
  80. FILE *hF;
  81. char  sz [1024], *p, *szProxyURL;
  82. char szVer [128];
  83.  
  84.   Tcp4uVer (szVer, sizeof szVer);
  85.   printf ("Using %s\n", szVer);
  86.  
  87.   if (argc==3 && argv[1][0]=='-' )
  88.   {
  89.      switch (argv[1][1])
  90.      {
  91.     case 'd' :  Tcp4uEnableLog (LOG4U_ALL); break;
  92.     default  :  Usage();
  93.      }
  94.   }
  95.   else if (argc!=2)  Usage ();   /* does not return */
  96.   
  97.  
  98.   /* Necessary under Windows */
  99.   Tcp4uInit();  
  100.   /* set a five minutes timeout */
  101.   Http4uSetTimeout (300);
  102.  
  103.   /* Open file and calls ProcessLine foreach line */  
  104.   hF = fopen (argv[argc-1], "r");
  105.   if (hF==NULL)   { printf ("Can not open %s\n", argv[argc-1]);  exit(0); }
  106.   while (fgets (sz, sizeof sz, hF) != NULL) 
  107.   {
  108.      if (sz[0]==';'  || sz[0]=='#')  continue;
  109.      p = strchr (sz, '\r');     if (p!=NULL)   *p = 0;
  110.      p = strchr (sz, '\n');     if (p!=NULL)   *p = 0;
  111.      /* search for Proxy */
  112.      if (  (p = strchr (sz, ' '))==NULL  &&  (p = strchr (sz, '\t'))==NULL)  
  113.             szProxyURL=NULL;
  114.      else
  115.      {
  116.          for ( szProxyURL = p ;  
  117.               *szProxyURL!=0  && isspace(*szProxyURL) ; 
  118.                szProxyURL++ );
  119.          if (*szProxyURL==0)  szProxyURL=NULL;
  120.         *p=0 ;          /* end of string szURL */
  121.      }
  122.      ProcessLine (sz, szProxyURL);
  123.   } /* until whole file is processed */
  124.   fclose (hF);
  125.  
  126.   Tcp4uCleanup();       
  127. return 0;
  128. } /* main */
  129.  
  130.