HI
DP-ler,
dieses Mal zeige ich Euch, wie man auf einfachste Art und Weise eine einfache CGI-Web Anwendung erstellt.
Hinweis:Dieses Beispiel wurde mit Delphi 5 Enterprise erstellt. Nicht alle Delphi Versionen unterstützen die hier genutzten Features.
Schritt 1: Eine neue Anwendung
Zum Anfang erstellen wir ein neues Projekt. Anstatt einer normalen Anwendung, erstellen wir eine
Web Server Anwendung. Im folgenden Dialog entscheiden wir uns für eine WinCGI-
DLL.
http://www.gatenetwork.com/delphi-sa...ectproject.gif
http://www.gatenetwork.com/delphi-sa...ectapptype.gif
Delphi hat uns zwei Dateien vorbereitet
- Unsere Projektdatei (speichern als: CGIDemo.dpr)
- Unser Webmodul (speichern als: uMainWeb.pas)
Schritt 2: ...und Actions
Als nächstes wollen wir zwei Funktionen für unsere CGI Anwendung festlegen. Eine zur Begrüßung, welche es dem Besucher unserer Webseite ermöglicht, die andere zu starten. Die zweite Funktion ermöglicht dem Nutzer das durchstöbern der Festplatte des Servers. (Bitte nicht auf einen Live-Server installieren, der Admin könnte Euch das übel nehmen

)
Dazu wählt einfach zweimal die Funktion
Add item (Ich habe nur die engl. Delphi Version.) Das erste Item habe ich
wbaDefault, das zweite
wbaSurfDisk genannt. Zusätzlich kommt noch eine
TPageProducer Komponente in das Webmodul.
http://www.gatenetwork.com/delphi-sa...03_additem.gif
http://www.gatenetwork.com/delphi-sa...geproducer.gif
Schritt 3: Setup
Jetzt müssen wir nur noch die Einstellungen für die beiden
Actioneinträge setzen.
http://www.gatenetwork.com/delphi-sa...setdefault.gif
http://www.gatenetwork.com/delphi-sa...etsurfdisk.gif
Für die TPageProducer Komponente pgpDefault müssen wir noch die Eigenschaft
Strings setzen
Code:
<
html>
<head>
<title>Jetzt gehts los!</title>
<meta name="generator" content="<#generator>">
</head>
<body>
<h1>Jetzt gehts los</h1>
[
url="/cgi-bin/CGIDemo.dll/surfdisk?path=C:\"]Surf HD C:\[/
url]
It is <#now>.
</body>
</
html>
Schritt 4: The meat
In den Eventhandler der zweiten Actionkomponenten (wbaSurfDisk) muss auch noch ein wenig Code und dann haben wir schon das Gesamtergebniss
Delphi-Quellcode:
unit uMainWeb;
interface
uses
Windows, Messages, SysUtils, Classes, HTTPApp;
type
TwbmHelloWorld =
class(TWebModule)
pgpDefault: TPageProducer;
procedure pgpDefaultHTMLTag(
Sender: TObject; Tag: TTag;
const TagString:
String; TagParams: TStrings;
var ReplaceText:
String
);
procedure wbmHelloWorldwbaSurfDiskAction(
Sender: TObject; Request: TWebRequest; Response: TWebResponse;
var Handled: Boolean
);
private
{ Private declarations }
function ParentLink(
URL, Path:
String):
String;
function GetFolders(
URL, Path:
String):
String;
function GetFiles(
URL, Path:
String):
String;
public
{ Public declarations }
end;
var
wbmHelloWorld: TwbmHelloWorld;
implementation
uses
FileCtrl;
{$R *.DFM}
function TwbmHelloWorld.GetFiles(
URL, Path:
String):
String;
var
SR: TSearchRec;
begin
if FindFirst(Path + '
\*', faAnyFile
and not faDirectory, SR) = 0
then
try
Result := '
<h1>Files</h1><pre>';
repeat
Result := Result + SR.
Name + #13#10;
until FindNext(SR) <> 0;
finally
FindClose(SR);
Result := Result + '
</pre>';
end else
Result := '
';
end;
function TwbmHelloWorld.GetFolders(
URL, Path:
String):
String;
var
SR: TSearchRec;
begin
if FindFirst(Path + '
\*', faDirectory, SR) = 0
then
try
Result := '
<h1>Sub Folders</h1><pre>';
repeat
if (SR.
Name <> '
.')
and (SR.
Name <> '
..')
then
if SR.Attr
and faDirectory = faDirectory
then
Result :=
Result +
'
<a href="' +
URL + '
?path=' + Path + '
\' + SR.
Name + '
">' +
SR.
Name +
'
</a>'#13#10;
until FindNext(SR) <> 0;
finally
FindClose(SR);
Result := Result + '
</pre>';
end else
Result := '
';
end;
function TwbmHelloWorld.ParentLink(
URL, Path:
String):
String;
begin
Result := '
';
while Path <> '
'
do
begin
if AnsiLastChar(Path)^ = '
\'
then
Break;
SetLength(Path, Pred(Length(Path)));
end;
if Path = '
'
then
Exit;
if DirectoryExists(Path)
then
Result :=
'
<h1>Parent Folder</h1>' +
'
<a href="' +
URL + '
?path=' + Path + '
">' +
Path +
'
</a>'#13#10;
end;
procedure TwbmHelloWorld.pgpDefaultHTMLTag(
Sender: TObject; Tag: TTag;
const TagString:
String; TagParams: TStrings;
var ReplaceText:
String
);
begin
if TagString = '
generator'
then
ReplaceText := '
Web CGI App - Hello World v1'
else if TagString = '
now'
then
ReplaceText := DateTimeToStr(Now)
else
ReplaceText :=
'
<font color="red">[b]Unknown Tag:[/b]</font><font color="blue"> <#' +
TagString + '
' + TagParams.Text +
'
></font>';
end;
procedure TwbmHelloWorld.wbmHelloWorldwbaSurfDiskAction(
Sender: TObject; Request: TWebRequest; Response: TWebResponse;
var Handled: Boolean
);
var
Path, OutPut:
String;
begin
Path := Request.QueryFields.Values['
path'];
if AnsiLastChar(Path)^ = '
\'
then
SetLength(Path, Pred(Length(Path)));
if not DirectoryExists(Path)
then
begin
OutPut :=
'
<html><head><title>Oops</title></head><body>' +
'
<h1>Oops</h1>' +
'
Folder [b]' + Path + '
[/b] does not exist' +
'
</body></html>';
end else begin
OutPut :=
'
<html><head><title>' + Path + '
</title></head><body>' +
ParentLink(Request.URL + Request.PathInfo, Path) +
GetFolders(Request.URL + Request.PathInfo, Path) +
GetFiles(Request.URL + Request.PathInfo, Path) +
'
</body></html>';
end;
Response.Content := OutPut;
end;
end.
Schritt 5: Plug and Play
Jetzt noch kompilieren und die
DLL in ein ausführbares Verzeichnis eines Webserver (PWS oder IIS) installieren und aufrufen. Ich habe die
DLL in das Verzeichnis
cgi-bin kopiert, damit kann ich diese wie folgt aufrufen:
Code:
http://
localhost/cgi-bin/CGIDemo.dll
Und noch der
Download des Beispiels (3 KB).
[edit=CalganX]Mfg, CalganX[/edit]