A Native Fortran-77 CGI Interface

We want to pass input parameters and upload a file from a web browser to an f77 fortran program on an Apache web server, but were unable to find much usefull information about doing so. Our goal is to use no libraries, no Perl, and nothing beyond standard f77 just as it comes with FreeBSD. There are some limitations due to the lack of generality in f77 I/O, but it does do what we need quite straightforwardly and in only a few lines of code.

Parameter passing

Here is an example of a page asking for two values,passing them to a fortran program which writes an HTML response:

<html> Please fill each box with 4 characters.. <Form method="POST" action="./test1.cgi"> <Input name="val1" type="text" maxlength=20> <Input name="val2" type="text" maxlength=20> <Input Type="submit" value=" Submit"> </html> The "POST" method of parameter passing is chosen because it transmits data to the executable file through the standard input. The alternative "GET" method uses environment variables, which are not accessible from standard Fortran-77. The text passed to test1.cgi is in a decidedly fortran non-friendly string such as: &val1=abcd&val2=1234 This isn't easy to parse in fortran since the column locations of the values will drift across the string according to the lengths of names and values - but since you control the length of the variable names on the HTML page, you can control the column locations of the data in the special case that the variable contents are fixed length, which is the case for selections, checkboxes and radio buttons. In those cases a simple fixed format read can extract the variable data. For my example you have to enter 4 characters in each box to get the correct output but I don't propose this as a general solution. That is described later.

Here is test1.for which assumes a cooperative user:.

character*19 a write(*,100) 100 format('Content-type: text/html'//) read(*,*) a write(*,*) '<html>' write(*,*) 'Here is what test1.cgi sees:<br>' write(*,'(a72)') a write(*,*) '<p>' write(*,*) 'First box:',a(6:9),'<br>' write(*,*) 'Second box:',a(16:19),'</html>' stop end It needs to be compiled: f77 test1.for -o test1.cgi into test1.cgi (the .cgi filetype is required by Apache) but doesn't need any special libraries: You can't use free-format writes for the Content-type because that needs to start in column 1. The double slash is required, otherwise you will only get a server error ("Premature end of headers" in the log), as you will if you provide no header at all. Inconveniently, error messages go to error_log.

Since we used text boxes in the example, the program is rather specialized! I"ll discuss a better method for passing variable length stings below. There is the possibility that the browser will present the values in some other order - there is an option "tabindex" to prevent this, but I haven't needed it yet with recent versions of Safari, MSIE and Firefox, the only browsers I have tested.

Uploading files

We also need to let users upload an ASCII text file. Files are passed as MIME attachments to the standard input but is still quite simple. Here is the html for an example:

<html> <Form method="POST" action="./test2.cgi" ENCTYPE="multipart/form-data"> <input name="userfile" type="file" value=""><br> <Input Type="submit" value=" Submit"> </html> The browser supplies the file to the server as a MIME attachment. Here is the fortran code for the example, it copies the headers and the file back to the browser for display. character*72 a write(*,100) 100 format("Content-type: text/html"//) write(*,*)'<html>' do 10 i=1,20 read (*,'(a72)',end=99) a write(*,'(i2,1x,a72,a4)') i,a,"<br>" 10 continue 99 continue write(*,*) "</html>" stop end

I prepared a test file for uploading consisting of three lines "line 1", "line 2", and "line 3". Here is the result returned, as prepared by the browser for the CGI program:

1 -----------------------------6535751115256 2 Content-Disposition: form-data; name="userfile"; filename="New Text Docu 3 Content-Type: text/plain 4 5 line 1 6 line 2 7 line 3 8 -----------------------------6535751115256--

The first line establishes the file separator string, then several header lines descrbe what is being transmitted, then a blank line to signal end of headers, then the file itself, and then the file separator again (plus 2 more dashes). The 72 character input buffer wasn't long enough to hold the 2nd header line with the file name, so that was truncated and reading resumed after the newline.

There is a nusisance problem here that isn't obvious from running the test program above. The browser sends the file with a line ending convention crlf or (hex) 0d0a, while our server is Unix, which uses a single character (0a). f77 doesn't discard the 0d when reading, so each line will end with 0d. Reading a null line into a character variable gives you an 0d right filled with spaces. In practice I have found that the code snippet:

5 read(*,*) a if(a(1:1).ge.'!') goto 5 will skip non-blank lines because ASCII bang is bigger than any whitespace and less than any other printable character. With standard f77 it won't be possible to read binary files - that doesn't bother us at the moment.

I have tested this on our FreeBSD systems with MSIE and Firefox, with f77 and g95. A correspondent points out that gfortran will choke when asked to read an "a" format item that streches beyond the end-of-line. Instead of copying the available characters up to the end of line it continues to read onto the next line. This would make that compiler unsuitable for this purpose.

Variable length text fields

We do need to read variable length parameter fields. The trick is to use the "multi-part/form-data" MIME type, to separate the text and numeric fields onto separate lines, and then use a fortran free-format read on the data lines (skipping the headers). This form type also allows for uploading files. An example:

<html> Please fill in the blanks. <Form method="POST" action="./test2.cgi" ENCTYPE="multipart/form-data"> <Input name="val1" type="text" maxlength=40> <Input name="val2" type="text" maxlength=40> <br> <input name="userfile" type="file" value=""><br> <Input Type="submit" value=" Submit"> </html> With this method each parameter is passed as a separate MIME part to standard input, as is the file for uploading. Here is what is passed to the fortran (with prepended line numbers): 1 ------------0xKhTmLbOuNdArY 2 Content-Disposition: form-data; name="val1" 3 4 123.999 5 ------------0xKhTmLbOuNdArY 6 Content-Disposition: form-data; name="val2" 7 8 abcd 9 ------------0xKhTmLbOuNdArY 10 Content-Disposition: form-data; name="userfile"; filename="test.txt" 11 Content-Type: text/plain 12 13 line 1 14 line 2 15 line 3 16 17 ------------0xKhTmLbOuNdArY-- Here is the fortran to respond to that web page: character*72 a data x/-9999./,nrec/0/ write(*,100) 100 format("Content-type: text/html"//) write(*,*) "<html>Here are the values seen by fortran:<br>" 5 read(*,'(a72)') a if(a(1:1).gt.'!') goto 5 read(*,*,err=6) x 6 read(*,*) a if(a(1:1).gt.'!') goto 6 read(*,'(a20)') a write(*,*) 'x=',x,'<br>' write(*,*) 'a=',a,'<br>' write(*,*) 'Here is the file with line numbers added:<br>' 7 read(*,*) a if(a(1:1).gt.'!') goto 7 8 continue read(*,'(a72)',end=99) a nrec= nrec+1 if(a(1:10).ne.'----------') goto 8 write(*,'(i4,1x,a72,a4)') nrec,a,'<br>' 99 continue write(*,*) '</html>' stop end So far all my fields have been numeric, and the f77 free format read does regard 0d as a valid separator between numeric fields. Character reads will require deleting the last non-blank, as it will always be 0d.

Comments, suggestions and links are very welcome, please write to me at the address below. I am especially interested in the extent to which these techniques are browser, server, or compiler specific, or otherwise limited. Also, I expect that there may be some way to avoid the line ending problems that I am not aware of - if you know, I hope you will inform me.

I have heard from Clive Page that on his system the web page isn't sent to the browser till the Fortran program ends: I've tried all those things - closing the output, flushing it, etc. I've tried 3 compilers (g95, gfortran, and Nag) and all can close standard output as far as I can tell. I am pretty sure that the CGI output isn't declared to be complete until the process exits. So even if you send and might expect the page to be finished, the user sees the browser showing a busy condition, right until the CGI process (and any subprocesses) finally exit. That may be a function of our web server (Apache). This hasn't been my experience, nor can either of us understand what the difference in systems might be.

Another correspondent reports that his Intel compiler read statement is unable to read the standard input from the Microsoft web server. However, the getc function does work for him.

Recently I have been working with this and found the content-type line no longer showing the field name. I am not sure why this is so. Also, I notice that unchecked checkboxes do not show up in the standard input at all - making checkboxes unusable. Radioboxes do work.

An online version of echo.for is available at http://www.nber.org/sys-admin/fortran-cgi/echo.cgi (not a hot link) where you can use it for testing purposes. Just change the href in your web page to point to that page and and our server will echo back to you whatever the browser sends it. Sort of like a reverse "show source".

Daniel Feenberg
National Bureau of Economic Research
617-588-0343
feenberg isat nber dotte org

Sources and more information:

  1. rfc3875 is the official CGI interface documentation
  2. Mime form data type documentation
  3. Format of Internet Message Bodies
  4. Fortran 90 example
  5. A fortran 90 interface to CGI
  6. CGI with environment variables
  7. Another example
  8. A C-language interface callable from Fortran 77
  9. Fortran with AJAX
  10. A translation into Serbo-Croation by Jovana Milutinovich of this article.
  11. A translation into Ukrainian by Agnessa Petrova of this article.

Last revised Sep 11, 2012 by DRF