home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 1998 May
/
Chip_1998-05_cd.bin
/
servis
/
ie401sk
/
CGIUTIL.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1997-11-09
|
5KB
|
212 lines
/******************************************************************************\
*
* Copyright (C) 1996 Microsoft Corporation
* All rights reserved.
*
******************************************************************************
*
* Module: CGIUtil.cpp
*
* Purpose: Little library of CGI utility functions.
*
\******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#include "windows.h"
#include "cgiutil.h"
static char InputBuffer[4096] ; // a large buffer for request info
static char *InputList[40] ; // list of pointers to return
static int nCGIvars = 0 ; // number of CGI variables we've parsed
/******************************************************************************\
*
* Function to parse the CGI request into a buffer
*
\******************************************************************************/
/* Function */ int ParseCGI()
{
int i ;
void DecodeHex ( char *p ) ; // fwd refs
void StrCvt ( char* cStr,char cOld,char cNew ) ;
// What method were we invoked through?
char* pRequestMethod = getenv ( "REQUEST_METHOD" ) ;
if ( pRequestMethod == NULL )
{
return ( -1 ) ;
}
if ( stricmp ( pRequestMethod, "POST" ) == 0 )
{
int ContentLength ;
// Read in the data from the client
char *p = getenv ( "CONTENT_LENGTH" ) ;
if ( p != NULL )
{
ContentLength = atoi ( p ) ;
}
else
{
ContentLength = 0 ;
}
if ( ContentLength > sizeof(InputBuffer)-1 )
{
ContentLength = sizeof(InputBuffer)-1 ;
}
for ( i = 0 ; i < ContentLength ; i++ )
{
int x = fgetc ( stdin ) ;
if ( x == EOF ) break ;
InputBuffer[i] = x ;
}
InputBuffer[i] = '\0' ;
// convert to normal text
DecodeHex ( InputBuffer ) ;
StrCvt ( InputBuffer, '+', ' ' ) ;
p = getenv ( "CONTENT_TYPE" ) ;
if ( p == NULL )
{
return ( -1 ) ; // no content type found
}
if ( _stricmp ( p, "application/x-www-form-urlencoded" ) == 0 )
{
// Parse the data
for ( p = strtok ( InputBuffer, "&" ), i = 0 ;
p != NULL ;
p = strtok ( NULL , "&" ), i++ )
{
InputList[i] = p ;
}
InputList[i] = p ; // Add extra EOS
}
else
{
InputList[i=0] = InputBuffer ;
}
InputBuffer[strlen(InputBuffer)] = '\0' ; // Add extra EOS
return ( nCGIvars = i ) ;
}
else if ( stricmp ( pRequestMethod, "GET" ) == 0 )
{
// copy QUERY_STRING into our buffer and convert to normal text
char *p = getenv ( "QUERY_STRING" ) ;
if ( p != NULL )
{
strncpy ( InputBuffer, p, sizeof(InputBuffer) ) ;
DecodeHex ( InputBuffer ) ;
StrCvt ( InputBuffer, '+', ' ' ) ;
// Parse the data in the search term
for ( p = strtok ( InputBuffer, "&" ), i = 0 ;
p != NULL ;
p = strtok ( NULL, "&" ), i++ )
{
InputList[i] = p ;
}
InputList[i] = p ; // Add extra EOS
InputBuffer[strlen(InputBuffer)] = '\0' ; // Add extra EOS
return ( nCGIvars = i ) ;
}
}
return ( -1 ) ; // else, bogus request method
}
/******************************************************************************\
*
* Function to fetch a CGI variable from the parsed CGI buffer
*
* Note: ParseCGI() _must_ be called prior to FetchVar()
*
\******************************************************************************/
/* Function */ int FetchVar ( char *key, char *ret )
{
char e[MAX_PATH] ;
char *p ;
for ( int i = 0 ; i < nCGIvars ; i++ )
{
strcpy ( e, InputList[i] ) ;
p = strchr ( e, '=' ) ;
*p++ = '\0' ;
if ( stricmp ( e, key ) == 0 ) break ;
}
if ( i == nCGIvars ) return ( 0 ) ;
strcpy ( ret, p ) ;
return ( 1 ) ;
}
/******************************************************************************\
*
* Decode the given string in-place by converting %XX escapes to characters
*
\******************************************************************************/
/* Function */ void DecodeHex ( char *p )
{
int Hex2Int ( char *p ) ; // fwd ref
for ( char *pD = p ; *p ; )
{
if ( ( p[0] == '%' ) && isxdigit(p[1]) && isxdigit(p[2]) )
{
// Escape: next 2 chars are hex representation of the actual character
*p++ ; // skip '%'
*pD++ = (char)Hex2Int ( p ) ;
p += 2 ; // skip 2 digits
}
else // just copy and increment buffer ptrs
{
*pD++ = *p++ ;
}
}
*pD = '\0'; // make sure destination is terminated
}
/******************************************************************************\
*
* Function to convert hex ascii character pair to integer
*
\******************************************************************************/
/* Function */ int Hex2Int ( char *p )
{
char str[3] ;
char *stop ;
memcpy ( str, p, 2 ) ;
str[2] = '\0' ;
int ret = strtoul ( str, &stop, 16 ) ;
return ( ret ) ;
}
/******************************************************************************\
*
* Function to replace character in old string with new one
*
\******************************************************************************/
/* Function */ void StrCvt ( char* cStr,char cOld,char cNew )
{
for ( char *p = cStr ; *p ; *p++ )
{
if ( *p == cOld ) *p = cNew ;
}
}