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.
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. |
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.
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. |
This drawback can be remedied by redefining datarec as follows:
type datarec = record; name,value : pchar; end; |
getmem (data[nrdata].value,needed_number_of_bytes); |
strlcopy (data[nrdata].value,p,needed_number_of_bytes); |
You may have noticed the following unorthodox call :
inc(longint(p)); |
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.