-
Notifications
You must be signed in to change notification settings - Fork 4
/
CommandRegistry.pas
222 lines (180 loc) · 5.16 KB
/
CommandRegistry.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
unit CommandRegistry;
interface
uses
Vcl.forms,
classes, IdContext, IdCustomHTTPServer, generics.collections,
System.SysUtils;
type
TRESTCommandClass = class of TRESTCommand;
TRESTCommandREG = class;
TRESTCommand = class
private
FParams: TStringList;
FContext: TIdContext;
FRequestInfo: TIdHTTPRequestInfo;
FResponseInfo: TIdHTTPResponseInfo;
FReg: TRESTCommandREG;
FStreamContents: String;
procedure ParseParams(const AURI, AMask:String);
procedure ReadStream(ARequestInfo: TIdHTTPRequestInfo);
public
class function GetCommand: String; virtual;
class function GetRoute: String; virtual;
constructor Create;
procedure Start(AReg: TRESTCommandREG; AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo; AParams: TStringList);
procedure Execute(AOwner: TForm); virtual;
procedure ResponseJSON(Json: String);
procedure Error(code: integer);
destructor Destroy; override;
property Context: TIdContext read FContext;
property RequestInfo: TIdHTTPRequestInfo read FRequestInfo;
property ResponseInfo: TIdHTTPResponseInfo read FResponseInfo;
property Params: TStringList read FParams;
property StreamContents : String read FStreamContents;
property Reg: TRESTCommandREG read FReg;
end;
TRESTCommandREG = class
public
FTYPE: String;
FPATH: String;
FCommand: TRESTCommandClass;
FHost: TForm; // TComponent;
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); overload;
procedure Register(ACommand: TRESTCommandClass); overload;
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;
implementation
uses
RegularExpressions;
{ 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
x: Integer;
M : TMatch;
begin
result := TRegEx.IsMatch(AURI, AMask);
M := TRegEx.Match(AURI, AMask);
for x := 0 to M.Groups.Count-1 do
begin
AParams.Add(M.Groups[x].Value);
end;
end;
procedure THttpServerCommandRegister.Register(ATYPE, APATH: String; ACommand: TRESTCommandClass);
begin
FList.Add(TRESTCommandREG.Create(ATYPE, APATH, ACommand));
end;
procedure THttpServerCommandRegister.Register(ACommand: TRESTCommandClass);
begin
FList.Add(TRESTCommandREG.Create(ACommand.GetCommand, ACommand.GetRoute, ACommand));
end;
{ TRESTCommandREG }
constructor TRESTCommandREG.Create(ATYPE, APATH: String; ACommand: TRESTCommandClass);
begin
FTYPE := AType;
FPATH := APATH;
FCommand := ACommand;
end;
{ TRESTCommand }
class function TRESTCommand.GetCommand: String;
begin
result := '';
end;
class function TRESTCommand.GetRoute: String;
begin
result := '';
end;
constructor TRESTCommand.create;
begin
FParams:= TStringList.Create;
end;
procedure TRESTCommand.ReadStream(ARequestInfo: TIdHTTPRequestInfo);
var
oStream : TStringStream;
begin
// Decode stream
if ArequestInfo.PostStream <> nil then
begin
oStream := TStringStream.create;
try
oStream.CopyFrom(ArequestInfo.PostStream, ArequestInfo.PostStream.Size);
oStream.Position := 0;
FStreamContents := oStream.readString(oStream.Size);
finally
oStream.free;
end;
end;
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);
ReadStream(ARequestInfo);
end;
destructor TRESTCommand.Destroy;
begin
FParams.free;
inherited;
end;
procedure TRESTCommand.Execute(AOwner: TForm);
begin
end;
procedure TRESTCommand.ParseParams(const AURI, AMask: String);
var
x: Integer;
M : TMatch;
begin
M := TRegEx.Match(AURI, AMask);
for x := 0 to M.Groups.Count-1 do
begin
FParams.Add(M.Groups[x].Value);
end;
end;
procedure TRESTCommand.ResponseJSON(Json: String);
begin
ResponseInfo.ContentText := Json;
ResponseInfo.ContentType := 'Application/JSON';
end;
procedure TRESTCommand.Error(code: integer);
begin
ResponseInfo.ResponseNo := code;
end;
end.