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);
142 * Read and save a file fro a multipart request
145 char *cgiReadFile (FILE *stream, char *boundary)
147 char *crlfboundary, *buf;
152 char template[]= "/tmp/cgilibXXXXXX";
156 boundarylen = strlen(boundary)+3;
157 if ((crlfboundary = (char *)malloc (boundarylen)) == NULL)
159 sprintf (crlfboundary, "\r\n%s", boundary);
161 if ((buf = (char *)malloc (boundarylen)) == NULL) {
165 memset (buf, 0, boundarylen);
168 if ((fd = mkstemp (template)) == -1) {
174 if ((tmpfile = fdopen (fd, "w")) == NULL) {
181 while (!feof (stream)) {
186 for (cp=buf; *cp; cp++)
188 memset (buf, 0, boundarylen);
196 if (crlfboundary[pivot+1] == c) {
199 if (strlen (buf) == strlen (crlfboundary))
204 for (cp=buf; *cp; cp++)
206 memset (buf, 0, boundarylen);
211 if (crlfboundary[0] == c) {
219 fgets (buf, boundarylen, stream);
226 return strdup (template);
229 /* cgiReadMultipart()
231 * Decode multipart/form-data
233 #define MULTIPART_DELTA 5
234 s_cgi *cgiReadMultipart (char *boundary)
238 char *name = NULL, *type = NULL;
242 s_var **result = NULL;
244 int numresults = 0, current = 0;
245 s_file **files = NULL;
252 while ((line = cgiGetLine (stdin)) != NULL) {
254 if (!strncmp (line, boundary, strlen(boundary))) {
256 } else if (!strncasecmp (line, "Content-Disposition: form-data; ", 32)) {
258 if ((cp = strstr (line, "name=\"")) == NULL)
261 if ((xp = strchr (cp, '\"')) == NULL)
263 name = strndup (cp, xp-cp);
264 cgiDebugOutput (2, "Found field name %s", name);
266 if ((cp = strstr (line, "filename=\"")) == NULL)
269 if ((xp = strchr (cp, '\"')) == NULL)
271 fname = strndup (cp, xp-cp);
272 cgiDebugOutput (2, "Found filename %s", fname);
275 } else if (!strncasecmp (line, "Content-Type: ", 14)) {
279 cgiDebugOutput (2, "Found mime type %s", type);
287 tmpfile = cgiReadFile (stdin, boundary);
294 name = fname = type = NULL;
297 cgiDebugOutput (2, "Wrote %s (%s) to file: %s", name, fname, tmpfile);
299 if (!strlen (fname)) {
300 cgiDebugOutput (3, "Found empty filename, removing");
307 name = fname = type = NULL;
309 if ((file = (s_file *)malloc (sizeof (s_file))) == NULL) {
310 cgiDebugOutput (3, "malloc failed, ignoring %s=%s", name, fname);
317 name = fname = type = NULL;
323 file->tmpfile = tmpfile;
324 if ((cp = rindex (fname, '/')) == NULL)
325 file->filename = fname;
327 file->filename = strdup (++cp);
330 name = type = fname = NULL;
333 if ((files = (s_file **)malloc(2*sizeof (s_file *))) == NULL) {
334 cgiDebugOutput (3, "malloc failed, ignoring %s=%s", name, fname);
343 free (file->filename);
347 memset (files, 0, 2*sizeof (s_file *));
350 for (index=0; files[index]; index++);
351 if ((tmpf = (s_file **)realloc(files, (index+2)*sizeof (s_file *))) == NULL) {
352 cgiDebugOutput (3, "realloc failed, ignoring %s=%s", name, fname);
358 free (file->filename);
360 name = type = fname = NULL;
364 memset (files + index, 0, 2*sizeof (s_file *));
373 /* try to find out if there's already such a variable */
374 for (index=0; index<current && strcmp (result[index]->name,name); index++);
376 if (index == current) {
378 len = MULTIPART_DELTA * sizeof (s_var *);
379 if ((result = (s_var **)malloc (len)) == NULL) {
385 numresults = MULTIPART_DELTA;
386 memset (result, 0, len);
389 if (current+2 > numresults) {
390 len = (numresults + MULTIPART_DELTA) * sizeof(s_var *);
391 if ((tmp = (s_var **)realloc (result, len)) == NULL) {
392 for (index=0; result[index]; index++)
393 free (result[index]);
401 memset (result + numresults, 0, len - numresults*sizeof(s_var *));
402 numresults += MULTIPART_DELTA;
405 if ((result[current] = (s_var *)malloc(sizeof(s_var))) == NULL) {
406 for (index=0; result[index]; index++)
407 free (result[index]);
415 cgiDebugOutput (3, "Set #%d to %s=%s", index, name, line);
416 result[index]->name = name; name = NULL;
417 result[index]->value = strdup (line);
423 cgiDebugOutput (3, "Set #%d to %s=%s", index, name, line);
426 if ((cp = (char *)realloc (result[index]->value, strlen(result[index]->value)+strlen(line)+2)) != NULL) {
429 result[index]->value = cp;
438 if ((name = (char *)malloc (strlen(result[index]->value)+strlen(line)+3)) == NULL) {
439 for (index=0; result[index]; index++)
440 free (result[index]);
444 sprintf (name, "%s\r\n%s", result[index]->value, line);
445 free (result[index]->value);
446 result[index]->value = name;
453 if ((res = (s_cgi *)malloc (sizeof (s_cgi))) == NULL)
463 /* cgiReadVariables()
465 * Read from stdin if no string is provided via CGI. Variables that
466 * doesn't have a value associated with it doesn't get stored.
468 s_cgi *cgiReadVariables ()
473 char *cp, *ip, *esp, *sptr;
479 cp = getenv("CONTENT_TYPE");
480 cgiDebugOutput (2, "Content-Type: %s", cp);
481 if (cp && strstr(cp, "multipart/form-data") && strstr(cp, "boundary=")) {
482 cp = strstr(cp, "boundary=") + strlen ("boundary=") - 2;
484 return cgiReadMultipart (cp);
487 if ((res = (s_cgi *)malloc (sizeof (s_cgi))) == NULL)
490 cp = getenv("REQUEST_METHOD");
491 cgiDebugOutput (2, "REQUEST_METHOD: %s", cp);
492 ip = getenv("CONTENT_LENGTH");
494 if (cp && !strcmp(cp, "POST")) {
499 if ((line = (char *)malloc (length+2)) == NULL)
501 fgets(line, length+1, stdin);
504 } else if (cp && !strcmp(cp, "GET")) {
505 esp = getenv("QUERY_STRING");
506 if (esp && strlen(esp)) {
507 if ((line = (char *)malloc (strlen(esp)+2)) == NULL)
509 sprintf (line, "%s", esp);
514 printf ("(offline mode: enter name=value pairs on standard input)\n");
515 memset (tmp, 0, sizeof(tmp));
516 while((cp = fgets (tmp, 100, stdin)) != NULL) {
518 if (tmp[strlen(tmp)-1] == '\n')
519 tmp[strlen(tmp)-1] = '&';
521 length += strlen(tmp);
522 len = (length+1) * sizeof(char);
523 if ((line = (char *)realloc (line, len)) == NULL)
527 length = strlen(tmp);
528 len = (length+1) * sizeof(char);
529 if ((line = (char *)malloc (len)) == NULL)
531 memset (line, 0, len);
535 memset (tmp, 0, sizeof(tmp));
539 if (line[strlen(line)-1] == '&')
540 line[strlen(line)-1] = '\0';
544 * From now on all cgi variables are stored in the variable line
545 * and look like foo=bar&foobar=barfoo&foofoo=
548 cgiDebugOutput (1, "Received CGI input: %s", line);
550 for (cp=line; *cp; cp++)
555 for (numargs=1,cp=line; *cp; cp++)
556 if (*cp == '&' || *cp == ';' ) numargs++;
559 cgiDebugOutput (1, "%d cgi variables found.", numargs);
561 len = (numargs+1) * sizeof(s_var *);
562 if ((result = (s_var **)malloc (len)) == NULL)
564 memset (result, 0, len);
569 if ((ip = (char *)strchr(cp, '&')) != NULL) {
571 } else if ((ip = (char *)strchr(cp, ';')) != NULL) {
574 ip = cp + strlen(cp);
576 if ((esp=(char *)strchr(cp, '=')) == NULL) {
588 /* try to find out if there's already such a variable */
589 for (k=0; k<i && (strncmp (result[k]->name,cp, esp-cp) || !(strlen (result[k]->name) == esp-cp)); k++);
591 if (k == i) { /* No such variable yet */
592 if ((result[i] = (s_var *)malloc(sizeof(s_var))) == NULL)
594 if ((result[i]->name = (char *)malloc((esp-cp+1) * sizeof(char))) == NULL)
596 memset (result[i]->name, 0, esp-cp+1);
597 strncpy(result[i]->name, cp, esp-cp);
599 if ((result[i]->value = (char *)malloc((ip-esp+1) * sizeof(char))) == NULL)
601 memset (result[i]->value, 0, ip-esp+1);
602 strncpy(result[i]->value, cp, ip-esp);
603 result[i]->value = cgiDecodeString(result[i]->value);
604 cgiDebugOutput (1, "%s: %s", result[i]->name, result[i]->value);
606 } else { /* There is already such a name, suppose a mutiple field */
608 len = (strlen(result[k]->value)+(ip-esp)+2) * sizeof (char);
609 if ((sptr = (char *)malloc(len)) == NULL)
611 memset (sptr, 0, len);
612 sprintf (sptr, "%s\n", result[k]->value);
613 strncat(sptr, cp, ip-esp);
614 free(result[k]->value);
615 result[k]->value = cgiDecodeString (sptr);
630 * Read from stdin if no string is provided via CGI. Variables that
631 * doesn't have a value associated with it doesn't get stored.
637 res = cgiReadVariables ();
639 res->cookies = cgiReadCookies ();
643 if (!res->vars && !res->cookies && !res->files) {
651 char *cgiGetValue (s_cgi *parms, const char *name)
655 if (!parms || !parms->vars)
657 for (i=0;parms->vars[i]; i++)
658 if (!strcmp(name,parms->vars[i]->name)) {
659 cgiDebugOutput (1, "%s found as %s", name, parms->vars[i]->value);
660 if (strlen(parms->vars[i]->value) > 0)
661 return parms->vars[i]->value;
665 cgiDebugOutput (1, "%s not found", name);
669 char **cgiGetVariables (s_cgi *parms)
675 if (!parms || !parms->vars)
677 for (i=0;parms->vars[i]; i++);
678 len = sizeof (char *) * ++i;
679 if ((res = (char **)malloc (len)) == NULL)
681 memset (res, 0, len);
682 for (i=0;parms->vars[i]; i++) {
683 len = strlen (parms->vars[i]->name) +1;
684 if ((res[i] = (char *)malloc (len)) == NULL)
686 memset (res[i], 0, len);
687 strcpy (res[i], parms->vars[i]->name);
692 void cgiRedirect (const char *url)
694 if (url && strlen(url)) {
695 printf ("Content-type: text/html\r\nContent-length: %d\r\n", 77+(strlen(url)*2));
696 printf ("Status: 302 Temporal Relocation\r\n");
697 printf ("Location: %s\r\n\r\n", url);
698 printf ("<html>\n<body>\nThe page has been moved to <a href=\"%s\">%s</a>\n</body>\n</html>\n", url, url);
702 void cgiFreeList (char **list)
706 for (i=0; list[i] != NULL; i++)
711 void cgiFree (s_cgi *parms)
718 for (i=0;parms->vars[i]; i++) {
719 if (parms->vars[i]->name)
720 free (parms->vars[i]->name);
721 if (parms->vars[i]->value)
722 free (parms->vars[i]->value);
723 free (parms->vars[i]);
727 if (parms->cookies) {
728 if (parms->cookies[0]->version)
729 free (parms->cookies[0]->version);
730 for (i=0;parms->cookies[i]; i++) {
731 if (parms->cookies[i]->name)
732 free (parms->cookies[i]->name);
733 if (parms->cookies[i]->value)
734 free (parms->cookies[i]->value);
735 if (parms->cookies[i]->path)
736 free (parms->cookies[i]->path);
737 if (parms->cookies[i]->domain)
738 free (parms->cookies[i]->domain);
739 free (parms->cookies[i]);
741 free (parms->cookies);
745 if (cgiHeaderString) {
746 free (cgiHeaderString);
747 cgiHeaderString = NULL;