forked from jaykizhou/php-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfastcgi.c
More file actions
executable file
·312 lines (272 loc) · 8.74 KB
/
fastcgi.c
File metadata and controls
executable file
·312 lines (272 loc) · 8.74 KB
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
#include "fastcgi.h"
/*
* 构造协议记录头部,返回FCGI_Header结构体
*/
FCGI_Header makeHeader(
int type,
int requestId,
int contentLength,
int paddingLength)
{
FCGI_Header header;
header.version = FCGI_VERSION_1;
header.type = (unsigned char) type;
header.requestIdB1 = (unsigned char) ((requestId >> 8) & 0xff);
header.requestIdB0 = (unsigned char) ((requestId ) & 0xff);
header.contentLengthB1 = (unsigned char) ((contentLength >> 8) & 0xff);
header.contentLengthB0 = (unsigned char) ((contentLength ) & 0xff);
header.paddingLength = (unsigned char) paddingLength;
header.reserved = 0;
return header;
}
/*
* 构造请求开始记录协议体,返回FCGI_BeginRequestBody结构体
*/
FCGI_BeginRequestBody makeBeginRequestBody(int role, int keepConn)
{
FCGI_BeginRequestBody body;
body.roleB1 = (unsigned char) ((role >> 8) & 0xff);
body.roleB0 = (unsigned char) (role & 0xff);
body.flags = (unsigned char) ((keepConn) ? 1 : 0); // 1为长连接,0为短连接
memset(body.reserved, 0, sizeof(body.reserved));
return body;
}
/*
* 发送开始请求记录
* 发送成功返回0
* 出错返回-1
*/
int sendBeginRequestRecord(write_record wr, int fd, int requestId)
{
int ret;
// 构造一个FCGI_BeginRequestRecord结构
FCGI_BeginRequestRecord beginRecord;
beginRecord.header =
makeHeader(FCGI_BEGIN_REQUEST, requestId, sizeof(beginRecord.body), 0);
beginRecord.body = makeBeginRequestBody(FCGI_RESPONDER, 0);
ret = wr(fd, &beginRecord, sizeof(beginRecord));
if (ret == sizeof(beginRecord)) {
return 0;
} else {
return -1;
}
}
/*
* 发送名值对参数
* 发送成功返回0
* 出错返回-1
*/
int sendParamsRecord(
write_record wr,
int fd,
int requestId,
char *name,
int nlen,
char *value,
int vlen)
{
unsigned char *buf, *old;
int ret, pl, cl = nlen + vlen;
cl = (nlen < 128) ? ++cl : cl + 4;
cl = (vlen < 128) ? ++cl : cl + 4;
// 计算填充数据长度
pl = (cl % 8) == 0 ? 0 : 8 - (cl % 8);
old = buf = (unsigned char *)malloc(FCGI_HEADER_LEN + cl + pl);
FCGI_Header nvHeader = makeHeader(FCGI_PARAMS, requestId, cl, pl);
memcpy(buf, (char *)&nvHeader, FCGI_HEADER_LEN);
buf = buf + FCGI_HEADER_LEN;
if (nlen < 128) { // name长度小于128字节,用一个字节保存长度
*buf++ = (unsigned char)nlen;
} else { // 大于等于128用4个字节保存长度
*buf++ = (unsigned char)((nlen >> 24) | 0x80);
*buf++ = (unsigned char)(nlen >> 16);
*buf++ = (unsigned char)(nlen >> 8);
*buf++ = (unsigned char)nlen;
}
if (vlen < 128) { // value长度小于128字节,用一个字节保存长度
*buf++ = (unsigned char)vlen;
} else { // 大于等于128用4个字节保存长度
*buf++ = (unsigned char)((vlen >> 24) | 0x80);
*buf++ = (unsigned char)(vlen >> 16);
*buf++ = (unsigned char)(vlen >> 8);
*buf++ = (unsigned char)vlen;
}
memcpy(buf, name, nlen);
buf = buf + nlen;
memcpy(buf, value, vlen);
ret = wr(fd, old, FCGI_HEADER_LEN + cl + pl);
free(old);
if (ret == (FCGI_HEADER_LEN + cl + pl)) {
return 0;
} else {
return -1;
}
}
/*
* 发送空的params记录
* 发送成功返回0
* 出错返回-1
*/
int sendEmptyParamsRecord(write_record wr, int fd, int requestId)
{
int ret;
FCGI_Header nvHeader = makeHeader(FCGI_PARAMS, requestId, 0, 0);
ret = wr(fd, (char *)&nvHeader, FCGI_HEADER_LEN);
if (ret == FCGI_HEADER_LEN) {
return 0;
} else {
return -1;
}
}
/*
* 发送FCGI_STDIN数据
* 发送成功返回0
* 出错返回-1
*/
int sendStdinRecord(
write_record wr,
int fd,
int requestId,
char *data,
int len)
{
int cl = len, pl, ret;
char buf[8] = {0};
while (len > 0) {
// 判断STDIN数据是否大于传输最大值FCGI_MAX_LENGTH
if (len > FCGI_MAX_LENGTH) {
cl = FCGI_MAX_LENGTH;
}
// 计算填充数据长度
pl = (cl % 8) == 0 ? 0 : 8 - (cl % 8);
FCGI_Header sinHeader = makeHeader(FCGI_STDIN, requestId, cl, pl);
ret = wr(fd, (char *)&sinHeader, FCGI_HEADER_LEN); // 发送协议头部
if (ret != FCGI_HEADER_LEN) {
return -1;
}
ret = wr(fd, data, cl); // 发送stdin数据
if (ret != cl) {
return -1;
}
if (pl > 0) {
ret = wr(fd, buf, pl); // 发送填充数据
if (ret != pl) {
return -1;
}
}
len -= cl;
data += cl;
}
return 0;
}
/*
* 发送空的FCGI_STDIN记录
* 发送成功返回0
* 出错返回-1
*/
int sendEmptyStdinRecord(write_record wr, int fd, int requestId)
{
int ret;
FCGI_Header sinHeader = makeHeader(FCGI_STDIN, requestId, 0, 0);
ret = wr(fd, (char *)&sinHeader, FCGI_HEADER_LEN);
if (ret == FCGI_HEADER_LEN) {
return 0;
} else {
return -1;
}
}
/*
* 读取php-fpm处理结果
* 读取成功返回0
* 出错返回-1
*/
int recvRecord(
read_record rr,
send_to_client stc,
int cfd,
int fd,
int requestId/*,
char **sout,
int *outlen,
char **serr,
int *errlen,
FCGI_EndRequestBody *endRequest*/)
{
FCGI_Header responHeader;
FCGI_EndRequestBody endr;
char *conBuf = NULL, *errBuf = NULL;
int buf[8], cl, ret;
int fcgi_rid; // 保存fpm发送过来的request id
int outlen = 0, errlen = 0;
//*outlen = 0;
//*errlen = 0;
// 读取协议记录头部
while (rr(fd, &responHeader, FCGI_HEADER_LEN) > 0) {
fcgi_rid = (int)(responHeader.requestIdB1 << 8) + (int)(responHeader.requestIdB0);
if (responHeader.type == FCGI_STDOUT && fcgi_rid == requestId) {
// 获取内容长度
cl = (int)(responHeader.contentLengthB1 << 8) + (int)(responHeader.contentLengthB0);
//*outlen += cl;
outlen += cl;
// 如果不是第一次读取FCGI_STDOUT记录
if (conBuf != NULL) {
// 扩展空间
//conBuf = realloc(*sout, *outlen);
conBuf = realloc(conBuf, outlen);
} else {
conBuf = (char *)malloc(cl);
//*sout = conBuf;
}
ret = rr(fd, conBuf, cl);
if (ret == -1 || ret != cl) {
printf("read fcgi_stdout record error\n");
return -1;
}
// 读取填充内容,忽略
if (responHeader.paddingLength > 0) {
ret = rr(fd, buf, responHeader.paddingLength);
if (ret == -1 || ret != responHeader.paddingLength) {
printf("read fcgi_stdout padding error %d\n", responHeader.paddingLength);
return -1;
}
}
} else if (responHeader.type == FCGI_STDERR && fcgi_rid == requestId) {
// 获取内容长度
cl = (int)(responHeader.contentLengthB1 << 8) + (int)(responHeader.contentLengthB0);
//*errlen += cl;
errlen += cl;
// 如果不是第一次读取FCGI_STDOUT记录
if (errBuf != NULL) {
// 扩展空间
errBuf = realloc(errBuf, errlen);
} else {
errBuf = (char *)malloc(cl);
//*serr = errBuf;
}
ret = rr(fd, errBuf, cl);
if (ret == -1 || ret != cl) {
return -1;
}
// 读取填充内容,忽略
if (responHeader.paddingLength > 0) {
ret = rr(fd, buf, responHeader.paddingLength);
if (ret == -1 || ret != responHeader.paddingLength) {
return -1;
}
}
} else if (responHeader.type == FCGI_END_REQUEST && fcgi_rid == requestId) {
// 读取结束请求协议体
ret = rr(fd, &endr, sizeof(FCGI_EndRequestBody));
if (ret == -1 || ret != sizeof(FCGI_EndRequestBody)) {
free(conBuf);
free(errBuf);
return -1;
}
stc(cfd, outlen, conBuf, errlen, errBuf, &endr);
free(conBuf);
free(errBuf);
return 0;
}
}
return 0;
}