forked from zamronypj/fano
/
FcgiFrameParserImpl.pas
322 lines (284 loc) · 11.3 KB
/
FcgiFrameParserImpl.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
{*!
* Fano Web Framework (https://fanoframework.github.io)
*
* @link https://github.com/fanoframework/fano
* @copyright Copyright (c) 2018 Zamrony P. Juhara
* @license https://github.com/fanoframework/fano/blob/master/LICENSE (MIT)
*}
unit FcgiFrameParserImpl;
interface
{$MODE OBJFPC}
{$H+}
uses
InjectableObjectImpl,
FcgiRecordIntf,
FcgiRecordFactoryIntf,
FcgiFrameParserIntf,
StreamAdapterIntf,
MemoryAllocatorIntf,
MemoryDeallocatorIntf;
type
(*!-----------------------------------------------
* FastCGI Frame Parser
*
* @author Zamrony P. Juhara <zamronypj@yahoo.com>
*-----------------------------------------------*)
TFcgiFrameParser = class (TInjectableObject, IFcgiFrameParser, IMemoryAllocator, IMemoryDeallocator)
private
fRecordFactories : TFcgiRecordFactoryArray;
procedure raiseExceptionIfBufferNil(const buffer : pointer);
procedure raiseExceptionIfInvalidBufferSize(const bufferSize : int64);
procedure raiseExceptionIfInvalidBuffer(const buffer : pointer; const bufferSize : int64);
function isValidRecordType(const reqType : byte) : boolean;
procedure raiseExceptionIfInvalidRecordType(const reqType : byte);
function readBytes(
const stream : IStreamAdapter;
buf : pointer;
amountToRead : integer;
out streamEmpty : boolean
) : integer;
public
constructor create(const factories : TFcgiRecordFactoryArray);
destructor destroy(); override;
(*!------------------------------------------------
* read stream and return found record in memory buffer
*-----------------------------------------------
* @param bufPtr, memory buffer to store FastCGI record
* @param bufSize, memory buffer size
* @return true if stream is exhausted
*-----------------------------------------------*)
function readRecord(
const stream : IStreamAdapter;
out bufPtr : pointer;
out bufSize : ptrUint
) : boolean;
(*!------------------------------------------------
* test if buffer contain FastCGI frame package
* i.e FastCGI header + payload
*-----------------------------------------------
* @param buffer, pointer to current buffer
* @return true if buffer contain valid frame
*-----------------------------------------------*)
function hasFrame(const buffer : pointer; const bufferSize : ptrUint) : boolean;
(*!------------------------------------------------
* parse current buffer and create its corresponding
* FastCGI record instance
*-----------------------------------------------
* @param buffer, pointer to current buffer
* @param bufferSize, size of buffer
* @return IFcgiRecord instance
* @throws EInvalidFcgiBuffer exception when buffer is nil
* @throws EInvalidFcgiHeaderLen exception when header size not valid
*-----------------------------------------------*)
function parseFrame(const buffer : pointer; const bufferSize : ptrUint) : IFcgiRecord;
(*!------------------------------------------------
* allocate memory
*-----------------------------------------------
* @param requestedSize, number of bytes to allocate
* @return pointer of allocated memory
*-----------------------------------------------*)
function allocate(const requestedSize : PtrUint) : pointer;
(*!------------------------------------------------
* deallocate memory
*-----------------------------------------------
* @param ptr, pointer of memory to be allocated
* @param requestedSize, number of bytes to deallocate
*-----------------------------------------------*)
procedure deallocate(const ptr : pointer; const requestedSize : PtrUint);
end;
implementation
uses
fastcgi,
EInvalidFcgiBufferImpl,
EInvalidFcgiRecordTypeImpl,
EInvalidFcgiHeaderLenImpl;
constructor TFcgiFrameParser.create(const factories : TFcgiRecordFactoryArray);
begin
inherited create();
fRecordFactories := factories;
end;
destructor TFcgiFrameParser.destroy();
var i : integer;
begin
inherited destroy();
for i:= length(fRecordFactories) -1 downto 0 do
begin
fRecordFactories[i] := nil;
end;
setLength(fRecordFactories, 0);
fRecordFactories := nil;
end;
procedure TFcgiFrameParser.raiseExceptionIfBufferNil(const buffer : pointer);
begin
if (buffer = nil) then
begin
raise EInvalidFcgiBuffer.create('FastCGI buffer nil');
end;
end;
procedure TFcgiFrameParser.raiseExceptionIfInvalidBufferSize(const bufferSize : int64);
begin
if (bufferSize < FCGI_HEADER_LEN) then
begin
raise EInvalidFcgiHeaderLen.create('Not enough data in the buffer to parse');
end;
end;
procedure TFcgiFrameParser.raiseExceptionIfInvalidBuffer(const buffer : pointer; const bufferSize : int64);
begin
raiseExceptionIfBufferNil(buffer);
raiseExceptionIfInvalidBufferSize(bufferSize);
end;
(*!------------------------------------------------
* allocate memory
*-----------------------------------------------
* @param requestedSize, number of bytes to allocate
* @return pointer of allocated memory
*-----------------------------------------------*)
function TFcgiFrameParser.allocate(const requestedSize : PtrUint) : pointer;
begin
result := getMem(requestedSize);
end;
(*!------------------------------------------------
* deallocate memory
*-----------------------------------------------
* @param ptr, pointer of memory to be allocated
* @param requestedSize, number of bytes to deallocate
*-----------------------------------------------*)
procedure TFcgiFrameParser.deallocate(const ptr : pointer; const requestedSize : PtrUint);
begin
freeMem(ptr, requestedSize);
end;
(*!------------------------------------------------
* read stream and return found record in memory buffer
*-----------------------------------------------
* @param bufPtr, memory buffer to store FastCGI record
* @param bufSize, memory buffer size
* @return true if stream is exhausted
*-----------------------------------------------*)
function TFcgiFrameParser.readRecord(
const stream : IStreamAdapter;
out bufPtr : pointer;
out bufSize : ptrUint
) : boolean;
var tmp : pointer;
header : FCGI_HEADER;
totBytes, headerSize : integer;
streamEmpty : boolean;
begin
result:= false;
bufPtr := nil;
bufSize := 0;
headerSize := sizeof(FCGI_HEADER);
totBytes := readBytes(stream, @header, headerSize, streamEmpty);
if (streamEmpty) then
begin
result := true;
exit;
end;
if (totBytes <> headerSize) then
begin
raise EInvalidFcgiHeaderLen.createFmt('Invalid header length %d', [totBytes]);
end;
bufSize := FCGI_HEADER_LEN + BEtoN(header.contentLength) + header.paddingLength;
bufPtr := allocate(bufSize);
//copy header and advance position to skip header part
tmp := bufPtr;
PFCGI_HEADER(tmp)^ := header;
inc(tmp, headerSize);
//read content and padding
totBytes := readBytes(stream, tmp, bufSize - headerSize, streamEmpty) + totBytes;
result := streamEmpty;
end;
function TFcgiFrameParser.readBytes(
const stream : IStreamAdapter;
buf : pointer;
amountToRead : integer;
out streamEmpty : boolean
) : integer;
var bytesRead : int64;
begin
result := 0;
streamEmpty := false;
if amountToRead <= 0 then
begin
exit;
end;
repeat
bytesRead := stream.read(buf^, amountToRead);
if (bytesRead > 0) then
begin
dec(amountToRead, bytesRead);
inc(buf, bytesRead);
inc(result, bytesRead);
end else
begin
streamEmpty := true;
end;
until (amountToRead = 0) or streamEmpty;
end;
(*!------------------------------------------------
* test if buffer contain FastCGI frame package
* i.e FastCGI header + payload
*-----------------------------------------------
* @param buffer, pointer to current buffer
* @param bufferSize, size of buffer
* @return true if buffer contain valid frame
*-----------------------------------------------*)
function TFcgiFrameParser.hasFrame(const buffer : pointer; const bufferSize : ptrUint) : boolean;
var header : PFCGI_Header;
contentLength :word;
begin
raiseExceptionIfBufferNil(buffer);
header := buffer;
//header^.contentlength is big endian, convert it to native endian
contentLength := BEtoN(header^.contentLength);
result := (bufferSize >= (FCGI_HEADER_LEN + contentLength + header^.paddingLength));
end;
function TFcgiFrameParser.isValidRecordType(const reqType : byte) : boolean;
begin
result := (reqType = FCGI_BEGIN_REQUEST) or
(reqType = FCGI_ABORT_REQUEST) or
(reqType = FCGI_END_REQUEST) or
(reqType = FCGI_PARAMS) or
(reqType = FCGI_STDIN) or
(reqType = FCGI_STDOUT) or
(reqType = FCGI_STDERR) or
(reqType = FCGI_DATA) or
(reqType = FCGI_GET_VALUES) or
(reqType = FCGI_GET_VALUES_RESULT) or
(reqType = FCGI_UNKNOWN_TYPE);
end;
procedure TFcgiFrameParser.raiseExceptionIfInvalidRecordType(const reqType : byte);
begin
if (not isValidRecordType(reqType)) then
begin
raise EInvalidFcgiRecordType.createFmt('Invalid FCGI record type %d received', [reqType]);
end;
end;
(*!------------------------------------------------
* parse current buffer and create its corresponding
* FastCGI record instance
*-----------------------------------------------
* @param buffer, pointer to current buffer
* @param bufferSize, size of buffer
* @return IFcgiRecord instance
* @throws EInvalidFcgiHeaderLen exception when header size not valid
*-----------------------------------------------*)
function TFcgiFrameParser.parseFrame(const buffer : pointer; const bufferSize : ptrUint) : IFcgiRecord;
var header : PFCGI_Header;
factory : IFcgiRecordFactory;
begin
raiseExceptionIfInvalidBuffer(buffer, bufferSize);
header := buffer;
raiseExceptionIfInvalidRecordType(header^.reqtype);
factory := fRecordFactories[header^.reqtype];
factory.setDeallocator(self);
try
result := factory.setBuffer(buffer, bufferSize).build();
finally
//remove reference to ourself, so reference count properly incr/decr
//to avoid memory leak
factory.setDeallocator(nil);
factory := nil;
end;
end;
end.