Subsections

- 1 Getting your data
  - 1 Data coming through standard input.
  - 2 Data passed through an environment variable
- 2 Producing output
- 3 I'm under Windows, what now ?

#
11 CGI programming in Free Pascal

In these days of heavy WWW traffic on the Internet, CGI scripts have become an important topic in computer programming. While CGI programming can be done with almost any tool you wish, most languages aren't designed for it. Perl may be a notable exception, but perl is an interpreted language, the executable is quite big, and hence puts a big load on the server machine.

Because of its simple, almost intuitive, string handling and its easy syntax, Pascal is very well suited for CGI programming. Pascal allows you to quickly produce some results, while giving you all the tools you need for more complex programming. The basic RTL routines in principle are enough to get the job done, but you can create, with relatively little effort, some units which can be used as a base for more complex CGI programming.

That's why, in this chapter, we will discuss the basics of CGI in Free Pascal. In the subsequent, we will assume that the server for which the programs are created, are based upon the NCSA `httpd` WWW server, as the examples will be based upon the NCSA method of CGI programming. They have been tested with the apache server on LINUX, and the xitami server on WINDOWS NT.

The two example programs in this chapter have been tested on the command line and worked, under the condition that no spaces were present in the name and value pairs provided to them.

There is however, a faster and generally better uncgi unit available, you can find it on the contributed units page of the Free Pascal web site. It uses techniques discussed here, but in a generally more efficient way, and it also provides some extra functionality, not discussed here.

# 1 Getting your data

Your CGI program must react on data the user has filled in on the form which your web-server gave him. The Web server takes the response on the form, and feeds it to the CGI script.

There are essentially two ways of feeding the data to the CGI script. We will discuss both.

## 1 Data coming through standard input.

The first method of getting your data is through standard input. This method is invoked when the form uses a form submission method of `POST`. The web browser sets three environment variables `REQUEST_METHOD`, `CONTENT_TYPE` and `CONTENT_LENGTH`. It feeds then the results of the different fields through standard input to the CGI script. All the Pascal program has to do is :

- Check the value of the `REQUEST_METHOD` environment variable. The `getenv` function will retrieve this value this for you.
- Check the value of the `CONTENT_TYPE` environment variable.
- Read `CONTENT_LENGTH` characters from standard input. `read (c)` with `c` of type `char` will take care of that.

if you know that the request method will always be `POST`, and the `CONTENT_TYPE` will be correct, then you can skip the first two steps. The third step can be done easier: read characters until you reach the end-of-file marker of standard input.

The following example shows how this can be achieved:

    program cgi_post;

    uses dos;

    const max_data = 1000;

    type datarec = record
      name,value : string;
      end;

    var data : array[1..max_data] of datarec;
        i,nrdata : longint;
        c  : char;
        literal,aname : boolean;

    begin
    writeln ('Content-type: text/html');
    writeln;
    if getenv('REQUEST_METHOD')<>'POST' then
       begin
       writeln ('This script should be referenced with a METHOD of POST');
       write ('If you don''t understand this, see this ');
       write ('< A HREF="http://www.ncsa.uiuc.edu/SDG/Softare/Mosaic');
       writeln ('/Docs/fill-out-forms/overview.html">forms overview</A>.');
       halt(1);
       end;
    if getenv('CONTENT_TYPE')<>'application/x-www-form-urlencoded' then
       begin
       writeln ('This script can only be used to decode form results');
       halt(1)
       end;
    nrdata:=1;
    aname:=true;
    while not eof(input) do
      begin
      literal:=false;
      read(c);
      if c='\' then
         begin
         literal:=true;
         read(c);
         end;
      if literal or ((c<>'=') and (c<>'&')) then
         with data[nrdata] do
            if aname then name:=name+c else value:=value+c
      else
         begin
         if c='&' then
             begin
             inc (nrdata);
             aname:=true;
             end
          else
             aname:=false;
          end
      end;
    writeln ('<H1>Form Results :</H1>');
    writeln ('You submitted the following name/value pairs :');
    writeln ('<UL>');
    for i:=1 to nrdata do writeln ('<LI> ',data[i].name,' = ',data[i].value);
    writeln ('</UL>');
    end.

While this program isn't shorter than the C program provided as an example at NCSA, it doesn't need any other units. everythig is done using standard Pascal procedures.

Note that this program has a limitation: the length of names and values is limited to 255 characters. This is due to the fact that strings in Pascal have a maximal length of 255. It is of course easy to redefine the `datarec` record in such a way that longer values are allowed. In case you have to read the contents of a `TEXTAREA` form element, this may be needed.

## 2 Data passed through an environment variable

If your form uses the `GET` method of passing its data, the CGI script needs to read the `QUERY_STRING` environment variable to get its data. Since this variable can, and probably will, be more than 255 characters long, you will not be able to use normal string methods, present in pascal. Free Pascal implements the `pchar` type, which is a pointer to a null-terminated array of characters. And, fortunately, Free Pascal has a strings unit, which eases the use of the `pchar` type.

The following example illustrates what to do in case of a method of `GET`

    program cgi_get;

    uses strings,linux;

    const max_data = 1000;

    type datarec = record
      name,value : string;
      end;

    var data : array[1..max_data] of datarec;
        i,nrdata : longint;
        p  : PChar;
        literal,aname : boolean;

    begin
    Writeln ('Content-type: text/html');
    Writeln;
    if StrComp(GetEnv('REQUEST_METHOD'),'POST')<>0 then
       begin
       Writeln ('This script should be referenced with a METHOD of GET');
       write ('If you don''t understand this, see this ');
       write ('< A HREF="http://www.ncsa.uiuc.edu/SDG/Softare/Mosaic');
       Writeln ('/Docs/fill-out-forms/overview.html">forms overview</A>.');
       halt(1);
       end;
    p:=GetEnv('QUERY_STRING');
    nrdata:=1;
    aname:=true;
    while p^<>#0  do
      begin
      literal:=false;
      if p^='\' then
         begin
         literal:=true;
         inc(longint(p));
         end;
      if ((p^<>'=') and (p^<>'&')) or literal then
         with data[nrdata] do
            if aname then name:=name+p^ else value:=value+p^
      else
         begin
         if p^='&' then
             begin
             inc (nrdata);
             aname:=true;
             end
          else
             aname:=false;
          end;
      inc(longint(p));
      end;
    Writeln ('<H1>Form Results :</H1>');
    Writeln ('You submitted the following name/value pairs :');
    Writeln ('<UL>');
    for i:=1 to nrdata do writeln ('<LI> ',data[i].name,' = ',data[i].value);
    Writeln ('</UL>');
    end.

Although it may not be written in the most elegant way, this program does the same thing as the previous one. It also suffers from the same drawback, namely the limited length of the `value` field of the `datarec`.

This drawback can be remedied by redefining `datarec` as follows:

    type datarec = record;
          name,value : pchar;
         end;

and assigning at run time enough space to keep the contents of the value field. This can be done with a

     getmem (data[nrdata].value,needed_number_of_bytes);

call. After that you can do a

    strlcopy (data[nrdata].value,p,needed_number_of_bytes);

to copy the data into place.

You may have noticed the following unorthodox call :

    inc(longint(p));

Free Pascal doesn't give you pointer arithmetic as in C. However, `longints` and `pointers` have the same length (namely 4 bytes). Doing a type-cast to a `longint` allows you to do arithmetic on the `pointer`.

Note however, that this is a non-portable call. This may work on the I386 processor, but not on a ALPHA processor (where a pointer is 8 bytes long). This will be remedied in future releases of Free Pascal.

# 2 Producing output

The previous section concentrated mostly on getting input from the web server. To send the reply to the server, you don't need to do anything special.You just print your data on standard output, and the Web-server will intercept this, and send your output to the WWW-client waiting for it.

You can print anything you want, the only thing you must take care of is that you supply a `Contents-type` line, followed by an empty line, as follows:

    Writeln ('Content-type: text/html');
    Writeln;
    { ...start output of the form... }

And that's all there is to it !

# 3 I'm under Windows, what now ?

Under Windows the system of writing CGI scripts can be totally different. If you use Free Pascal under Windows then you also should be able to do CGI programming, but the above instructions may not work. They are known to work for the xitami server, however.

If some kind soul is willing to write a section on CGI programming under Windows for other servers, I'd be willing to include it here.