?? webserv1.pas
字號:
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Author: Fran鏾is PIETTE
Creation: Oct 10, 1999
Description: WebSrv1 show how to use THttpServer component to implement
a web server.
Version: 1.00 BETA
EMail: http://users.swing.be/francois.piette francois.piette@swing.be
http://www.rtfm.be/fpiette francois.piette@rtfm.be
francois.piette@pophost.eunet.be
Support: Use the mailing list twsocket@rtfm.be See website for details.
Legal issues: Copyright (C) 1999 by Fran鏾is PIETTE
Rue de Grady 24, 4053 Embourg, Belgium. Fax: +32-4-365.74.56
<francois.piette@pophost.eunet.be><francois.piette@swing.be>
This software is provided 'as-is', without any express or
implied warranty. In no event will the author be held liable
for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it
and redistribute it freely, subject to the following
restrictions:
1. The origin of this software must not be misrepresented,
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but is
not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
4. You must register this software by sending a picture postcard
to the author. Use a nice stamp and mention your name, street
address, EMail address and any comment you like to say.
History:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
unit WebServ1;
interface
uses
WinTypes, WinProcs, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, IniFiles, StdCtrls, ExtCtrls, WinSock, WSocket, WSocketS, HttpSrv;
const
CopyRight : String = 'WebServ (c) 1999-2000 F. Piette V1.00 BETA ';
type
{ This component is used for client connection instead of default one. }
{ This enables to add any data we need to handle our application. }
{ As this data is located in client component, each connected client has }
{ his own private data. }
TMyHttpConnection = class(THttpConnection)
protected
FPostedDataBuffer : PChar; { Will hold dynamically allocated buffer }
FPostedDataSize : Integer; { Databuffer size }
FDataLen : Integer; { Keep track of received byte count. }
public
destructor Destroy; override;
end;
{ This is the main form for our application. Any data here is global for }
{ all clients. Put provate data in TMyHttpConnection class (see above). }
TAppBaseForm = class(TForm)
ToolsPanel: TPanel;
DisplayMemo: TMemo;
HttpServer1: THttpServer;
Label1: TLabel;
DocDirEdit: TEdit;
Label2: TLabel;
DefaultDocEdit: TEdit;
StartButton: TButton;
StopButton: TButton;
Label3: TLabel;
PortEdit: TEdit;
ClientCountLabel: TLabel;
Label5: TLabel;
ClearButton: TButton;
DisplayHeaderCheckBox: TCheckBox;
procedure FormShow(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormCreate(Sender: TObject);
procedure HttpServer1GetDocument(Sender, Client: TObject;
var Flags: THttpGetFlag);
procedure StartButtonClick(Sender: TObject);
procedure StopButtonClick(Sender: TObject);
procedure HttpServer1ClientConnect(Sender: TObject;
Client: TObject; Error: Word);
procedure HttpServer1ClientDisconnect(Sender: TObject;
Client: TObject; Error: Word);
procedure HttpServer1ServerStarted(Sender: TObject);
procedure HttpServer1ServerStopped(Sender: TObject);
procedure HttpServer1HeadDocument(Sender, Client: TObject;
var Flags: THttpGetFlag);
procedure HttpServer1PostedData(Sender: TObject;
Client: TObject; Error: Word);
procedure HttpServer1PostDocument(Sender, Client: TObject;
var Flags: THttpGetFlag);
procedure ClearButtonClick(Sender: TObject);
private
FIniFileName : String;
FInitialized : Boolean;
FCountRequests : Integer;
procedure CreateVirtualDocument_time_htm(Sender : TObject;
Client : TObject;
var Flags : THttpGetFlag);
procedure DisplayHeader(Client : TMyHttpConnection);
procedure ProcessPostedData_CgiFrm1(Client : TMyHttpConnection);
public
procedure Display(Msg : String);
property IniFileName : String read FIniFileName write FIniFileName;
end;
var
AppBaseForm: TAppBaseForm;
implementation
{$R *.DFM}
const
{ IniFile layout for persistent data }
SectionWindow = 'WindowMain';
KeyTop = 'Top';
KeyLeft = 'Left';
KeyWidth = 'Width';
KeyHeight = 'Height';
SectionData = 'Data';
KeyDocDir = 'DocDir';
KeyDefaultDoc = 'DefaultDoc';
KeyPort = 'Port';
KeyDisplayHeader = 'DisplayHeader';
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{$IFDEF VER80}
function TrimRight(Str : String) : String;
var
i : Integer;
begin
i := Length(Str);
while (i > 0) and (Str[i] = ' ') do
i := i - 1;
Result := Copy(Str, 1, i);
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function TrimLeft(Str : String) : String;
var
i : Integer;
begin
if Str[1] <> ' ' then
Result := Str
else begin
i := 1;
while (i <= Length(Str)) and (Str[i] = ' ') do
i := i + 1;
Result := Copy(Str, i, Length(Str) - i + 1);
end;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function Trim(Str : String) : String;
begin
Result := TrimLeft(TrimRight(Str));
end;
{$ENDIF}
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TAppBaseForm.FormCreate(Sender: TObject);
begin
{ Create IniFileName based on EXE file name; }
FIniFileName := LowerCase(ExtractFileName(Application.ExeName));
FIniFileName := Copy(FIniFileName, 1, Length(FIniFileName) - 3) + 'ini';
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TAppBaseForm.FormShow(Sender: TObject);
var
IniFile : TIniFile;
wsi : TWSADATA;
begin
if not FInitialized then begin
FInitialized := TRUE;
{ Restore persistent data from INI file }
IniFile := TIniFile.Create(FIniFileName);
Width := IniFile.ReadInteger(SectionWindow, KeyWidth, Width);
Height := IniFile.ReadInteger(SectionWindow, KeyHeight, Height);
Top := IniFile.ReadInteger(SectionWindow, KeyTop,
(Screen.Height - Height) div 2);
Left := IniFile.ReadInteger(SectionWindow, KeyLeft,
(Screen.Width - Width) div 2);
DocDirEdit.Text := IniFile.ReadString(SectionData, KeyDocDir,
'c:\WwwRoot');
DefaultDocEdit.Text := IniFile.ReadString(SectionData, KeyDefaultDoc,
'index.html');
PortEdit.Text := IniFile.ReadString(SectionData, KeyPort,
'80');
DisplayHeaderCheckBox.Checked :=
Boolean(IniFile.ReadInteger(SectionData, KeyDisplayHeader, 0));
IniFile.Destroy;
{ Initialize client count caption }
ClientCountLabel.Caption := '0';
{ Display version info for program and use components }
wsi := WinsockInfo;
DisplayMemo.Clear;
Display(CopyRight);
Display('Using:');
Display(' ' + WSocket.CopyRight);
Display(' ' + WSocketS.CopyRight);
Display(' ' + HttpSrv.CopyRight);
Display(' Winsock:');
Display(' Version ' +
Format('%d.%d', [WinsockInfo.wHighVersion shr 8,
WinsockInfo.wHighVersion and 15]));
Display(' ' + StrPas(@wsi.szDescription));
Display(' ' + StrPas(@wsi.szSystemStatus));
if wsi.lpVendorInfo <> nil then
Display(' ' + StrPas(wsi.lpVendorInfo));
{ Automatically start server }
StartButtonClick(Self);
end;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TAppBaseForm.FormClose(Sender: TObject; var Action: TCloseAction);
var
IniFile : TIniFile;
begin
{ Save persistent data to INI file }
IniFile := TIniFile.Create(FIniFileName);
IniFile.WriteInteger(SectionWindow, KeyTop, Top);
IniFile.WriteInteger(SectionWindow, KeyLeft, Left);
IniFile.WriteInteger(SectionWindow, KeyWidth, Width);
IniFile.WriteInteger(SectionWindow, KeyHeight, Height);
IniFile.WriteString(SectionData, KeyDocDir, HttpServer1.DocDir);
IniFile.WriteString(SectionData, KeyDefaultDoc, HttpServer1.DefaultDoc);
IniFile.WriteString(SectionData, KeyPort, HttpServer1.Port);
IniFile.WriteInteger(SectionData, KeyDisplayHeader,
ord(DisplayHeaderCheckBox.Checked));
IniFile.Destroy;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{ Display a message in display memo box, making sure we don't overflow it. }
procedure TAppBaseForm.Display(Msg : String);
begin
DisplayMemo.Lines.BeginUpdate;
try
if DisplayMemo.Lines.Count > 200 then begin
{ We preserve only 200 lines }
while DisplayMemo.Lines.Count > 200 do
DisplayMemo.Lines.Delete(0);
end;
DisplayMemo.Lines.Add(Msg);
finally
DisplayMemo.Lines.EndUpdate;
{ Makes last line visible }
SendMessage(DisplayMemo.Handle, EM_SCROLLCARET, 0, 0);
end;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{ This event handler is called when user clicks on start button. It is also }
{ called from FormShow event handler, at program startup. It starts server. }
{ We need to pass default document, document directory and client class }
{ to HTTP server component. Client class is very usefull because it }
{ instruct server component to instanciate our own client class instead of }
{ defualt client class. Using our own client class will enables you to add }
{ any data we need to handle our application. This data is private for each }
{ client. }
{ When server is started, we will get OnServerStarted event triggered. }
procedure TAppBaseForm.StartButtonClick(Sender: TObject);
begin
HttpServer1.DocDir := Trim(DocDirEdit.Text);
HttpServer1.DefaultDoc := Trim(DefaultDocEdit.Text);
HttpServer1.Port := Trim(PortEdit.Text);
HttpServer1.ClientClass := TMyHttpConnection;
HttpServer1.Start;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{ This event handler is triggered when user clicks on stop button. We just }
{ stop the server. We will get OnServerStopped event triggered. }
procedure TAppBaseForm.StopButtonClick(Sender: TObject);
begin
HttpServer1.Stop;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{ This event handler is triggered when user clicks on clear buttoN; We just }
{ clear the memo used for displaying activity. }
procedure TAppBaseForm.ClearButtonClick(Sender: TObject);
begin
DisplayMemo.Clear;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{ This event handler is triggered when HTTP server is started, that is when }
{ server socket has started listening. }
procedure TAppBaseForm.HttpServer1ServerStarted(Sender: TObject);
begin
DocDirEdit.Enabled := FALSE;
DefaultDocEdit.Enabled := FALSE;
PortEdit.Enabled := FALSE;
StartButton.Enabled := FALSE;
StopButton.Enabled := TRUE;
Display('Server is waiting for connections');
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{ This event handler is triggered when server has been stopped, that is }
{ when server socket stop listening. }
procedure TAppBaseForm.HttpServer1ServerStopped(Sender: TObject);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -