-
Notifications
You must be signed in to change notification settings - Fork 5
/
tcgi.c
427 lines (378 loc) · 10.4 KB
/
tcgi.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
/*-------------------------------------------------------------------------*/
/**
@file tcgi.c
@author N. Devillard
@date Apr 2011
@brief CGI engine
This single source file implements a parser for CGI requests coming
from a web server. It supports GET and POST and will correctly parse
POST form data, both URL-encoded and multipart.
*/
/*--------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------
Includes
---------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <regex.h>
#include "dict.h"
/*---------------------------------------------------------------------------
Defines
---------------------------------------------------------------------------*/
/** Max accepted size for an URL */
#define URL_MAX_SZ 1024
/** Empty value in a dictionary */
#define EMPTY_VALUE "empty"
/** Boundary signal in form-encoded data */
#define ENCODED_BOUNDARY "boundary="
/** Regexp for Content-Disposition */
#define C_DISP "[Cc]ontent-[Dd]isposition: form-data;[^\n]*name=\"([^\"]*)\""
/** Regexp for Content-Type */
#define C_TYPE "[Cc]ontent-[Tt]ype: ([^\n]+)"
/** Regexp for Content-Transfer-Encoding */
#define C_CODE "[Cc]ontent-[Tt]ransfer-[Ee]ncoding: ([^\n]+)"
/** Regexp for Content */
#define C_CONT "\n\n(.*)\n$"
/*---------------------------------------------------------------------------
Macros
---------------------------------------------------------------------------*/
/** Debug mode only */
#define DEBUG_MODE 0
#if DEBUG_MODE
#define debug_code(x) x
#else
#define debug_code(x)
#endif
/*---------------------------------------------------------------------------
Function codes
---------------------------------------------------------------------------*/
/* stolen from NCSA code */
static char x2c(char *what)
{
char digit;
digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
digit *= 16;
digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
return digit ;
}
/* stolen from NCSA code */
static void unescape_url(char *url)
{
int x,y;
for (x=0, y=0 ; url[y] ; ++x, ++y) {
if((url[x] = url[y]) == '%') {
url[x] = x2c(&url[y+1]);
y+=2;
}
}
url[x] = '\0';
}
/* Replace a char by another in a writable string */
static void replace(char * s, char prev, char next)
{
int i ;
if (!s)
return ;
for (i=0 ; s[i] ; i++) {
if (s[i]==prev) {
s[i]=next ;
}
}
return ;
}
/* Parse a URL and put detected variables into dictionay */
static int tcgi_parse_url(dict * cgid, char * url)
{
char * url_cp ;
int sz ;
char * decl ;
char * ctx=0 ;
char key[URL_MAX_SZ+1] ;
char val[URL_MAX_SZ+1] ;
if (cgid==NULL || url==NULL) {
return -1 ;
}
sz = (int)strlen(url);
if (sz<1 || sz>=URL_MAX_SZ) {
return -1 ;
}
url_cp = calloc(sz+1, 1);
if (url_cp==NULL) {
return -1 ;
}
memcpy(url_cp, url, sz);
debug_code(
printf(" url-buffer: [%s]\n", url_cp);
);
/* Cut input into tokens */
decl = strtok_r(url_cp, "&", &ctx);
if (decl) {
do {
memset(key, 0, URL_MAX_SZ+1);
memset(val, 0, URL_MAX_SZ+1);
sscanf(decl, "%[^=]=%s", key, val);
unescape_url(val);
replace(val, '+', ' ');
debug_code(
printf("url-decoded:\n\tkey: [%s]\n\tval: [%s]\n", key, val);
);
dict_add(cgid, key, val);
decl = strtok_r(NULL, "&", &ctx);
} while (decl);
}
free(url_cp);
return 0 ;
}
/* Parse a POST block and put detected variables into dictionary */
static int tcgi_blockprocess(dict * cgid, char * block, int blocksz)
{
regex_t c_disp,
c_cont ;
regmatch_t pmatch[2] ;
int len ;
int err ;
char * key=0 ;
char * val=0 ;
if (!cgid || !block || blocksz<1) {
return -1 ;
}
regcomp(&c_disp, C_DISP, REG_EXTENDED);
regcomp(&c_cont, C_CONT, REG_EXTENDED);
err = regexec(&c_disp, block, 2, pmatch, 0);
if (!err) {
len=pmatch[1].rm_eo - pmatch[1].rm_so ;
if (len>0) {
key=calloc(len+1, 1);
if (key==NULL) {
regfree(&c_disp);
return -1 ;
}
strncpy(key, block+pmatch[1].rm_so, len);
}
}
err = regexec(&c_cont, block, 2, pmatch, 0);
if (!err) {
len=pmatch[1].rm_eo - pmatch[1].rm_so ;
if (len>0) {
val=calloc(len+1, 1);
if (val==NULL) {
free(key);
regfree(&c_disp);
regfree(&c_cont);
return -1 ;
}
strncpy(val, block+pmatch[1].rm_so, len);
}
}
if (key && val) {
dict_add(cgid, key, val);
debug_code(
printf("frm-decoded:\n\tkey: [%s]\n\tval: [%s]\n", key, val);
);
}
if (key) free(key) ;
if (val) free(val) ;
regfree(&c_disp);
regfree(&c_cont);
return 0 ;
}
/* Parse POST form data and put detected variables into dictionary */
static int tcgi_parse_formdata(dict * cgid, char * buffer, int sz)
{
char * val =0;
char * boundary ;
int boundlen ;
char * content_type =0;
char * beg =0;
char * end =0;
char * block =0;
int blocksz=0 ;
int i ;
/* Retrieve boundary into a string */
content_type = dict_get(cgid, "CONTENT_TYPE", EMPTY_VALUE);
if (!content_type || !strcmp(content_type, EMPTY_VALUE)) {
return -1 ;
}
val = strstr(content_type, ENCODED_BOUNDARY);
if (!val) {
return -1 ;
}
val+=strlen(ENCODED_BOUNDARY);
boundlen=strlen(val)+3 ; /* 3 chars: prefix '--' and trailing zero */
boundary=malloc(boundlen);
if (boundary==NULL) {
return -1 ;
}
boundary[0]='-';
boundary[1]='-';
strcpy(boundary+2, val);
/* Find first boundary-limited block */
beg = strstr(buffer, boundary);
if (!beg) {
free(boundary);
return 0;
}
/* Parse each block */
while (beg) {
beg+=boundlen ;
end = strstr(beg, boundary);
if (end) {
blocksz = end-beg ;
block=malloc(blocksz+1);
if (block==NULL) {
free(boundary);
return -1 ;
}
for (i=0; i<blocksz ; i++) {
block[i]=beg[i];
}
block[blocksz]=(char)0 ;
/* Process individual block */
tcgi_blockprocess(cgid, block, blocksz);
free(block);
}
beg=end ;
}
free(boundary);
/* Dictionary has been updated for each block */
return 0 ;
}
/* Parse a GET request into dictionary */
static int tcgi_parse_GET(dict * cgid)
{
char * query ;
if (cgid==NULL) {
return -1 ;
}
/* Get GET parameters */
query = dict_get(cgid, "QUERY_STRING", NULL);
if (!query || !strcmp(query, EMPTY_VALUE)) {
return 0 ;
}
return tcgi_parse_url(cgid, query) ;
}
/* Strip a writable character string of an unwanted character */
static void strip(char * buffer, char unwanted)
{
int i, j ;
int len ;
if (!buffer)
return ;
len = (int)strlen(buffer);
for (i=0, j=0 ; j<len ; j++) {
if (buffer[j]!=unwanted) {
buffer[i]=buffer[j] ;
i++ ;
}
}
buffer[i]=0 ;
}
/* Parse a POST request into a dictionary */
static int tcgi_parse_POST(dict * cgid)
{
int content_len ;
char * content_type ;
char * val ;
char * buffer ;
if (cgid==NULL) {
return -1 ;
}
content_type = dict_get(cgid, "CONTENT_TYPE", EMPTY_VALUE);
if (!content_type || !strcmp(content_type, EMPTY_VALUE)) {
return -1 ;
}
val = dict_get(cgid, "CONTENT_LENGTH", EMPTY_VALUE);
if (!val || !strcmp(val, EMPTY_VALUE)) {
return -1 ;
}
content_len = atoi(val);
buffer = calloc(content_len + 1, sizeof(char));
if (!buffer) {
return -1 ;
}
if (fread(buffer, sizeof(char), content_len, stdin) != content_len) {
free(buffer);
return -1 ;
}
strip(buffer, '\r');
/* Extract data from buffer */
if (!strncmp(content_type, "application/x-www-form-urlencoded", 33)) {
tcgi_parse_url(cgid, buffer);
} else if (!strncmp(content_type, "multipart/form-data", 19)) {
tcgi_parse_formdata(cgid, buffer, content_len);
} else {
dict_add(cgid, "content", buffer);
}
free(buffer);
return 0 ;
}
/*
* Determine if the current program is being called in a CGI context
*/
int tcgi_active(void)
{
int active=0 ;
if (getenv("NOCGI"))
return 0 ;
if (getenv("SERVER_SOFTWARE") &&
getenv("SERVER_NAME") &&
getenv("GATEWAY_INTERFACE"))
active=1 ;
return active ;
}
/* Parse a CGI request and place results into a dictionary */
dict * tcgi_parse(void)
{
dict * cgid ;
char * val ;
int i ;
char * env_vars[] = {
"SERVER_SOFTWARE",
"SERVER_NAME",
"GATEWAY_INTERFACE",
"SERVER_PROTOCOL",
"SERVER_PORT",
"REQUEST_METHOD",
"PATH_INFO",
"PATH_TRANSLATED",
"SCRIPT_NAME",
"QUERY_STRING",
"REMOTE_HOST",
"REMOTE_ADDR",
"AUTH_TYPE",
"REMOTE_USER",
"REMOTE_IDENT",
"CONTENT_TYPE",
"CONTENT_LENGTH",
"HTTP_ACCEPT",
"HTTP_USER_AGENT",
"HTTP_COOKIES",
NULL
};
cgid = dict_new();
/* Read in all environment */
for (i=0 ; env_vars[i]!=NULL ; i++) {
val = getenv(env_vars[i]);
dict_add(cgid, env_vars[i], val ? val : EMPTY_VALUE);
}
/* Parse GET parameters, i.e. the URL found in QUERY_STRING */
tcgi_parse_GET(cgid);
/* Parse POST parameters: stuff coming from stdin */
tcgi_parse_POST(cgid);
return cgid ;
}
#ifdef TCGI_MAIN
int main(int argc, char * argv[])
{
dict * cgid;
printf("Content-type: text/plain\r\n\r\n");
cgid = tcgi_parse();
dict_dump(cgid, stdout);
dict_free(cgid);
return 0 ;
}
#endif
/* vim: set ts=4 et sw=4 tw=75 */