Guide to Writing CGI Scripts in REXX and Perl

Last Update: July 24, 1998.
[ SLAC Utilities | cgi-lib.rxx | Security Wrapper | Security Concerns ]
[ Translations: Bulgarian1 | German2 | Serbo-Croatian3 ]

Contents


Introduction

This Guide is aimed at people who wish to write their own WWW executable scripts using WWW's Common Gateway Interface ( CGI). Though the main emphasis is on REXX many examples are also provided in Perl.

There are some simple software libraries to facilitate writing CGI scripts. cgi-lib.rxx is a REXX library of functions (available at SLAC by using the REXX
CALL PUTENV 'REXXPATH=/afs/slac/www/slac/www/tool/cgi-rexx'
statement to include the library at execution time)and cgi-lib.pl is a similar library in Perl written by Steve Brenner (there is an executable copy of this libary at SLAC in /afs/slac/g/www/cgi-lib/cgi-lib.pl). NCSA has a very useful set of Perl CGI handler subroutines that are available via anonymous FTP.Another set of Perl CGI Server Side Scripts written by Brigitte Jellinek is available under Gnu public license. There is also the Source code for www.stanford.edu scripts and programs. There is also an index to Perl WWW programs gathered by Earl Hood. Finally see the Web Development Center.

Since there are security and other risks associated with executing user scripts in a WWW server, the reader may wish to first view a document providing information on a SLAC Security Wrapper for users' CGI scripts. Besides improving security, this wrapper also simplifies the task of writing a CGI script for a beginner.

Before embarking on writing a script, you may also want to check out some rough notes on SLAC Web Utilities Provided by CGI Scripts.

The CGI is an interface for running external programs, or gateways, under an information server. Currently, the supported information servers are HTTP (the Transport Protocol used by WWW) servers.

Gateway programs are executable programs (e.g. UNIX scripts) which can be run by themselves (but you wouldn't want to except for debugging purposes). They have been made executable to allow them to run under various (possibly very different) information servers interchangeably. Gateway programs conforming to this specification can be written in any language, including REXX or Perl, which produces an executable file

Getting the Input to the Script

The input may be sent to the script in several ways depending on the client's Uniform Resource Locator (URL) or an HyperText Markup Language (HTML) Form: You can review the REXX Code Fragment giving an example of how to read the various form of input into your script.

The REXX PROCEDUREs ReadForm together with MethGet and MethPost, all available in cgi-lib.rxx, may be used to simplify the task of reading input from a Form.

Decoding Forms Input

When you write a Form, each of your input items has a name tag. When the user places data in these items in the Form, that information is encoded into the Form data. The value each of the input items is given by the user is called the value.

Form data is a stream of name=value pairs separated by the ampersand (&) character. Each name=value pair is URL encoded, i.e. spaces are changed into plus signs and some characters are encoded into hexadecimal. To decode the Form data you must first parse the Form data block into separate name=value pairs tossing out the ampersands. Then you must parse each name=value pair into the separate name and value. Use the first equal sign you encounter to split the data. If there is more than one, then something is wrong with the data. Again toss out the equals signs. Finally undo the URL encoding of each name and value.

You can review the REXX or the Perl code fragment giving examples of decoding the Form input.

When using the name and value information in the script, you need to be aware that:

Sending Document Back to Client

CGI programs can return a myriad of document types. They can send back an image to the client, an HTML document, a plaintext document, a Postscript documents or perhaps even an audio clip of your bodily functions. They can also return references to other documents (to save space we will ignore this latter case here, more information may be found in NCSA's CGI Primer). The client must know what kind of document you're sending it so it can present it accordingly. In order for the client to know this, your CGI program must tell the server what type of document it is returning.

In order to tell the server what kind of document you are sending back, CGI requires you to place a short header on your output. This header is ASCII text, consisting of lines separated by either linefeeds or carriage returns followed by linefeeds. Your script must output at least two such lines before its data will be sent directly back to the client. These lines are used to indicate the MIME type of the following document

Some common MIME types relevant to WWW are:

In order to tell the server your output's content type, the first line of your output should read:
Content-type: type/subtype
where type/subtype is the MIME type and subtype for your output.

Next, you have to send the second line. With the current specification, THE SECOND LINE SHOULD BE BLANK. This means that it should have nothing on it except a linefeed. Once the server retrieves this line, it knows that you're finished telling the server about your output and will now begin the actual output. If you skip this line, the server will attempt to parse your output trying to find further information about your request and you will become very unhappy.

You can review a REXX Code Fragment giving an example of handling the Content-type information.

After these two lines have been outputted, any output to stdout (e.g. a REXX SAY command) will be included in the document sent to the client. This output must be consistent with the Content-type header. For example if the header specified Content-type text/html then the following output must include HTML formatting such as using <BR> or <P> for starting new lines or <PRE> to remove HTML's automatic formatting.

Diagnostics and Reporting Errors

Since stdout is included in the document sent to the, diagnostics diagnostics outputted with the SAY command will appear in the document. You can review a REXX Code Fragment giving an example of diagnostic reporting.

If errors are encountered (e.g. no input provided, invalid characters found, too many arguments specified, requested an invalid command to be executed, invalid syntax or undefined variable encountered in the REXX script) the script should provide detailed information on what is wrong etc. It may be very useful to provide information on the settings of various WWW Environment Variables that are set.

The CGIerror, CGIdie and MyURL REXX PROCEDUREs in cgi-lib.rxx provide some assistance for error reporting. In addition review the REXX code fragments using CGIerror and using CGIdie and also typical CGIerror output and CGIdie output.

Two Simple REXX WWW CGI Scripts

To get your Web server to execute a CGI script you must: The Web-Master will want to insure that Security Aspects of your script have been addressed before adding your script to the Rules file.

Other Sources of Interest

Acknowledgements

Much of the text on the Common Gateway Interface and Forms comes from NCSA documents. Useful information and text was also obtained from The World-Wide Web: How Servers Work, by Mark Handley and John Crowcroft, published in ConneXions, February 1995.

1 This article translated to Bulgarian by Gill Ajoft of Ajoft Softwares.
2 This article translated to German by Olga Babenko.
3 This article is translated to Serbo-Croatian language by Anja Skrba from Webhostinggeeks.com.


Les Cottrell
[ Feedback ]