-
Notifications
You must be signed in to change notification settings - Fork 13
/
apacheadapter.pas
328 lines (267 loc) · 7.73 KB
/
apacheadapter.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
unit ApacheAdapter;
{
*******************************************************************************************************************
ApaacheApapter: Decodes Apache Module reqeuest and response, triggers ApacheModule component
Author: Motaz Abdel Azeem
Forked from: http://wiki.freepascal.org/FPC_and_Apache_Modules
email: motaz@code.sd
Home page: http://code.sd
License: LGPL
Last modified: 27.July.2012
*******************************************************************************************************************
}
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, httpd, apr, SpiderApache, SpiderUtils, syncobjs;
function ProcessHandler(r: Prequest_rec; WebModule: TDataModuleClass; ModuleName,
HandlerName: string; ThreadPool: Boolean = True): Integer;
implementation
type
{ TMyWeb }
TMyWeb = class
public
IsFinished: Boolean;
HasError: Boolean;
Web: TDataModule;
constructor Create;
destructor Destroy; override;
end;
var
myWebPool: array of TMyWeb;
MyCS: TCriticalSection;
SecCS: TCriticalSection;
LastRequest: TDateTime;
(* GetDataModuleFromPool: Thread Pooling implementation *)
function GetDataModuleFromPool(WebModule: TDataModuleClass): TMyWeb;
var
i: Integer;
aWeb: TMyWeb;
Found: Boolean;
FirstEmpty: Integer;
AllFinished: Boolean;
begin
Result:= nil;
MyCS.Enter; // Enter critical section
Found:= False;
FirstEmpty:= -1;
try
// Remove all web modules in idle time to reduce memory leak
AllFinished:= True;
if (LastRequest + EncodeTime(0, 1, 0, 0) < Now) and (Length(myWebPool) > 0) then
begin
for i:= 0 to High(myWebPool) do
if (Assigned(myWebPool[i])) and (myWebPool[i].IsFinished) or (myWebPool[i].HasError) then
begin
myWebPool[i].Web.Free;
myWebPool[i].Free;
myWebPool[i]:= nil;;
end
else
if (AllFinished) and (Assigned(myWebPool[i])) then
AllFinished:= False;
if (Length(myWebPool) > 0) and AllFinished then
SetLength(myWebPool, 0);
end;
LastRequest:= Now;
// Search for new/reusable Web module
for i:= 0 to High(myWebPool) do
begin
// Remove buggy modules
if Assigned(myWebPool[i]) and (myWebPool[i].HasError) then
begin
myWebPool[i].Web.Free;
myWebPool[i].Free;
myWebPool[i]:= nil;
end;
// Get first empty slot index
if (FirstEmpty = -1) and not Assigned(myWebPool[i]) then
FirstEmpty:= i;
// Get usable web module
if Assigned(myWebPool[i]) and (myWebPool[i].IsFinished) and (not myWebPool[i].HasError) then
begin
Result:= myWebPool[i];
Found:= True;
Break;
end;
end;
// No available web module, create new one, add it to the pool
if not Found then
begin
Result:= TMyWeb.Create;
Result.IsFinished:= False;
Result.Web:= WebModule.Create(nil);
if FirstEmpty = -1 then
begin
SetLength(myWebPool, Length(myWebPool) + 1);
FirstEmpty:= High(myWebPool);
end;
myWebPool[FirstEmpty]:= Result;
end;
finally
MyCS.Leave;
end;
end;
function ProcessHandler(r: Prequest_rec; WebModule: TDataModuleClass; ModuleName,
HandlerName: string; ThreadPool: Boolean = True): Integer;
var
RequestedHandler: string;
Buf: array [0 .. 20024] of Char;
NumRead: Integer;
Line: string;
Head: Papr_array_header_t;
Access: Phtaccess_result;
aWeb: TMyWeb;
i: Integer;
SpiderApacheObj: TSpiderApache;
aResponse: TSpiderResponse;
ContentType: string;
j: Integer;
PostedData: string;
DataLen: Integer;
WebFound: Boolean;
FoundIndex: Integer;
CGICom: TSpiderCGI;
begin
RequestedHandler := r^.handler;
aWeb:= nil;
aResponse:= nil;
{ We decline to handle a request if configured handler is not the value of r^.handler }
if not SameText(RequestedHandler, HANDLERNAME) then
begin
Result := DECLINED;
Exit;
end;
ap_set_content_type(r, 'text/html');
{ If the request is for a header only, and not a request for
the whole content, then return OK now. We don't have to do
anything else. }
if (r^.header_only <> 0) then
begin
Result := OK;
Exit;
end;
try
Line:= '';
// read posted data
PostedData:= '';
if (r^.method = 'POST') then
begin
ap_setup_client_block(r, REQUEST_CHUNKED_DECHUNK);
repeat
NumRead:= ap_get_client_block(r, Buf, SizeOf(Buf));
SetLength(Line, NumRead);
Move(Buf, Pointer(Line)^, NumRead);
PostedData:= PostedData + Line;
until NumRead = 0;
end;
if ThreadPool then
begin
repeat
aWeb:= GetDataModuleFromPool(WebModule);
Sleep(2);
until aWeb <> nil;
end
else
begin
aWeb:= TMyWeb.Create;
aweb.Web:= WebModule.Create(nil);
end;
// Search for SpiderApache component in Web Data Module
with aWeb.web do
for i:= 0 to ComponentCount - 1 do
if Components[i] is TSpiderApache then
begin
aResponse:= nil;
SpiderApacheObj:= Components[i] as TSpiderApache;
ContentType:= apr_table_get(r^.headers_in, 'CONTENT-TYPE');
if Trim(ContentType) = '' then
ContentType:= r^.content_type;
// Intialize SpiderApache:
SpiderApacheObj.Init(r^.path_info, ContentType, r^.method, r^.args, apr_table_get(r^.headers_in, 'COOKIE'),
apr_table_get(r^.headers_in, 'User-Agent'), Posteddata, apr_table_get(r^.headers_in, 'Content-Length'),
apr_table_get(r^.headers_in, 'REFERER'), r^.connection^.remote_ip, r^.uri, ap_get_server_version, r^.hostname);
// Execute web application
aResponse:= SpiderApacheObj.Execute;
// Send response to the browser
if Assigned(aResponse) then
begin
// Content type
ap_set_content_type(r, PChar(aResponse.ContentType));
// Custome header
with aResponse.CustomHeader do
for j:= 0 to Count - 1 do
apr_table_set(r^.headers_out, PChar(Copy(Strings[j], 1, Pos(':', Strings[j]) - 1)),
PChar(Copy(Strings[j], Pos(':', Strings[j]) + 1, Length(Strings[j])) ));
// Response contents
DataLen:= Length(aResponse.Content.Text);
ap_rwrite(Pointer(aResponse.Content.Text), DataLen, r);
// Set cookies:
with aResponse.CookieList do
for j:= 0 to Count - 1 do
apr_table_set(r^.headers_out, 'Set-Cookie', PChar(Copy(Strings[j],
Pos(':', Strings[j]) + 1, Length(Strings[j]))));
aResponse.CookieList.Clear;
aResponse.CustomHeader.Clear;
aResponse.Content.Clear;
end;
Break;
end;
except
on e: exception do
begin
// Remove from pool
if ThreadPool and Assigned(aWeb) then
begin
if Assigned(aResponse) then
begin
aResponse.CookieList.Clear;
aResponse.CustomHeader.Clear;
aResponse.Content.Clear;
end;
SecCS.Enter;
try
aWeb.HasError:= True;
finally
SecCS.Leave;
end;
end;
ap_rputs(PChar('<br/> Error in WebModule : <font color=red>' + e.Message + '</font>'), r);
ap_log_error(PChar(MODULENAME), 54, APLOG_NOERRNO or APLOG_NOTICE,1,
r^.server, PChar(ModuleName + ': %s'), [PChar(e.Message)]);
Result:= 1;
end;
end;
if Assigned(aWeb) then
if ThreadPool then
begin
SecCS.Enter;
try
aWeb.IsFinished:= True
finally
SecCS.Leave;
end;
end
else
begin
aWeb.Web.Free;
aWeb.Free;
end;
Result:= Ok;
end;
{ TMyWeb }
constructor TMyWeb.Create;
begin
inherited Create;
IsFinished:= False;
HasError:= False;
Web:= nil;
end;
destructor TMyWeb.Destroy;
begin
inherited Destroy;
end;
initialization
MyCS:= TCriticalSection.Create;
SecCS:= TCriticalSection.Create;
end.