next up previous contents
Next: Producing output Up: CGI programming in Free Previous: CGI programming in Free

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.

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 :

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 proceduresgif.

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.

Data passed through an environment variable

If your form uses the GET method of passing it's data, the CGI script needs to read the QUERY_STRING environment variable to get it's 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.


next up previous contents
Next: Producing output Up: CGI programming in Free Previous: CGI programming in Free

Michael Van Canneyt
Thu Sep 10 13:56:17 CEST 1998