2 cgi.c - Some simple routines for CGI programming
3 Copyright (c) 1996-9,2007 Martin Schulze <joey@infodrom.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
32 char *cgiHeaderString = NULL;
35 extern s_cookie **cgiReadCookies();
37 int cgiSetHeader (char *name, char *value)
39 char *cp, *vp, *pivot;
42 if (!name || !strlen (name) || !value || !strlen (value))
45 for (cp=name;*cp && *cp!=' ' && *cp!='\r' && *cp!='\n' && *cp!=':';cp++);
46 for (vp=value;*vp && *vp!='\r' && *vp!='\n';vp++);
48 if (cgiHeaderString) {
49 len = (strlen (cgiHeaderString) + cp-name + vp-value + 5) * sizeof (char);
50 if ((pivot = (char *)realloc (cgiHeaderString,len)) == NULL)
52 cgiHeaderString = pivot;
53 pivot += strlen (cgiHeaderString);
55 len = (cp-name + vp-value + 5) * sizeof (char);
56 if ((cgiHeaderString = (char *)malloc (len)) == NULL)
58 pivot = cgiHeaderString;
60 strncpy (pivot, name, cp-name);
61 strncat (pivot, ": ", 2);
62 strncat (pivot, value, vp-value);
63 strncat (pivot, "\r\n", 2);
68 int cgiSetType (char *type)
73 if (!type || !strlen (type))
78 for (cp=type;*cp && *cp!='\r' && *cp!='\n';cp++);
80 len = (cp-type+1) * sizeof (char);
81 if ((cgiType = (char *)malloc (len+20)) == NULL)
83 memset (cgiType, 0, len);
84 strncpy (cgiType, type, cp-type);
92 printf ("Content-type: %s\r\n", cgiType);
94 printf ("Content-type: text/html\r\n");
96 printf ("%s", cgiHeaderString);
100 void cgiDebug (int level, int where)
103 cgiDebugLevel = level;
108 cgiDebugType = where;
110 openlog("cgilib", LOG_PID, LOG_USER);
116 char *cgiDecodeString (char *text)
120 for (cp=text,xp=text; *cp; cp++) {
122 if (strchr("0123456789ABCDEFabcdef", *(cp+1))
123 && strchr("0123456789ABCDEFabcdef", *(cp+2))) {
124 if (islower(*(cp+1)))
125 *(cp+1) = toupper(*(cp+1));
126 if (islower(*(cp+2)))
127 *(cp+2) = toupper(*(cp+2));
128 *(xp) = (*(cp+1) >= 'A' ? *(cp+1) - 'A' + 10 : *(cp+1) - '0' ) * 16
129 + (*(cp+2) >= 'A' ? *(cp+2) - 'A' + 10 : *(cp+2) - '0');
136 memset(xp, 0, cp-xp);
140 /* cgiReadMultipart()
142 * Decode multipart/form-data
144 #define MULTIPART_DELTA 5
145 s_var **cgiReadMultipart (char *boundary)
149 char *name = NULL, *type = NULL;
151 s_var **result = NULL;
153 int numresults = 0, current = 0;
157 while ((line = cgiGetLine (stdin)) != NULL) {
159 if (!strncmp (line, boundary, strlen(boundary))) {
161 } else if (!strncasecmp (line, "Content-Disposition: form-data; ", 32)) {
163 if ((cp = strstr (line, "name=\"")) == NULL)
166 if ((xp = strchr (cp, '\"')) == NULL)
168 name = strndup (cp, xp-cp);
169 cgiDebugOutput (2, "Found field name %s", name);
171 } else if (!strncasecmp (line, "Content-Type: ", 14)) {
175 cgiDebugOutput (2, "Found mime type %s", type);
181 cgiDebugOutput (2, "Found data: %s", line);
184 /* try to find out if there's already such a variable */
185 for (index=0; index<current && strcmp (result[index]->name,name); index++);
187 if (index == current) {
189 len = MULTIPART_DELTA * sizeof (s_var *);
190 if ((result = (s_var **)malloc (len)) == NULL)
192 numresults = MULTIPART_DELTA;
193 memset (result, 0, len);
196 if (current+2 > numresults) {
197 len = (numresults + MULTIPART_DELTA) * sizeof(s_var *);
198 if ((tmp = (s_var **)realloc (result, len)) == NULL) {
199 for (index=0; result[index]; index++)
200 free (result[index]);
206 memset (result + numresults*sizeof(s_var *), 0, len - numresults*sizeof(s_var *));
207 numresults += MULTIPART_DELTA;
210 if ((result[current] = (s_var *)malloc(sizeof(s_var))) == NULL) {
211 for (index=0; result[index]; index++)
212 free (result[index]);
218 cgiDebugOutput (3, "Set #%d to %s=%s", index, name, line);
219 result[index]->name = name; name = NULL;
220 result[index]->value = strdup (line);
222 cgiDebugOutput (3, "Set #%d to %s=%s", index, name, line);
224 if ((name = (char *)realloc (result[index]->value, strlen(result[index]->value)+strlen(line)+2)) != NULL) {
227 result[index]->value = name;
233 if ((name = (char *)malloc (strlen(result[index]->value)+strlen(line)+3)) == NULL) {
234 for (index=0; result[index]; index++)
235 free (result[index]);
239 sprintf (name, "%s\r\n%s", result[index]->value, line);
240 free (result[index]->value);
241 result[index]->value = name;
252 /* cgiReadVariables()
254 * Read from stdin if no string is provided via CGI. Variables that
255 * doesn't have a value associated with it doesn't get stored.
257 s_var **cgiReadVariables ()
262 char *cp, *ip, *esp, *sptr;
267 cp = getenv("CONTENT_TYPE");
268 cgiDebugOutput (2, "Content-Type: %s", cp);
269 if (cp && strstr(cp, "multipart/form-data") && strstr(cp, "boundary=")) {
270 cp = strstr(cp, "boundary=") + strlen ("boundary=") - 2;
272 return cgiReadMultipart (cp);
275 cp = getenv("REQUEST_METHOD");
276 cgiDebugOutput (2, "REQUEST_METHOD: %s", cp);
277 ip = getenv("CONTENT_LENGTH");
279 if (cp && !strcmp(cp, "POST")) {
284 if ((line = (char *)malloc (length+2)) == NULL)
286 fgets(line, length+1, stdin);
289 } else if (cp && !strcmp(cp, "GET")) {
290 esp = getenv("QUERY_STRING");
291 if (esp && strlen(esp)) {
292 if ((line = (char *)malloc (strlen(esp)+2)) == NULL)
294 sprintf (line, "%s", esp);
299 printf ("(offline mode: enter name=value pairs on standard input)\n");
300 memset (tmp, 0, sizeof(tmp));
301 while((cp = fgets (tmp, 100, stdin)) != NULL) {
303 if (tmp[strlen(tmp)-1] == '\n')
304 tmp[strlen(tmp)-1] = '&';
306 length += strlen(tmp);
307 len = (length+1) * sizeof(char);
308 if ((line = (char *)realloc (line, len)) == NULL)
312 length = strlen(tmp);
313 len = (length+1) * sizeof(char);
314 if ((line = (char *)malloc (len)) == NULL)
316 memset (line, 0, len);
320 memset (tmp, 0, sizeof(tmp));
324 if (line[strlen(line)-1] == '&')
325 line[strlen(line)-1] = '\0';
329 * From now on all cgi variables are stored in the variable line
330 * and look like foo=bar&foobar=barfoo&foofoo=
333 cgiDebugOutput (1, "Received CGI input: %s", line);
335 for (cp=line; *cp; cp++)
340 for (numargs=1,cp=line; *cp; cp++)
341 if (*cp == '&' || *cp == ';' ) numargs++;
344 cgiDebugOutput (1, "%d cgi variables found.", numargs);
346 len = (numargs+1) * sizeof(s_var *);
347 if ((result = (s_var **)malloc (len)) == NULL)
349 memset (result, 0, len);
354 if ((ip = (char *)strchr(cp, '&')) != NULL) {
356 } else if ((ip = (char *)strchr(cp, ';')) != NULL) {
359 ip = cp + strlen(cp);
361 if ((esp=(char *)strchr(cp, '=')) == NULL) {
373 /* try to find out if there's already such a variable */
374 for (k=0; k<i && (strncmp (result[k]->name,cp, esp-cp) || !(strlen (result[k]->name) == esp-cp)); k++);
376 if (k == i) { /* No such variable yet */
377 if ((result[i] = (s_var *)malloc(sizeof(s_var))) == NULL)
379 if ((result[i]->name = (char *)malloc((esp-cp+1) * sizeof(char))) == NULL)
381 memset (result[i]->name, 0, esp-cp+1);
382 strncpy(result[i]->name, cp, esp-cp);
384 if ((result[i]->value = (char *)malloc((ip-esp+1) * sizeof(char))) == NULL)
386 memset (result[i]->value, 0, ip-esp+1);
387 strncpy(result[i]->value, cp, ip-esp);
388 result[i]->value = cgiDecodeString(result[i]->value);
389 cgiDebugOutput (1, "%s: %s", result[i]->name, result[i]->value);
391 } else { /* There is already such a name, suppose a mutiple field */
393 len = (strlen(result[k]->value)+(ip-esp)+2) * sizeof (char);
394 if ((sptr = (char *)malloc(len)) == NULL)
396 memset (sptr, 0, len);
397 sprintf (sptr, "%s\n", result[k]->value);
398 strncat(sptr, cp, ip-esp);
399 free(result[k]->value);
400 result[k]->value = cgiDecodeString (sptr);
410 * Read from stdin if no string is provided via CGI. Variables that
411 * doesn't have a value associated with it doesn't get stored.
419 vars = cgiReadVariables ();
420 cookies = cgiReadCookies ();
422 if (!vars && !cookies)
425 if ((res = (s_cgi *)malloc (sizeof (s_cgi))) == NULL)
428 res->cookies = cookies;
433 char *cgiGetValue (s_cgi *parms, const char *name)
437 if (!parms || !parms->vars)
439 for (i=0;parms->vars[i]; i++)
440 if (!strcmp(name,parms->vars[i]->name)) {
441 cgiDebugOutput (1, "%s found as %s", name, parms->vars[i]->value);
442 if (strlen(parms->vars[i]->value) > 0)
443 return parms->vars[i]->value;
447 cgiDebugOutput (1, "%s not found", name);
451 char **cgiGetVariables (s_cgi *parms)
457 if (!parms || !parms->vars)
459 for (i=0;parms->vars[i]; i++);
460 len = sizeof (char *) * ++i;
461 if ((res = (char **)malloc (len)) == NULL)
463 memset (res, 0, len);
464 for (i=0;parms->vars[i]; i++) {
465 len = strlen (parms->vars[i]->name) +1;
466 if ((res[i] = (char *)malloc (len)) == NULL)
468 memset (res[i], 0, len);
469 strcpy (res[i], parms->vars[i]->name);
474 void cgiRedirect (const char *url)
476 if (url && strlen(url)) {
477 printf ("Content-type: text/html\r\nContent-length: %d\r\n", 77+(strlen(url)*2));
478 printf ("Status: 302 Temporal Relocation\r\n");
479 printf ("Location: %s\r\n\r\n", url);
480 printf ("<html>\n<body>\nThe page has been moved to <a href=\"%s\">%s</a>\n</body>\n</html>\n", url, url);
484 void cgiFreeList (char **list)
488 for (i=0; list[i] != NULL; i++)
493 void cgiFree (s_cgi *parms)
500 for (i=0;parms->vars[i]; i++) {
501 if (parms->vars[i]->name)
502 free (parms->vars[i]->name);
503 if (parms->vars[i]->value)
504 free (parms->vars[i]->value);
505 free (parms->vars[i]);
509 if (parms->cookies) {
510 if (parms->cookies[0]->version)
511 free (parms->cookies[0]->version);
512 for (i=0;parms->cookies[i]; i++) {
513 if (parms->cookies[i]->name)
514 free (parms->cookies[i]->name);
515 if (parms->cookies[i]->value)
516 free (parms->cookies[i]->value);
517 if (parms->cookies[i]->path)
518 free (parms->cookies[i]->path);
519 if (parms->cookies[i]->domain)
520 free (parms->cookies[i]->domain);
521 free (parms->cookies[i]);
523 free (parms->cookies);
527 if (cgiHeaderString) {
528 free (cgiHeaderString);
529 cgiHeaderString = NULL;