CGI Application demos with source

I am after some CGI demos with source.....I would like the demos to use the standard internet components provided with Delphi32 C/S...Thankyou

greenrcAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

greenrcAuthor Commented:
Edited text of question
0
ahalyaCommented:
Here goes a simple CGI example that returns an image:
i got it from someplace on the net.
=====================================================

program cgi;

uses SysUtils;
{$APPTYPE CONSOLE}

var  j, i :integer;
     p    :pointer;
     f    :file;

begin
  WriteLn('Content-type: image/gif'); WriteLn;
  Assign(f, 'k:\www\server\docs\images\atwork.gif');
  Reset(f, 1);
  i:=FileSize(f);
  GetMem(p, i);
  try
    BlockRead(f, p^, i);
    for j:=0 to i-1 do
      Write(char(pointer(integer(p)+j)^));
  finally
    FreeMem(p);
    close(f);
  end;
end.

0
ZifNabCommented:
Hi, I sendend this to hamishd, maybe it's also good for you:

                   some components with source :

                   http://torry.rimini.com/vcl/internet/delphcgi.zip

                   and a little demo :

                   Not made by me!

                   L> I don't understand though how to send the output from the CGI to the web
                   RL> server.

                   Yeah, I spend a whole weekend before I got it right. Here's a sample program
                   I wrote for developing 32bit Delphi console GCI apps. It still needs work,
                   like return variable values and so on. But it is at least a start.

                   regards,
                   Billy
                   program test;

                   {$APPTYPE CONSOLE}

                   uses WinProcs, WinTypes, SysUtils;

                   type
                     TCGIData = class
                       public
                         {following variables are set by the Web Server as o/s environmental
                         variables}
                         RequestMethod : String;  {from html FORM METHOD=??}
                         PathInfo      : String;
                         QueryString   : String;  {query string from server}
                         RemoteHost    : String;  {ip address of client browser}
                         ContentType   : String;
                         ContentLength : LongInt;
                         HttpUserAgent : String;  {client's browser signature}
                         constructor Create;
                     end;


                   constructor TCGIData.Create;
                   {reads the environmental variables}
                   var
                     lpsz : Pchar;
                   begin
                     lpsz := StrAlloc(1024*10);

                     if GetEnvironmentVariable( 'Request_Method', lpsz, 1024*10 ) > 0 then
                       RequestMethod := StrPas(lpsz)
                     else
                       RequestMethod := '';

                     if GetEnvironmentVariable( 'Path_Info', lpsz, 1024*10 ) > 0 then
                       PathInfo := StrPas(lpsz)
                     else
                       PathInfo := '';

                     if GetEnvironmentVariable( 'Query_String', lpsz, 1024*10 ) > 0 then
                       QueryString := StrPas(lpsz)
                     else
                       QueryString := '';

                     if GetEnvironmentVariable( 'Remote_Host', lpsz, 1024*10 ) > 0 then
                       RemoteHost := StrPas(lpsz)
                     else
                       RemoteHost := '';

                     if GetEnvironmentVariable( 'HTTP_User_Agent', lpsz, 1024*10 ) > 0 then
                       HttpUserAgent := StrPas(lpsz)
                     else
                       HttpUserAgent := '';

                     if GetEnvironmentVariable( 'Content_Type', lpsz, 1024*10 ) > 0 then
                       ContentType := StrPas(lpsz)
                     else
                       ContentType := '';

                     if GetEnvironmentVariable( 'Content_Length', lpsz, 1024*10 ) > 0 then
                       ContentLength := StrToInt(lpsz)
                     else
                       ContentLength := 0;

                     StrDispose(lpsz);
                   end;

                   var
                     CGI : TCGIData;

                   begin
                     CGI := TCGIData.Create;
                     WriteLn('MIME-version: 1.0');
                     WriteLn('Content-type: text/html');
                     WriteLn;
                     WriteLn('');
                     WriteLn('');
                     WriteLn('');
                     WriteLn('');
                     WriteLn('

                   ');
                     WriteLn('');
                     WriteLn('This CGI is written in Delphi II. The following information ');
                     WriteLn('is available to this CGI from the Web Server:
                   ');
                     WriteLn('');
                     WriteLn('
                      Request method for this CGI : '+CGI.RequestMethod);
                     WriteLn('
                      Path : '+CGI.PathInfo);
                     WriteLn('
                      Parameters : '+CGI.QueryString);
                     WriteLn('
                      Content Type : '+CGI.ContentType);
                     WriteLn('
                      Your IP address : '+CGI.RemoteHost);
                     WriteLn('
                      Your Browser''s signature : '+CGI.HttpUserAgent);
                     WriteLn('
                   ');
                     WriteLn('

                   ');
                     WriteLn('');
                     WriteLn('
                   Coyright ) Whoever 1996
                   ');
                     WriteLn('');

                     CGI.Free
                   end.

                   regards,Zif.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

greenrcAuthor Commented:
Thank you for your responses, but

I would really like a demo that uses the standard delphi components provided with Delphi 3 C/S


0
ZifNabCommented:
Ok, for that, look at the Delphi Developers Journal, e.g. this article :

small example + 'how to do'-information

http://www.borland.com/delphi/news/cobb/ddj1_6b/ddj1_6b.html

Hope this helps, otherwise I don't know how I can help you.
Regards, Zif.
0
greenrcAuthor Commented:
.
0
greenrcAuthor Commented:
Adjusted points to 245
0
williams2Commented:
Ok, I'm not even sure too. Do you want to make a CGI-WebServer apllication, or just a CGI-Executable ????? I won't respond with any examples at all, untill I know for sure.
0
greenrcAuthor Commented:
A CGI-Executable.
0
williams2Commented:
This is easy, Here goes an example of a generel working CGI-executable:

The HTML page, the user sees looks like this:

<html>
<title>Testing CGI</title>

<body>
<body background = http://mis.lamar.edu/mis/Grundy/parchmen.gif>
<form action="http://localhost/cgi-win/findelev.exe" method=post>
<h2><Center>Searching persons</Center></h2>
<HR>Word to search:
<input type= text name= searchfield1 size=20><br>
(with automatic wildcard selection)<br>
Kategori:
<select name=formfield1>
<option value="Firstname">Firstname
<option value="Surname">Surname
<option value="UserID">Username
<option value="Age">Age
</select><BR>
<HR><select name=formfield3>
<option value="IGNORE">ignore
<option value="AND">and
<option value="OR">or
</select>
second word:
<input type= text name= searchfield2 size=20><br>
(with automatic wildcard selection)<br>
Kategori:
<select name=formfield2>
<option value="Firstname">Firstname
<option value="Surname">Surname
<option value="UserID">Username
<option value="Age">Age
</select><BR>

<HR>

<input type=submit value="Begin search">

<h5><Center><b>Example made by:</b><br><k>Thomas Williams</k></Center></h5>
</form>
</body>
</html>



The next program should be placed in some CGI-Dir (Ex. CGI-Win):

program FindPerson;

uses
  CGIunit in 'CGIunit.pas';

{$R *.RES}

begin
  Application.Initialize;
  Application.Run;
end.


THE CGI UNIT :

unit CGIunit;

interface

uses
  SysUtils, DB, DBTables;

// See to that the HTML-page contains http://localhost/cgi-win/FindPerson.exe
// and 'PicDir' should be directed to a public path
// like 'file:///F|/Pictures/'
// (You can change this at the end of this unit)

// The DBase contains a table 'persons' containing following attributes:
// firstname,Surname,Username,Age,Tlf,PicID: String;
// PicID is a unique name of the picture of a person

// The DBase could be replaced by an .INI file, to fasten up querying and avoid use of SQL (SQL servers are expensive.)
Const
  DBName = 'MyDB';
  // DBName is the name of an INTRBASE dbase located at C:\
var
  db1: Tdatabase;
  dBase: TQuery;
  PicDir: String;

implementation

Uses Inifiles;

  procedure InitDB;
  begin
    db1 := TDatabase.create(nil);
    db1.databasename := 'MyDB';
    db1.drivername   := 'INTRBASE';

    db1.params.clear;
    db1.params.add('SERVER NAME=C:\'+DBName+'.GDB');
    db1.params.add('USER NAME=SYSDBA');
    db1.params.add('OPEN MODE=READ/WRITE');
    db1.params.add('SCHEMA CACHE SIZE=8');
    db1.params.add('LANGDRIVER=');
    db1.params.add('SQLQRYMODE=');
    db1.params.add('SQLPASSTHRU MODE=SHARED AUTOCOMMIT');
    db1.params.add('SCHEMA CACHE TIME=-1');
    db1.params.add('MAX ROWS=-1');
    db1.params.add('BATCH COUNT=200');
    db1.params.add('ENABLE SCHEMA CACHE=FALSE');
    db1.params.add('SCHEMA CACHE DIR=');
    db1.params.add('PASSWORD=masterkey');

    db1.loginprompt  := false;
    db1.connected    := true;

    DBase:= TQuery.create(nil);
    DBase.databaseName:= DBName;
    DBase.requestLive:= true

  end;

  function Correct(s: String): String;
  var // Makes every first letter a capital letter.
    i: Integer;
    oldc: Char;
  begin
    oldc:=' ';
    For i:=1 to length(s) do
    begin
      if (oldc=' ') and (s[i]<>' ') then s[i]:=ANSIUpperCase(s[i])[1];
      oldc:=s[i];
    end;
    Correct:=s;
  end;

  Procedure GetTabel(var IniFile: TIniFile);
  var
    s: String; // Searching String
    t: String; // Search types
    DoubleSearch: String;
    Statement: String; //SQL udtryk
    Bool: String;
  begin
    dbase.sql.clear;
    t := IniFile.ReadString('Form Literal','formfield1','');
    s := ANSILowerCase(IniFile.ReadString('Form Literal','searchfield1','""'));
    Statement:='select * from Persons where ('+t+' Like ' + s + ')';
    dbase.sql.add(Statement);
    Bool := IniFile.ReadString('Form Literal','formfield3','');
    if Bool<>'IGNORE'then
    begin
      t := IniFile.ReadString('Form Literal','formfield2','');
      s := ANSILowerCase(IniFile.ReadString('Form Literal','searchfield2','""'));
      Statement:=Bool+' ('+t+' Like ' + s + ')';
      dbase.sql.add(Statement);
    end;
    dbase.open;
  end;

  Procedure WriteResult(var f: textFile);
  var
    Fname, Sname, User: String;
    Age, Tlf, PicID: String;

  begin
    DBase.first;
    If DBase.RecordCount>0 then
    begin
      while not DBase.eof do
      begin
        Fname:= DBase.FieldByName('Firstname').asString;
        Sname:= DBase.FieldByName('Surname').asString;
        User:= DBase.FieldByName('Username').asString;
        Age:= DBase.FieldByName('Age').asString;
        Tlf:= DBase.FieldByName('Tlf').asString;
        PicID:= DBase.FieldByName('PicID').asString;

        Writeln(f,'<B>Firstname: </B>'+Correct(Fname)+'<BR>');
        Writeln(f,'<B>Surname: </B>'+Correct(Sname)+'<BR>');
        Writeln(f,'<B>User Ident.: </B>'+ANSIUppercase(User)+'<BR>');
        Writeln(f,'<B>Age: </B>'+Age+'<BR>');
        Writeln(f,'<B>Telephone #: </B>'+Tlf+'<BR>');
        Writeln(f,'<CENTER><IMG SRC="'+PicDir+PicID+'"></CENTER><br><hr>');

        dBase.next;
      end;
    end else
    begin
      Writeln(f,'<BR><HR><BR><B> The query returned nothing </B><BR>');
    end;
  end;

  Procedure ProcessScript;
  var
     IniFile: TInifile;
     filename: string;
     f: textfile;
     i: integer;
  begin
     IniFile:= TInifile.create(Paramstr(1));

     // *** header start ***

     filename:= IniFile.Readstring('System','Output File','');
     assignfile(f,filename);
     rewrite(f);
     writeln(f,'HTTP/1.0 200 OK');
     writeln(f,'Date: Friday, 28 Nov-97, 13:36:12 GMT');
     writeln(f,'Server:Website/1.1e');
     writeln(f,'Allow-ranges: bytes');
     writeln(f,'Content-type: text/html');
     writeln(f,'Content-length: 0');
     writeln(f);
     writeln(f,'<HTML>');
     writeln(f,'<Title>Result</Title>');
     writeln(f,'<Body>');
     writeln(f,'<body background = http://mis.lamar.edu/mis/Grundy/parchmen.gif>');
     writeln(f,'Following matched the query: <BR><BR>');

     // *** header end ***

     GetTabel(IniFile);
     WriteResult(f);

     writeln(f,'</body>');
     writeln(f,'</HTML>');
     DBase.Close;
     Closefile(f);
  end;

begin
  PicDir:= 'file:///F|/Pictures/';
  InitDB;
  ProcessScript;
end.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
williams2Commented:
****, I forgot to replace "Findelev.exe" with "FindPerson.Exe" in the HTML-script.. ..Else you should just cut'n'paste, create a DBase and you're ready to go.
0
williams2Commented:
I found out, due to an error in Delphi 3, that you won't be able to write to the Database with SQL with the provided code (Though it succeeds using the Dataset properties instead). But registering the Database in DBExplorer first with the same parameters as in the "InitDB" - code, and then setting ALIAS=DBNAME instead solves the problem (Weird????).

Regards
Williams
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.