Writing CGI executbales with Modula-2.
Nothing beats an example. This is my first CGI executable, written in Modula-2 and compiled for Linux.
Although it also runs on a server running FreeBSD.
The file is called 'testCGI' and it does just that. It lists all the environment variables that belong to the
CGI system on the server it is installed on.
'testCGI' was written with the Mocka Modula-2 compiler. This results in clear sources and fast execution since this is not an interpreted language. OK, the executable is slightly bigger than a few lines of Perl, but a compiled executable is tamperproof. You can only change it if you know:
Show the CGI environment with Modula-2.
MODULE testCGI;
FROM InOut IMPORT WriteCard, WriteLn, WriteString, WriteBf;
FROM Arguments IMPORT ArgTable, GetEnv;
FROM Strings IMPORT Assign, Length;
TYPE aString = ARRAY [0 .. 511] OF CHAR;
VAR EnvVars : ArgTable;
PROCEDURE ReadEnvVar;
VAR Value : aString;
i : CARDINAL;
BEGIN
WriteString ('<table border="1" width="80%" align="center" cellpadding="4">');
i := 0;
LOOP
IF EnvVars^ [i] = NIL THEN EXIT END;
Assign (Value, EnvVars^ [i]^);
WriteString ("<tr><td>");
WriteCard (i, 4);
WriteString (' : ['); WriteCard (Length (Value), 4);
WriteString ('] </td><td>'); WriteString (Value);
WriteString ("</td></tr>");
WriteLn;
INC (i)
END;
WriteString("</table>");
END ReadEnvVar;
BEGIN
GetEnv (EnvVars);
WriteString ('Content-type:text/html');
WriteLn;
WriteLn;
WriteString ('<html><head>');
WriteString ('<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">');
WriteString ('</head><body>');
WriteString ('<h3 align=center><tt>cgi compiled with Mocka Modula-2</tt></h3>');
ReadEnvVar;
WriteString ('</body></html>');
WriteBf;
END testCGI.
How it works.
You can run 'testCGI' in a terminal to list the Unix environment variables (although it will be a bit tough to
read, try it!) but it was meant to explore the environment variables that are active when you're browsing the
web.
These environment variables are then the CGI (Common Gateway Interface) variables. They enable the browser to
communicate with the (remote) server and exchange data.
The 'testCGI' program gets these environment variables from the operating system and prints them one by one in the active window of your web browser. To make reading a bit easier, testCGI exports it's data in HTML format so the browser will print a two column table.
testCGI is fully based upon the operation of 'GetEnv' (qv). The GetEnv program (called 'trye') just prints
characters to the user console. But we're in HTML country now so we'd better fit in with the rest.
So I made 'testCGI' to deliver HTML data to be interpreted by a browser. In order to facilitate this, there
are some STRICT rules that 'must' be followed to the letter:
Explanation of the Modula-2 CGI executable.
This is a Modula-2 program, so if you know some english, you can at least read the sources and understand 90% of it's operation. In order to fully understand the flow of execution of the program for those 10%, I will explain in some more detail below:
Wrapping up.
You can download the sources and the executable for 'testCGI' in the download section (see navigator frame on
the right). The source is handy for studying this program and can be printed well with soup and emit (consult
navigator frame).
You will need to do the following if you want to run the 'testCGI' executable in a Unix environment:
| Unpack the tar.gz file |
gzip -d testcgi.tar.gz tar xvf testcgi.tar |
| for testing the executable |
chmod 755 testCGI testCGI |
| for running the CGI 'script' |
su root <enter password> cp testCGI /var/www/cgi-bin/ exit <start your browser> surf to 'http://yourserver/cgi-bin/testCGI' |
Making it active on the web.
What use is a bicycle in the shed? Nothing. You need to ride it to make it worth the effort of making it.
Same with this testCGI executable. You need to run it. Not on your own computer, but out there, in the harsh
world of the internet. So I put it on-line at my webhost.
One remark however: testCGI is an executable, not a script. So it will only run on a server running Linux. If
your webhost runs under Windows, you cannot run testCGI. You could try, but chances are you will halt this
magnificent Windows computer...
Nuf said. Time to act. The following steps were taken by me to get testCGI online and working:
Horsing around with 'testCGI'.
If you want to check it out, run the script in your browser (I will open a new window for your convenience):
run testCGI now
via my webhost: http://verhoeven272.nl/cgi-bin/testCGI.
If you are running this on a local machine, like I do when debugging,
run it local
via http://192.168.56.4/cgi-bin/testCGI.
Now you can also do some tests with the commandline of a CGI script. In the URL window, add the following strings after '/testCGI' and watch how the output changes:
By now you should have the knack of CGI operation and production. If I can do it in Modula-2, it should be a
snap to do a comparable thing with gcc.
But keep in mind that we do NOT make scripts that are interpreted on all platforms. We make executables that
will only run on the platform it was developed for. In my case: Linux.
Have fun with the experiments. Come back now and then to see what I added to the CGI events.
Page created on August 25, 2004.
Page equipped with FroogleBuster technology