To understand CGI, it is important to understand HTTP. There are six phases of HTTP: 3 by the browser (request), 3 by the server (response).
After studying calculus, it doesn't take long to see that 5% of any problem is calculus. The remaining 95% is algebra. CGI programming is the same way: 5% is CGI, 95% is programming. There are only a few assumptions that are being made:
Pragma: no-cache
Content-type: text/html
Location: /some/new/page.html
I have added a library name CGILib that contains useful functions for doing CGI scripts. These are the functions that are in it. I have indicated the parameters that are expected, and the return type of the function.
# File: CGILib.pm # hash get_query() # Return a hash array of all the name value pairs in the QUERY_STRING variable, or from STDIN # hash get_cookie() # Return a hash array of all the name value pairs in the HTTP_COOKIE variable # string time_gmt(seconds) # Return a GMT formatted string from the current time pluse seconds # string url_encode(string) # Return a string that has special characters translated to %xx # string url_decode(string) # Return a string that translates %xx back to special characters # string html_encode(string) # Return a string that translates & < > to & < > # void print_head(title, bgcolor) # Print the standard HTML codes, setting the title and background color # void print_HTTP_header(mime_type) # Print the Content-type header for the given mime type, text/html is the default # void print_tail() # Print the standard ending tags for an HTML page. # void display_file(system_path, mime_type, mode) # Print a file to standard out with the given content-type # Use a non-zero mode to indicate text mode instead of the default binary mode. # void redirect(url) # Print the relocation header for the given URL
To use CGILib, add these statements to any Perl script
use lib '/home/scsfac/downeyt/public/scripts';
use CGILib;
There are two ways to generate output for the browser
Use the get_query and get_cookie methods to parse the appropriate
environment variable into a hash array.
my %query = get_query();
if ($query{fullname} eq "") {
print "You must fill in your full name";
}
Use the url_encode and url_decode to parse any information that you are sending in the repsonse header.
Use the time_gmt function to create a string that corresponds to a time. This can be used when settin the expiration time for a cookie or for a cached document.
Use html_encode to encode special characters that are being sent to the browser.
Syntax errors will generate 500 Internal Server Errors when the script is run from the web. The first syntax error will be placed into the error log of your server. However, syntax errors rarely travel alone. It is better to see a lot of them at once. You can test your script for syntax errors from the shell prompt.
cd ~/COP3832/server/cgi-bin
./image.cgi
./
so that the operating system
knows to look in the current directory for the script.