Sample Assignment: Using Your Polynomial in a CGI Program
mkdir public_html chmod 755 public_html
cd public_html mkdir cgi-bin chmod 755 cgi-bin
cp ~main/public_html/poly.html poly.html chmod 644 poly.htmlUse the editor to open up this file. If you know a bit about html programming, then you'll understand what I've done. For now though, you don't need to understand much: Just change my name (Michael Main) to yours at the top of the file. Then find this line:
ACTION="http://csel.cs.colorado.edu/cgi-bin/cgiwrap/~main/polycgi"This line tells the web server where to find the program that will process the form information. You should change the name
~mainto the tilde character (
~) followed by your login name instead of
Apart from this one change, you won't need to know any html programming for this assignment, but you might want to learn some for fun, using a tutorial such as http://www.mit.edu/afs/athena/user/w/s/wsmart/WEB/tutorial/HTMLtutor.html.
cdcommand), and copy your most recent version of the polynomial (
poly2.h) into the directory. Note: If you use ftp to transfer the files from a PC you should first type the command
asciibefore transfering the files. This will correctly handle the different forms of end-line that are used.
cgithat simplifys writing a CGI program. The file was written by S. Martin at Marketrends Productions ( www.marketrends.net/C++/).
cgi, for example:
cgiobject to get information from the form that you are processing or to write information back to the user of the form.
#include <cstdlib> // Provides EXIT_SUCCESS, atof #include <iostream> // Provides cout #include <string> // Provides string class #include "CGI.h" // From www.marketrends.net/C++/ #include "poly2.h" // My polynomial class using namespace std; using namespace main_savitch_5;
atof(polyinfo.get_value("coef2"));You'll set the values for the four bounds in a similar way using these keys for the
get_valuemember function: "low_x", "high_x", "low_y" and "high_y".
systemcommand to execute a Unix command that will erase these files:
system("rm -f ../poly*.gif");The
rm -fmeans to remove files with no warning messages if there is an error. The
../poly*.gifgets any file that starts with
polyand ends with
.gifin the directory that is one level up (indicated by the
../. As you'll see in a moment, these gif files will be present in your public_html directory, whereas the CGI program is running in public_html/cgi-bin, and this is why you need to specify that the files are "one level up".
filename = "poly" + colon + polyinfo.get_value("coef0") + colon + polyinfo.get_value("coef1") + colon + polyinfo.get_value("coef2") + colon + polyinfo.get_value("coef3") + colon + polyinfo.get_value("coef4") + colon + polyinfo.get_value("low_x") + colon + polyinfo.get_value("high_x") + colon + polyinfo.get_value("low_y") + colon + polyinfo.get_value("high_y") + colon + ".gif";Each number is separated from the next one by a colon. This will result in some long and strange file names such as this:
poly:1:0:1)2:0:-2:2:-4:4:.gifAfter you create this basic file name, create two more names:
write_to: This is the name of the file that you will actually write to. It is the filename defined above with
"../"tacked on to the front.
url: This is the name of the file that will be used in a web page to actually display the gif. It is the filename defined above, with
http://csel.cs.colorado.edu/~YOURLOGIN/"tacked to the front. (Of course, use your actual login name in this!)
make_giffunction to actually write that gif file. Use
write_to.c_str( )as the name of the file. (The c_str converts it from a C++ string to an ordinary C-style string.)
polyinfo.start_html("STUDENT NAME", "Polynomial Display", "white"); cout << "<H2>Graph of Your Polynomial</H2>" << endl; cout << "Polynomial: " << p << endl; cout << "<BR>Range of x: " << low_x << " to " << high_x << endl; cout << "<BR>Range of y: " << low_y << " to " << high_y << endl; cout << "<P><IMG SRC =\"" << url << "\" ALIGN=\"CENTER\">" << endl; polyinfo.end_html( );
g++ --version. The answer should be 2.95.2 or later. If not, then you can reset your .cshrc file by typing the command
reset-world, then log out and log back in.
All your compilations must occur on one of the Solaris PC machines listed at the bottom of http://csel.cs.colorado.edu/udp/machines.html. For example, if you want to do your compilations on swallow, then you should first do a secure remote login to swallow, like this:
slogin swallowYou have to use these Solaris PC machines because the executables they generate are the right format for our csel web server.
You'll compile your file in the usual way. For example, my makefile has these items (with tabs before the g++ commands, of course):
polycgi: polycgi.o poly2.o g++ -gstabs -Wall polycgi.o poly2.o -o polycgi polycgi.o: polycgi.cxx poly2.h CGI.h g++ -gstabs -Wall -c polycgi.cxx poly2.o: poly2.cxx poly2.h g++ -gstabs -Wall -c poly2.cxx
After you compile your polycgi, you should copy the executable to your cgi-bin directory and make sure that the world can execute it with these commands:
cp polycgi ~/public_html/cgi-bin chmod 755 ~/public_html/cgi-bin/polycgi
Where do you go to submit that form and run the program? The answer is this URL, with your login name...