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_cgi *cgiReadMultipart (char *boundary)
149 char *name = NULL, *type = NULL;
151 s_var **result = NULL;
153 int numresults = 0, current = 0;
158 while ((line = cgiGetLine (stdin)) != NULL) {
160 if (!strncmp (line, boundary, strlen(boundary))) {
162 } else if (!strncasecmp (line, "Content-Disposition: form-data; ", 32)) {
164 if ((cp = strstr (line, "name=\"")) == NULL)
167 if ((xp = strchr (cp, '\"')) == NULL)
169 name = strndup (cp, xp-cp);
170 cgiDebugOutput (2, "Found field name %s", name);
172 } else if (!strncasecmp (line, "Content-Type: ", 14)) {
176 cgiDebugOutput (2, "Found mime type %s", type);
182 cgiDebugOutput (2, "Found data: %s", line);
185 /* try to find out if there's already such a variable */
186 for (index=0; index<current && strcmp (result[index]->name,name); index++);
188 if (index == current) {
190 len = MULTIPART_DELTA * sizeof (s_var *);
191 if ((result = (s_var **)malloc (len)) == NULL)
193 numresults = MULTIPART_DELTA;
194 memset (result, 0, len);
197 if (current+2 > numresults) {
198 len = (numresults + MULTIPART_DELTA) * sizeof(s_var *);
199 if ((tmp = (s_var **)realloc (result, len)) == NULL) {
200 for (index=0; result[index]; index++)
201 free (result[index]);
207 memset (result + numresults, 0, len - numresults*sizeof(s_var *));
208 numresults += MULTIPART_DELTA;
211 if ((result[current] = (s_var *)malloc(sizeof(s_var))) == NULL) {
212 for (index=0; result[index]; index++)
213 free (result[index]);
219 cgiDebugOutput (3, "Set #%d to %s=%s", index, name, line);
220 result[index]->name = name; name = NULL;
221 result[index]->value = strdup (line);
223 cgiDebugOutput (3, "Set #%d to %s=%s", index, name, line);
225 if ((name = (char *)realloc (result[index]->value, strlen(result[index]->value)+strlen(line)+2)) != NULL) {
228 result[index]->value = name;
234 if ((name = (char *)malloc (strlen(result[index]->value)+strlen(line)+3)) == NULL) {
235 for (index=0; result[index]; index++)
236 free (result[index]);
240 sprintf (name, "%s\r\n%s", result[index]->value, line);
241 free (result[index]->value);
242 result[index]->value = name;
250 if ((res = (s_cgi *)malloc (sizeof (s_cgi))) == NULL)
260 /* cgiReadVariables()
262 * Read from stdin if no string is provided via CGI. Variables that
263 * doesn't have a value associated with it doesn't get stored.
265 s_cgi *cgiReadVariables ()
270 char *cp, *ip, *esp, *sptr;
276 cp = getenv("CONTENT_TYPE");
277 cgiDebugOutput (2, "Content-Type: %s", cp);
278 if (cp && strstr(cp, "multipart/form-data") && strstr(cp, "boundary=")) {
279 cp = strstr(cp, "boundary=") + strlen ("boundary=") - 2;
281 return cgiReadMultipart (cp);
284 cp = getenv("REQUEST_METHOD");
285 cgiDebugOutput (2, "REQUEST_METHOD: %s", cp);
286 ip = getenv("CONTENT_LENGTH");
288 if (cp && !strcmp(cp, "POST")) {
293 if ((line = (char *)malloc (length+2)) == NULL)
295 fgets(line, length+1, stdin);
298 } else if (cp && !strcmp(cp, "GET")) {
299 esp = getenv("QUERY_STRING");
300 if (esp && strlen(esp)) {
301 if ((line = (char *)malloc (strlen(esp)+2)) == NULL)
303 sprintf (line, "%s", esp);
308 printf ("(offline mode: enter name=value pairs on standard input)\n");
309 memset (tmp, 0, sizeof(tmp));
310 while((cp = fgets (tmp, 100, stdin)) != NULL) {
312 if (tmp[strlen(tmp)-1] == '\n')
313 tmp[strlen(tmp)-1] = '&';
315 length += strlen(tmp);
316 len = (length+1) * sizeof(char);
317 if ((line = (char *)realloc (line, len)) == NULL)
321 length = strlen(tmp);
322 len = (length+1) * sizeof(char);
323 if ((line = (char *)malloc (len)) == NULL)
325 memset (line, 0, len);
329 memset (tmp, 0, sizeof(tmp));
333 if (line[strlen(line)-1] == '&')
334 line[strlen(line)-1] = '\0';
338 * From now on all cgi variables are stored in the variable line
339 * and look like foo=bar&foobar=barfoo&foofoo=
342 cgiDebugOutput (1, "Received CGI input: %s", line);
344 for (cp=line; *cp; cp++)
349 for (numargs=1,cp=line; *cp; cp++)
350 if (*cp == '&' || *cp == ';' ) numargs++;
353 cgiDebugOutput (1, "%d cgi variables found.", numargs);
355 len = (numargs+1) * sizeof(s_var *);
356 if ((result = (s_var **)malloc (len)) == NULL)
358 memset (result, 0, len);
363 if ((ip = (char *)strchr(cp, '&')) != NULL) {
365 } else if ((ip = (char *)strchr(cp, ';')) != NULL) {
368 ip = cp + strlen(cp);
370 if ((esp=(char *)strchr(cp, '=')) == NULL) {
382 /* try to find out if there's already such a variable */
383 for (k=0; k<i && (strncmp (result[k]->name,cp, esp-cp) || !(strlen (result[k]->name) == esp-cp)); k++);
385 if (k == i) { /* No such variable yet */
386 if ((result[i] = (s_var *)malloc(sizeof(s_var))) == NULL)
388 if ((result[i]->name = (char *)malloc((esp-cp+1) * sizeof(char))) == NULL)
390 memset (result[i]->name, 0, esp-cp+1);
391 strncpy(result[i]->name, cp, esp-cp);
393 if ((result[i]->value = (char *)malloc((ip-esp+1) * sizeof(char))) == NULL)
395 memset (result[i]->value, 0, ip-esp+1);
396 strncpy(result[i]->value, cp, ip-esp);
397 result[i]->value = cgiDecodeString(result[i]->value);
398 cgiDebugOutput (1, "%s: %s", result[i]->name, result[i]->value);
400 } else { /* There is already such a name, suppose a mutiple field */
402 len = (strlen(result[k]->value)+(ip-esp)+2) * sizeof (char);
403 if ((sptr = (char *)malloc(len)) == NULL)
405 memset (sptr, 0, len);
406 sprintf (sptr, "%s\n", result[k]->value);
407 strncat(sptr, cp, ip-esp);
408 free(result[k]->value);
409 result[k]->value = cgiDecodeString (sptr);
415 if ((res = (s_cgi *)malloc (sizeof (s_cgi))) == NULL)
427 * Read from stdin if no string is provided via CGI. Variables that
428 * doesn't have a value associated with it doesn't get stored.
434 res = cgiReadVariables ();
435 res->cookies = cgiReadCookies ();
437 if (!res->vars && !res->cookies && !res->files) {
445 char *cgiGetValue (s_cgi *parms, const char *name)
449 if (!parms || !parms->vars)
451 for (i=0;parms->vars[i]; i++)
452 if (!strcmp(name,parms->vars[i]->name)) {
453 cgiDebugOutput (1, "%s found as %s", name, parms->vars[i]->value);
454 if (strlen(parms->vars[i]->value) > 0)
455 return parms->vars[i]->value;
459 cgiDebugOutput (1, "%s not found", name);
463 char **cgiGetVariables (s_cgi *parms)
469 if (!parms || !parms->vars)
471 for (i=0;parms->vars[i]; i++);
472 len = sizeof (char *) * ++i;
473 if ((res = (char **)malloc (len)) == NULL)
475 memset (res, 0, len);
476 for (i=0;parms->vars[i]; i++) {
477 len = strlen (parms->vars[i]->name) +1;
478 if ((res[i] = (char *)malloc (len)) == NULL)
480 memset (res[i], 0, len);
481 strcpy (res[i], parms->vars[i]->name);
486 void cgiRedirect (const char *url)
488 if (url && strlen(url)) {
489 printf ("Content-type: text/html\r\nContent-length: %d\r\n", 77+(strlen(url)*2));
490 printf ("Status: 302 Temporal Relocation\r\n");
491 printf ("Location: %s\r\n\r\n", url);
492 printf ("<html>\n<body>\nThe page has been moved to <a href=\"%s\">%s</a>\n</body>\n</html>\n", url, url);
496 void cgiFreeList (char **list)
500 for (i=0; list[i] != NULL; i++)
505 void cgiFree (s_cgi *parms)
512 for (i=0;parms->vars[i]; i++) {
513 if (parms->vars[i]->name)
514 free (parms->vars[i]->name);
515 if (parms->vars[i]->value)
516 free (parms->vars[i]->value);
517 free (parms->vars[i]);
521 if (parms->cookies) {
522 if (parms->cookies[0]->version)
523 free (parms->cookies[0]->version);
524 for (i=0;parms->cookies[i]; i++) {
525 if (parms->cookies[i]->name)
526 free (parms->cookies[i]->name);
527 if (parms->cookies[i]->value)
528 free (parms->cookies[i]->value);
529 if (parms->cookies[i]->path)
530 free (parms->cookies[i]->path);
531 if (parms->cookies[i]->domain)
532 free (parms->cookies[i]->domain);
533 free (parms->cookies[i]);
535 free (parms->cookies);
539 if (cgiHeaderString) {
540 free (cgiHeaderString);
541 cgiHeaderString = NULL;