home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / winbase / security / winnt / httpauth / httpget.c < prev    next >
C/C++ Source or Header  |  1997-10-08  |  5KB  |  203 lines

  1. /*++
  2.  
  3. Copyright 1996-1997 Microsoft Corporation
  4.  
  5. Module Name:
  6.  
  7.     httpget.c
  8.  
  9. Abstract:
  10.  
  11.     This is a web command line application.  It will allow a user to get a 
  12.     html document from the command line.
  13.  
  14. Environment:
  15.  
  16.     console app
  17.  
  18. --*/
  19.  
  20. #include <windows.h>
  21. #include <stdio.h>
  22. #include <io.h>
  23. #include <fcntl.h>
  24. #include <stdlib.h>
  25. #include <time.h>
  26. #include <malloc.h>
  27. #include "const.h"
  28. #include "proto.h"
  29.  
  30. void
  31. PrintUsage(void);
  32.  
  33. #define SOCKETS_METHOD 1
  34.  
  35. void
  36. _CRTAPI1
  37. main(
  38.     int argc,
  39.     char * argv[])
  40. {
  41.     char *        Server;
  42.     char *        URL;
  43.     char *        Verb = "GET";
  44.     char *        Gateway = NULL;
  45.     char *        AcceptTypes[2] = {"*/*", NULL};
  46.     char          Headers[] = 
  47.                       "Accept: */*\r\n"
  48.                       "User-Agent: Httpget\r\n"
  49.                       "Referer: Httpget\r\n"
  50.                       "\r\n";
  51.     int           Method = SOCKETS_METHOD;
  52.     BOOL          DisplayHeaders = FALSE;
  53.     DWORD         ClientDataSize = 0;
  54.     PSTR          pszUserName = "";
  55.     PSTR          pszPassword = "";
  56.     PSTR          pszStore = NULL;
  57.     PSTR          pszPref = NULL;
  58.  
  59.     //
  60.     // Parse the command line
  61.     //
  62.  
  63.     if (argc < 3) 
  64.     {
  65.         PrintUsage();
  66.         return;
  67.     }
  68.  
  69.     while (argc > 3) 
  70.     {
  71.         //
  72.         // parse options
  73.         //
  74.  
  75.         if (argv[1][0] == '-') 
  76.         {
  77.             switch (argv[1][1]) 
  78.             {
  79.             case 'V' :
  80.             case 'v' :
  81.                 //
  82.                 // Input verb
  83.                 //
  84.  
  85.                 Verb = &argv[1][3];
  86.  
  87.                 break;
  88.  
  89.             case 'H' :
  90.             case 'h' :
  91.                 //
  92.                 // Display headers
  93.                 //
  94.  
  95.                 DisplayHeaders = TRUE;
  96.  
  97.                 break;
  98.  
  99.             case 'D' :
  100.             case 'd' :
  101.                 //
  102.                 // Amount of data to send
  103.                 //
  104.  
  105.                 if (sscanf(&argv[1][3], "%u", &ClientDataSize) != 1) 
  106.                 {
  107.                     PrintUsage();
  108.                     return;
  109.                 }
  110.                 break;
  111.  
  112.             case 'G' :
  113.             case 'g' :
  114.                 //
  115.                 // Gateway
  116.                 //
  117.  
  118.                 Gateway = &argv[1][3];
  119.  
  120.                 break;
  121.  
  122.             case 'M':
  123.             case 'm':
  124.                 // User name
  125.                 pszPref = &argv[1][3];
  126.                 break;
  127.  
  128.             case 'N':
  129.             case 'n':
  130.                 // User name
  131.                 pszUserName = &argv[1][3];
  132.                 break;
  133.  
  134.             case 'P':
  135.             case 'p':
  136.                 // Password
  137.                 pszPassword = &argv[1][3];
  138.                 break;
  139.  
  140.             case 'S':
  141.             case 's':
  142.                 pszStore = &argv[1][3];
  143.                 break;
  144.  
  145.             default:
  146.                 PrintUsage();
  147.                 return;
  148.                 break;
  149.             }
  150.         } 
  151.         else 
  152.         {
  153.             PrintUsage();
  154.             return;
  155.         }
  156.  
  157.         argc --;
  158.         argv ++;
  159.     }
  160.  
  161.     Server = argv[1];
  162.  
  163.     URL = argv[2];
  164.  
  165.     switch (Method) 
  166.     {
  167.     case SOCKETS_METHOD:
  168.         HttpGetSocket(
  169.             Verb,
  170.             Server,
  171.             URL,
  172.             DisplayHeaders,
  173.             ClientDataSize,
  174.             pszUserName,
  175.             pszPassword,
  176.             pszStore,
  177.             pszPref );
  178.         break;
  179.     }
  180.  
  181.     return;
  182. }
  183.  
  184. void
  185. PrintUsage()
  186. {
  187.     fprintf(stderr,
  188.         "httpauth  [-h] [-d:<size>] [-m:<methodlist>] [-v:<verb>] [-n:<username>]\n"
  189.         "\t  [-p:<password>] [-g:gateway] [-s:storefile] <server> <path>\n"
  190.         "\t-h           - display result headers\n"
  191.         "\t<size>       - amount of client data to send\n"
  192.         "\t<verb>       - HTTP verb to use (default is GET)\n"
  193.         "\t<username>   - user name for authentication\n"
  194.         "\t<password>   - password for authentication\n"
  195.         "\t<methodlist> - comma separated list of authentication methods in order\n"
  196.         "\t               of preference (Default is to use first supported method\n"
  197.         "\t               returned by the HTTP server (e.g., -m:NTLM,BASIC)\n"
  198.         "\t<storefile>  - file where to store result message body\n"
  199.         "\t<server>     - web server to connect to (without http:)\n"
  200.         "\t<path>       - resource to get (e.g., /default.htm)\n"
  201.     );
  202. }
  203.