if ( requestMethod )
printf("<h4>REQUEST_METHOD:</h4>%s\n", requestMethod);
else
printf("Error extracting environment variable REQUEST_METHOD.\n");
/*------------------------------------------------------------------*/
/* html form data can be provided to the CGI program either on */
/* stdin or in environment variable QUERY_STRING. This can be */
/* determined by examining REQUEST_METHOD. */
/*------------------------------------------------------------------*/
if ( strcmp(requestMethod,"POST") ==0){
/*--------------------------------------------------------------*/
/* The REQUEST_METHOD is "POST". The environment variable */
/* CONTENT_LENGTH will tell us how many bytes of data to read */
/* from stdin. Note: CONTENT_LENGTH must be convert to an int. */
/*--------------------------------------------------------------*/
contentLenString = getenv("CONTENT_LENGTH");
contentLength = atoi(contentLenString);
/*--------------------------------------------------------------*/
/* Write CONTENT_LENGTH to stdout. */
/*--------------------------------------------------------------*/
printf("<h4>CONTENT_LENGTH:</h4>%i<br><br>\n",contentLength);
if ( contentLength ) {
/*----------------------------------------------------------*/
/* Allocate and set memory to read stdin data into. */
/*----------------------------------------------------------*/
stdInData = malloc(contentLength);
if ( stdInData )
memset(stdInData, 0x00, contentLength);
else
printf("ERROR: Unable to allocate memory\n");
/*----------------------------------------------------------*/
/* A CGI program MUST read standard input as a stream */
/* file only up to and including CONTENT_LENGTH bytes. */
/* Never should a program read more than CONTENT_LENGTH */
/* bytes. A CGI program that reads standard input must */
/* never depend on an end of file flag. This will cause */
/* unpredictable results when the CGI program reads */
/* standard input. */
/*----------------------------------------------------------*/
printf("<h4>Server standard input:</h4>\n");
bytesRead = fread((char*)stdInData, 1, contentLength, stdin);
/*----------------------------------------------------------*/
/* If we successfully read all bytes from stdin, format and */
/* write the data to stdout using the writeData function. */
/*----------------------------------------------------------*/
if ( bytesRead == contentLength )
writeData(stdInData, bytesRead);
else
printf("<br>Error reading standard input\n");
/*----------------------------------------------------------*/
/* Free the storage allocated to hold the stdin data. */
/*----------------------------------------------------------*/
free(stdInData);
} else
printf("<br><br><b>There is no standard input data.</b>");
} else if (strcmp(requestMethod, "GET") ==0){
/*--------------------------------------------------------------*/
/* The REQUEST_METHOD is "GET". The environment variable */
98 WebProgramming Guide V4R5