-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathHttpServerCommand.pas
288 lines (247 loc) · 7.26 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
unit HttpServerCommand;
interface
uses
classes, IdContext, IdCustomHTTPServer, generics.collections,
System.SysUtils;
type
TRESTCommandClass = class of TRESTCommand;
THttpServerCommand= class;
TRESTCommandREG= class;
TRESTCommand=class
private
FParams: TStringList;
FContext: TIdContext;
FRequestInfo: TIdHTTPRequestInfo;
FResponseInfo: TIdHTTPResponseInfo;
FReg: TRESTCommandREG;
procedure start(AReg: TRESTCommandREG; AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo; AParams: TStringList);
procedure ParseParams(AURI, AMask:String);
protected
procedure execute(); virtual;
public
constructor create(AServer: THttpServerCommand);
procedure ResponseJSON(Json: String);
destructor Destroy; override;
property Context: TIdContext read FContext;
property RequestInfo: TIdHTTPRequestInfo read FRequestInfo;
property ResponseInfo: TIdHTTPResponseInfo read FResponseInfo;
property Params: TStringList read FParams;
end;
TRESTCommandREG= class
public
FTYPE: String;
FPATH: String;
FCommand: TRESTCommandClass;
constructor Create(ATYPE:String; APATH: String; ACommand: TRESTCommandClass);
end;
THttpServerCommandRegister=class(TComponent)
private
FList: TObjectList<TRESTCommandREG>;
public
procedure Register(ATYPE:String; APATH: String; ACommand: TRESTCommandClass);
constructor Create(AOwner: TComponent); override;
function isUri(AURI: String; AMask: String; AParams: TStringList): boolean;
function FindCommand(ACommand: String; AURI: String; Params: TStringList): TRESTCommandREG;
destructor Destroy; override;
end;
THttpServerCommand= class (TComponent)
private
FCommands: THttpServerCommandRegister;
procedure SetCommands(const Value: THttpServerCommandRegister);
function TrataCommand(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo): boolean;
procedure TrataErro(ACmd: TRESTCommandREG;AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo; E: Exception);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure CommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
property Commands: THttpServerCommandRegister read FCommands;
end;
implementation
{ THttpServerCommand }
function THttpServerCommand.TrataCommand(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(self);
try
cmd.start(cmdReg, AContext, ARequestInfo, AResponseInfo, Params);
cmd.execute;
finally
cmd.Free;
end;
except
on e:Exception do
begin
TrataErro(cmdReg, AContext, ARequestInfo, AResponseInfo, e);
end;
end;
result:= true;
finally
params.Free;
end;
end;
procedure THttpServerCommand.TrataErro(ACmd: TRESTCommandREG;
AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo;
AResponseInfo: TIdHTTPResponseInfo; E: Exception);
begin
// TODO ...
AResponseInfo.ContentText := format('<http><body>RestServer<br>Erro no commando conhecido:%s => %s: %s </body></http>',[ARequestInfo.Command, ARequestInfo.URI, e.Message]);
end;
procedure THttpServerCommand.CommandGet(AContext: TIdContext;
ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
begin
if not TrataCommand(AContext,ARequestInfo,AResponseInfo) then
AResponseInfo.ContentText := format('<http><body>RestServer<br>Commando não conhecido:%s => %s </body></http>',[ARequestInfo.Command, ARequestInfo.URI]);
end;
constructor THttpServerCommand.Create(AOwner: TComponent);
begin
inherited;
FCommands:= THttpServerCommandRegister.Create(self);
end;
destructor THttpServerCommand.Destroy;
begin
FCommands.Free;
inherited;
end;
procedure THttpServerCommand.SetCommands(
const Value: THttpServerCommandRegister);
begin
end;
{ THttpServerCommandRegister }
constructor THttpServerCommandRegister.Create(AOwner: TComponent);
begin
inherited;
FList:= TObjectList<TRESTCommandREG>.Create(True);
end;
destructor THttpServerCommandRegister.Destroy;
begin
FList.Free;
inherited;
end;
function THttpServerCommandRegister.FindCommand(ACommand, AURI: String; Params: TStringList): TRESTCommandREG;
var
I: Integer;
begin
for I := 0 to FList.Count-1 do
begin
if SameText(ACommand,FList[i].FTYPE) then
begin
if isURI(AURI,FList[i].FPATH, Params) then
begin
exit(FList[i]);
end;
end;
end;
result:= nil;
end;
function THttpServerCommandRegister.isUri(AURI, AMask: String; AParams: TStringList): boolean;
var
sl1: TStringList;
sl2: TStringList;
I: Integer;
begin
sl1:= TStringList.Create;
sl2:= TStringList.Create;
try
sl1.StrictDelimiter:= true;
sl1.Delimiter:= '/';
sl1.DelimitedText := AMask;
sl2.StrictDelimiter:= true;
sl2.Delimiter:= '/';
sl2.DelimitedText := AURI;
for I := 0 to sl1.Count-1 do
begin
if sl1[i].StartsWith(':') then
begin
AParams.Values[sl1[i].Substring(1,255)]:= sl2[i];
end else
begin
if not SameText(sl1[i],sl2[i]) then exit(false);
end;
end;
result:= true;
finally
sl1.Free;
sl2.Free;
end;
end;
//begin
// if SameText(AURI, AMask) then exit(true);
// result:= false;
//end;
procedure THttpServerCommandRegister.Register(ATYPE, APATH: String;
ACommand: TRESTCommandClass);
begin
FList.Add(TRESTCommandREG.Create(ATYPE, APATH, ACommand));
end;
{ TRESTCommandREG }
constructor TRESTCommandREG.Create(ATYPE, APATH: String;
ACommand: TRESTCommandClass);
begin
FTYPE:= AType;
FPATH:= APATH;
FCommand:= ACommand;
end;
{ TRESTCommand }
constructor TRESTCommand.create(AServer: THttpServerCommand);
begin
FParams:= TStringList.Create;
end;
procedure TRESTCommand.start(AReg: TRESTCommandREG; AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo; AParams: TStringList);
begin
FContext:= AContext;
FRequestInfo:= ARequestInfo;
FResponseInfo:= AResponseInfo;
FReg:= AReg;
FParams.Assign(AParams);
ParseParams(ARequestInfo.URI, AReg.FPATH);
end;
destructor TRESTCommand.Destroy;
begin
FParams.free;
inherited;
end;
procedure TRESTCommand.execute;
begin
end;
procedure TRESTCommand.ParseParams(AURI, AMask: String);
var
sl1: TStringList;
sl2: TStringList;
I: Integer;
begin
sl1:= TStringList.Create;
sl2:= TStringList.Create;
try
sl1.StrictDelimiter:= true;
sl1.Delimiter:= '/';
sl1.DelimitedText := AMask;
sl2.StrictDelimiter:= true;
sl2.Delimiter:= '/';
sl2.DelimitedText := AURI;
for I := 0 to sl1.Count-1 do
begin
if sl1[i].StartsWith(':') then
begin
FParams.Values[sl1[i].Substring(1,255)]:= sl2[i];
end;
end;
finally
sl1.Free;
sl2.Free;
end;
end;
procedure TRESTCommand.ResponseJSON(Json: String);
begin
ResponseInfo.ContentText := Json;
ResponseInfo.ContentType := 'Application/JSON';
end;
end.