This repository has been archived by the owner on Jan 25, 2019. It is now read-only.
/
BrookTardigradeBroker.pas
315 lines (273 loc) · 7.55 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
(* _ _
* | |__ _ __ ___ ___ | | __
* | '_ \| '__/ _ \ / _ \| |/ /
* | |_) | | | (_) | (_) | <
* |_.__/|_| \___/ \___/|_|\_\
*
* –– an ideal Pascal microframework to develop cross-platform HTTP servers.
*
* Copyright (c) 2012-2018 Silvio Clecio <silvioprog@gmail.com>
*
* This file is part of Brook library.
*
* Brook framework is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Brook framework 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. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Brook framework. If not, see <http://www.gnu.org/licenses/>.
*)
{ Use this unit ONLY if you have a legacy application in Brook 3 and wants to
run it in Brook 4. }
unit BrookTardigradeBroker;
{$MODE DELPHI}
interface
uses
SysUtils,
Classes,
HTTPDefs,
HTTPProtocol,
CustApp,
BrookLibraryLoader,
BrookUtility,
BrookStringMap,
BrookHTTPUploads,
BrookHTTPRequest,
BrookHTTPResponse,
BrookHTTPServer,
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 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;
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;
public
procedure Initialize; virtual;
end;
{ TApplication }
TApplication = class(TCustomApplication, IBrookApplication)
private
FLibraryLoader: TBrookLibraryLoader;
FServer: THTTPServer;
function GetTerminated: Boolean;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Initialize; override;
procedure Run; virtual;
function Instance: TObject;
property LibraryLoader: TBrookLibraryLoader read FLibraryLoader;
property Server: THTTPServer read FServer;
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
FetchUploads(AHandle.Uploads);
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.AddPair(P.Name, P.Value);
end;
end;
procedure THTTPRequest.FetchFields(AMap: TBrookStringMap; ADest: TStrings);
var
P: TBrookStringPair;
begin
for P in AMap do
ADest.AddPair(P.Name, P.Value);
end;
procedure THTTPRequest.FetchParams(AMap: TBrookStringMap; ADest: TStrings);
var
P: TBrookStringPair;
begin
for P in AMap do
ADest.AddPair(P.Name, P.Value);
end;
procedure THTTPRequest.FetchCookies(AMap: TBrookStringMap; ADest: TStrings);
var
P: TBrookStringPair;
begin
for P in AMap do
ADest.AddPair(P.Name, P.Value);
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;
{ 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;
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;
procedure THTTPServer.Initialize;
begin
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;
destructor TApplication.Destroy;
begin
FServer.Free;
FLibraryLoader.Free;
inherited Destroy;
end;
procedure TApplication.Initialize;
begin
inherited Initialize;
FLibraryLoader.Open;
FServer.Initialize;
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.