home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Programming with VisualAge for Java
/
IBMVJAVA.ISO
/
icswin95
/
httpdw32.z
/
cgixmp.c
< prev
next >
Wrap
Text File
|
1997-04-07
|
11KB
|
378 lines
#ifdef _OE_
#define NULL 0
#pragma csect(CODE,"IMWCIXMP")
#pragma comment(date)
#pragma variable(IMWID,NORENT)
static char IMWID[] = "IMWCIXMP " __FILE__;
#endif
/* Includes */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "icsdebug.h" /* general-purpose debugging header */
#if defined(OS2)
#include <os2.h>
#endif
#if defined(WIN32)
#include <windows.h>
#endif
#include "cgixmp.h"
/* Definition of structure used in linked list of variables */
typedef struct _argument
{
char *VariableName;
char *Value;
struct _argument *pNext;
} PARMLIST, *PPARMLIST;
/* Function definitions */
int ErrMsg (char *msg);
PPARMLIST ReadArguments(int InputLength);
static void PlusesToSpaces(char *Str);
static int HexVal(char c);
static void TranslateEscapes(char *Str);
/******************************************************************************/
/* */
/* Function : main */
/* */
/* Description : This is a CGI program which takes the output of a form */
/* submitted with a method of POST, and displays a list */
/* of the variable names and values. */
/* */
/******************************************************************************/
main(int argc, char *argv[], char *envp[])
{
char *requestMethod;
char *contentLength;
int argLength;
PPARMLIST pParm = NULL;
PPARMLIST pHead = NULL;
/* This CGI program must be called with a method of POST */
#ifdef _OE_
requestMethod = strdup(getenv("REQUEST_METHOD"));
#else
requestMethod = getenv("REQUEST_METHOD");
#endif
if ((requestMethod == NULL) ||
(stricmp(requestMethod, "POST")))
{
ErrMsg(HTML_ERROR_MSG1);
}
else
{
/* Get the length of the arguments passed in to this program */
#ifdef _OE_
contentLength = strdup(getenv("CONTENT_LENGTH"));
#else
contentLength = getenv("CONTENT_LENGTH");
#endif
if (contentLength == NULL)
{
ErrMsg(HTML_ERROR_MSG2);
}
else
{
/* Begin output of HTML to display results of CGI program */
printf("Content-type: text/html\n\n");
printf("<html>\n");
printf("<head>\n");
printf("<title>\n");
printf(HTML_TITLE);
printf("</title>\n");
printf("</head>\n");
printf("<body>\n");
printf("<h1>\n");
printf(HTML_HEAD);
printf("</h1>\n");
printf("<hr>\n");
printf("<p>\n");
/* Read the arguments passed in to this program and place them in */
/* a singly linked list - one link per variable */
argLength = atoi(contentLength);
pHead = ReadArguments(argLength);
pParm = pHead;
/* Output the list of variable names and values */
while (pParm)
{
printf("<pre>%s = %s</pre><p>",
pParm->VariableName,
pParm->Value);
pParm = pParm->pNext;
}
/* Output the remainder of the HTML used to display the results */
printf("<pre>");
printf("<p>\n");
printf("<hr>\n");
printf("</body>\n");
printf("</html>\n");
}
}
}
/******************************************************************************/
/* */
/* Function : ReadArguments */
/* */
/* Description : Read the arguments from stdin that are supplied */
/* to a CGI program when the method is POST. */
/* Breaks up the input into (Variable, Value) pairs. */
/* Handles translating of all the special characters */
/* that HTTP puts into the strings. */
/* */
/******************************************************************************/
PPARMLIST ReadArguments(int InputLength)
{
PPARMLIST pCur= NULL;
PPARMLIST pHead= NULL;
PPARMLIST pPrev= NULL;
char *Input;
char *pToken;
if (InputLength < 1)
{
return(NULL);
}
/* Allocate a buffer for the input */
Input = malloc(InputLength + 1);
if (Input == NULL)
{
return(NULL);
}
/* Read the input */
gets(Input);
/* Variables are separated by the "&" character */
pToken = strtok(Input, "&");
while (pToken)
{
/* Create and fill in linked list of variable information */
pCur = malloc(sizeof(PARMLIST));
pCur->VariableName = pToken;
pToken = strchr(pToken, '=');
if (pToken)
{
*pToken = '\0';
pCur->Value = ++pToken;
PlusesToSpaces( pToken );
TranslateEscapes( pToken );
}
else
{
pCur->Value = NULL;
}
if (pPrev)
{
pPrev->pNext = pCur;
}
if (!pHead)
{
pHead = pCur;
}
pPrev = pCur;
pToken = strtok(NULL, "&");
}
if (pHead)
{
pPrev->pNext = NULL;
}
return(pHead);
}
/******************************************************************************/
/* */
/* Function : PlusesToSpaces (STATIC) */
/* */
/* Description : This one's easy. It just translates any '+' */
/* characters found into ' ' characters. */
/* */
/******************************************************************************/
static void PlusesToSpaces(char *Str)
{
if (Str != NULL)
{
while (*Str != '\0')
{
if (*Str == '+')
{
*Str = ' ';
}
++Str;
}
}
}
/******************************************************************************/
/* */
/* Function : HexVal (STATIC) */
/* */
/* Description : This function returns a number that corresponds */
/* corresponds to the value of a character treated as */
/* a hex digit. Case insensitive. Characters outside */
/* 0-9,a-f,A-F have a value of 0. */
/* */
/******************************************************************************/
static int HexVal(char c)
{
int rc;
switch (c)
{
case '1':
rc = 1;
break;
case '2':
rc = 2;
break;
case '3':
rc = 3;
break;
case '4':
rc = 4;
break;
case '5':
rc = 5;
break;
case '6':
rc = 6;
break;
case '7':
rc = 7;
break;
case '8':
rc = 8;
break;
case '9':
rc = 9;
break;
case 'A':
case 'a':
rc = 10;
break;
case 'B':
case 'b':
rc = 11;
break;
case 'C':
case 'c':
rc = 12;
break;
case 'D':
case 'd':
rc = 13;
break;
case 'E':
case 'e':
rc = 14;
break;
case 'F':
case 'f':
rc = 15;
break;
default:
rc = 0;
break;
}
return(rc);
}
/******************************************************************************/
/* */
/* Function : TranslateEscapes (STATIC) */
/* */
/* Description : Translate the escape sequences induced by HTTP. The */
/* sequences consist of %xx, where xx is a hex number. */
/* We replace the % character with the actual character */
/* (i.e., the one whose ASCII value is xx), and then */
/* shift over the rest of the string to remove the xx. */
/* This is done in-place. */
/* */
/******************************************************************************/
static void TranslateEscapes(char *Str)
{
char *NextEscape;
char RealValue;
int AsciiValue;
NextEscape = strchr(Str, '%');
while (NextEscape != NULL)
{
AsciiValue = (16 * HexVal(NextEscape[1])) + HexVal(NextEscape[2]);
*NextEscape = (char) AsciiValue;
memmove(&NextEscape[1], &NextEscape[3], strlen(&NextEscape[3]) + 1);
NextEscape = strchr(&NextEscape[1], '%');
}
}
/******************************************************************************/
/* This function will output a message if an error occurs when attempting */
/* to display a HTML page. */
/******************************************************************************/
int ErrMsg (char *msg)
{
printf("Content-type: text/html\n\n");
printf("<html>\n");
printf("<head>\n");
printf("<title>\n");
printf(HTML_ERROR_TITLE);
printf("</title>\n");
printf("</head>\n");
printf("<body>\n");
printf("<h1>\n");
printf(HTML_ERROR_HEAD);
printf("</h1>\n");
printf("<hr>\n");
printf("<p>\n");
printf(HTML_ERROR_TEXT);
printf("<p>\n");
printf("<pre>%s</pre>\n<p>", msg);
printf("<p>\n");
printf("<hr>\n");
printf("</body>\n");
printf("</html>\n");
return(TRUE);
}