home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / WEBSERVE / PI3 / PI3WEB.EXE / DEVEL / CGI / PostForm.c < prev    next >
C/C++ Source or Header  |  1997-10-22  |  5KB  |  159 lines

  1. /*____________________________________________________________________________*\
  2.  *
  3.  
  4.  Copyright (c) 1997 John Roy. All rights reserved.
  5.  
  6.  These sources, libraries and applications are
  7.  FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
  8.  as long as the following conditions are adhered to.
  9.  
  10.  Redistribution and use in source and binary forms, with or without
  11.  modification, are permitted provided that the following conditions
  12.  are met:
  13.  
  14.  1. Redistributions of source code must retain the above copyright
  15.     notice, this list of conditions and the following disclaimer. 
  16.  
  17.  2. Redistributions in binary form must reproduce the above copyright
  18.     notice, this list of conditions and the following disclaimer in
  19.     the documentation and/or other materials provided with the
  20.     distribution.
  21.  
  22.  3. Redistributions of any form whatsoever and all advertising materials 
  23.     mentioning features must contain the following
  24.     acknowledgment:
  25.     "This product includes software developed by John Roy
  26.     (http://www.johnroy.com/pi3/)."
  27.  
  28.  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  29.  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30.  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  31.  IN NO EVENT SHALL THE AUTHORS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  32.  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33.  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34.  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35.  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  36.  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  37.  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  38.  OF THE POSSIBILITY OF SUCH DAMAGE.
  39.  
  40.  *____________________________________________________________________________*|
  41.  *
  42.  * $Source: Hello.c$
  43.  * $Date: Sun Aug 10 06:41:30 1997$
  44.  *
  45.  Description:
  46.     Get CGI Post method.
  47.     
  48.     Speaks HTTP/1.0
  49. \*____________________________________________________________________________*/
  50. /* $SourceTop:$ */
  51.  
  52. #include <stdlib.h>
  53. #include <stdio.h>
  54. #include <time.h>
  55.  
  56. /*____________________________________________________________________________*\
  57.  *
  58.  Description:
  59. \*____________________________________________________________________________*/
  60. extern char **_environ;
  61.  
  62. /*____________________________________________________________________________*\
  63.  *
  64.  Function:
  65.  Synopsis:
  66.  Description:
  67. \*____________________________________________________________________________*/
  68. int Error( const char *pErrorMessage, int iLen, char *pInput )
  69. {
  70.     const char *pCurrent;
  71.     int iIndex;
  72.  
  73.     printf("Content-Type: text/html\n");
  74.     printf("\n" );
  75.     printf("<HTML>\n");
  76.     printf("<TITLE>PostForm: Error</TITLE>\n");
  77.     printf("<BODY>\n");
  78.     printf("<H2>PostForm: Error</H2>\n");
  79.     printf("%s<BR>\n", pErrorMessage);
  80.     printf("<BR>Stdin data length:<BR><B>%d</B><BR>\n", iLen );
  81.     if ( iLen>0 && pInput )
  82.         {
  83.         printf("<BR>Stdin data:<BR><B>%s</B><BR>\n", pInput );
  84.         };
  85.     printf("<BR>Environment variables follow:<BR>\n");
  86.     printf("<BR><B>\n");
  87.  
  88.     for( iIndex = 0; ; iIndex++ )
  89.         {
  90.         pCurrent = _environ[iIndex];
  91.         if ( !pCurrent ) { break; };
  92.  
  93.         printf( "<CODE>%s</CODE><BR>\n", pCurrent );
  94.         };
  95.     printf("<BR></B>\n");
  96.  
  97.     printf("</BODY>\n");
  98.  
  99.     exit( 0 );
  100. }
  101.  
  102. /*____________________________________________________________________________*\
  103.  *
  104.  Function:
  105.  Synopsis:
  106.  Description:
  107. \*____________________________________________________________________________*/
  108. int main( int iArgc, const char *ppArgv[] )
  109. {
  110.     const char *pContentLength;
  111.     const char *pContentType;
  112.     int iInLen;
  113.     char *pInputData;
  114.     int iRLen;
  115.  
  116.     pContentLength = getenv( "CONTENT_LENGTH" );
  117.     if ( !pContentLength )
  118.         { Error( "Couldn\'t read 'CONTENT_LENGTH'", 0, 0 ); };
  119.  
  120.     pContentType = getenv( "CONTENT_TYPE" );
  121.     if ( !pContentType )
  122.         { Error( "Couldn\'t read 'CONTENT_TYPE'", 0, 0 ); };
  123.  
  124.     iInLen = atoi( pContentLength );
  125.     if ( iInLen<=0 )
  126.         { Error( "CONTENT_LENGTH <= 0", 0, 0 ); };
  127.  
  128.     /*
  129.     ** Allocate buffer for data
  130.     */
  131.     pInputData = (char *)malloc( iInLen+1 ); 
  132.  
  133.     /*
  134.     ** Read data
  135.     */
  136.     iRLen = fread( pInputData, sizeof( char ), iInLen, stdin );
  137.     if ( iRLen!=iInLen )
  138.         {
  139.         Error( "Wrong number of bytes read in fread()", iRLen,
  140.             pInputData ); 
  141.         };
  142.     pInputData[iInLen] = '\0';
  143.  
  144.     /* --- Hello World! --- */
  145.     printf("Content-Type: text/html\n");
  146.     printf("\n" );
  147.     printf("<HTML>\n");
  148.     printf("<TITLE>CGI Post Form Program Output</TITLE>\n");
  149.     printf("<BODY>\n");
  150.     printf("<H1>CGI Post Form Program Output</H1>\n");
  151.     printf("CONTENT_TYPE: <CODE>%s</CODE><BR><BR>\n", pContentType );
  152.     printf("%d Bytes read:<BR>\n<VERBATIM>%s</VERBATIM><BR><BR>\n",
  153.         iInLen, pInputData );
  154.     printf("</BODY>\n");
  155.  
  156.     return 0;
  157. }
  158.  
  159.