
# Parses the form
sub Parse_Form {
if ($ENV{'REQUEST_METHOD'} eq 'GET') {
   @pairs = split(/&/, $ENV{'QUERY_STRING'});
} elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
      read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
      @pairs = split(/&/, $buffer);

        if ($ENV{'QUERY_STRING'}) {
          @getpairs = split(/&/, $ENV{'QUERY_STRING'});
          push(@pairs, @getpairs);
        }
} else {
    &ErrorMessage("Must use Post or Get.");
  }

foreach $pair (@pairs) {
  ($key, $value) = split(/=/, $pair);
  $key =~ tr/+/ /;
  $key =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  $value =~ tr/+/ /;
  $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

  $value =~ s/<!--(.|\n)*-->//g;

if ($formdata{$key}) {
  $formdata{$key} .= ", $value";
} else {
  $formdata{$key} = $value;
  }
}
}

# Standard little error message routine
sub ErrorMessage
{
   my $msg = shift;
   print "Content-type: text/html\n\n$msg";
   exit;
}

# Prints a html file to the screen
sub Print_Htm {
 open(MYFILE, "<$_[0]") || &ErrorMessage("$_[0] not found in html directory");
 &Mime;
 print while <MYFILE>;
 close(MYFILE);
}

# Prints the mime header only
sub Mime {
  print "Content-type: text/html\nPragma: no-cache\n\n";
}

1;