How to handle messages sent via WWW forms

The way I do it, I use the same cgi-bin script for all my various "comment" forms, assuming that the URL used in the form declaration will contain the name of the file in which I want to archive the message, right after the name of the cgi-bin script (I want to archive the message in an HTML document at the same time I have it emailed to me by the www server machine). My www server runs on a Sun workstation running SunOS 4.1.3. If you're using a PC or a Mac, I don't know how much of what follows will still apply. Here is the cgi-bin script I wrote:

file ~www/cgi-bin/suggest

#!/bin/sh

if test "$REQUEST_METHOD" = GET
then
  query=$QUERY_STRING
elif test "$REQUEST_METHOD" = POST
then
  read query
fi

if test "$REQUEST_METHOD" = HEAD
then
  cat << eof
Content-type: text/html

<html>
<head>
<title>Suggestion form - document generated on-the-fly by a cgi-bin script</title>
</head>
</html>
eof
# Check if empty query, in which case don't archive message, but
# offer more info.
#
elif test "$query" = ""; then
  cat /user/ub2/eao/www/suggest/TestAck.txt
else
  # figure out the name of the file in which to archive the message
  #
  filename=`basename $PATH_INFO`
  FullPath=/user/ub2/eao/www/suggest/$filename.html

  # start appending HTML to the archive file, including the date the 
  # message is received
  #
  echo "<dl><dt>" >> $FullPath
  date >> $FullPath
  echo ", from remote host $REMOTE_HOST" >> $FullPath
  echo "<dd>" >> $FullPath

  # filter suggestion and send it to bertrand@cui.unige.ch via email as well as
  # appending it to suggestion file ($FullPath)
  #
  echo "$query"|/user/ub2/eao/www/ncsa_httpd/cgi-bin/filter| \
    tee -a $FullPath |/usr/lib/sendmail bertrand@cui.unige.ch -s \
    "added suggestion in $filename from $REMOTE_HOST ($REMOTE_ADDR)"
  echo "</dl>" >> $FullPath

  # send acknowledgement to the correspondent, as a reply to the form
  # request she/he has just used
  #
  cat /user/ub2/eao/www/suggest/Ack.txt

fi

exit 0
The "filter" program is a little Pascal program I wrote to convert all the "special" characters that are encoded in the query string as #xx into HTML syntax. Here it is:

file ~www/cgi-bin/filter.pas

{ program used in the "suggest" script to filter characters encoded in query.

Name            Date    Modification
B.Ibrahim       940701  first draft

This program works as a filter. It takes text in standard input, applies
some transformation and returns the result on standard output.
The transformations are the following:

'+' -> ' '
'&' -> '<br>'
'%0A' -> '<br>'
'%xx' -> '&#yy;' where xx represent a two hexadecimal digit value and
                       yy is this same value in decimal.
anything else -> same character

}
program filter;

var c,c2:char;
    i: integer;
    Conv: array [' '..'~'] of integer;

begin
  for c := ' ' to '~' do Conv[c] := 0;
  for c := '1' to '9' do Conv[c] := ord(c)-ord('0');
  for c := 'A' to 'F' do Conv[c] := 10+ord(c)-ord('A');
  while not eof do begin
    read(c);
    if c='+' then write(' ')
    else if c='%' then begin
      read(c,c2);
      if (c='0') and (c2='A') then writeln('<br>')
      else begin
        i:= (Conv[c])*16 + Conv[c2];
        write('&#',i:1,';');
        end { if (c=... }
      end { if c='%' }
    else if c = '&' then writeln('<br>')
    else write(c);
    end; {while}
end.
Bertrand Ibrahim

Site Hosting: Bronco