Solved

CGI Application demos with source

Posted on 1998-02-26
12
346 Views
Last Modified: 2013-11-18
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

0
Comment
Question by:greenrc
  • 5
  • 4
  • 2
  • +1
12 Comments
 

Author Comment

by:greenrc
ID: 1359850
Edited text of question
0
 
LVL 7

Expert Comment

by:ahalya
ID: 1359851
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
 
LVL 8

Expert Comment

by:ZifNab
ID: 1359852
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
 

Author Comment

by:greenrc
ID: 1359853
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
 
LVL 8

Expert Comment

by:ZifNab
ID: 1359854
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
 

Author Comment

by:greenrc
ID: 1359855
.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:greenrc
ID: 1359856
Adjusted points to 245
0
 
LVL 3

Expert Comment

by:williams2
ID: 1359857
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
 

Author Comment

by:greenrc
ID: 1359858
A CGI-Executable.
0
 
LVL 3

Accepted Solution

by:
williams2 earned 240 total points
ID: 1359859
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
 
LVL 3

Expert Comment

by:williams2
ID: 1359860
****, 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
 
LVL 3

Expert Comment

by:williams2
ID: 1359861
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now