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 + 4) * 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;
68 memset(pivot+1, 0, (cp-name + vp-value + 4));
70 strncpy (pivot, name, cp-name);
71 strncat (pivot, ": ", 2);
72 strncat (pivot, value, vp-value);
73 strncat (pivot, "\r\n", 2);
78 int cgiSetType (const char *type)
83 if (!type || !strlen (type))
88 for (cp=type;*cp && *cp!='\r' && *cp!='\n';cp++);
90 len = (cp-type+1) * sizeof (char);
91 if ((cgiType = (char *)malloc (len+20)) == NULL)
93 memset (cgiType, 0, len);
94 strncpy (cgiType, type, cp-type);
102 printf ("Content-type: %s\r\n", cgiType);
104 printf ("Content-type: text/html\r\n");
106 printf ("%s", cgiHeaderString);
110 void cgiDebug (int level, int where)
113 cgiDebugLevel = level;
118 cgiDebugType = where;
120 openlog("cgilib", LOG_PID, LOG_USER);
126 char *cgiDecodeString (char *text)
130 for (cp=text,xp=text; *cp; cp++) {
132 if (strchr("0123456789ABCDEFabcdef", *(cp+1))
133 && strchr("0123456789ABCDEFabcdef", *(cp+2))) {
134 if (islower(*(cp+1)))
135 *(cp+1) = toupper(*(cp+1));
136 if (islower(*(cp+2)))
137 *(cp+2) = toupper(*(cp+2));
138 *(xp) = (*(cp+1) >= 'A' ? *(cp+1) - 'A' + 10 : *(cp+1) - '0' ) * 16
139 + (*(cp+2) >= 'A' ? *(cp+2) - 'A' + 10 : *(cp+2) - '0');
146 memset(xp, 0, cp-xp);
152 * Read and save a file fro a multipart request
155 char *cgiReadFile (FILE *stream, char *boundary)
157 char *crlfboundary, *buf;
162 char template[]= "/tmp/cgilibXXXXXX";
166 boundarylen = strlen(boundary)+3;
167 if ((crlfboundary = (char *)malloc (boundarylen)) == NULL)
169 sprintf (crlfboundary, "\r\n%s", boundary);
171 if ((buf = (char *)malloc (boundarylen)) == NULL) {
175 memset (buf, 0, boundarylen);
178 if ((fd = mkstemp (template)) == -1) {
184 if ((tmpfile = fdopen (fd, "w")) == NULL) {
191 while (!feof (stream)) {
196 for (cp=buf; *cp; cp++)
198 memset (buf, 0, boundarylen);
206 if (crlfboundary[pivot+1] == c) {
209 if (strlen (buf) == strlen (crlfboundary))
214 for (cp=buf; *cp; cp++)
216 memset (buf, 0, boundarylen);
221 if (crlfboundary[0] == c) {
229 fgets (buf, boundarylen, stream);
236 return strdup (template);
239 /* cgiReadMultipart()
241 * Decode multipart/form-data
243 #define MULTIPART_DELTA 5
244 s_cgi *cgiReadMultipart (char *boundary)
248 char *name = NULL, *type = NULL;
252 s_var **result = NULL;
254 int numresults = 0, current = 0;
255 s_file **files = NULL;
262 while ((line = cgiGetLine (stdin)) != NULL) {
264 if (!strncmp (line, boundary, strlen(boundary))) {
266 } else if (!strncasecmp (line, "Content-Disposition: form-data; ", 32)) {
268 if ((cp = strstr (line, "name=\"")) == NULL)
271 if ((xp = strchr (cp, '\"')) == NULL)
273 name = strndup (cp, xp-cp);
274 cgiDecodeString (name);
275 cgiDebugOutput (2, "Found field name %s", name);
277 if ((cp = strstr (line, "filename=\"")) == NULL)
280 if ((xp = strchr (cp, '\"')) == NULL)
282 fname = strndup (cp, xp-cp);
283 cgiDecodeString (fname);
284 cgiDebugOutput (2, "Found filename %s", fname);
286 } else if (!strncasecmp (line, "Content-Type: ", 14)) {
290 cgiDebugOutput (2, "Found mime type %s", type);
298 tmpfile = cgiReadFile (stdin, boundary);
305 name = fname = type = NULL;
308 cgiDebugOutput (2, "Wrote %s (%s) to file: %s", name, fname, tmpfile);
310 if (!strlen (fname)) {
311 cgiDebugOutput (3, "Found empty filename, removing");
318 name = fname = type = NULL;
320 if ((file = (s_file *)malloc (sizeof (s_file))) == NULL) {
321 cgiDebugOutput (3, "malloc failed, ignoring %s=%s", name, fname);
328 name = fname = type = NULL;
334 file->tmpfile = tmpfile;
335 if ((cp = rindex (fname, '/')) == NULL)
336 file->filename = fname;
338 file->filename = strdup (++cp);
341 name = type = fname = NULL;
344 if ((files = (s_file **)malloc(2*sizeof (s_file *))) == NULL) {
345 cgiDebugOutput (3, "malloc failed, ignoring %s=%s", name, fname);
354 free (file->filename);
358 memset (files, 0, 2*sizeof (s_file *));
361 for (index=0; files[index]; index++);
362 if ((tmpf = (s_file **)realloc(files, (index+2)*sizeof (s_file *))) == NULL) {
363 cgiDebugOutput (3, "realloc failed, ignoring %s=%s", name, fname);
369 free (file->filename);
371 name = type = fname = NULL;
375 memset (files + index, 0, 2*sizeof (s_file *));
384 /* try to find out if there's already such a variable */
385 for (index=0; index<current && strcmp (result[index]->name,name); index++);
387 if (index == current) {
389 len = MULTIPART_DELTA * sizeof (s_var *);
390 if ((result = (s_var **)malloc (len)) == NULL) {
396 numresults = MULTIPART_DELTA;
397 memset (result, 0, len);
400 if (current+2 > numresults) {
401 len = (numresults + MULTIPART_DELTA) * sizeof(s_var *);
402 if ((tmp = (s_var **)realloc (result, len)) == NULL) {
403 for (index=0; result[index]; index++)
404 free (result[index]);
412 memset (result + numresults, 0, len - numresults*sizeof(s_var *));
413 numresults += MULTIPART_DELTA;
416 if ((result[current] = (s_var *)malloc(sizeof(s_var))) == NULL) {
417 for (index=0; result[index]; index++)
418 free (result[index]);
426 cgiDebugOutput (3, "Set #%d to %s=%s", index, name, line);
427 result[index]->name = name; name = NULL;
428 result[index]->value = strdup (line);
429 cgiDecodeString (result[index]->value);
435 cgiDebugOutput (3, "Set #%d to %s=%s", index, name, line);
438 if ((cp = (char *)realloc (result[index]->value, strlen(result[index]->value)+strlen(line)+2)) != NULL) {
441 result[index]->value = cp;
451 cgiDecodeString (xp);
453 if ((name = (char *)malloc (strlen(result[index]->value)+strlen(xp)+3)) == NULL) {
454 for (index=0; result[index]; index++)
455 free (result[index]);
460 sprintf (name, "%s\r\n%s", result[index]->value, xp);
461 free (result[index]->value);
462 result[index]->value = name;
470 if ((res = (s_cgi *)malloc (sizeof (s_cgi))) == NULL)
480 /* cgiReadVariables()
482 * Read from stdin if no string is provided via CGI. Variables that
483 * doesn't have a value associated with it doesn't get stored.
485 s_cgi *cgiReadVariables ()
490 char *cp, *ip, *esp, *sptr;
496 cp = getenv("CONTENT_TYPE");
498 cgiDebugOutput (2, "Content-Type: %s", cp);
499 if (cp && strstr(cp, "multipart/form-data") && strstr(cp, "boundary=")) {
500 cp = strstr(cp, "boundary=") + strlen ("boundary=") - 2;
502 return cgiReadMultipart (cp);
505 if ((res = (s_cgi *)malloc (sizeof (s_cgi))) == NULL)
508 cp = getenv("REQUEST_METHOD");
510 cgiDebugOutput (2, "REQUEST_METHOD: %s", cp);
511 ip = getenv("CONTENT_LENGTH");
513 cgiDebugOutput (2, "CONTENT_LENGTH: %s", ip);
515 if (cp && !strcmp(cp, "POST")) {
520 if ((line = (char *)malloc (length+2)) == NULL)
522 fgets(line, length+1, stdin);
525 } else if (cp && !strcmp(cp, "GET")) {
526 esp = getenv("QUERY_STRING");
527 if (esp && strlen(esp)) {
528 if ((line = (char *)malloc (strlen(esp)+2)) == NULL)
530 sprintf (line, "%s", esp);
535 printf ("(offline mode: enter name=value pairs on standard input)\n");
536 memset (tmp, 0, sizeof(tmp));
537 while((cp = fgets (tmp, 100, stdin)) != NULL) {
539 if (tmp[strlen(tmp)-1] == '\n')
540 tmp[strlen(tmp)-1] = '&';
542 length += strlen(tmp);
543 len = (length+1) * sizeof(char);
544 if ((line = (char *)realloc (line, len)) == NULL)
548 length = strlen(tmp);
549 len = (length+1) * sizeof(char);
550 if ((line = (char *)malloc (len)) == NULL)
552 memset (line, 0, len);
556 memset (tmp, 0, sizeof(tmp));
560 if (line[strlen(line)-1] == '&')
561 line[strlen(line)-1] = '\0';
565 * From now on all cgi variables are stored in the variable line
566 * and look like foo=bar&foobar=barfoo&foofoo=
569 cgiDebugOutput (1, "Received CGI input: %s", line);
571 for (cp=line; *cp; cp++)
576 for (numargs=1,cp=line; *cp; cp++)
577 if (*cp == '&' || *cp == ';' ) numargs++;
580 cgiDebugOutput (1, "%d cgi variables found.", numargs);
582 len = (numargs+1) * sizeof(s_var *);
583 if ((result = (s_var **)malloc (len)) == NULL)
585 memset (result, 0, len);
590 if ((ip = (char *)strchr(cp, '&')) != NULL) {
592 } else if ((ip = (char *)strchr(cp, ';')) != NULL) {
595 ip = cp + strlen(cp);
597 if ((esp=(char *)strchr(cp, '=')) == NULL) {
611 if ((name = (char *)malloc((esp-cp+1) * sizeof (char))) == NULL)
613 strncpy(name, cp, esp-cp);
615 cgiDecodeString (name);
619 if ((value = (char *)malloc((ip-esp+1) * sizeof (char))) == NULL) {
623 strncpy(value, cp, ip-esp);
624 value[ip-esp] = '\0';
625 cgiDecodeString (value);
627 /* try to find out if there's already such a variable */
628 for (k=0; k<i && strcmp (result[k]->name, name); k++);
630 if (k == i) { /* No such variable yet */
631 if ((result[i] = (s_var *)malloc(sizeof(s_var))) == NULL)
633 result[i]->name = name;
634 result[i]->value = value;
635 cgiDebugOutput (1, "%s: %s", result[i]->name, result[i]->value);
637 } else { /* There is already such a name, suppose a mutiple field */
639 len = (strlen(result[k]->value)+strlen(value)+2) * sizeof (char);
640 if ((sptr = (char *)malloc(len)) == NULL) {
644 memset (sptr, 0, len);
645 sprintf (sptr, "%s\n%s", result[k]->value, value);
646 free (result[k]->value);
648 result[k]->value = sptr;
649 cgiDebugOutput (1, "%s: %s", result[i]->name, result[i]->value);
664 * Read from stdin if no string is provided via CGI. Variables that
665 * doesn't have a value associated with it doesn't get stored.
671 res = cgiReadVariables ();
673 res->cookies = cgiReadCookies ();
676 /* In some cases, we might have no other CGI results.
677 But we still have cookies! */
679 cookies = cgiReadCookies();
681 /* We need to create a s_cgi structure. */
682 if ((res = (s_cgi *)malloc (sizeof (s_cgi))) == NULL)
685 res->cookies = cookies;
693 if (!res->vars && !res->cookies && !res->files) {
701 char *cgiGetValue (s_cgi *parms, const char *name)
705 if (!parms || !parms->vars)
707 for (i=0;parms->vars[i]; i++)
708 if (!strcmp(name,parms->vars[i]->name)) {
709 cgiDebugOutput (1, "%s found as %s", name, parms->vars[i]->value);
710 if (strlen(parms->vars[i]->value) > 0)
711 return parms->vars[i]->value;
715 cgiDebugOutput (1, "%s not found", name);
719 char **cgiGetVariables (s_cgi *parms)
725 if (!parms || !parms->vars)
727 for (i=0;parms->vars[i]; i++);
728 len = sizeof (char *) * ++i;
729 if ((res = (char **)malloc (len)) == NULL)
731 memset (res, 0, len);
732 for (i=0;parms->vars[i]; i++) {
733 len = strlen (parms->vars[i]->name) +1;
734 if ((res[i] = (char *)malloc (len)) == NULL)
736 memset (res[i], 0, len);
737 strcpy (res[i], parms->vars[i]->name);
744 * Returns a list of names of all files.
746 char **cgiGetFiles (s_cgi *parms)
752 if (!parms || !parms->files)
755 for (i=0;parms->files[i]; i++);
756 len = sizeof (char *) * ++i;
757 if ((res = (char **)malloc (len)) == NULL)
759 memset (res, 0, len);
760 for (i=0;parms->files[i]; i++) {
761 len = strlen (parms->files[i]->name) +1;
762 if ((res[i] = (char *)malloc (len)) == NULL)
764 memset (res[i], 0, len);
765 strcpy (res[i], parms->files[i]->name);
772 * Return data structure for CGI file variable
774 s_file *cgiGetFile (s_cgi *parms, const char *name)
778 if (!parms || !parms->files)
781 for (i=0;parms->files[i]; i++)
782 if (!strcmp(name,parms->files[i]->name)) {
783 cgiDebugOutput (1, "%s found as %s", name, parms->files[i]->filename);
784 return parms->files[i];
786 cgiDebugOutput (1, "%s not found", name);
790 void cgiRedirect (const char *url)
792 if (url && strlen(url)) {
793 printf ("Content-type: text/html\r\nContent-length: %d\r\n", 77+(strlen(url)*2));
794 printf ("Status: 302 Temporal Relocation\r\n");
795 printf ("Location: %s\r\n\r\n", url);
796 printf ("<html>\n<body>\nThe page has been moved to <a href=\"%s\">%s</a>\n</body>\n</html>\n", url, url);
800 void cgiFreeList (char **list)
804 for (i=0; list[i] != NULL; i++)
809 void cgiFree (s_cgi *parms)
816 for (i=0;parms->vars[i]; i++) {
817 if (parms->vars[i]->name)
818 free (parms->vars[i]->name);
819 if (parms->vars[i]->value)
820 free (parms->vars[i]->value);
821 free (parms->vars[i]);
825 if (parms->cookies) {
826 if (parms->cookies[0]->version)
827 free (parms->cookies[0]->version);
828 for (i=0;parms->cookies[i]; i++) {
829 if (parms->cookies[i]->name)
830 free (parms->cookies[i]->name);
831 if (parms->cookies[i]->value)
832 free (parms->cookies[i]->value);
833 if (parms->cookies[i]->path)
834 free (parms->cookies[i]->path);
835 if (parms->cookies[i]->domain)
836 free (parms->cookies[i]->domain);
837 free (parms->cookies[i]);
839 free (parms->cookies);
842 for (i=0;parms->files[i]; i++) {
843 if (parms->files[i]->name)
844 free (parms->files[i]->name);
845 if (parms->files[i]->type)
846 free (parms->files[i]->type);
847 if (parms->files[i]->filename)
848 free (parms->files[i]->filename);
849 if (parms->files[i]->tmpfile) {
850 unlink (parms->files[i]->tmpfile);
851 free (parms->files[i]->tmpfile);
853 free (parms->files[i]);
859 if (cgiHeaderString) {
860 free (cgiHeaderString);
861 cgiHeaderString = NULL;
870 char *strndup(const char *s, size_t n) {
873 if ((fnval = (char *)malloc (n + 1)) == NULL)
875 strncpy(fnval, s, n);