Using Fortran programs as CGI scripts
|
|
|
|
Introduction |
How the library routines work |
Documentation provided on this page |
Source code distribution |
Introduction
In the last weeks (time of writing is 10 February 1999) I encountered a few posts
on comp.lang.fortran and the comp.infosystems.www.* newsgroups dealing with usage
of Fortran programs as back end for CGI executables called from HTML forms.
Worth mentioning among these the
examples by D.Bickel
If you do not know what CGI are refer to the
NCSA tutorial.
Since it was at least a couple of years that the idea of a general library
for using CGI in Fortran programs (I mean directly, not via an intermediate
shell script as I did so far) was lurking in my mind, the above postings instigated
me to put my hands on the keyboard and write down the code.
One more proof that
Real Programmers
Do It in Fortran :-)
L.Chiappetti - 11 February 1999
modified cgiheader.f 18 October 2000
modified Bickel's link 20 May 2002
How to do it
The library addresses the following problems :
- Retrieving the query string generated by the HTML form. This is handled in
both the POST and the GET method. The main difference is that the POST method
reads from standard input a known number of bytes passed in an environment
variable. The GET method passes the query string in an environment variable
without any indication of the length.
The routine CGIRETRIEVE loads the query string in common and is
called transparently by the first call of one of the following two parsing
routines.
- The query string contains a list of variable names and values in a compact,
encoded form, and must be parsed. Two parsing routines are provided.
- The routine GET_NEXT_VARIABLE can be called repeatedly to parse all
variables in the order in which they occur, returning the name and value.
- The routine GET_NAMED_VARIABLE can be called to parse a known named
variable at a time in random order, returning the value if such variable is
found.
- The string components in the query string are URL-encoded (see RFC...). The
decoding is handled transparently by routine URLDECODE, while the
reverse URLENCODE is needed for service purposes to check the match
of variable names.
- once you have decoded the variables, you can do whatever processing you like ...
... that's not my business :-)
- to produce HTML output (or text, or any other output) you must write a valid HTML
header on standard output, this is done by routine CGIHEADER
- the rest of the HTML code can be written by plain WRITE statements, however a
little utility routine CGITOKEN is provided to write strings of the
form <any>string</any>.
Material provided
The material I provide here (without any express or implied guarantee etc. etc.)
for demonstration purposes is :
- some ripped-off HTML forms of my own, in which I replaced the original CGI script
with a test script which essentially lists all the variables in the form, and
demonstrates a few features. These forms are :
- a simple POST method example
- the same example with GET method
- a longer example of POST and
GET method demonstrating errors one
can get if the query string is longer than the buffer destined to hold it
(I know how to deal with it by dynamic memory allocation, but that will
complicate the tutorial, and I intend to keep it private so far)
Note that at the moment I'm keeping the size of the buffers in
cgi.inc very small (256 characters)
just to demonstrate the error. Behaviour may change without notice.
With more suitable larger values no
error will be generated by an empty form, but could be generated by
filling it. Also the error is signalled only by the POST method, while
GET silently truncates the query.
- an HTML commented
copy of the source of the test program, which is also provided in
source form
- source code of the CGI library routines, with
some additional comments
Source code distribution
Since the relevant material consists of quite a few lines of code, I consider
simple to give access to each source file, more than preparing gzipped tar files
or anything like that. You should just retrieve via Web (Save As...)
all the source files listed below and
read the relevant comments. Then compile the routines and place them in a relocatable library, or anyhow link the test program, or any CGI program of
yours against them in the way you are accustomed to.
Portability issues
Not all routines listed below have been written ad-hoc. A few of them (those "below the red line"
are taken by libraries I developed for other purposes). The latter have either been extensively used on several OSs (starting from VAX/VMS to SunOS, Ultrix, HP-UX, Digital Unix and
Solaris). The few routines for which a separate version is necessary for different systems are clearly indicated.
However note I did not do any extensive tests on my new CGI routines. I just tried
them on my DU 3.2 Alpha with NCSA httpd 1.5.2. While I did not expect any portability issue in most of my code, I must say I was pleasantly surprised to see that the POST method worked
with plain Fortran i/o. In the past I noticed in fact that the POST method (which uses stdin
to communicate between form and CGI) sometimes had problems :
- in some cases httpd seemed to append a carriage-return (control-M) to the end of
the query string, which then makes its way into the value of the last variable
- in other cases on Digital machines, if the last string on stdin (or any file) is
not terminated by a line-feed it's ignored by a Fortran READ
sax.iasf-milano.inaf.it/~lucio/WWW/cgi-lib/
:: original creation 2005 apr 13 12:22:07 CEST ::
last edit 2005 Apr 13 12:22:07 CEST