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 (cp-name == 0 || vp-value == 0)
56 if (cgiHeaderString) {
57 len = (strlen (cgiHeaderString) + cp-name + vp-value + 5) * sizeof (char);
58 if ((pivot = (char *)realloc (cgiHeaderString,len)) == NULL)
60 cgiHeaderString = pivot;
61 pivot += strlen (cgiHeaderString);
63 len = (cp-name + vp-value + 5) * sizeof (char);
64 if ((cgiHeaderString = (char *)malloc (len)) == NULL)
66 pivot = cgiHeaderString;
69 memset(pivot, 0, (cp-name + vp-value + 5) * sizeof (char));
71 strncpy (pivot, name, cp-name);
72 strncat (pivot, ": ", 2);
73 strncat (pivot, value, vp-value);
74 strncat (pivot, "\r\n", 2);
79 int cgiSetType (const char *type)
84 if (!type || !strlen (type))
89 for (cp=type;*cp && *cp!='\r' && *cp!='\n';cp++);
91 len = (cp-type+1) * sizeof (char);
92 if ((cgiType = (char *)malloc (len+20)) == NULL)
94 memset (cgiType, 0, len);
95 strncpy (cgiType, type, cp-type);
103 printf ("Content-type: %s\r\n", cgiType);
105 printf ("Content-type: text/html\r\n");
107 printf ("%s", cgiHeaderString);
111 void cgiDebug (int level, int where)
114 cgiDebugLevel = level;
119 cgiDebugType = where;
121 openlog("cgilib", LOG_PID, LOG_USER);
127 char *cgiDecodeString (char *text)
131 for (cp=text,xp=text; *cp; cp++) {
133 if (strchr("0123456789ABCDEFabcdef", *(cp+1))
134 && strchr("0123456789ABCDEFabcdef", *(cp+2))) {
135 if (islower(*(cp+1)))
136 *(cp+1) = toupper(*(cp+1));
137 if (islower(*(cp+2)))
138 *(cp+2) = toupper(*(cp+2));
139 *(xp) = (*(cp+1) >= 'A' ? *(cp+1) - 'A' + 10 : *(cp+1) - '0' ) * 16
140 + (*(cp+2) >= 'A' ? *(cp+2) - 'A' + 10 : *(cp+2) - '0');
147 memset(xp, 0, cp-xp);
153 * Read and save a file fro a multipart request
156 char *cgiReadFile (FILE *stream, char *boundary)
158 char *crlfboundary, *buf;
163 char template[]= "/tmp/cgilibXXXXXX";
167 boundarylen = strlen(boundary)+3;
168 if ((crlfboundary = (char *)malloc (boundarylen)) == NULL)
170 sprintf (crlfboundary, "\r\n%s", boundary);
172 if ((buf = (char *)malloc (boundarylen)) == NULL) {
176 memset (buf, 0, boundarylen);
179 if ((fd = mkstemp (template)) == -1) {
185 if ((tmpfile = fdopen (fd, "w")) == NULL) {
192 while (!feof (stream)) {
197 for (cp=buf; *cp; cp++)
199 memset (buf, 0, boundarylen);
207 if (crlfboundary[pivot+1] == c) {
210 if (strlen (buf) == strlen (crlfboundary))
215 for (cp=buf; *cp; cp++)
217 memset (buf, 0, boundarylen);
222 if (crlfboundary[0] == c) {
230 fgets (buf, boundarylen, stream);
237 return strdup (template);
240 /* cgiReadMultipart()
242 * Decode multipart/form-data
244 #define MULTIPART_DELTA 5
245 s_cgi *cgiReadMultipart (char *boundary)
249 char *name = NULL, *type = NULL;
253 s_var **result = NULL;
255 int numresults = 0, current = 0;
256 s_file **files = NULL;
263 while ((line = cgiGetLine (stdin)) != NULL) {
265 if (!strncmp (line, boundary, strlen(boundary))) {
267 } else if (!strncasecmp (line, "Content-Disposition: form-data; ", 32)) {
269 if ((cp = strstr (line, "name=\"")) == NULL)
272 if ((xp = strchr (cp, '\"')) == NULL)
274 name = strndup (cp, xp-cp);
275 cgiDecodeString (name);
276 cgiDebugOutput (2, "Found field name %s", name);
278 if ((cp = strstr (line, "filename=\"")) == NULL)
281 if ((xp = strchr (cp, '\"')) == NULL)
283 fname = strndup (cp, xp-cp);
284 cgiDecodeString (fname);
285 cgiDebugOutput (2, "Found filename %s", fname);
287 } else if (!strncasecmp (line, "Content-Type: ", 14)) {
291 cgiDebugOutput (2, "Found mime type %s", type);
299 tmpfile = cgiReadFile (stdin, boundary);
306 name = fname = type = NULL;
309 cgiDebugOutput (2, "Wrote %s (%s) to file: %s", name, fname, tmpfile);
311 if (!strlen (fname)) {
312 cgiDebugOutput (3, "Found empty filename, removing");
319 name = fname = type = NULL;
321 if ((file = (s_file *)malloc (sizeof (s_file))) == NULL) {
322 cgiDebugOutput (3, "malloc failed, ignoring %s=%s", name, fname);
329 name = fname = type = NULL;
335 file->tmpfile = tmpfile;
336 if ((cp = rindex (fname, '/')) == NULL)
337 file->filename = fname;
339 file->filename = strdup (++cp);
342 name = type = fname = NULL;
345 if ((files = (s_file **)malloc(2*sizeof (s_file *))) == NULL) {
346 cgiDebugOutput (3, "malloc failed, ignoring %s=%s", name, fname);
355 free (file->filename);
359 memset (files, 0, 2*sizeof (s_file *));
362 for (index=0; files[index]; index++);
363 if ((tmpf = (s_file **)realloc(files, (index+2)*sizeof (s_file *))) == NULL) {
364 cgiDebugOutput (3, "realloc failed, ignoring %s=%s", name, fname);
370 free (file->filename);
372 name = type = fname = NULL;
376 memset (files + index, 0, 2*sizeof (s_file *));
385 /* try to find out if there's already such a variable */
386 for (index=0; index<current && strcmp (result[index]->name,name); index++);
388 if (index == current) {
390 len = MULTIPART_DELTA * sizeof (s_var *);
391 if ((result = (s_var **)malloc (len)) == NULL) {
397 numresults = MULTIPART_DELTA;
398 memset (result, 0, len);
401 if (current+2 > numresults) {
402 len = (numresults + MULTIPART_DELTA) * sizeof(s_var *);
403 if ((tmp = (s_var **)realloc (result, len)) == NULL) {
404 for (index=0; result[index]; index++)
405 free (result[index]);
413 memset (result + numresults, 0, len - numresults*sizeof(s_var *));
414 numresults += MULTIPART_DELTA;
417 if ((result[current] = (s_var *)malloc(sizeof(s_var))) == NULL) {
418 for (index=0; result[index]; index++)
419 free (result[index]);
427 cgiDebugOutput (3, "Set #%d to %s=%s", index, name, line);
428 result[index]->name = name; name = NULL;
429 result[index]->value = strdup (line);
430 cgiDecodeString (result[index]->value);
436 cgiDebugOutput (3, "Set #%d to %s=%s", index, name, line);
439 if ((cp = (char *)realloc (result[index]->value, strlen(result[index]->value)+strlen(line)+2)) != NULL) {
442 result[index]->value = cp;
452 cgiDecodeString (xp);
454 if ((name = (char *)malloc (strlen(result[index]->value)+strlen(xp)+3)) == NULL) {
455 for (index=0; result[index]; index++)
456 free (result[index]);
461 sprintf (name, "%s\r\n%s", result[index]->value, xp);
462 free (result[index]->value);
463 result[index]->value = name;
471 if ((res = (s_cgi *)malloc (sizeof (s_cgi))) == NULL)
481 /* cgiReadVariables()
483 * Read from stdin if no string is provided via CGI. Variables that
484 * doesn't have a value associated with it doesn't get stored.
486 s_cgi *cgiReadVariables ()
491 char *cp, *ip, *esp, *sptr;
497 cp = getenv("CONTENT_TYPE");
499 cgiDebugOutput (2, "Content-Type: %s", cp);
500 if (cp && strstr(cp, "multipart/form-data") && strstr(cp, "boundary=")) {
501 cp = strstr(cp, "boundary=") + strlen ("boundary=") - 2;
503 return cgiReadMultipart (cp);
506 if ((res = (s_cgi *)malloc (sizeof (s_cgi))) == NULL)
509 cp = getenv("REQUEST_METHOD");
511 cgiDebugOutput (2, "REQUEST_METHOD: %s", cp);
512 ip = getenv("CONTENT_LENGTH");
514 cgiDebugOutput (2, "CONTENT_LENGTH: %s", ip);
516 if (cp && !strcmp(cp, "POST")) {
521 if ((line = (char *)malloc (length+2)) == NULL)
523 fgets(line, length+1, stdin);
526 } else if (cp && !strcmp(cp, "GET")) {
527 esp = getenv("QUERY_STRING");
528 if (esp && strlen(esp)) {
529 if ((line = (char *)malloc (strlen(esp)+2)) == NULL)
531 sprintf (line, "%s", esp);
536 printf ("(offline mode: enter name=value pairs on standard input)\n");
537 memset (tmp, 0, sizeof(tmp));
538 while((cp = fgets (tmp, 100, stdin)) != NULL) {
540 if (tmp[strlen(tmp)-1] == '\n')
541 tmp[strlen(tmp)-1] = '&';
543 length += strlen(tmp);
544 len = (length+1) * sizeof(char);
545 if ((line = (char *)realloc (line, len)) == NULL)
549 length = strlen(tmp);
550 len = (length+1) * sizeof(char);
551 if ((line = (char *)malloc (len)) == NULL)
553 memset (line, 0, len);
557 memset (tmp, 0, sizeof(tmp));
561 if (line[strlen(line)-1] == '&')
562 line[strlen(line)-1] = '\0';
566 * From now on all cgi variables are stored in the variable line
567 * and look like foo=bar&foobar=barfoo&foofoo=
570 cgiDebugOutput (1, "Received CGI input: %s", line);
572 for (cp=line; *cp; cp++)
577 for (numargs=1,cp=line; *cp; cp++)
578 if (*cp == '&' || *cp == ';' ) numargs++;
581 cgiDebugOutput (1, "%d cgi variables found.", numargs);
583 len = (numargs+1) * sizeof(s_var *);
584 if ((result = (s_var **)malloc (len)) == NULL)
586 memset (result, 0, len);
591 if ((ip = (char *)strchr(cp, '&')) != NULL) {
593 } else if ((ip = (char *)strchr(cp, ';')) != NULL) {
596 ip = cp + strlen(cp);
598 if ((esp=(char *)strchr(cp, '=')) == NULL) {
612 if ((name = (char *)malloc((esp-cp+1) * sizeof (char))) == NULL)
614 strncpy(name, cp, esp-cp);
616 cgiDecodeString (name);
620 if ((value = (char *)malloc((ip-esp+1) * sizeof (char))) == NULL) {
624 strncpy(value, cp, ip-esp);
625 value[ip-esp] = '\0';
626 cgiDecodeString (value);
628 /* try to find out if there's already such a variable */
629 for (k=0; k<i && strcmp (result[k]->name, name); k++);
631 if (k == i) { /* No such variable yet */
632 if ((result[i] = (s_var *)malloc(sizeof(s_var))) == NULL)
634 result[i]->name = name;
635 result[i]->value = value;
636 cgiDebugOutput (1, "%s: %s", result[i]->name, result[i]->value);
638 } else { /* There is already such a name, suppose a mutiple field */
640 len = (strlen(result[k]->value)+strlen(value)+2) * sizeof (char);
641 if ((sptr = (char *)malloc(len)) == NULL) {
645 memset (sptr, 0, len);
646 sprintf (sptr, "%s\n%s", result[k]->value, value);
647 free (result[k]->value);
649 result[k]->value = sptr;
650 cgiDebugOutput (1, "%s: %s", result[i]->name, result[i]->value);
665 * Read from stdin if no string is provided via CGI. Variables that
666 * doesn't have a value associated with it doesn't get stored.
672 res = cgiReadVariables ();
674 res->cookies = cgiReadCookies ();
677 /* In some cases, we might have no other CGI results.
678 But we still have cookies! */
680 cookies = cgiReadCookies();
682 /* We need to create a s_cgi structure. */
683 if ((res = (s_cgi *)malloc (sizeof (s_cgi))) == NULL)
686 res->cookies = cookies;
694 if (!res->vars && !res->cookies && !res->files) {
702 char *cgiGetValue (s_cgi *parms, const char *name)
706 if (!parms || !parms->vars)
708 for (i=0;parms->vars[i]; i++)
709 if (!strcmp(name,parms->vars[i]->name)) {
710 cgiDebugOutput (1, "%s found as %s", name, parms->vars[i]->value);
711 if (strlen(parms->vars[i]->value) > 0)
712 return parms->vars[i]->value;
716 cgiDebugOutput (1, "%s not found", name);
720 char **cgiGetVariables (s_cgi *parms)
726 if (!parms || !parms->vars)
728 for (i=0;parms->vars[i]; i++);
729 len = sizeof (char *) * ++i;
730 if ((res = (char **)malloc (len)) == NULL)
732 memset (res, 0, len);
733 for (i=0;parms->vars[i]; i++) {
734 len = strlen (parms->vars[i]->name) +1;
735 if ((res[i] = (char *)malloc (len)) == NULL)
737 memset (res[i], 0, len);
738 strcpy (res[i], parms->vars[i]->name);
745 * Returns a list of names of all files.
747 char **cgiGetFiles (s_cgi *parms)
753 if (!parms || !parms->files)
756 for (i=0;parms->files[i]; i++);
757 len = sizeof (char *) * ++i;
758 if ((res = (char **)malloc (len)) == NULL)
760 memset (res, 0, len);
761 for (i=0;parms->files[i]; i++) {
762 len = strlen (parms->files[i]->name) +1;
763 if ((res[i] = (char *)malloc (len)) == NULL)
765 memset (res[i], 0, len);
766 strcpy (res[i], parms->files[i]->name);
773 * Return data structure for CGI file variable
775 s_file *cgiGetFile (s_cgi *parms, const char *name)
779 if (!parms || !parms->files)
782 for (i=0;parms->files[i]; i++)
783 if (!strcmp(name,parms->files[i]->name)) {
784 cgiDebugOutput (1, "%s found as %s", name, parms->files[i]->filename);
785 return parms->files[i];
787 cgiDebugOutput (1, "%s not found", name);
791 void cgiRedirect (const char *url)
793 if (url && strlen(url)) {
794 printf ("Content-type: text/html\r\nContent-length: %d\r\n", 77+(strlen(url)*2));
795 printf ("Status: 302 Temporal Relocation\r\n");
796 printf ("Location: %s\r\n\r\n", url);
797 printf ("<html>\n<body>\nThe page has been moved to <a href=\"%s\">%s</a>\n</body>\n</html>\n", url, url);
801 void cgiFreeList (char **list)
805 for (i=0; list[i] != NULL; i++)
810 void cgiFree (s_cgi *parms)
817 for (i=0;parms->vars[i]; i++) {
818 if (parms->vars[i]->name)
819 free (parms->vars[i]->name);
820 if (parms->vars[i]->value)
821 free (parms->vars[i]->value);
822 free (parms->vars[i]);
826 if (parms->cookies) {
827 if (parms->cookies[0]->version)
828 free (parms->cookies[0]->version);
829 for (i=0;parms->cookies[i]; i++) {
830 if (parms->cookies[i]->name)
831 free (parms->cookies[i]->name);
832 if (parms->cookies[i]->value)
833 free (parms->cookies[i]->value);
834 if (parms->cookies[i]->path)
835 free (parms->cookies[i]->path);
836 if (parms->cookies[i]->domain)
837 free (parms->cookies[i]->domain);
838 free (parms->cookies[i]);
840 free (parms->cookies);
843 for (i=0;parms->files[i]; i++) {
844 if (parms->files[i]->name)
845 free (parms->files[i]->name);
846 if (parms->files[i]->type)
847 free (parms->files[i]->type);
848 if (parms->files[i]->filename)
849 free (parms->files[i]->filename);
850 if (parms->files[i]->tmpfile) {
851 unlink (parms->files[i]->tmpfile);
852 free (parms->files[i]->tmpfile);
854 free (parms->files[i]);
860 if (cgiHeaderString) {
861 free (cgiHeaderString);
862 cgiHeaderString = NULL;
871 char *strndup(const char *s, size_t n) {
874 if ((fnval = (char *)malloc (n + 1)) == NULL)
876 strncpy(fnval, s, n);