forked from abhigna/fcgi-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fcgi_header.c
257 lines (221 loc) · 6.57 KB
/
fcgi_header.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
#include "fcgi_defs.h"
#include "fcgi_header.h"
void print_mem(void const *vp, size_t n)
{
unsigned char const *p = vp;
size_t i;
for (i=0; i<n; i++)
printf("%02X\n", p[i]);
putchar('\n');
};
void fcgi_header_set_request_id(fcgi_header *h, uint16_t request_id){
h->request_id_lo = BYTE_0(request_id);
h->request_id_hi = BYTE_1(request_id);
}
void fcgi_header_set_content_len(fcgi_header *h, uint16_t len){
h->content_len_lo = BYTE_0(len);
h->content_len_hi = BYTE_1(len);
}
uint32_t fcgi_header_get_content_len(fcgi_header *h){
return (h->content_len_hi << 8) + h->content_len_lo;
}
fcgi_header* create_header(uchar type, uint16_t request_id){
fcgi_header* tmp = (fcgi_header *) calloc(1,sizeof(fcgi_header));
tmp->version = FCGI_VERSION_1;
tmp->type = type;
fcgi_header_set_request_id(tmp, request_id);
return tmp;
}
fcgi_begin_request* create_begin_request(uint16_t request_id){
fcgi_begin_request *h = (fcgi_begin_request *) malloc(sizeof(fcgi_begin_request));
h->header = create_header(FCGI_BEGIN_REQUEST, request_id);
h->body = calloc(1, sizeof(fcgi_begin_request_body));
h->body->role_lo = FCGI_RESPONDER;
h->header->content_len_lo = sizeof(fcgi_begin_request_body);
return h;
}
fcgi_record* fcgi_record_create()
{
fcgi_record* tmp = (fcgi_record *) malloc(sizeof(fcgi_record));
tmp->header = (fcgi_header *) calloc(sizeof(fcgi_header), 1);
tmp->next = NULL;
tmp->state = 0;
return tmp;
}
int fcgi_process_header(uchar ch,
fcgi_record* rec){
fcgi_header *h;
fcgi_state *state = &(rec->state);
h = rec->header;
switch(*state){
case fcgi_state_version:
h->version = ch;
*state = fcgi_state_type;
break;
case fcgi_state_type:
h->type = ch;
*state = fcgi_state_request_id_hi;
break;
case fcgi_state_request_id_hi:
h->request_id_hi = ch;
*state = fcgi_state_request_id_lo;
break;
case fcgi_state_request_id_lo:
h->request_id_lo = ch;
*state = fcgi_state_content_len_hi;
break;
case fcgi_state_content_len_hi:
h->content_len_hi = ch;
*state = fcgi_state_content_len_lo;
break;
case fcgi_state_content_len_lo:
h->content_len_lo = ch;
*state = fcgi_state_padding_len;
break;
case fcgi_state_padding_len:
h->padding_len = ch;
*state = fcgi_state_reserved;
break;
case fcgi_state_reserved:
h->reserved = ch;
*state = fcgi_state_content_begin;
break;
case fcgi_state_content_begin:
case fcgi_state_content_proc:
case fcgi_state_padding:
case fcgi_state_done:
return FCGI_PROCESS_DONE;
}
return FCGI_PROCESS_AGAIN;
}
int fcgi_process_content(uchar **beg_buf, uchar *end_buf,
fcgi_record *rec){
size_t tot_len, con_len, cpy_len, offset, nb = end_buf - *beg_buf;
fcgi_state *state = &(rec->state);
fcgi_header *h = rec->header;
offset = rec->offset;
if ( *state == fcgi_state_padding ){
*state = fcgi_state_done;
/*printf("padding %d\n", (int)rec->length - (int)offset + (int)h->padding_len);*/
*beg_buf += (size_t) ((int)rec->length - (int)offset + (int)h->padding_len);
return FCGI_PROCESS_DONE;
}
con_len = rec->length - offset;
tot_len = con_len + h->padding_len;
if(con_len <= nb)
cpy_len = con_len;
else
cpy_len = nb;
memcpy(rec->content + offset, *beg_buf, cpy_len);
if(tot_len <= nb)
{
rec->offset += tot_len;
*state = fcgi_state_done;
*beg_buf += tot_len;
return FCGI_PROCESS_DONE;
}
else if( con_len <= nb )
{
/* Have to still skip all or some of padding */
*state = fcgi_state_padding;
rec->offset += nb;
*beg_buf += nb;
return FCGI_PROCESS_AGAIN;
}
else
{
rec->offset += nb;
*beg_buf += nb;
return FCGI_PROCESS_AGAIN;
}
return 0;
}
int fcgi_process_record(uchar **beg_buf, uchar *end_buf, fcgi_record *rec){
int rv;
while(rec->state < fcgi_state_content_begin)
{
if((rv = fcgi_process_header(**beg_buf, rec)) == FCGI_PROCESS_ERR)
return FCGI_PROCESS_ERR;
(*beg_buf)++;
if( *beg_buf == end_buf )
return FCGI_PROCESS_AGAIN;
}
if(rec->state == fcgi_state_content_begin)
{
rec->length = fcgi_header_get_content_len(rec->header);
rec->content = malloc(rec->length);
rec->state++;
}
return fcgi_process_content(beg_buf, end_buf, rec);
}
void fcgi_process_buffer(uchar *beg_buf, uchar *end_buf,
fcgi_record_list** head){
fcgi_record* tmp, *h;
size_t i;
if(*head == NULL)
*head = fcgi_record_create();
h = *head;
while(1)
{
if( h->state == fcgi_state_done )
{
tmp = h;
*head = fcgi_record_create();
h = *head;
h->next = tmp;
}
if( fcgi_process_record(&beg_buf, end_buf, h) == FCGI_PROCESS_DONE ){
/*fprintf(stderr, "TYPE%d:LEN:%d\n", h->header->type, h->length);*/
if(h->header->type == FCGI_STDOUT)
for(i=0;i < h->length; i++)
fprintf(stdout, "%c", ((uchar *)h->content)[i]);
}
if ( beg_buf == end_buf )
return;
}
}
void serialize(uchar* buffer, void *st, size_t size){
/*unsigned char const *p = st;*/
/*size_t i;*/
/*for(i=0; i < size; i++){*/
/**buffer++ = *p++;*/
/*[>printf("|%02X|", *(p-1));<]*/
/*}*/
memcpy(buffer, st, size);
}
uint32_t serialize_name_value(uchar* buffer, fcgi_name_value* nv){
uchar *p = buffer;
uint32_t nl, vl;
nl = strlen(nv->name);
vl = strlen(nv->value);
if( nl < 128 )
*p++ = BYTE_0(nl);
else
{
*p++ = BYTE_0(nl);
*p++ = BYTE_1(nl);
*p++ = BYTE_2(nl);
*p++ = BYTE_3(nl);
}
if ( vl < 128 )
*p++ = BYTE_0(vl);
else
{
*p++ = BYTE_0(vl);
*p++ = BYTE_1(vl);
*p++ = BYTE_2(vl);
*p++ = BYTE_3(vl);
}
memcpy(p, nv->name, nl);
p+=nl;
memcpy(p, nv->value, vl);
p+= vl;
return p - buffer;
}
void print_bytes(uchar *buf, int n){
int i;
printf("{");
for(i=0;i<n;i++)
printf("%02X.", buf[i]);
printf("}\n");
}