-
Notifications
You must be signed in to change notification settings - Fork 29
/
brooktardigradebroker.pas
383 lines (329 loc) · 8.96 KB
/
brooktardigradebroker.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
(*
Brook for Free Pascal
Copyright (C) 2018-2019 Silvio Clecio
See the file LICENSE.txt, included in this distribution,
for details about the copyright.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*)
{ Use this unit ONLY if you want to run Brook for Free Pascal
under Brook Framework. }
unit BrookTardigradeBroker;
{$MODE DELPHI}
interface
uses
SysUtils,
Classes,
HTTPDefs,
HTTPProtocol,
CustApp,
BrookLibraryLoader,
BrookUtility,
BrookString,
BrookStringMap,
BrookHTTPUploads,
BrookHTTPRequest,
BrookHTTPResponse,
BrookHTTPServer,
BrookUtils,
BrookHttpConsts,
BrookApplication,
BrookRouter;
type
{ THTTPRequest }
THTTPRequest = class(TRequest)
private
FHandle: TBrookHTTPRequest;
protected
procedure FetchHeaders(AMap: TBrookStringMap; ADest: TStrings); virtual;
procedure FetchFields(AMap: TBrookStringMap; ADest: TStrings); virtual;
procedure FetchParams(AMap: TBrookStringMap; ADest: TStrings); virtual;
procedure FetchCookies(AMap: TBrookStringMap; ADest: TStrings); virtual;
procedure FetchPayload(ABuffer: TBrookString); virtual;
procedure FetchUploads(AUploads: TBrookHTTPUploads); virtual;
property Handle: TBrookHTTPRequest read FHandle;
public
constructor Create(AHandle: TBrookHTTPRequest); reintroduce; virtual;
end;
{ THTTPResponse }
THTTPResponse = class(TResponse)
private
FHandle: TBrookHTTPResponse;
protected
procedure DoSendHeaders(AHeaders: TStrings); override;
procedure DoSendContent; override;
procedure CollectHeaders(AHeaders: TStrings); override;
property Handle: TBrookHTTPResponse read FHandle;
public
constructor Create(AHandle: TBrookHTTPResponse;
ARequest: TRequest); reintroduce; virtual;
end;
{ THTTPServer }
THTTPServer = class(TBrookHTTPServer)
protected
function CreateHTTPRequest(AHandle: TBrookHTTPRequest): THTTPRequest; virtual;
function CreateHTTPResponse(AHandle: TBrookHTTPResponse;
ARequest: THTTPRequest): THTTPResponse; virtual;
procedure DoRequest(ASender: TObject; ARequest: TBrookHTTPRequest;
AResponse: TBrookHTTPResponse); override;
procedure DoRequestError(ASender: TObject; ARequest: TBrookHTTPRequest;
AResponse: TBrookHTTPResponse; AException: Exception); override;
public
procedure Initialize; virtual;
end;
{ TApplication }
TApplication = class(TCustomApplication, IBrookApplication)
private
FLibraryLoader: TBrookLibraryLoader;
FOnIdle: TNotifyEvent;
FServer: THTTPServer;
function GetTerminated: Boolean;
protected
procedure DoRun; override;
public
constructor Create(AOwner: TComponent); override;
procedure CreateForm(AInstanceClass: TComponentClass; out AReference);
destructor Destroy; override;
procedure Initialize; override;
procedure Run; virtual;
function Instance: TObject;
property LibraryLoader: TBrookLibraryLoader read FLibraryLoader;
property Server: THTTPServer read FServer;
property OnIdle: TNotifyEvent read FOnIdle write FOnIdle;
end;
var
Application: TApplication = nil;
implementation
{ THTTPRequest }
constructor THTTPRequest.Create(AHandle: TBrookHTTPRequest);
begin
inherited Create;
FHandle := AHandle;
FetchHeaders(AHandle.Headers, CustomHeaders);
FetchFields(AHandle.Fields, ContentFields);
FetchParams(AHandle.Params, QueryFields);
FetchCookies(AHandle.Cookies, CookieFields);
if AHandle.IsUploading then
begin
FetchPayload(AHandle.Payload);
FetchUploads(AHandle.Uploads);
end;
HttpVersion := FHandle.Version;
Method := FHandle.Method;
PathInfo := FHandle.Path;
UserAgent := FHandle.UserAgent;
Referer := FHandle.Referer;
end;
procedure THTTPRequest.FetchHeaders(AMap: TBrookStringMap; ADest: TStrings);
var
P: TBrookStringPair;
H: THeader;
begin
for P in AMap do
begin
H := HeaderType(P.Name);
if H <> hhUnknown then
SetHeader(H, P.Value)
else
ADest.Add(Concat(P.Name, ADest.NameValueSeparator, P.Value));
end;
end;
procedure THTTPRequest.FetchFields(AMap: TBrookStringMap; ADest: TStrings);
var
P: TBrookStringPair;
begin
for P in AMap do
ADest.Add(Concat(P.Name, ADest.NameValueSeparator, P.Value));
end;
procedure THTTPRequest.FetchParams(AMap: TBrookStringMap; ADest: TStrings);
var
P: TBrookStringPair;
begin
for P in AMap do
ADest.Add(Concat(P.Name, ADest.NameValueSeparator, P.Value));
end;
procedure THTTPRequest.FetchCookies(AMap: TBrookStringMap; ADest: TStrings);
var
P: TBrookStringPair;
begin
for P in AMap do
ADest.Add(Concat(P.Name, ADest.NameValueSeparator, P.Value));
end;
procedure THTTPRequest.FetchPayload(ABuffer: TBrookString);
var
S: string;
begin
S := ABuffer.Text;
InitContent(S);
end;
procedure THTTPRequest.FetchUploads(AUploads: TBrookHTTPUploads);
var
U: TBrookHTTPUpload;
F: TUploadedFile;
E: string;
begin
for U in AUploads do
begin
if not U.Save(True, E) then
raise EFCreateError.Create(E);
F := TUploadedFile.Create(Files);
F.FieldName := U.Field;
F.FileName := U.Name;
F.ContentType := U.Mime;
F.LocalFileName := ConcatPaths([U.Directory, U.Name]);
F.Size := U.Size;
end;
end;
{ THTTPResponse }
constructor THTTPResponse.Create(AHandle: TBrookHTTPResponse; ARequest: TRequest);
begin
inherited Create(ARequest);
FHandle := AHandle;
Contents.LineBreak := '';
end;
procedure THTTPResponse.DoSendHeaders(AHeaders: TStrings);
var
NVS: Char;
I: Integer;
N, V: string;
begin
NVS := AHeaders.NameValueSeparator;
try
AHeaders.NameValueSeparator := ':';
FHandle.Headers.Clear;
for I := 0 to Pred(AHeaders.Count) do
begin
AHeaders.GetNameValue(I, N, V);
FHandle.Headers.Add(N, V.Trim);
end;
FHandle.Headers.Remove('Content-Length');
finally
AHeaders.NameValueSeparator := NVS;
end;
end;
procedure THTTPResponse.DoSendContent;
begin
FHandle.Send(Contents.Text, ContentType, Code);
end;
procedure THTTPResponse.CollectHeaders(AHeaders: TStrings);
begin
AHeaders.Add(Concat(BROOK_HTTP_HEADER_X_POWERED_BY,
AHeaders.NameValueSeparator, 'Brook for Free Pascal'));
inherited CollectHeaders(AHeaders);
end;
{ THTTPServer }
function THTTPServer.CreateHTTPRequest(AHandle: TBrookHTTPRequest): THTTPRequest;
begin
Result := THTTPRequest.Create(AHandle);
end;
function THTTPServer.CreateHTTPResponse(AHandle: TBrookHTTPResponse;
ARequest: THTTPRequest): THTTPResponse;
begin
Result := THTTPResponse.Create(AHandle, ARequest);
end;
{$PUSH}{$WARN 5024 OFF}
procedure THTTPServer.DoRequest(ASender: TObject; ARequest: TBrookHTTPRequest;
AResponse: TBrookHTTPResponse);
var
VReq: THTTPRequest;
VRes: THTTPResponse;
begin
VReq := CreateHTTPRequest(ARequest);
VRes := CreateHTTPResponse(AResponse, VReq);
try
TBrookRouter.Service.Route(VReq, VRes);
VRes.SendContent;
finally
VReq.Free;
VRes.Free;
end;
end;
{$POP}
procedure THTTPServer.DoRequestError(ASender: TObject;
ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse;
AException: Exception);
var
VReq: THTTPRequest;
VRes: THTTPResponse;
VHandled: Boolean;
begin
VReq := CreateHTTPRequest(ARequest);
VRes := CreateHTTPResponse(AResponse, VReq);
try
if Assigned(BrookSettings.OnError) then
begin
VHandled:= True;
BrookSettings.OnError(VRes, AException, VHandled);
if VHandled then
Exit;
end;
inherited DoRequestError(ASender, ARequest, AResponse, AException);
finally
VRes.Free;
VReq.Free;
end;
end;
procedure THTTPServer.Initialize;
begin
if BrookSettings.Port <> 0 then
Port := BrookSettings.Port;
TRequest.DefaultRequestUploadDir := BrookSettings.DirectoryForUploads;
if TRequest.DefaultRequestUploadDir = '' then
TRequest.DefaultRequestUploadDir := Sagui.TmpDir;
UploadsDir := TRequest.DefaultRequestUploadDir;
end;
{ TApplication }
constructor TApplication.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FLibraryLoader := TBrookLibraryLoader.Create(nil);
FServer := THTTPServer.Create(nil);
end;
{$PUSH}{$WARN 5024 OFF}
procedure TApplication.CreateForm(AInstanceClass: TComponentClass;
out AReference);
begin
TComponent(AReference) := AInstanceClass.Create(Self);
end;
{$POP}
destructor TApplication.Destroy;
begin
FServer.Free;
FLibraryLoader.Free;
inherited Destroy;
end;
procedure TApplication.Initialize;
begin
inherited Initialize;
FLibraryLoader.Open;
FServer.Initialize;
end;
procedure TApplication.DoRun;
begin
inherited DoRun;
if Assigned(FOnIdle) then
FOnIdle(Self)
else
Sleep(1);
end;
procedure TApplication.Run;
begin
FServer.Open;
inherited Run;
end;
function TApplication.GetTerminated: Boolean;
begin
Result := Terminated;
end;
function TApplication.Instance: TObject;
begin
Result := FServer;
end;
initialization
Application := TApplication.Create(nil);
BrookRegisterApp(Application);
finalization
BrookUnregisterApp;
FreeAndNil(Application);
end.