home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1999 October / VPR9910A.BIN / WIN98SP1 / IE401.SP2 / ieak4.cab / cgiutil.cpp < prev    next >
C/C++ Source or Header  |  1999-03-17  |  5KB  |  212 lines

  1. /******************************************************************************\
  2.  *
  3.  *    Copyright (C) 1996 Microsoft Corporation
  4.  *    All rights reserved.
  5.  *
  6.  ******************************************************************************
  7.  *
  8.  *    Module: CGIUtil.cpp
  9.  *
  10.  *    Purpose: Little library of CGI utility functions.
  11.  *
  12. \******************************************************************************/
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <ctype.h>
  18. #include <stdarg.h>
  19. #include "windows.h"
  20. #include "cgiutil.h"
  21.  
  22. static char     InputBuffer[4096] ;    // a large buffer for request info
  23. static char    *InputList[40] ;        // list of pointers to return
  24. static int     nCGIvars = 0 ;            // number of CGI variables we've parsed
  25.  
  26.  
  27. /******************************************************************************\
  28.  *
  29.  *    Function to parse the CGI request into a buffer
  30.  *
  31. \******************************************************************************/
  32.  
  33. /* Function */ int ParseCGI()
  34.  
  35. {
  36.     int    i ;
  37.     void DecodeHex ( char *p ) ;    // fwd refs
  38.     void StrCvt ( char* cStr,char cOld,char cNew ) ;
  39.  
  40.     // What method were we invoked through?
  41.     char* pRequestMethod = getenv ( "REQUEST_METHOD" ) ;
  42.     if ( pRequestMethod == NULL )
  43.     {
  44.         return ( -1 ) ;
  45.     }
  46.  
  47.     if ( stricmp ( pRequestMethod, "POST" ) == 0 )
  48.     {
  49.         int ContentLength ;
  50.  
  51.         // Read in the data from the client
  52.         char *p = getenv ( "CONTENT_LENGTH" ) ;
  53.         if ( p != NULL )
  54.         {
  55.             ContentLength = atoi ( p ) ;
  56.         }
  57.         else
  58.         {
  59.             ContentLength = 0 ;
  60.         }
  61.         if ( ContentLength > sizeof(InputBuffer)-1 )
  62.         {
  63.             ContentLength = sizeof(InputBuffer)-1 ;
  64.         }
  65.         for ( i = 0 ; i < ContentLength ; i++ )
  66.         {
  67.             int x = fgetc ( stdin ) ;
  68.             if ( x == EOF ) break ;
  69.             InputBuffer[i] = x ;
  70.         }
  71.         InputBuffer[i] = '\0' ;
  72.         // convert to normal text
  73.         DecodeHex ( InputBuffer ) ;
  74.         StrCvt ( InputBuffer, '+', ' ' ) ;
  75.  
  76.         p = getenv ( "CONTENT_TYPE" ) ;
  77.         if ( p == NULL )
  78.         {
  79.             return ( -1 ) ;    // no content type found
  80.         }
  81.         if ( _stricmp ( p, "application/x-www-form-urlencoded" ) == 0 )
  82.         {
  83.             // Parse the data
  84.             for ( p = strtok ( InputBuffer, "&" ), i = 0 ;
  85.                   p != NULL ;
  86.                   p = strtok ( NULL , "&" ), i++ )
  87.             {
  88.                 InputList[i] = p ;
  89.             }
  90.             InputList[i] = p ;    // Add extra EOS
  91.         }
  92.         else
  93.         {
  94.             InputList[i=0] = InputBuffer ;
  95.         }
  96.         InputBuffer[strlen(InputBuffer)] = '\0' ;    // Add extra EOS
  97.         return ( nCGIvars = i ) ;
  98.     }
  99.  
  100.     else if ( stricmp ( pRequestMethod, "GET" ) == 0 )
  101.     {
  102.         // copy QUERY_STRING into our buffer and convert to normal text
  103.         char *p = getenv ( "QUERY_STRING" ) ;
  104.         if ( p != NULL )
  105.         {
  106.             strncpy ( InputBuffer, p, sizeof(InputBuffer) ) ;
  107.             DecodeHex ( InputBuffer ) ;
  108.             StrCvt ( InputBuffer, '+', ' ' ) ;
  109.             // Parse the data in the search term
  110.             for ( p = strtok ( InputBuffer, "&" ), i = 0 ;
  111.                   p != NULL ;
  112.                   p = strtok ( NULL, "&" ), i++ )
  113.             {
  114.                 InputList[i] = p ;
  115.             }
  116.             InputList[i] = p ;    // Add extra EOS
  117.             InputBuffer[strlen(InputBuffer)] = '\0' ;    // Add extra EOS
  118.             return ( nCGIvars = i ) ;
  119.         }
  120.     }
  121.  
  122.     return ( -1 ) ;    // else, bogus request method
  123.  
  124. }
  125.  
  126. /******************************************************************************\
  127.  *
  128.  *    Function to fetch a CGI variable from the parsed CGI buffer
  129.  *
  130.  *    Note: ParseCGI() _must_ be called prior to FetchVar()
  131.  *
  132. \******************************************************************************/
  133.  
  134. /* Function */ int FetchVar ( char *key, char *ret )
  135.  
  136. {
  137.     char  e[MAX_PATH] ;
  138.     char *p ;
  139.  
  140.     for ( int i = 0 ; i < nCGIvars ; i++ )
  141.     {
  142.         strcpy ( e, InputList[i] ) ;
  143.         p = strchr ( e, '=' ) ;
  144.         *p++ = '\0' ;
  145.         if ( stricmp ( e, key ) == 0 ) break ;
  146.     }
  147.     if ( i == nCGIvars ) return ( 0 ) ;
  148.     strcpy ( ret, p ) ;
  149.     return ( 1 ) ;
  150. }
  151.  
  152. /******************************************************************************\
  153.  *
  154.  *  Decode the given string in-place by converting %XX escapes to characters
  155.  *
  156. \******************************************************************************/
  157.  
  158. /* Function */ void DecodeHex ( char *p )
  159.  
  160. {
  161.     int Hex2Int ( char *p ) ;    // fwd ref
  162.  
  163.     for ( char *pD = p ; *p ; )
  164.     {
  165.         if ( ( p[0] == '%' ) && isxdigit(p[1]) && isxdigit(p[2]) )
  166.         {
  167.             // Escape: next 2 chars are hex representation of the actual character
  168.             *p++ ;        // skip '%'
  169.             *pD++ = (char)Hex2Int ( p ) ;
  170.             p += 2 ;    // skip 2 digits
  171.         }
  172.         else    // just copy and increment buffer ptrs
  173.         {
  174.             *pD++ = *p++ ;
  175.         }
  176.     }
  177.     *pD = '\0';    // make sure destination is terminated
  178. }
  179.  
  180. /******************************************************************************\
  181.  *
  182.  *    Function to convert hex ascii character pair to integer
  183.  *
  184. \******************************************************************************/
  185.  
  186. /* Function */ int Hex2Int ( char *p )
  187.  
  188. {
  189.     char str[3] ;
  190.     char *stop ;
  191.  
  192.     memcpy ( str, p, 2 ) ;
  193.     str[2] = '\0' ;
  194.     int ret = strtoul ( str, &stop, 16 ) ;
  195.     return ( ret ) ;
  196. }
  197.  
  198. /******************************************************************************\
  199.  *
  200.  *    Function to replace character in old string with new one
  201.  *
  202. \******************************************************************************/
  203.  
  204. /* Function */ void StrCvt ( char* cStr,char cOld,char cNew )
  205.  
  206. {
  207.     for ( char *p = cStr ; *p ; *p++ )
  208.     {
  209.         if ( *p == cOld ) *p = cNew ;
  210.     }
  211. }
  212.