2 cgi.c - Some simple routines for CGI programming
3 Copyright (c) 1996-9,2007,8 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 Foundation
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
33 char *strndup(const char *s, size_t n);
36 char *cgiHeaderString = NULL;
39 extern s_cookie **cgiReadCookies();
41 int cgiSetHeader (const char *name, const char *value)
47 if (!name || !strlen (name) || !value || !strlen (value))
50 for (cp=name;*cp && *cp!=' ' && *cp!='\r' && *cp!='\n' && *cp!=':';cp++);
51 for (vp=value;*vp && *vp!='\r' && *vp!='\n';vp++);
53 if (cgiHeaderString) {
54 len = (strlen (cgiHeaderString) + cp-name + vp-value + 5) * sizeof (char);
55 if ((pivot = (char *)realloc (cgiHeaderString,len)) == NULL)
57 cgiHeaderString = pivot;
58 pivot += strlen (cgiHeaderString);
60 len = (cp-name + vp-value + 5) * sizeof (char);
61 if ((cgiHeaderString = (char *)malloc (len)) == NULL)
63 pivot = cgiHeaderString;
65 strncpy (pivot, name, cp-name);
66 strncat (pivot, ": ", 2);
67 strncat (pivot, value, vp-value);
68 strncat (pivot, "\r\n", 2);
73 int cgiSetType (const char *type)
78 if (!type || !strlen (type))
83 for (cp=type;*cp && *cp!='\r' && *cp!='\n';cp++);
85 len = (cp-type+1) * sizeof (char);
86 if ((cgiType = (char *)malloc (len+20)) == NULL)
88 memset (cgiType, 0, len);
89 strncpy (cgiType, type, cp-type);
97 printf ("Content-type: %s\r\n", cgiType);
99 printf ("Content-type: text/html\r\n");
101 printf ("%s", cgiHeaderString);
105 void cgiDebug (int level, int where)
108 cgiDebugLevel = level;
113 cgiDebugType = where;
115 openlog("cgilib", LOG_PID, LOG_USER);
121 char *cgiDecodeString (char *text)
125 for (cp=text,xp=text; *cp; cp++) {
127 if (strchr("0123456789ABCDEFabcdef", *(cp+1))
128 && strchr("0123456789ABCDEFabcdef", *(cp+2))) {
129 if (islower(*(cp+1)))
130 *(cp+1) = toupper(*(cp+1));
131 if (islower(*(cp+2)))
132 *(cp+2) = toupper(*(cp+2));
133 *(xp) = (*(cp+1) >= 'A' ? *(cp+1) - 'A' + 10 : *(cp+1) - '0' ) * 16
134 + (*(cp+2) >= 'A' ? *(cp+2) - 'A' + 10 : *(cp+2) - '0');
141 memset(xp, 0, cp-xp);
147 * Read and save a file fro a multipart request
150 char *cgiReadFile (FILE *stream, char *boundary)
152 char *crlfboundary, *buf;
157 char template[]= "/tmp/cgilibXXXXXX";
161 boundarylen = strlen(boundary)+3;
162 if ((crlfboundary = (char *)malloc (boundarylen)) == NULL)
164 sprintf (crlfboundary, "\r\n%s", boundary);
166 if ((buf = (char *)malloc (boundarylen)) == NULL) {
170 memset (buf, 0, boundarylen);
173 if ((fd = mkstemp (template)) == -1) {
179 if ((tmpfile = fdopen (fd, "w")) == NULL) {
186 while (!feof (stream)) {
191 for (cp=buf; *cp; cp++)
193 memset (buf, 0, boundarylen);
201 if (crlfboundary[pivot+1] == c) {
204 if (strlen (buf) == strlen (crlfboundary))
209 for (cp=buf; *cp; cp++)
211 memset (buf, 0, boundarylen);
216 if (crlfboundary[0] == c) {
224 fgets (buf, boundarylen, stream);
231 return strdup (template);
234 /* cgiReadMultipart()
236 * Decode multipart/form-data
238 #define MULTIPART_DELTA 5
239 s_cgi *cgiReadMultipart (char *boundary)
243 char *name = NULL, *type = NULL;
247 s_var **result = NULL;
249 int numresults = 0, current = 0;
250 s_file **files = NULL;
257 while ((line = cgiGetLine (stdin)) != NULL) {
259 if (!strncmp (line, boundary, strlen(boundary))) {
261 } else if (!strncasecmp (line, "Content-Disposition: form-data; ", 32)) {
263 if ((cp = strstr (line, "name=\"")) == NULL)
266 if ((xp = strchr (cp, '\"')) == NULL)
268 name = strndup (cp, xp-cp);
269 cgiDecodeString (name);
270 cgiDebugOutput (2, "Found field name %s", name);
272 if ((cp = strstr (line, "filename=\"")) == NULL)
275 if ((xp = strchr (cp, '\"')) == NULL)
277 fname = strndup (cp, xp-cp);
278 cgiDecodeString (fname);
279 cgiDebugOutput (2, "Found filename %s", fname);
281 } else if (!strncasecmp (line, "Content-Type: ", 14)) {
285 cgiDebugOutput (2, "Found mime type %s", type);
293 tmpfile = cgiReadFile (stdin, boundary);
300 name = fname = type = NULL;
303 cgiDebugOutput (2, "Wrote %s (%s) to file: %s", name, fname, tmpfile);
305 if (!strlen (fname)) {
306 cgiDebugOutput (3, "Found empty filename, removing");
313 name = fname = type = NULL;
315 if ((file = (s_file *)malloc (sizeof (s_file))) == NULL) {
316 cgiDebugOutput (3, "malloc failed, ignoring %s=%s", name, fname);
323 name = fname = type = NULL;
329 file->tmpfile = tmpfile;
330 if ((cp = rindex (fname, '/')) == NULL)
331 file->filename = fname;
333 file->filename = strdup (++cp);
336 name = type = fname = NULL;
339 if ((files = (s_file **)malloc(2*sizeof (s_file *))) == NULL) {
340 cgiDebugOutput (3, "malloc failed, ignoring %s=%s", name, fname);
349 free (file->filename);
353 memset (files, 0, 2*sizeof (s_file *));
356 for (index=0; files[index]; index++);
357 if ((tmpf = (s_file **)realloc(files, (index+2)*sizeof (s_file *))) == NULL) {
358 cgiDebugOutput (3, "realloc failed, ignoring %s=%s", name, fname);
364 free (file->filename);
366 name = type = fname = NULL;
370 memset (files + index, 0, 2*sizeof (s_file *));
379 /* try to find out if there's already such a variable */
380 for (index=0; index<current && strcmp (result[index]->name,name); index++);
382 if (index == current) {
384 len = MULTIPART_DELTA * sizeof (s_var *);
385 if ((result = (s_var **)malloc (len)) == NULL) {
391 numresults = MULTIPART_DELTA;
392 memset (result, 0, len);
395 if (current+2 > numresults) {
396 len = (numresults + MULTIPART_DELTA) * sizeof(s_var *);
397 if ((tmp = (s_var **)realloc (result, len)) == NULL) {
398 for (index=0; result[index]; index++)
399 free (result[index]);
407 memset (result + numresults, 0, len - numresults*sizeof(s_var *));
408 numresults += MULTIPART_DELTA;
411 if ((result[current] = (s_var *)malloc(sizeof(s_var))) == NULL) {
412 for (index=0; result[index]; index++)
413 free (result[index]);
421 cgiDebugOutput (3, "Set #%d to %s=%s", index, name, line);
422 result[index]->name = name; name = NULL;
423 result[index]->value = strdup (line);
424 cgiDecodeString (result[index]->value);
430 cgiDebugOutput (3, "Set #%d to %s=%s", index, name, line);
433 if ((cp = (char *)realloc (result[index]->value, strlen(result[index]->value)+strlen(line)+2)) != NULL) {
436 result[index]->value = cp;
446 cgiDecodeString (xp);
448 if ((name = (char *)malloc (strlen(result[index]->value)+strlen(xp)+3)) == NULL) {
449 for (index=0; result[index]; index++)
450 free (result[index]);
455 sprintf (name, "%s\r\n%s", result[index]->value, xp);
456 free (result[index]->value);
457 result[index]->value = name;
465 if ((res = (s_cgi *)malloc (sizeof (s_cgi))) == NULL)
475 /* cgiReadVariables()
477 * Read from stdin if no string is provided via CGI. Variables that
478 * doesn't have a value associated with it doesn't get stored.
480 s_cgi *cgiReadVariables ()
485 char *cp, *ip, *esp, *sptr;
491 cp = getenv("CONTENT_TYPE");
493 cgiDebugOutput (2, "Content-Type: %s", cp);
494 if (cp && strstr(cp, "multipart/form-data") && strstr(cp, "boundary=")) {
495 cp = strstr(cp, "boundary=") + strlen ("boundary=") - 2;
497 return cgiReadMultipart (cp);
500 if ((res = (s_cgi *)malloc (sizeof (s_cgi))) == NULL)
503 cp = getenv("REQUEST_METHOD");
505 cgiDebugOutput (2, "REQUEST_METHOD: %s", cp);
506 ip = getenv("CONTENT_LENGTH");
508 cgiDebugOutput (2, "CONTENT_LENGTH: %s", ip);
510 if (cp && !strcmp(cp, "POST")) {
515 if ((line = (char *)malloc (length+2)) == NULL)
517 fgets(line, length+1, stdin);
520 } else if (cp && !strcmp(cp, "GET")) {
521 esp = getenv("QUERY_STRING");
522 if (esp && strlen(esp)) {
523 if ((line = (char *)malloc (strlen(esp)+2)) == NULL)
525 sprintf (line, "%s", esp);
530 printf ("(offline mode: enter name=value pairs on standard input)\n");
531 memset (tmp, 0, sizeof(tmp));
532 while((cp = fgets (tmp, 100, stdin)) != NULL) {
534 if (tmp[strlen(tmp)-1] == '\n')
535 tmp[strlen(tmp)-1] = '&';
537 length += strlen(tmp);
538 len = (length+1) * sizeof(char);
539 if ((line = (char *)realloc (line, len)) == NULL)
543 length = strlen(tmp);
544 len = (length+1) * sizeof(char);
545 if ((line = (char *)malloc (len)) == NULL)
547 memset (line, 0, len);
551 memset (tmp, 0, sizeof(tmp));
555 if (line[strlen(line)-1] == '&')
556 line[strlen(line)-1] = '\0';
560 * From now on all cgi variables are stored in the variable line
561 * and look like foo=bar&foobar=barfoo&foofoo=
564 cgiDebugOutput (1, "Received CGI input: %s", line);
566 for (cp=line; *cp; cp++)
571 for (numargs=1,cp=line; *cp; cp++)
572 if (*cp == '&' || *cp == ';' ) numargs++;
575 cgiDebugOutput (1, "%d cgi variables found.", numargs);
577 len = (numargs+1) * sizeof(s_var *);
578 if ((result = (s_var **)malloc (len)) == NULL)
580 memset (result, 0, len);
585 if ((ip = (char *)strchr(cp, '&')) != NULL) {
587 } else if ((ip = (char *)strchr(cp, ';')) != NULL) {
590 ip = cp + strlen(cp);
592 if ((esp=(char *)strchr(cp, '=')) == NULL) {
606 if ((name = (char *)malloc((esp-cp+1) * sizeof (char))) == NULL)
608 strncpy(name, cp, esp-cp);
610 cgiDecodeString (name);
614 if ((value = (char *)malloc((ip-esp+1) * sizeof (char))) == NULL) {
618 strncpy(value, cp, ip-esp);
619 value[ip-esp] = '\0';
620 cgiDecodeString (value);
622 /* try to find out if there's already such a variable */
623 for (k=0; k<i && strcmp (result[k]->name, name); k++);
625 if (k == i) { /* No such variable yet */
626 if ((result[i] = (s_var *)malloc(sizeof(s_var))) == NULL)
628 result[i]->name = name;
629 result[i]->value = value;
630 cgiDebugOutput (1, "%s: %s", result[i]->name, result[i]->value);
632 } else { /* There is already such a name, suppose a mutiple field */
634 len = (strlen(result[k]->value)+strlen(value)+2) * sizeof (char);
635 if ((sptr = (char *)malloc(len)) == NULL) {
639 memset (sptr, 0, len);
640 sprintf (sptr, "%s\n%s", result[k]->value, value);
641 free (result[k]->value);
643 result[k]->value = sptr;
644 cgiDebugOutput (1, "%s: %s", result[i]->name, result[i]->value);
659 * Read from stdin if no string is provided via CGI. Variables that
660 * doesn't have a value associated with it doesn't get stored.
666 res = cgiReadVariables ();
668 res->cookies = cgiReadCookies ();
671 /* In some cases, we might have no other CGI results.
672 But we still have cookies! */
674 cookies = cgiReadCookies();
676 /* We need to create a s_cgi structure. */
677 if ((res = (s_cgi *)malloc (sizeof (s_cgi))) == NULL)
680 res->cookies = cookies;
688 if (!res->vars && !res->cookies && !res->files) {
696 char *cgiGetValue (s_cgi *parms, const char *name)
700 if (!parms || !parms->vars)
702 for (i=0;parms->vars[i]; i++)
703 if (!strcmp(name,parms->vars[i]->name)) {
704 cgiDebugOutput (1, "%s found as %s", name, parms->vars[i]->value);
705 if (strlen(parms->vars[i]->value) > 0)
706 return parms->vars[i]->value;
710 cgiDebugOutput (1, "%s not found", name);
714 char **cgiGetVariables (s_cgi *parms)
720 if (!parms || !parms->vars)
722 for (i=0;parms->vars[i]; i++);
723 len = sizeof (char *) * ++i;
724 if ((res = (char **)malloc (len)) == NULL)
726 memset (res, 0, len);
727 for (i=0;parms->vars[i]; i++) {
728 len = strlen (parms->vars[i]->name) +1;
729 if ((res[i] = (char *)malloc (len)) == NULL)
731 memset (res[i], 0, len);
732 strcpy (res[i], parms->vars[i]->name);
739 * Returns a list of names of all files.
741 char **cgiGetFiles (s_cgi *parms)
747 if (!parms || !parms->files)
750 for (i=0;parms->files[i]; i++);
751 len = sizeof (char *) * ++i;
752 if ((res = (char **)malloc (len)) == NULL)
754 memset (res, 0, len);
755 for (i=0;parms->files[i]; i++) {
756 len = strlen (parms->files[i]->name) +1;
757 if ((res[i] = (char *)malloc (len)) == NULL)
759 memset (res[i], 0, len);
760 strcpy (res[i], parms->files[i]->name);
767 * Return data structure for CGI file variable
769 s_file *cgiGetFile (s_cgi *parms, const char *name)
773 if (!parms || !parms->files)
776 for (i=0;parms->files[i]; i++)
777 if (!strcmp(name,parms->files[i]->name)) {
778 cgiDebugOutput (1, "%s found as %s", name, parms->files[i]->filename);
779 return parms->files[i];
781 cgiDebugOutput (1, "%s not found", name);
785 void cgiRedirect (const char *url)
787 if (url && strlen(url)) {
788 printf ("Content-type: text/html\r\nContent-length: %d\r\n", 77+(strlen(url)*2));
789 printf ("Status: 302 Temporal Relocation\r\n");
790 printf ("Location: %s\r\n\r\n", url);
791 printf ("<html>\n<body>\nThe page has been moved to <a href=\"%s\">%s</a>\n</body>\n</html>\n", url, url);
795 void cgiFreeList (char **list)
799 for (i=0; list[i] != NULL; i++)
804 void cgiFree (s_cgi *parms)
811 for (i=0;parms->vars[i]; i++) {
812 if (parms->vars[i]->name)
813 free (parms->vars[i]->name);
814 if (parms->vars[i]->value)
815 free (parms->vars[i]->value);
816 free (parms->vars[i]);
820 if (parms->cookies) {
821 if (parms->cookies[0]->version)
822 free (parms->cookies[0]->version);
823 for (i=0;parms->cookies[i]; i++) {
824 if (parms->cookies[i]->name)
825 free (parms->cookies[i]->name);
826 if (parms->cookies[i]->value)
827 free (parms->cookies[i]->value);
828 if (parms->cookies[i]->path)
829 free (parms->cookies[i]->path);
830 if (parms->cookies[i]->domain)
831 free (parms->cookies[i]->domain);
832 free (parms->cookies[i]);
834 free (parms->cookies);
837 for (i=0;parms->files[i]; i++) {
838 if (parms->files[i]->name)
839 free (parms->files[i]->name);
840 if (parms->files[i]->type)
841 free (parms->files[i]->type);
842 if (parms->files[i]->filename)
843 free (parms->files[i]->filename);
844 if (parms->files[i]->tmpfile) {
845 unlink (parms->files[i]->tmpfile);
846 free (parms->files[i]->tmpfile);
848 free (parms->files[i]);
854 if (cgiHeaderString) {
855 free (cgiHeaderString);
856 cgiHeaderString = NULL;
865 char *strndup(const char *s, size_t n) {
868 if ((fnval = (char *)malloc (n + 1)) == NULL)
870 strncpy(fnval, s, n);