-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSBD.AP24.ApacheApp.pas
240 lines (202 loc) · 6.36 KB
/
SBD.AP24.ApacheApp.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
unit SBD.AP24.ApacheApp;
interface
uses SysUtils, Classes, SBD.AP24.httpd, SBD.AP24.ApacheHTTP, HTTPApp, WebBroker,
SBD.HTTPDCustomDirective, Generics.Collections;
const
UnitName = 'SBD.AP24.ApacheApp.pas';
type
IWebRequestProcessor = interface
['{8D0FE838-C875-45F6-B679-B8DA31553F63}']
function HandleRequest( Request: TWebRequest; Response: TWebResponse): boolean;
end;
TApacheTwoApplication = class(TWebApplication, IWebRequestProcessor)
private
FDirectives: TList<TDirective>;
Fmod: pmodule;
FApacheModuleName: string;
FComArray: array of command_rec;
FHandlers: TDictionary<string,IWebRequestProcessor>;
procedure ApacheHandleException(Sender: TObject);
protected
function NewRequest(var r: request_rec): TApacheTwoRequest;
function NewResponse(ApacheRequest: TApacheTwoRequest): TApacheTwoResponse;
public
constructor CreateApache2App( AOwner: TComponent; mod1: pmodule; const ApacheModuleName1, HandlerName1: string);
destructor Destroy; override;
procedure Initialize; override;
procedure AddDirective( Addend: TDirective);
procedure RegisterHandler( const HandlerName1: string; const Processor: IWebRequestProcessor = nil);
function ProcessRequest( var r: request_rec; const Processor: IWebRequestProcessor): Integer;
end;
TRegisterHooksEvent = procedure(p: Papr_pool_t);
var
ModuleFileName: UTF8String = UnitName;
apache_module: module;
implementation
uses
{$IFDEF MSWINDOWS}
Windows,
{$ENDIF}
BrkrConst;
procedure HandleServerException(E: TObject; var r: request_rec);
var
ErrorMessage, EMsg: string;
begin
if E is Exception then
EMsg := Exception(E).Message
else
EMsg := '';
ErrorMessage := Format(sInternalServerError, [E.ClassName, EMsg]);
r.content_type := 'text/html';
apr_table_set( r.headers_out^, 'Content-Length', pansichar( AnsiString( IntToStr( Length( ErrorMessage)))));
ap_rputs_DelphiW( ErrorMessage, r)
end;
function ComputeModulePathedFileName: string;
begin
SetLength( result, MAX_PATH + 1);
SetLength( result, GetModuleFileName( HInstance, PChar( result), Length( result)))
end;
function ComputeModuleFileName: string;
begin
result := lowerCase( ExtractFileName( ComputeModulePathedFileName))
end;
constructor TApacheTwoApplication.CreateApache2App( AOwner: TComponent; mod1: pmodule; const ApacheModuleName1, HandlerName1: string);
var
HandlerName: string;
begin
inherited Create( AOwner);
FHandlers := TDictionary<string,IWebRequestProcessor>.Create;
FDirectives := TList<TDirective>.Create;
Classes.ApplicationHandleException := ApacheHandleException;
Fmod := mod1;
if not assigned( Fmod) then
Fmod := @apache_module;
FApacheModuleName := ApacheModuleName1;
HandlerName := HandlerName1;
if HandlerName = '' then
HandlerName := ChangeFileExt( ComputeModuleFileName, '-handler');
RegisterHandler( HandlerName, nil)
end;
destructor TApacheTwoApplication.Destroy;
var
Proc1, Proc2: procedure (Sender: TObject) of object;
begin
FHandlers.Free;
Proc1 := Classes.ApplicationHandleException;
Proc2 := ApacheHandleException;
if @Proc1 = @Proc2 then
Classes.ApplicationHandleException := nil;
FDirectives.Free;
inherited Destroy;
end;
function Application_Handler( var r: request_rec): integer; cdecl;
var
RequestedHandler: string;
App: TApacheTwoApplication;
begin
RequestedHandler := UTF8ToString( r.handler);
App := Application as TApacheTwoApplication;
if App.FHandlers.ContainsKey( RequestedHandler) then
result := App.ProcessRequest( r, App.FHandlers[ RequestedHandler])
else
result := AP_DECLINED
end;
procedure RegisterHooks( p: papr_pool_t); cdecl;
begin
ap_hook_handler( Application_Handler, nil, nil, APR_HOOK_MIDDLE)
end;
procedure TApacheTwoApplication.Initialize;
var
j, L: integer;
begin
FillChar( Fmod^, SizeOf( Fmod^), 0);
L := FDirectives.Count;
if L = 0 then
FMod^.cmds := nil
else
begin
SetLength( FComArray, L + 1);
for j := 0 to L - 1 do
FDirectives[j].Build_command_rec( FComArray[j]);
with FComArray[ L] do
begin
name := nil;
func.raw_args := nil;
cmd_data := nil;
req_override := 0;
args_how := RAW_ARGS;
errmsg := nil
end;
FMod^.cmds := @FComArray[0]
end;
with Fmod^ do
begin
version := integer( MODULE_MAGIC_NUMBER_MAJOR);
minor_version := MODULE_MAGIC_NUMBER_MINOR;
magic := cardinal( MODULE_MAGIC_COOKIE);
module_index := -1;
name := PAnsiChar( ModuleFileName);
register_hooks := RegisterHooks
end
end;
function TApacheTwoApplication.ProcessRequest( var r: request_rec; const Processor: IWebRequestProcessor): integer;
var
HTTPRequest: TApacheTwoRequest;
HTTPResponse: TApacheTwoResponse;
begin
try
HTTPRequest := NewRequest( r);
try
HTTPResponse := NewResponse( HTTPRequest);
try
if assigned( Processor) then
Processor.HandleRequest( HTTPRequest, HTTPResponse)
else
HandleRequest( HTTPRequest, HTTPResponse);
result := HTTPResponse.ReturnCode
finally
HTTPResponse.Free
end;
finally
HTTPRequest.Free
end;
except
HandleServerException( ExceptObject, r);
result := AP_OK
end
end;
procedure TApacheTwoApplication.RegisterHandler( const HandlerName1: string; const Processor: IWebRequestProcessor);
begin
FHandlers.AddOrSetValue( HandlerName1, Processor)
end;
procedure TApacheTwoApplication.AddDirective( Addend: TDirective);
begin
FDirectives.Add( Addend);
Addend.FApp := self
end;
procedure TApacheTwoApplication.ApacheHandleException( Sender: TObject);
var
Handled: Boolean;
Intf: IWebExceptionHandler;
E: TObject;
begin
Handled := False;
if (ExceptObject is Exception) and
Supports(Sender, IWebExceptionHandler, Intf) then
Intf.HandleException( Exception( ExceptObject), Handled);
if not Handled then
begin
E := ExceptObject;
AcquireExceptionObject;
raise E
end
end;
function TApacheTwoApplication.NewRequest( var r: request_rec): TApacheTwoRequest;
begin
result := TApacheTwoRequest.Create( r)
end;
function TApacheTwoApplication.NewResponse( ApacheRequest: TApacheTwoRequest): TApacheTwoResponse;
begin
result := TApacheTwoResponse.Create( ApacheRequest)
end;
end.