-
Notifications
You must be signed in to change notification settings - Fork 4
/
HttpServerCommand.pas
100 lines (86 loc) · 2.72 KB
/
HttpServerCommand.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
unit HttpServerCommand;
interface
uses
vcl.forms,
IdContext, IdCustomHTTPServer,
CommandRegistry,
System.SysUtils, classes;
type
TOnLogMessage = procedure (const msg: String) of Object;
type
THttpServerCommand = class(TComponent)
strict private
FOnLogMessage: TOnLogMessage;
FCommands: THttpServerCommandRegister;
private
function FindCommand(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo): boolean;
procedure SendError(ACmd: TRESTCommandREG;AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo; E: Exception);
procedure LogMessage(const msg: String);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure CommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
property Commands: THttpServerCommandRegister read FCommands;
property OnLogMessage: TOnLogMessage read FOnLogMessage write FOnLogMessage;
end;
implementation
{ THttpServerCommand }
procedure THttpServerCommand.LogMessage(const msg: String);
begin
if assigned(FOnLogMessage) then
OnLogMessage(msg);
end;
function THttpServerCommand.FindCommand(AContext: TIdContext;
ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo): boolean;
var
cmdReg: TRESTCommandREG;
cmd: TRESTCommand;
Params: TStringList;
begin
Params:= TStringList.Create;
try
cmdReg:= FCommands.FindCommand(ARequestInfo.Command,ARequestInfo.URI, Params);
if cmdReg=nil then exit(false);
try
cmd := cmdReg.FCommand.create;
try
cmd.Start(cmdReg, AContext, ARequestInfo, AResponseInfo, Params);
LogMessage(cmd.StreamContents);
cmd.Execute(self.Owner as TForm); // Again with the TForm
finally
cmd.Free;
end;
except
on e:Exception do
begin
SendError(cmdReg, AContext, ARequestInfo, AResponseInfo, e);
end;
end;
result:= true;
finally
params.Free;
end;
end;
procedure THttpServerCommand.SendError(ACmd: TRESTCommandREG;
AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo;
AResponseInfo: TIdHTTPResponseInfo; E: Exception);
begin
AResponseInfo.ResponseNo := 404;
end;
procedure THttpServerCommand.CommandGet(AContext: TIdContext;
ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
begin
if not FindCommand(AContext,ARequestInfo,AResponseInfo) then
AResponseInfo.ResponseNo := 404;
end;
constructor THttpServerCommand.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FCommands:= THttpServerCommandRegister.Create(self);
end;
destructor THttpServerCommand.Destroy;
begin
FCommands.Free;
inherited;
end;
end.