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.
32 char *cgiHeaderString = NULL;
35 extern s_cookie **cgiReadCookies();
37 int cgiSetHeader (const char *name, const char *value)
43 if (!name || !strlen (name) || !value || !strlen (value))
46 for (cp=name;*cp && *cp!=' ' && *cp!='\r' && *cp!='\n' && *cp!=':';cp++);
47 for (vp=value;*vp && *vp!='\r' && *vp!='\n';vp++);
49 if (cgiHeaderString) {
50 len = (strlen (cgiHeaderString) + cp-name + vp-value + 5) * sizeof (char);
51 if ((pivot = (char *)realloc (cgiHeaderString,len)) == NULL)
53 cgiHeaderString = pivot;
54 pivot += strlen (cgiHeaderString);
56 len = (cp-name + vp-value + 5) * sizeof (char);
57 if ((cgiHeaderString = (char *)malloc (len)) == NULL)
59 pivot = cgiHeaderString;
61 strncpy (pivot, name, cp-name);
62 strncat (pivot, ": ", 2);
63 strncat (pivot, value, vp-value);
64 strncat (pivot, "\r\n", 2);
69 int cgiSetType (const char *type)
74 if (!type || !strlen (type))
79 for (cp=type;*cp && *cp!='\r' && *cp!='\n';cp++);
81 len = (cp-type+1) * sizeof (char);
82 if ((cgiType = (char *)malloc (len+20)) == NULL)
84 memset (cgiType, 0, len);
85 strncpy (cgiType, type, cp-type);
93 printf ("Content-type: %s\r\n", cgiType);
95 printf ("Content-type: text/html\r\n");
97 printf ("%s", cgiHeaderString);
101 void cgiDebug (int level, int where)
104 cgiDebugLevel = level;
109 cgiDebugType = where;
111 openlog("cgilib", LOG_PID, LOG_USER);
117 char *cgiDecodeString (char *text)
121 for (cp=text,xp=text; *cp; cp++) {
123 if (strchr("0123456789ABCDEFabcdef", *(cp+1))
124 && strchr("0123456789ABCDEFabcdef", *(cp+2))) {
125 if (islower(*(cp+1)))
126 *(cp+1) = toupper(*(cp+1));
127 if (islower(*(cp+2)))
128 *(cp+2) = toupper(*(cp+2));
129 *(xp) = (*(cp+1) >= 'A' ? *(cp+1) - 'A' + 10 : *(cp+1) - '0' ) * 16
130 + (*(cp+2) >= 'A' ? *(cp+2) - 'A' + 10 : *(cp+2) - '0');
137 memset(xp, 0, cp-xp);
143 * Read and save a file fro a multipart request
146 char *cgiReadFile (FILE *stream, char *boundary)
148 char *crlfboundary, *buf;
153 char template[]= "/tmp/cgilibXXXXXX";
157 boundarylen = strlen(boundary)+3;
158 if ((crlfboundary = (char *)malloc (boundarylen)) == NULL)
160 sprintf (crlfboundary, "\r\n%s", boundary);
162 if ((buf = (char *)malloc (boundarylen)) == NULL) {
166 memset (buf, 0, boundarylen);
169 if ((fd = mkstemp (template)) == -1) {
175 if ((tmpfile = fdopen (fd, "w")) == NULL) {
182 while (!feof (stream)) {
187 for (cp=buf; *cp; cp++)
189 memset (buf, 0, boundarylen);
197 if (crlfboundary[pivot+1] == c) {
200 if (strlen (buf) == strlen (crlfboundary))
205 for (cp=buf; *cp; cp++)
207 memset (buf, 0, boundarylen);
212 if (crlfboundary[0] == c) {
220 fgets (buf, boundarylen, stream);
227 return strdup (template);
230 /* cgiReadMultipart()
232 * Decode multipart/form-data
234 #define MULTIPART_DELTA 5
235 s_cgi *cgiReadMultipart (char *boundary)
239 char *name = NULL, *type = NULL;
243 s_var **result = NULL;
245 int numresults = 0, current = 0;
246 s_file **files = NULL;
253 while ((line = cgiGetLine (stdin)) != NULL) {
255 if (!strncmp (line, boundary, strlen(boundary))) {
257 } else if (!strncasecmp (line, "Content-Disposition: form-data; ", 32)) {
259 if ((cp = strstr (line, "name=\"")) == NULL)
262 if ((xp = strchr (cp, '\"')) == NULL)
264 name = strndup (cp, xp-cp);
265 cgiDecodeString (name);
266 cgiDebugOutput (2, "Found field name %s", name);
268 if ((cp = strstr (line, "filename=\"")) == NULL)
271 if ((xp = strchr (cp, '\"')) == NULL)
273 fname = strndup (cp, xp-cp);
274 cgiDecodeString (fname);
275 cgiDebugOutput (2, "Found filename %s", fname);
277 } else if (!strncasecmp (line, "Content-Type: ", 14)) {
281 cgiDebugOutput (2, "Found mime type %s", type);
289 tmpfile = cgiReadFile (stdin, boundary);
296 name = fname = type = NULL;
299 cgiDebugOutput (2, "Wrote %s (%s) to file: %s", name, fname, tmpfile);
301 if (!strlen (fname)) {
302 cgiDebugOutput (3, "Found empty filename, removing");
309 name = fname = type = NULL;
311 if ((file = (s_file *)malloc (sizeof (s_file))) == NULL) {
312 cgiDebugOutput (3, "malloc failed, ignoring %s=%s", name, fname);
319 name = fname = type = NULL;
325 file->tmpfile = tmpfile;
326 if ((cp = rindex (fname, '/')) == NULL)
327 file->filename = fname;
329 file->filename = strdup (++cp);
332 name = type = fname = NULL;
335 if ((files = (s_file **)malloc(2*sizeof (s_file *))) == NULL) {
336 cgiDebugOutput (3, "malloc failed, ignoring %s=%s", name, fname);
345 free (file->filename);
349 memset (files, 0, 2*sizeof (s_file *));
352 for (index=0; files[index]; index++);
353 if ((tmpf = (s_file **)realloc(files, (index+2)*sizeof (s_file *))) == NULL) {
354 cgiDebugOutput (3, "realloc failed, ignoring %s=%s", name, fname);
360 free (file->filename);
362 name = type = fname = NULL;
366 memset (files + index, 0, 2*sizeof (s_file *));
375 /* try to find out if there's already such a variable */
376 for (index=0; index<current && strcmp (result[index]->name,name); index++);
378 if (index == current) {
380 len = MULTIPART_DELTA * sizeof (s_var *);
381 if ((result = (s_var **)malloc (len)) == NULL) {
387 numresults = MULTIPART_DELTA;
388 memset (result, 0, len);
391 if (current+2 > numresults) {
392 len = (numresults + MULTIPART_DELTA) * sizeof(s_var *);
393 if ((tmp = (s_var **)realloc (result, len)) == NULL) {
394 for (index=0; result[index]; index++)
395 free (result[index]);
403 memset (result + numresults, 0, len - numresults*sizeof(s_var *));
404 numresults += MULTIPART_DELTA;
407 if ((result[current] = (s_var *)malloc(sizeof(s_var))) == NULL) {
408 for (index=0; result[index]; index++)
409 free (result[index]);
417 cgiDebugOutput (3, "Set #%d to %s=%s", index, name, line);
418 result[index]->name = name; name = NULL;
419 result[index]->value = strdup (line);
420 cgiDecodeString (result[index]->value);
426 cgiDebugOutput (3, "Set #%d to %s=%s", index, name, line);
429 if ((cp = (char *)realloc (result[index]->value, strlen(result[index]->value)+strlen(line)+2)) != NULL) {
432 result[index]->value = cp;
442 cgiDecodeString (xp);
444 if ((name = (char *)malloc (strlen(result[index]->value)+strlen(xp)+3)) == NULL) {
445 for (index=0; result[index]; index++)
446 free (result[index]);
451 sprintf (name, "%s\r\n%s", result[index]->value, xp);
452 free (result[index]->value);
453 result[index]->value = name;
461 if ((res = (s_cgi *)malloc (sizeof (s_cgi))) == NULL)
471 /* cgiReadVariables()
473 * Read from stdin if no string is provided via CGI. Variables that
474 * doesn't have a value associated with it doesn't get stored.
476 s_cgi *cgiReadVariables ()
481 char *cp, *ip, *esp, *sptr;
487 cp = getenv("CONTENT_TYPE");
489 cgiDebugOutput (2, "Content-Type: %s", cp);
490 if (cp && strstr(cp, "multipart/form-data") && strstr(cp, "boundary=")) {
491 cp = strstr(cp, "boundary=") + strlen ("boundary=") - 2;
493 return cgiReadMultipart (cp);
496 if ((res = (s_cgi *)malloc (sizeof (s_cgi))) == NULL)
499 cp = getenv("REQUEST_METHOD");
501 cgiDebugOutput (2, "REQUEST_METHOD: %s", cp);
502 ip = getenv("CONTENT_LENGTH");
504 cgiDebugOutput (2, "CONTENT_LENGTH: %s", ip);
506 if (cp && !strcmp(cp, "POST")) {
511 if ((line = (char *)malloc (length+2)) == NULL)
513 fgets(line, length+1, stdin);
516 } else if (cp && !strcmp(cp, "GET")) {
517 esp = getenv("QUERY_STRING");
518 if (esp && strlen(esp)) {
519 if ((line = (char *)malloc (strlen(esp)+2)) == NULL)
521 sprintf (line, "%s", esp);
526 printf ("(offline mode: enter name=value pairs on standard input)\n");
527 memset (tmp, 0, sizeof(tmp));
528 while((cp = fgets (tmp, 100, stdin)) != NULL) {
530 if (tmp[strlen(tmp)-1] == '\n')
531 tmp[strlen(tmp)-1] = '&';
533 length += strlen(tmp);
534 len = (length+1) * sizeof(char);
535 if ((line = (char *)realloc (line, len)) == NULL)
539 length = strlen(tmp);
540 len = (length+1) * sizeof(char);
541 if ((line = (char *)malloc (len)) == NULL)
543 memset (line, 0, len);
547 memset (tmp, 0, sizeof(tmp));
551 if (line[strlen(line)-1] == '&')
552 line[strlen(line)-1] = '\0';
556 * From now on all cgi variables are stored in the variable line
557 * and look like foo=bar&foobar=barfoo&foofoo=
560 cgiDebugOutput (1, "Received CGI input: %s", line);
562 for (cp=line; *cp; cp++)
567 for (numargs=1,cp=line; *cp; cp++)
568 if (*cp == '&' || *cp == ';' ) numargs++;
571 cgiDebugOutput (1, "%d cgi variables found.", numargs);
573 len = (numargs+1) * sizeof(s_var *);
574 if ((result = (s_var **)malloc (len)) == NULL)
576 memset (result, 0, len);
581 if ((ip = (char *)strchr(cp, '&')) != NULL) {
583 } else if ((ip = (char *)strchr(cp, ';')) != NULL) {
586 ip = cp + strlen(cp);
588 if ((esp=(char *)strchr(cp, '=')) == NULL) {
602 if ((name = (char *)malloc((esp-cp+1) * sizeof (char))) == NULL)
604 strncpy(name, cp, esp-cp);
606 cgiDecodeString (name);
610 if ((value = (char *)malloc((ip-esp+1) * sizeof (char))) == NULL) {
614 strncpy(value, cp, ip-esp);
615 value[ip-esp] = '\0';
616 cgiDecodeString (value);
618 /* try to find out if there's already such a variable */
619 for (k=0; k<i && strcmp (result[k]->name, name); k++);
621 if (k == i) { /* No such variable yet */
622 if ((result[i] = (s_var *)malloc(sizeof(s_var))) == NULL)
624 result[i]->name = name;
625 result[i]->value = value;
626 cgiDebugOutput (1, "%s: %s", result[i]->name, result[i]->value);
628 } else { /* There is already such a name, suppose a mutiple field */
630 len = (strlen(result[k]->value)+strlen(value)+2) * sizeof (char);
631 if ((sptr = (char *)malloc(len)) == NULL) {
635 memset (sptr, 0, len);
636 sprintf (sptr, "%s\n%s", result[k]->value, value);
637 free (result[k]->value);
639 result[k]->value = sptr;
640 cgiDebugOutput (1, "%s: %s", result[i]->name, result[i]->value);
655 * Read from stdin if no string is provided via CGI. Variables that
656 * doesn't have a value associated with it doesn't get stored.
662 res = cgiReadVariables ();
664 res->cookies = cgiReadCookies ();
667 /* In some cases, we might have no other CGI results.
668 But we still have cookies! */
670 cookies = cgiReadCookies();
672 /* We need to create a s_cgi structure. */
673 if ((res = (s_cgi *)malloc (sizeof (s_cgi))) == NULL)
676 res->cookies = cookies;
684 if (!res->vars && !res->cookies && !res->files) {
692 char *cgiGetValue (s_cgi *parms, const char *name)
696 if (!parms || !parms->vars)
698 for (i=0;parms->vars[i]; i++)
699 if (!strcmp(name,parms->vars[i]->name)) {
700 cgiDebugOutput (1, "%s found as %s", name, parms->vars[i]->value);
701 if (strlen(parms->vars[i]->value) > 0)
702 return parms->vars[i]->value;
706 cgiDebugOutput (1, "%s not found", name);
710 char **cgiGetVariables (s_cgi *parms)
716 if (!parms || !parms->vars)
718 for (i=0;parms->vars[i]; i++);
719 len = sizeof (char *) * ++i;
720 if ((res = (char **)malloc (len)) == NULL)
722 memset (res, 0, len);
723 for (i=0;parms->vars[i]; i++) {
724 len = strlen (parms->vars[i]->name) +1;
725 if ((res[i] = (char *)malloc (len)) == NULL)
727 memset (res[i], 0, len);
728 strcpy (res[i], parms->vars[i]->name);
735 * Returns a list of names of all files.
737 char **cgiGetFiles (s_cgi *parms)
743 if (!parms || !parms->files)
746 for (i=0;parms->files[i]; i++);
747 len = sizeof (char *) * ++i;
748 if ((res = (char **)malloc (len)) == NULL)
750 memset (res, 0, len);
751 for (i=0;parms->files[i]; i++) {
752 len = strlen (parms->files[i]->name) +1;
753 if ((res[i] = (char *)malloc (len)) == NULL)
755 memset (res[i], 0, len);
756 strcpy (res[i], parms->files[i]->name);
763 * Return data structure for CGI file variable
765 s_file *cgiGetFile (s_cgi *parms, const char *name)
769 if (!parms || !parms->files)
772 for (i=0;parms->files[i]; i++)
773 if (!strcmp(name,parms->files[i]->name)) {
774 cgiDebugOutput (1, "%s found as %s", name, parms->files[i]->filename);
775 return parms->files[i];
777 cgiDebugOutput (1, "%s not found", name);
781 void cgiRedirect (const char *url)
783 if (url && strlen(url)) {
784 printf ("Content-type: text/html\r\nContent-length: %d\r\n", 77+(strlen(url)*2));
785 printf ("Status: 302 Temporal Relocation\r\n");
786 printf ("Location: %s\r\n\r\n", url);
787 printf ("<html>\n<body>\nThe page has been moved to <a href=\"%s\">%s</a>\n</body>\n</html>\n", url, url);
791 void cgiFreeList (char **list)
795 for (i=0; list[i] != NULL; i++)
800 void cgiFree (s_cgi *parms)
807 for (i=0;parms->vars[i]; i++) {
808 if (parms->vars[i]->name)
809 free (parms->vars[i]->name);
810 if (parms->vars[i]->value)
811 free (parms->vars[i]->value);
812 free (parms->vars[i]);
816 if (parms->cookies) {
817 if (parms->cookies[0]->version)
818 free (parms->cookies[0]->version);
819 for (i=0;parms->cookies[i]; i++) {
820 if (parms->cookies[i]->name)
821 free (parms->cookies[i]->name);
822 if (parms->cookies[i]->value)
823 free (parms->cookies[i]->value);
824 if (parms->cookies[i]->path)
825 free (parms->cookies[i]->path);
826 if (parms->cookies[i]->domain)
827 free (parms->cookies[i]->domain);
828 free (parms->cookies[i]);
830 free (parms->cookies);
833 for (i=0;parms->files[i]; i++) {
834 if (parms->files[i]->name)
835 free (parms->files[i]->name);
836 if (parms->files[i]->type)
837 free (parms->files[i]->type);
838 if (parms->files[i]->filename)
839 free (parms->files[i]->filename);
840 if (parms->files[i]->tmpfile) {
841 unlink (parms->files[i]->tmpfile);
842 free (parms->files[i]->tmpfile);
844 free (parms->files[i]);
850 if (cgiHeaderString) {
851 free (cgiHeaderString);
852 cgiHeaderString = NULL;