A CGI Web Server is an *.exe file - ISAPI and NSAPI Web Servers are *.dll files.
*.dll Web Servers are better (faster and more efficient) for high volume sites, but they are very difficult to debug because the main Web Server (IIS, Netscape, Apache, and the like) must be shut down each time a new *.dll is compiled. As a result, it is always easier to create a CGI server first and convert it to an *.dll latter. (Only the project (*.dpr) file is different.)
See C:\Program Files\BORLAND\Delphi5\Demos\Webserv for a demo where the CGI (*.exe) and ISAPI (*.dll) applications use a common module (*.pas file).
For an overview, see Creating Internet server applications in the Delphi help. (In the help contents, it is under Writing Distributed Applications. You can also launch it via Start / Programs / Delphi / Help / Developing Distributed Applications, but then many of the links won't work.)
Creating the Application
Web servers do not have a "form" as such (it actually runs as a console application), instead they produce html which is sent back to a browser.
This example will access the Interbase/Firebird database employee.gdb (it ships with Delphi).
Tab Component InterBase TIBDatabase InterBase TIBTransaction InterBase TIBTable Internet TDataSetTableProducer Internet TDataSetPageProducer
Component | Property | Value | Comments |
---|---|---|---|
IBDatabase1 | DatabaseName | Set this to the *.gdb or *.fdb file | |
Params |
user_name=sysdba password=masterkey | These are used to connect to the database. In a "real" application, these should be read from an *.ini file or some other location - Never hardcode this type of data. | |
LoginPrompt | false | This program MUST connect automatically without user intervention | |
DefaultTransaction | IBTransaction1 | ||
Connected | True | This connects the database | |
IBTransaction1 | DefaultDatabase | IBDatabase1 | |
Active | True | ||
IBTable1 | Database | IBDatabase1 | |
Transaction | IBTransaction1 | Automatically selected | |
TableName | Pick one from the list | ||
Active | True | ||
DataSetTableProducer1 | DataSet | IBTable1 | |
Header | Optional | ||
Footer | Optional | ||
DataSetPageProducer1 | DataSet | IBTable1 | |
Header | Optional | ||
Footer | Optional |
To connect to the Interbase example database distributed with Delphi, set IBDatabase1.DatabaseName to
MachineID:C:\Program Files\Common Files\Borland Shared\Data\employee.gdb
This is only one of many ways to produce a Web Server. For an advanced application, you will probably want many WebActionItem's - each one associated with a separate function - and a default WebActionItem to handle 404 (page not found) type errors.
Producing html
This will manually return the html from a "Producer"
Response.Content := SomeProducer.Content;These are the main producers
TDataSetTableProducer
However, several properties have problems - they don't work at all UNLESS you replace the default fields with one you have "manually" entered.
At design time, it is possible to set properties for the individual columns and their headers. The changes take effect immediately in the provided dialog box, but do not appear in the final output.
Formating Individual Cells
Reading Parameters
Various http headers can be read as TWebRequest properties, many more can be read via
tempStr := Request.GetFieldByName('parameterName');Query parameters should be read via the QueryFields name/value pair String List
tempStr := Request.QueryFields['parameterName'];
Running your program under IIS
Your files are stored as
c:\Inetpub\wwwroot\xyz\WebCGI.exeand accessed as
http://localhost/xyz/WebCGI.exeThe Apache configuration is slightly different.
Version Issues
{$IFDEF VER130} // Delphi 5 {$DEFINE mc_LE_D5} // Less than or equal to Delphi 5 // I use "mc" to help avoid conflicts {$ENDIF} interface uses SysUtils, Classes, HTTPApp, {$IFNDEF mc_LE_D5} HTTPProd, // Delphi 6 and above only {$ENDIF} DSProd, DBWeb, mcCommon;
Testing
There are several ways to test your CGI programs
This can be simplified by making the following registry modification
Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\exefile\shell\shell] @="Run in command shell" [HKEY_CLASSES_ROOT\exefile\shell\shell\command] @="C:\\WINDOWS\\system32\\cmd.exe /k %1"
Notice that the default action is called (I don't know how to select a specific page). To get another page, just select another action as the default.