TCGI – A Delphi CGI Component.
Shareware $10 Australian – See below for details
By Robert Wuttke.
©Copyright 1997
 This document is a copy of the document TCGI.doc that comes with downloading the component.
 

Introduction:

 

The TCGI component was created to enable users to create CGI applications using the Delphi Environment. This component is a non-visual component, which can be used with visual components. There is one caveat to this though. If you use visual components that require user intervention, you will create a situation where you need to be actively monitoring the Web Server and processing all these forms.

 

So TCGI mixes both worlds, i.e. console apps and GUI apps. The console side of it is so that we can write to standard output. The CGI specification requires that all output going back to the user be sent via standard output to the web server.

 

TCGI gives you access to all environment variables by default, except HTTP_*. The reason for this is due to server differences. Some server may set one set of HTTP_* variables and others, others. It will allow you to obtain any other environment variable that exists as well.

 

This works with Delphi 2.0, not sure about Delphi 3.0 (hopefully) and I am pretty sure it won’t with Delphi 1.0, but I may be wrong, if someone want’s to test 1.0 and 3.0, please let me know at the address given below.

 

Let’s get into it, shall we?

 

But wait, I have never installed a component before, HOW ???
 
 

Glad you asked, it is actually quite simple. From the Component menu, select Install, then click the add button on the screen that appears. Okay, now hit the browse button and find the .DCU file that you just installed (you do remember where you put it don’t you? The default location is c:\program files\components\cgi.). If you put it somewhere else and can’t remember do a find for cgi.dcu and that will find it. Ok, now select OK and wait until Delphi recompiles the Component Palette and viola it’s there. What, you can’t see it? See the double arrow at the top right hand corner of the component palette, click that a couple of times and the CGI tab will come into view.

 

If you still can’t see it, either re-read the Delphi manuals, or failing that, consult your local Delphi GURU (preferably a friends, these guys cost big money) to figure out the problem as it is beyond the scope of this document.
 
 

How do I use it?
 

To use the CGI component, select CGI from the CGI component palette (sorry, will change this later) and drop it on your form. There is one more major step. Add {$APPTYPE CONSOLE} to your project source file. Compile this and you have a working CGI application. Doesn’t do anything, but it will work properly when called from a web page.

 

The most important thing to remember is that everything should be in the forms FormActivate procedure. This way you can drop a Ttable or Tquery on the form and access the data. Do not use other methods, as they will not be called. The last line in the FormActivate must be close. This is so that the program will exit and return data to the web server.

 

There are just three methods you can call and lot’s of variables you can use.

 

 

Methods

 

    1. GetCGIStrings. This procedure takes no parameters and creates the default environment variables for you (see Variables for more information). This MUST be called before anything else.
    2. Example: CGI1.GetCGIStrings;

    3. GetVariable. This function requires you to pass the name of the environment variable to it. It then finds that environment variable in the environment and passes you back the value. Note: Do not use this to get QUERY_STRING, as it does not unmangle the string.
    4. Example: Value: =CGI1.GetVariable (‘CONTENT_LENGTH’);

      (Don’t use this one though as it’s value already exists, use it to gain server dependent variables like the HTTP_* variables).

    5. FindValue. This function requires you to pass the name of the variable to it. It then finds this variable and passes the value back to you. Note: The variables in question are the names of buttons, checkboxes, text boxes, etc. from a form and not an environment variable.
Example: Value: =CGI1.FindValue(‘FirstName’);

 

Variables

 

 

These are default environment variables passed from the Web Server to your CGI program. There may be others; please consult your server documentation. You can obtain these others using the function call GetVariable. To read any of these variables, use Value: =CGI1.GATEWAY_INTERFACE.

 

 

 

Example Program.
 
 

The project source.

 

program CGI_Project;

 

uses

Forms,

CGI_Project_F in 'CGI_Project_F.pas' {Form1};

 

{$R *.RES}

{$APPTYPE CONSOLE} ß This is mega important.

 

begin

Application.Initialize;

Application.CreateForm(TForm1, Form1);

Application.Run;

end.

 

The unit code.

 

unit CGI_Project_F;

 

interface

 

uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,

cgi;

 

type

TForm1 = class(TForm)

CGI1: TCGI;

procedure FormActivate(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

 

var

Form1: TForm1;

 

implementation

 

{$R *.DFM}

 

procedure TForm1.FormActivate(Sender: TObject);

begin

with CGI1 do

begin

GetCGIStrings;

writeln('<HTML><HEAD><TITLE>Test CGI</TITLE></HEAD>');

writeln('<BODY');

writeln('<h1>Here is the results of the program test.exe after execution.</h1>');

writeln('<br>AUTH_TYPE=',AUTH_TYPE);

writeln('<br>CONTENT_LENGTH=',CONTENT_LENGTH);

writeln('<br>CONTENT_TYPE=',CONTENT_TYPE);

writeln('<br>GATEWAY_INTERFACE=',GATEWAY_INTERFACE);

writeln('<br>PATH_INFO=',PATH_INFO);

writeln('<br>PATH_TRANSLATED=',PATH_TRANSLATED);

writeln('<br>REMOTE_ADDR=',REMOTE_ADDR);

writeln('<br>REMOTE_HOST=',REMOTE_HOST);

writeln('<br>REMOTE_IDENT=',REMOTE_IDENT);

writeln('<br>REMOTE_USER=',REMOTE_USER);

writeln('<br>REQUEST_METHOD=',REQUEST_METHOD);

writeln('<br>SCRIPT_NAME=',SCRIPT_NAME);

writeln('<br>SERVER_NAME=',SERVER_NAME);

writeln('<br>SERVER_PORT=',SERVER_PORT);

writeln('<br>SERVER_PROTOCOL=',SERVER_PROTOCOL);

writeln('<br>SERVER_SOFTWARE=',SERVER_SOFTWARE);

writeln('<br>QUERY_STRING=',QUERY_STRING);

writeln(‘</BODY></HTML>’);

end;

close;

end;

 

end.

 

The DFM file in text format.

 

object Form1: TForm1

Left = 200

Top = 1000

Width = 195

Height = 99

Caption = 'Form1'

Font.Color = clWindowText

Font.Height = -11

Font.Name = 'MS Sans Serif'

Font.Style = []

OnActivate = FormActivate

PixelsPerInch = 96

TextHeight = 13

object CGI1: TCGI

Left = 72

Top = 32

end

end

 

Note: Setting Top to 1000, stops the form from displaying on your screen when the application is run.

 

All this application does is return the values for all the default environment variables. Note that it just uses standard HTML to send data back, you don’t have to. Have a look at any good CGI tutorial to find out how to send back other types.

 

Well that is all there is to it, overall it is a simple component. Have fun.

 

Shareware Details.
 
 

If you like this product, please send a money order for $10AUD to

 

Robert Wuttke

 

10 Ashley Street,

Elizabeth North,

South Australia,

Australia, 5113.

 

Please note, that this component will not bug you to register, I rely on you honesty.

 

Revision History.
 
 

    1. Wrote it and this document.
 

Thanks for using this component. I hope it is all that you wanted/needed from a CGI component. If you have any difficulties, please contact me on rwuttke@hotmail.com

 

Rob Wuttke