| CGI Programming and You | ||
|---|---|---|
| Prev | ||
It is often useful to be able to start CGI programs without having to go through the trouble of using the web-based interface. For instance, perl programs which do not work correctly will simply generate an error when started through the web server, and will not generate any output. The programmer may want to run a debugger on the CGI program, which is impossible when the CGI program is run through the web server.
However, since the form output from the web server to the CGI program is in a well defined format, we can generate any given form data for which we would like to examine the effect on the program [for debugging purposes]. Alternatively, a simple sample script can be used to capture the form data, already converted into the correct format by the web server, to a file to later be piped into our program to be debugged. This script is available below, and is also available as formDataSave_cgi.txt.
#!/usr/bin/perl
print "Content-Type: text/html\n\n";
# Print HTML header
print <<EOHH;
<HTML>
<HEAD>
<TITLE>formDataSave.cgi</TITLE>
</HEAD>
<BODY BGCOLOR="#ffffff">
EOHH
# Get the request method
$reqMeth = $ENV{"REQUEST_METHOD"};
# Open the file to which to save the form data
open(FD, ">form.data") || print "<H1>Error opening \"form.data\"</H1>\n";
print "Form Data: ";
if ($reqMeth eq "GET") {
# If the method is "GET", the data is in the REQUEST_METHOD
# environment variable
print $ENV{"QUERY_STRING"};
print FD $ENV{"QUERY_STRING"};
} elsif ($reqMeth eq "POST") {
# If the method is "POST", the data is one line on standard input
$line = <STDIN>;
print $line;
print FD $line;
} else {
# Otherwise, we don't know.
print "<H1>Cannot determine form data method</H1>\n";
} # end if-elsif-else
# Close the file
close FD;
# Print the HTML footer
print <<EOHF;
</BODY>
</HTML>
EOHF |
An important point to remember is to set the CGI environment variables correctly, as would be setup by the web server. In effect, you must create a similar environment to that which the web server would create for execution of the CGI program. The important environment variables to set are the REQUEST_METHOD and the CONTENT_LENGTH variables. In addition, any other environment variables which the program uses [HTTP_USER_AGENT, REMOTE_ADDR, etc.] should be set to appropriate values.
The REQUEST_METHOD variable should be set to the appropriate method by which the form data would be received by the CGI program. In addition, the form data should be placed in the appropriate place. If the form is POSTed, REQUEST_METHOD should be set to "POST", and the form data should be piped in on standard input. If the form data is GETed, REQUEST_METHOD should be set to "GET" and the form data should be placed in the QUERY_STRING environment variable.
If the data is POSTed, then the CONTENT_LENGTH environment variable should be set to the length of the file which will be piped into the program's standard input. This will allow the CGI program to read the correct amount of data.
When using a command-line environment and the "POST" method, the faux-web-server form data should be piped directly into the program.
[~/public_html] % cat form.data | program_name.cgi << CGI output for that form data follows >> |
When using a command-line environment and the "GET" method, the faux-web-server form data should be put in the QUERY_STRING environment variable.
(with sh or bash) [~/public_html] % export QUERY_STRING=`cat form.data` (with csh or tcsh) [~/public_html] % setenv QUERY_STRING `cat form.data` |
After the form data is in the correct location, the program can be run and the output will come as the web server would receive it.