#!rexx 
/* **************************************************************************
 *  CGITEST.CMD - a REXX CGI script to test HTML forms
 *                returns an HTML page with the names and values
 *                of form elements
 *  Author:  Dirk Terrell  9/16/96
 *  modified: Dirk Terrell 11/30/96
 ************************************************************************* */
Parse Arg Argv
env = "OS2ENVIRONMENT"

/* Read in the product list */
i=0

/* The following two lines will not change */
Say "Content-type: text/html"
Say

/* Now comes the header of the returned HTML file */
Say "<HTML>"
Say "<HEAD>"
Say "<TITLE>Form Test Results</TITLE>"
Say "</HEAD>"
Say "<BODY>"
Say "<H1>Form Contents</H1>"

/*
   The following code checks to see which OS we're on.
   This is necessary because OS/2 and Unix use 
   different functions to get environment variables
*/
Parse Source OperatingSystem CallType

Select
   When OperatingSystem="OS/2" then do
      method = value("REQUEST_METHOD",,env)
      len = value("CONTENT_LENGTH",,env)
      if (method == "GET") Then Do
         query_string = value("QUERY_STRING",,env)
      end
      If (method == "POST") & (len \= "") Then Do
        /* use POST method to pass parameters */
        post_string = charin(,,len) 
        query_string = post_string
      End
   End
   When OperatingSystem="UNIX" then do /* We're on a Unix machine */
      method=getenv("REQUEST_METHOD")
      len = getenv("CONTENT_LENGTH")
      if (method=="GET")  then do
         query_string=getenv("QUERY_STRING")
      end
      If (method == "POST") & (len \= "") Then Do
        /* use POST method to pass parameters */
        post_string = charin(,,len) 
        query_string = post_string
      End
   End
   Otherwise
      NOP 
End /* Select */

NF = ParseQueryString( query_string) /* NF holds the # of fields */
Do j=1 to NF
   Say Parms.Tag.j "had a value of" Parms.XVal.j "<P>"
End

/* Now finish up the HTML */
Say "</BODY>"
Say "</HTML>"

return


/* Do not modify below this line --  Important parsing code... 
   From Frankie Fan's OS2HTTPD archive
*/
ParseQueryString: procedure expose Parms. NFields
  Parse arg P
  i = 1
  do while ((P \= '') & (i < 1000))
     Parse var P Parms.Text.i '&' rest
     Parse var Parms.Text.i Parms.Tag.i '=' Parms.KeyVal.i
     Parms.Tag.i = translate( Parms.Tag.i)
     Parms.XVal.i=DecodeKeyVal( Parms.KeyVal.i)
     P = rest
     i = i + 1
  end
  NFields = i - 1
  return NFields

DecodeKeyVal: procedure
  parse arg Code
  Text=''
  Code=translate(Code, ' ', '+')  /* Convert + signs to spaces */
  rest='%'
  do while (rest\='')
     Parse var Code T '%' rest
     Text=Text || T
     if (rest\='') then
      do
        ch = left( rest,2)
        c=X2C(ch)                /* Hex to character conversion */
        Text=Text || c
        Code=substr( rest, 3)
      end
  end
  return Text

