-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuws_fastcgi.c
493 lines (427 loc) · 17.8 KB
/
uws_fastcgi.c
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/epoll.h>
#include "uws_memory.h"
#include "uws_fastcgi.h"
#include "uws_header.h"
#include "uws_config.h"
#include "uws_utils.h"
#include "uws_status.h"
#include "uws_router.h"
#include "uws_http.h"
#define PARAMS_BUFF_LEN 1024
#define MAX_BODY_LEN 2048
/**
* flag definition
* 0x00 not create socket
* 0x01 not connect to fcgi server
* 0x02 write header
* 0x03 write stdin
* 0x04 write header
* 0x04 read response
*/
/*{{{*/
static char *
header_to_fcgi(const char *str)
{
int prefix_len = strlen("HTTP_");
int len = strlen(str) + prefix_len + 2;
char *newstr = (char*) uws_malloc(len * sizeof(char));
int i = 0;
strcpy(newstr, "HTTP_");
while(str[i]) {
if(str[i] == '-') {
newstr[i + prefix_len] = '_';
} else {
newstr[i + prefix_len] = toupper(str[i]);
}
i++;
}
newstr[i+ prefix_len] = 0;
return newstr;
}
static FCGI_Header
make_header(int type, int request_id, int content_len, int padding_len)
{
FCGI_Header header;
header.version = FCGI_VERSION_1;
header.type = (unsigned char) type;
header.requestIdB1 = (unsigned char) ((request_id >> 8) & 0xff);
header.requestIdB0 = (unsigned char) (request_id & 0xff);
header.contentLengthB1 = (unsigned char) ((content_len >> 8) & 0xff);
header.contentLengthB0 = (unsigned char) (content_len & 0xff);
header.paddingLength = (unsigned char) padding_len;
header.reserved = 0;
return header;
}
static FCGI_BeginRequestBody
make_begin_request_body(int role, int keep_conn)
{
FCGI_BeginRequestBody body;
body.roleB1 = (unsigned char) ((role >> 8) & 0xff);
body.roleB0 = (unsigned char) (role & 0xff);
body.flags = (unsigned char) (keep_conn ? FCGI_KEEP_CONN : 0);
bzero(body.reserved, sizeof(body.reserved));
return body;
}
static void
build_name_value_body(char *name, int name_len, char *value, int value_len, unsigned char *body_buff, int *body_len)
{
unsigned char *start_body_buff = body_buff;
if( name_len < 0x80) {
*body_buff++ = (unsigned char) name_len;
} else {
*body_buff++ = (unsigned char) ((name_len >> 24) | 0x80);
*body_buff++ = (unsigned char) (name_len >> 16);
*body_buff++ = (unsigned char) (name_len >> 8);
*body_buff++ = (unsigned char) name_len;
}
if( value_len < 0x80) {
*body_buff++ = (unsigned char) value_len;
} else {
*body_buff++ = (unsigned char) ((value_len >> 24) | 0x80);
*body_buff++ = (unsigned char) (value_len >> 16);
*body_buff++ = (unsigned char) (value_len >> 8);
*body_buff++ = (unsigned char) value_len;
}
while(*name != '\0') *body_buff++ = *name++;
while(*value != '\0') *body_buff++ = *value++;
*body_len = body_buff - start_body_buff;
}
static void
add_fcgi_param(int request_id, char* name, char* value, memory_t *smem) {
int name_len, value_len, body_len;
unsigned char body_buff[PARAMS_BUFF_LEN];
bzero(body_buff, PARAMS_BUFF_LEN);
name_len = strlen(name);
value_len = strlen(value);
build_name_value_body(name, name_len, value, value_len, &body_buff[0], &body_len);
FCGI_Header name_value_header;
name_value_header = make_header(FCGI_PARAMS, request_id, body_len, 0);
append_mem_t(smem, &name_value_header, FCGI_HEADER_LEN);
append_mem_t(smem, body_buff, body_len);
}
static void
begin_build_request(int request_id, memory_t *smem) {
FCGI_BeginRequestRecord begin_record;
begin_record.header = make_header(FCGI_BEGIN_REQUEST, request_id, sizeof(begin_record.body), 0);
begin_record.body = make_begin_request_body(FCGI_RESPONDER, 0);
append_mem_t(smem, &begin_record, sizeof(begin_record));
}/*}}}*//*}}}*/
static void
send_request(int sockfd, memory_t *smem) {
writen(sockfd, smem->mem, smem->len);
}
static bool
read_response(int sockfd, memory_t *mem_file)
{
int count;
FCGI_Header response_header;
//We defined max 1024*1024*2 byte per response
int already_read = 1024 * 1024 * 2;
unsigned char* content;
int content_len;
char tmp[8];
bzero(mem_file, sizeof(mem_file));
while(read(sockfd, &response_header, FCGI_HEADER_LEN) > 0) {
if(mem_file->len >= already_read) return true;
if(response_header.type == FCGI_STDOUT) {
content_len = (response_header.contentLengthB1 << 8) + (response_header.contentLengthB0);
content = (unsigned char*) uws_malloc(sizeof(char) * content_len);
count = read(sockfd, content, content_len);
append_mem_t(mem_file, content, content_len);
uws_free(content);
if(response_header.paddingLength > 0) {
count = read(sockfd, tmp, response_header.paddingLength);
if(count != response_header.paddingLength) perror("read response error");
}
}
else if(response_header.type == FCGI_STDERR) {
content_len = (response_header.contentLengthB1 << 8) + (response_header.contentLengthB0);
content = (unsigned char*) uws_malloc(content_len * sizeof(char));
count = read(sockfd, content, count);
uws_free(content);
if(response_header.paddingLength > 0) {
count = read(sockfd, tmp, response_header.paddingLength);
if(count != response_header.paddingLength) perror("read");
}
}
else if(response_header.type == FCGI_END_REQUEST) {
FCGI_EndRequestBody end_request;
count = read(sockfd, &end_request, FCGI_HEADER_LEN);
/*
if(count != 8) perror("read");
fprintf(stdout,"\nend_request:appStatus:%d,protocolStatus:%d\n",(end_request.appStatusB3<<24)+(end_request.appStatusB2<<16) +(end_request.appStatusB1<<8)+(end_request.appStatusB0),end_request.protocolStatus);
*/
}
}
close(sockfd);
return false;
}
typedef struct {
memory_t *smem;
int request_id;
size_t mem_offset;
size_t read_offset;
size_t post; // to determin if is post
char fhost[20];
char fport[10];
} FCGI_LOCAL_DATA;
void
add_to_epoll(pConnInfo conn_info, uint32_t flag) {
struct epoll_event ev;
ev.events = flag | EPOLLET;
conn_info->status = CS_UPSTREAM_READ;
ev.data.ptr = conn_info;
if(epoll_ctl(conn_info->epollfd, EPOLL_CTL_ADD, conn_info->serverfd, &ev) == -1)
exit_err("epoll_ctl");
}
void
fastcgi_router(pConnInfo conn_info)
{
FCGI_LOCAL_DATA *fdata;
if(conn_info->flag == 0x00) {/*{{{*/
if(conn_info->ptr == NULL) {
conn_info->ptr = fdata = (FCGI_LOCAL_DATA*) uws_calloc(1, sizeof(FCGI_LOCAL_DATA));
char *content_len = get_header_param("Content-Length", conn_info->request_header);
if(strcmp(conn_info->request_header->method, "POST") == 0 && content_len != NULL) {
fdata->post = (size_t)atol(content_len);//this is post method
}
//init some data
fdata->smem = (memory_t*) uws_calloc(1, sizeof(memory_t));
char *port = itoa(conn_info->running_server->listen);
fdata->request_id = conn_info->clientfd;
sscanf(conn_info->running_server->fastcgi_pass, "%[^:]:%s", fdata->fhost, fdata->fport);
Param_Value pv[] = {/*{{{*/
{"QUERY_STRING",conn_info->request_header->request_params},
{"REQUEST_METHOD", conn_info->request_header->method},
{"CONTENT_TYPE", nullstring(get_header_param("Content-Type", conn_info->request_header))},
{"CONTENT_LENGTH", nullstring(get_header_param("Content-Length", conn_info->request_header))},
{"SCRIPT_FILENAME", conn_info->request_header->path},
{"SCRIPT_NAME", strrchr(conn_info->request_header->path, '/')},
{"REQUEST_URI", conn_info->request_header->url},
{"DOCUMENT_URI", conn_info->request_header->path + strlen(conn_info->running_server->root)},
{"DOCUMENT_ROOT", conn_info->running_server->root},
{"SERVER_PROTOCOL", conn_info->request_header->http_ver},
{"GATEWAY_INTERFACE", "CGI/1.1"},
{"SERVER_SOFTWARE", UWS_SERVER},
{"REMOTE_ADDR", get_header_param("Client-IP", conn_info->request_header)},
{"REMOTE_PORT", get_header_param("Client-Port", conn_info->request_header)},
{"SERVER_ADDR", conn_info->server_ip},
{"SERVER_PORT", port},
{"SERVER_NAME", conn_info->running_server->server_name},
{"HTTPS", ""},
{"REDIRECT_STATUS", "200"},
{NULL,NULL}
};/*}}}*/
//start build request
begin_build_request(fdata->request_id, fdata->smem);
Param_Value *tmp = pv;
while(tmp->name != NULL){
add_fcgi_param(fdata->request_id, tmp->name, tmp->value, fdata->smem);
tmp++;
}
Http_Param *params = conn_info->request_header->params;
int count = 0;
char *new_header;
for(count = 0; count < conn_info->request_header->used_len; ++count) {
new_header = header_to_fcgi(params->name);
add_fcgi_param(fdata->request_id, new_header, params->value, fdata->smem);
uws_free(new_header);
params++;
}
uws_free(port);
//add more http headers
//terminate params
FCGI_Header end_params;
end_params = make_header(FCGI_PARAMS, fdata->request_id, 0, 0);
append_mem_t(fdata->smem, &end_params, FCGI_HEADER_LEN);
}
struct sockaddr_in address;
conn_info->serverfd = socket(AF_INET, SOCK_STREAM, 0);
if(conn_info->serverfd < 0) {
exit_err("connect");
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr(fdata->fhost);
address.sin_port = htons(atoi(fdata->fport));
setnonblocking(conn_info->serverfd);
int result = connect(conn_info->serverfd, (struct sockaddr*)&address, sizeof(address));
conn_info->flag = 0x01; //now go to next stage
if(result < 0 ) {
if(errno != EINPROGRESS) {
conn_info->status_code = 502;
apply_next_router(conn_info);
return;
} else {
add_to_epoll(conn_info, EPOLLOUT);
longjmp(conn_info->jmp_buff, 1);
return;
}
}
}/*}}}*/
fdata = (FCGI_LOCAL_DATA*) conn_info->ptr;
if(conn_info->flag == 0x01) {/*{{{*/
for( ; ; ) {
ssize_t res= write(conn_info->serverfd, fdata->smem->mem + fdata->mem_offset, fdata->smem->len - fdata->mem_offset);
if(res == -1 ) {
if(errno == EAGAIN) {
add_to_epoll(conn_info, EPOLLOUT);
longjmp(conn_info->jmp_buff, 1);
return;
} else {
conn_info->status_code = 502;
apply_next_router(conn_info);
return;
}
}
fdata->mem_offset += res;
if(fdata->mem_offset == fdata->smem->len) break;
}
fdata->mem_offset = 0;
free_mem_t(fdata->smem);
conn_info->flag = 0x02;
}/*}}}*/
//
if(conn_info->flag == 0x02) {/*{{{*/
if(fdata->post){
char line[MAX_BODY_LEN];
FCGI_Header content_header;
for(; ;) {
if(fdata->smem->len == 0) {
if(fdata->read_offset == fdata->post) break;
bzero(line, MAX_BODY_LEN);
size_t read_num = fread(line, sizeof(char), MAX_BODY_LEN, conn_info->input_file);
fdata->read_offset += read_num;
if(read_num == 0) {
if(errno == EAGAIN) {
struct epoll_event ev;
ev.events = EPOLLIN | EPOLLET | EPOLLONESHOT;
conn_info->status = CS_UPSTREAM_READ;
ev.data.ptr = conn_info;
if(epoll_ctl(conn_info->epollfd, EPOLL_CTL_ADD, conn_info->clientfd, &ev) == -1)
exit_err("epoll_ctl");
longjmp(conn_info->jmp_buff, 1);
return;
}
}
content_header = make_header(FCGI_STDIN, fdata->request_id, read_num, 0);
append_mem_t(fdata->smem, &content_header, FCGI_HEADER_LEN);
append_mem_t(fdata->smem, line, read_num);
}
for( ; ; ) {
ssize_t res= write(conn_info->serverfd, fdata->smem->mem + fdata->mem_offset, fdata->smem->len - fdata->mem_offset);
if(res == -1 ) {
if(errno == EAGAIN) {
add_to_epoll(conn_info, EPOLLOUT);
longjmp(conn_info->jmp_buff, 1);
return;
} else {
conn_info->status_code = 502;
apply_next_router(conn_info);
return;
}
}
fdata->mem_offset += res;
if(fdata->mem_offset == fdata->smem->len) break;
}
fdata->mem_offset = 0;
free_mem_t(fdata->smem);
}
}
//send finish request symbol
FCGI_Header end_body;
end_body = make_header(FCGI_STDIN, fdata->request_id, 0, 0);
append_mem_t(fdata->smem, &end_body, FCGI_HEADER_LEN);
FCGI_Header end_header;
end_header = make_header(FCGI_PARAMS, fdata->request_id, 0, 0);
append_mem_t(fdata->smem, &end_header, FCGI_HEADER_LEN);
conn_info->flag = 0x03;
}/*}}}*/
if(conn_info->flag == 0x03) {
for( ; ; ) {
ssize_t res= write(conn_info->serverfd, fdata->smem->mem + fdata->mem_offset, fdata->smem->len - fdata->mem_offset);
if(res == -1 ) {
if(errno == EAGAIN) {
add_to_epoll(conn_info, EPOLLOUT);
longjmp(conn_info->jmp_buff, 1);
return;
} else {
conn_info->status_code = 502;
apply_next_router(conn_info);
return;
}
}
fdata->mem_offset += res;
if(fdata->mem_offset == fdata->smem->len) break;
}
fdata->mem_offset = 0;
free_mem_t(fdata->smem);
conn_info->flag = 0x04;
struct epoll_event ev;
ev.events = EPOLLIN | EPOLLET;
conn_info->status = CS_UPSTREAM_READ;
ev.data.ptr = conn_info;
if(epoll_ctl(conn_info->epollfd, EPOLL_CTL_MOD, conn_info->serverfd, &ev) == -1)
exit_err("epoll_ctl");
}
//all_data_send!
// if we have more content from fastcgi
if(conn_info->flag == 0x04) {
setblocking(conn_info->serverfd);
bool more_content = read_response(conn_info->serverfd, fdata->smem);
if(fdata->smem->len == 0) {
conn_info->status_code = 500;
apply_next_router(conn_info);
return;
}
char line[LINE_LEN] = {0};
unsigned char *oldpos = fdata->smem->mem;
unsigned char *pos;
struct http_header fcgi_response_header;
bzero(&fcgi_response_header, sizeof(fcgi_response_header));
char key[LINE_LEN];
char value[LINE_LEN];
char *time_string = get_time_string(NULL);
add_header_param("Server", UWS_SERVER, &fcgi_response_header);
add_header_param("Date", time_string, &fcgi_response_header);
uws_free(time_string);
while((pos = strstr(oldpos, "\r\n"))) {
if(oldpos == pos) break;
bzero(line, LINE_LEN);
strncpy(line, oldpos, pos - oldpos);
sscanf(line, "%[^:]: %s", key, value);
if(strcmp(key, "Status") == 0) {
fcgi_response_header.status_code = atoi(value);
} else {
push_header_param(key, value, &fcgi_response_header);
}
oldpos = pos + strlen("\r\n");
}
int content_len = fdata->smem->len - (pos - fdata->smem->mem) - strlen("\r\n");
char *str_len = itoa(content_len);
add_header_param("Content-Length", str_len, &fcgi_response_header);
uws_free(str_len);
fcgi_response_header.http_ver = "HTTP/1.1";
if(fcgi_response_header.status_code == 0) fcgi_response_header.status_code = 200;
fcgi_response_header.status = get_by_code(fcgi_response_header.status_code);
char *header_str = str_response_header(&fcgi_response_header);
writen(conn_info->clientfd, header_str, strlen(header_str));
uws_free(header_str);
writen(conn_info->clientfd, pos, content_len + strlen("\r\n"));
while(more_content) {
free_mem_t(fdata->smem);
more_content = read_response(conn_info->serverfd, fdata->smem);
writen(conn_info->clientfd, fdata->smem->mem, fdata->smem->len);
}
free_header_params(&fcgi_response_header);
free_mem_t(fdata->smem);
uws_free(fdata);
conn_info->ptr = NULL;
apply_next_router(conn_info);
}
}