forked from zamronypj/fano
/
FcgiRecord.pas
253 lines (218 loc) · 8.26 KB
/
FcgiRecord.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
{*!
* 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 FcgiRecord;
interface
{$MODE OBJFPC}
{$H+}
uses
fastcgi,
StreamAdapterIntf,
FcgiRecordIntf;
type
(*!-----------------------------------------------
* Base FastCGI record
*
* @author Zamrony P. Juhara <zamronypj@yahoo.com>
*-----------------------------------------------*)
TFcgiRecord = class(TInterfacedObject, IFcgiRecord)
protected
fHeader : FCGI_Header;
fRequestId : word;
fContentData : IStreamAdapter;
(*!------------------------------------------------
* calculate number of bytes of padding to write per record
*-----------------------------------------------
* @param len, current data length
* @return number of bytes of padding required
*-----------------------------------------------*)
function getPaddingLength(const len: word) : byte;
public
constructor create(
const aVersion : byte;
const aType : byte;
const aRequestId : word;
const dataStream : IStreamAdapter
);
constructor createFromStream(
const srcStream : IStreamAdapter;
const dataStream : IStreamAdapter
);
destructor destroy(); override;
(*!------------------------------------------------
* get current record type
*-----------------------------------------------
* @return type of record
*-----------------------------------------------*)
function getType() : byte;
(*!------------------------------------------------
* get request id
*-----------------------------------------------
* @return request id
*-----------------------------------------------*)
function getRequestId() : word;
(*!------------------------------------------------
* get content length
*-----------------------------------------------
* @return content length
*-----------------------------------------------*)
function getContentLength() : word;
(*!------------------------------------------------
* get content of record data
*-----------------------------------------------
* @return data as stream
*-----------------------------------------------*)
function data() : IStreamAdapter;
(*!------------------------------------------------
* calculate total record data size
*-----------------------------------------------
* @return number of bytes of current record
*-----------------------------------------------*)
function getRecordSize() : integer;
(*!------------------------------------------------
* write record data to destination stream
*-----------------------------------------------
* @param stream, stream instance where to write
* @return number of bytes actually written
*-----------------------------------------------*)
function write(const dstStream : IStreamAdapter) : integer; virtual;
end;
implementation
constructor TFcgiRecord.create(
const aVersion : byte;
const aType : byte;
const aRequestId : word;
const dataStream : IStreamAdapter
);
begin
inherited create();
//fillDword(fHeader, sizeOf(FCGI_Header), 0);
fHeader.version := aVersion;
fHeader.reqtype := aType;
//FastCGI specification required Big-Endian
fHeader.requestID := NToBE(aRequestId);
fHeader.contentLength := 0;
fHeader.paddingLength := 0;
fHeader.reserved :=0;
fRequestId := aRequestId;
fContentData := dataStream;
end;
constructor TFcgiRecord.createFromStream(
const srcStream : IStreamAdapter;
const dataStream : IStreamAdapter
);
var contentLength : word;
begin
fContentData := dataStream;
srcStream.readBuffer(fHeader, sizeof(FCGI_Header));
//FastCGI specification required requestID and contentLength
//in big-endian format, need to convert it to native endian
fRequestId := BEtoN(fHeader.requestID);
contentLength := BEtoN(fHeader.contentLength);
fContentData.resize(contentLength);
fContentData.seek(0);
srcStream.readStream(fContentData, contentLength);
srcStream.seek(fHeader.paddingLength, FROM_CURRENT);
fContentData.seek(0);
end;
destructor TFcgiRecord.destroy();
begin
inherited destroy();
fContentData := nil;
end;
(*!------------------------------------------------
* get current record type
*-----------------------------------------------
* @return type of record
*-----------------------------------------------*)
function TFcgiRecord.getType() : byte;
begin
result := fHeader.reqtype;
end;
(*!------------------------------------------------
* get request id
*-----------------------------------------------
* @return request id
*-----------------------------------------------*)
function TFcgiRecord.getRequestId() : word;
begin
result := fRequestId;
end;
(*!------------------------------------------------
* get content length
*-----------------------------------------------
* @return content length
*-----------------------------------------------*)
function TFcgiRecord.getContentLength() : word;
begin
result := word(fContentData.size());
end;
(*!------------------------------------------------
* get content record data
*-----------------------------------------------
* @return stream
*-----------------------------------------------*)
function TFcgiRecord.data() : IStreamAdapter;
begin
result := fContentData;
end;
(*!------------------------------------------------
* calculate total record data size
*-----------------------------------------------
* @return number of bytes of current record
*-----------------------------------------------*)
function TFcgiRecord.getRecordSize() : integer;
var contentLength : word;
paddingLength : byte;
begin
contentlength := getContentLength();
paddingLength := getPaddingLength(contentLength);
result := FCGI_HEADER_LEN + contentLength + paddingLength;
end;
(*!------------------------------------------------
* calculate number of bytes to write per record
*-----------------------------------------------
* @param len, current data length
* @param excess, current data length excess
* @return number of bytes actually written
*-----------------------------------------------*)
function TFcgiRecord.getPaddingLength(const len: word) : byte;
begin
if ((len mod FCGI_HEADER_LEN) = 0) then
begin
result := 0;
end else
begin
result := FCGI_HEADER_LEN - (len mod FCGI_HEADER_LEN);
end;
end;
(*!------------------------------------------------
* write record data to destination stream
*-----------------------------------------------
* @param dstStream, stream instance where to write
* @return number of bytes actually written
*-----------------------------------------------*)
function TFcgiRecord.write(const dstStream : IStreamAdapter) : integer;
var contentLength : word;
padding : byte;
begin
contentlength := getContentLength();
fHeader.contentLength := NtoBE(contentlength);
fHeader.paddingLength := getPaddingLength(contentLength);
//write header part
dstStream.writeBuffer(fHeader, sizeof(FCGI_Header));
//write data part
dstStream.writeStream(fContentData, fContentData.size());
//write padding part if any
if (fHeader.paddingLength > 0) then
begin
padding := 0;
dstStream.writeBuffer(padding, fHeader.paddingLength);
end;
result := getRecordSize();
end;
end.