2 cgi.c - Some simple routines for CGI programming
3 Copyright (c) 1996-9 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.
21 Sat Aug 24 10:39:31 MET 1999: Martin Schulze <joey@infodrom.org>
22 Added cgiGetVariables(), corrected multiple values code
24 Tue Aug 17 13:13:00 CEST 1999: Martin Schulze <joey@infodrom.org>
25 Changed s_cgi to contain cookies as well. Added proper support for
36 int cgiDebugLevel = 0;
37 int cgiDebugStderr = 1;
38 char *cgiHeaderString = NULL;
41 extern s_cookie **cgiReadCookies();
43 int cgiSetHeader (char *name, char *value)
45 char *cp, *vp, *pivot;
48 if (!name || !strlen (name) || !value || !strlen (value))
51 for (cp=name;*cp && *cp!=' ' && *cp!='\r' && *cp!='\n' && *cp!=':';cp++);
52 for (vp=value;*vp && *vp!='\r' && *vp!='\n';vp++);
54 if (cgiHeaderString) {
55 len = (strlen (cgiHeaderString) + cp-name + vp-value + 5) * sizeof (char);
56 if ((pivot = (char *)realloc (cgiHeaderString,len)) == NULL)
58 cgiHeaderString = pivot;
59 pivot += strlen (cgiHeaderString);
61 len = (cp-name + vp-value + 5) * sizeof (char);
62 if ((cgiHeaderString = (char *)malloc (len)) == NULL)
64 pivot = cgiHeaderString;
66 strncpy (pivot, name, cp-name);
67 strncat (pivot, ": ", 2);
68 strncat (pivot, value, vp-value);
69 strncat (pivot, "\r\n", 2);
74 int cgiSetType (char *type)
79 if (!type || !strlen (type))
84 for (cp=type;*cp && *cp!='\r' && *cp!='\n';cp++);
86 len = (cp-type+1) * sizeof (char);
87 if ((cgiType = (char *)malloc (len+20)) == NULL)
89 memset (cgiType, 0, len);
90 strncpy (cgiType, type, cp-type);
98 printf ("Content-type: %s\r\n", cgiType);
100 printf ("Content-type: text/html\r\n");
102 printf ("%s", cgiHeaderString);
106 void cgiDebug (int level, int where)
109 cgiDebugLevel = level;
118 char *cgiDecodeString (char *text)
122 for (cp=text,xp=text; *cp; cp++) {
124 if (strchr("0123456789ABCDEFabcdef", *(cp+1))
125 && strchr("0123456789ABCDEFabcdef", *(cp+2))) {
126 if (islower(*(cp+1)))
127 *(cp+1) = toupper(*(cp+1));
128 if (islower(*(cp+2)))
129 *(cp+2) = toupper(*(cp+2));
130 *(xp) = (*(cp+1) >= 'A' ? *(cp+1) - 'A' + 10 : *(cp+1) - '0' ) * 16
131 + (*(cp+2) >= 'A' ? *(cp+2) - 'A' + 10 : *(cp+2) - '0');
138 memset(xp, 0, cp-xp);
142 /* cgiReadVariables()
144 * Read from stdin if no string is provided via CGI. Variables that
145 * doesn't have a value associated with it doesn't get stored.
147 s_var **cgiReadVariables ()
152 char *cp, *ip, *esp, *sptr;
157 cp = getenv("REQUEST_METHOD");
158 ip = getenv("CONTENT_LENGTH");
160 if (cp && !strcmp(cp, "POST")) {
163 if ((line = (char *)malloc (length+2)) == NULL)
165 fgets(line, length+1, stdin);
168 } else if (cp && !strcmp(cp, "GET")) {
169 esp = getenv("QUERY_STRING");
170 if (esp && strlen(esp)) {
171 if ((line = (char *)malloc (strlen(esp)+2)) == NULL)
173 sprintf (line, "%s", esp);
178 printf ("(offline mode: enter name=value pairs on standard input)\n");
179 memset (tmp, 0, sizeof(tmp));
180 while((cp = fgets (tmp, 100, stdin)) != NULL) {
182 if (tmp[strlen(tmp)-1] == '\n')
183 tmp[strlen(tmp)-1] = '&';
185 length += strlen(tmp);
186 len = (length+1) * sizeof(char);
187 if ((line = (char *)realloc (line, len)) == NULL)
191 length = strlen(tmp);
192 len = (length+1) * sizeof(char);
193 if ((line = (char *)malloc (len)) == NULL)
195 memset (line, 0, len);
199 memset (tmp, 0, sizeof(tmp));
203 if (line[strlen(line)-1] == '&')
204 line[strlen(line)-1] = '\0';
208 * From now on all cgi variables are stored in the variable line
209 * and look like foo=bar&foobar=barfoo&foofoo=
212 if (cgiDebugLevel > 0) {
214 fprintf (stderr, "Received cgi input: %s\n", line);
216 printf ("<b>Received cgi input</b><br>\n<pre>\n--\n%s\n--\n</pre>\n\n", line);
219 for (cp=line; *cp; cp++)
224 for (numargs=1,cp=line; *cp; cp++)
225 if (*cp == '&' || *cp == ';' ) numargs++;
228 if (cgiDebugLevel > 0) {
230 fprintf (stderr, "%d cgi variables found.\n", numargs);
232 printf ("%d cgi variables found.<br>\n", numargs);
235 len = (numargs+1) * sizeof(s_var *);
236 if ((result = (s_var **)malloc (len)) == NULL)
238 memset (result, 0, len);
243 if ((ip = (char *)strchr(cp, '&')) != NULL) {
245 } else if ((ip = (char *)strchr(cp, ';')) != NULL) {
248 ip = cp + strlen(cp);
250 if ((esp=(char *)strchr(cp, '=')) == NULL) {
262 /* try to find out if there's already such a variable */
263 for (k=0; k<i && (strncmp (result[k]->name,cp, esp-cp) || !(strlen (result[k]->name) == esp-cp)); k++);
265 if (k == i) { /* No such variable yet */
266 if ((result[i] = (s_var *)malloc(sizeof(s_var))) == NULL)
268 if ((result[i]->name = (char *)malloc((esp-cp+1) * sizeof(char))) == NULL)
270 memset (result[i]->name, 0, esp-cp+1);
271 strncpy(result[i]->name, cp, esp-cp);
273 if ((result[i]->value = (char *)malloc((ip-esp+1) * sizeof(char))) == NULL)
275 memset (result[i]->value, 0, ip-esp+1);
276 strncpy(result[i]->value, cp, ip-esp);
277 result[i]->value = cgiDecodeString(result[i]->value);
280 fprintf (stderr, "%s: %s\n", result[i]->name, result[i]->value);
282 printf ("<h3>Variable %s</h3>\n<pre>\n%s\n</pre>\n\n", result[i]->name, result[i]->value);
285 } else { /* There is already such a name, suppose a mutiple field */
287 len = (strlen(result[k]->value)+(ip-esp)+2) * sizeof (char);
288 if ((sptr = (char *)malloc(len)) == NULL)
290 memset (sptr, 0, len);
291 sprintf (sptr, "%s\n", result[k]->value);
292 strncat(sptr, cp, ip-esp);
293 free(result[k]->value);
294 result[k]->value = cgiDecodeString (sptr);
304 * Read from stdin if no string is provided via CGI. Variables that
305 * doesn't have a value associated with it doesn't get stored.
313 vars = cgiReadVariables ();
314 cookies = cgiReadCookies ();
316 if (!vars && !cookies)
319 if ((res = (s_cgi *)malloc (sizeof (s_cgi))) == NULL)
322 res->cookies = cookies;
327 char *cgiGetValue (s_cgi *parms, const char *name)
331 if (!parms || !parms->vars)
333 for (i=0;parms->vars[i]; i++)
334 if (!strcmp(name,parms->vars[i]->name)) {
335 if (cgiDebugLevel > 0) {
337 fprintf (stderr, "%s found as %s\n", name, parms->vars[i]->value);
339 printf ("%s found as %s<br>\n", name, parms->vars[i]->value);
341 if (strlen(parms->vars[i]->value) > 0)
342 return parms->vars[i]->value;
348 fprintf (stderr, "%s not found\n", name);
350 printf ("%s not found<br>\n", name);
355 char **cgiGetVariables (s_cgi *parms)
361 if (!parms || !parms->vars)
363 for (i=0;parms->vars[i]; i++);
364 len = sizeof (char *) * ++i;
365 if ((res = (char **)malloc (len)) == NULL)
367 memset (res, 0, len);
368 for (i=0;parms->vars[i]; i++) {
369 len = strlen (parms->vars[i]->name) +1;
370 if ((res[i] = (char *)malloc (len)) == NULL)
372 memset (res[i], 0, len);
373 strcpy (res[i], parms->vars[i]->name);
378 void cgiRedirect (const char *url)
380 if (url && strlen(url)) {
381 printf ("Content-type: text/html\r\nContent-length: %d\r\n", 77+(strlen(url)*2));
382 printf ("Status: 302 Temporal Relocation\r\n");
383 printf ("Location: %s\r\n\r\n", url);
384 printf ("<html>\n<body>\nThe page has been moved to <a href=\"%s\">%s</a>\n</body>\n</html>\n", url, url);
388 void cgiFreeList (char **list)
392 for (i=0; list[i] != NULL; i++)
397 void cgiFree (s_cgi *parms)
404 for (i=0;parms->vars[i]; i++) {
405 if (parms->vars[i]->name)
406 free (parms->vars[i]->name);
407 if (parms->vars[i]->value)
408 free (parms->vars[i]->value);
409 free (parms->vars[i]);
413 if (parms->cookies) {
414 if (parms->cookies[0]->version)
415 free (parms->cookies[0]->version);
416 for (i=0;parms->cookies[i]; i++) {
417 if (parms->cookies[i]->name)
418 free (parms->cookies[i]->name);
419 if (parms->cookies[i]->value)
420 free (parms->cookies[i]->value);
421 if (parms->cookies[i]->path)
422 free (parms->cookies[i]->path);
423 if (parms->cookies[i]->domain)
424 free (parms->cookies[i]->domain);
425 free (parms->cookies[i]);
427 free (parms->cookies);
431 if (cgiHeaderString) {
432 free (cgiHeaderString);
433 cgiHeaderString = NULL;