GNU General Public License for more details.
You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
+ along with this program; if not, write to the Free Software Foundation
+ Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#define _GNU_SOURCE 1
#include <cgi.h>
#include "aux.h"
+#ifndef HAVE_STRNDUP
+char *strndup(const char *s, size_t n);
+#endif
+
char *cgiHeaderString = NULL;
char *cgiType = NULL;
for (cp=name;*cp && *cp!=' ' && *cp!='\r' && *cp!='\n' && *cp!=':';cp++);
for (vp=value;*vp && *vp!='\r' && *vp!='\n';vp++);
+ if (cp-name == 0 || vp-value == 0)
+ return 0;
+
if (cgiHeaderString) {
len = (strlen (cgiHeaderString) + cp-name + vp-value + 5) * sizeof (char);
if ((pivot = (char *)realloc (cgiHeaderString,len)) == NULL)
if ((cgiHeaderString = (char *)malloc (len)) == NULL)
return 0;
pivot = cgiHeaderString;
+ *pivot = '\0';
}
+ memset(pivot, 0, (cp-name + vp-value + 5) * sizeof (char));
+
strncpy (pivot, name, cp-name);
+ pivot[cp-name] = '\0';
strncat (pivot, ": ", 2);
strncat (pivot, value, vp-value);
strncat (pivot, "\r\n", 2);
void cgiHeader ()
{
if (cgiType)
+ {
printf ("Content-type: %s\r\n", cgiType);
+ free(cgiType);
+ cgiType = NULL;
+ }
else
printf ("Content-type: text/html\r\n");
if (cgiHeaderString)
+ {
printf ("%s", cgiHeaderString);
+ free(cgiHeaderString);
+ cgiHeaderString = NULL;
+ }
printf ("\r\n");
}
if ((xp = strchr (cp, '\"')) == NULL)
continue;
name = strndup (cp, xp-cp);
+ cgiDecodeString (name);
cgiDebugOutput (2, "Found field name %s", name);
if ((cp = strstr (line, "filename=\"")) == NULL)
if ((xp = strchr (cp, '\"')) == NULL)
continue;
fname = strndup (cp, xp-cp);
+ cgiDecodeString (fname);
cgiDebugOutput (2, "Found filename %s", fname);
}
} else if (!strncasecmp (line, "Content-Type: ", 14)) {
cgiDebugOutput (3, "Set #%d to %s=%s", index, name, line);
result[index]->name = name; name = NULL;
result[index]->value = strdup (line);
+ cgiDecodeString (result[index]->value);
if (type) {
free (type);
type = NULL;
}
} else {
if (index > 0) {
- if ((name = (char *)malloc (strlen(result[index]->value)+strlen(line)+3)) == NULL) {
+ xp = strdup (line);
+ cgiDecodeString (xp);
+
+ if ((name = (char *)malloc (strlen(result[index]->value)+strlen(xp)+3)) == NULL) {
for (index=0; result[index]; index++)
free (result[index]);
free (result);
+ free (xp);
return NULL;
}
- sprintf (name, "%s\r\n%s", result[index]->value, line);
+ sprintf (name, "%s\r\n%s", result[index]->value, xp);
free (result[index]->value);
result[index]->value = name;
name = NULL;
+ free (xp);
}
}
}
}
if (i<numargs) {
+ char *name;
+ char *value;
+
+ if ((name = (char *)malloc((esp-cp+1) * sizeof (char))) == NULL)
+ return NULL;
+ strncpy(name, cp, esp-cp);
+ name[esp-cp] = '\0';
+ cgiDecodeString (name);
+
+ cp = ++esp;
+
+ if ((value = (char *)malloc((ip-esp+1) * sizeof (char))) == NULL) {
+ free (name);
+ return NULL;
+ }
+ strncpy(value, cp, ip-esp);
+ value[ip-esp] = '\0';
+ cgiDecodeString (value);
/* try to find out if there's already such a variable */
- for (k=0; k<i && (strncmp (result[k]->name,cp, esp-cp) || !(strlen (result[k]->name) == esp-cp)); k++);
+ for (k=0; k<i && strcmp (result[k]->name, name); k++);
if (k == i) { /* No such variable yet */
if ((result[i] = (s_var *)malloc(sizeof(s_var))) == NULL)
return NULL;
- if ((result[i]->name = (char *)malloc((esp-cp+1) * sizeof(char))) == NULL)
- return NULL;
- memset (result[i]->name, 0, esp-cp+1);
- strncpy(result[i]->name, cp, esp-cp);
- cp = ++esp;
- if ((result[i]->value = (char *)malloc((ip-esp+1) * sizeof(char))) == NULL)
- return NULL;
- memset (result[i]->value, 0, ip-esp+1);
- strncpy(result[i]->value, cp, ip-esp);
- result[i]->value = cgiDecodeString(result[i]->value);
+ result[i]->name = name;
+ result[i]->value = value;
cgiDebugOutput (1, "%s: %s", result[i]->name, result[i]->value);
i++;
} else { /* There is already such a name, suppose a mutiple field */
- cp = ++esp;
- len = (strlen(result[k]->value)+(ip-esp)+2) * sizeof (char);
- if ((sptr = (char *)malloc(len)) == NULL)
+ free (name);
+ len = (strlen(result[k]->value)+strlen(value)+2) * sizeof (char);
+ if ((sptr = (char *)malloc(len)) == NULL) {
+ free (value);
return NULL;
+ }
memset (sptr, 0, len);
- sprintf (sptr, "%s\n", result[k]->value);
- strncat(sptr, cp, ip-esp);
- free(result[k]->value);
- result[k]->value = cgiDecodeString (sptr);
+ sprintf (sptr, "%s\n%s", result[k]->value, value);
+ free (result[k]->value);
+ free (value);
+ result[k]->value = sptr;
+ cgiDebugOutput (1, "%s: %s", result[i]->name, result[i]->value);
}
}
cp = ++ip;
if (res)
res->cookies = cgiReadCookies ();
else
- return NULL;
+ {
+ /* In some cases, we might have no other CGI results.
+ But we still have cookies! */
+ s_cookie **cookies;
+ cookies = cgiReadCookies();
+ if (cookies) {
+ /* We need to create a s_cgi structure. */
+ if ((res = (s_cgi *)malloc (sizeof (s_cgi))) == NULL)
+ return NULL;
+ res->vars = NULL;
+ res->cookies = cookies;
+ res->files = NULL;
+
+ } else {
+ return NULL;
+ }
+ }
if (!res->vars && !res->cookies && !res->files) {
free (res);
}
}
+#ifndef HAVE_STRNDUP
+char *strndup(const char *s, size_t n) {
+ char *fnval;
+
+ if ((fnval = (char *)malloc (n + 1)) == NULL)
+ return NULL;
+ strncpy(fnval, s, n);
+ fnval[n] = '\0';
+ return fnval;
+}
+#endif
+
/*
* Local variables:
* c-indent-level: 4