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 ((cgiHeaderString = (char *)realloc (cgiHeaderString,len)) == NULL)
58 pivot = cgiHeaderString;
59 pivot += strlen (cgiHeaderString);
60 strncpy (pivot, name, cp-name);
62 pivot[cp-name+1] = ' ';
63 pivot[cp-name+2] = '\0';
64 strncat (pivot, value, vp-value);
65 pivot[cp-name+2+vp-value] = '\r';
66 pivot[cp-name+2+vp-value+1] = '\n';
68 len = (cp-name + vp-value + 5) * sizeof (char);
69 if ((cgiHeaderString = (char *)malloc (len)) == NULL)
71 strncpy (cgiHeaderString, name, cp-name);
72 cgiHeaderString[cp-name] = ':';
73 cgiHeaderString[cp-name+1] = ' ';
74 cgiHeaderString[cp-name+2] = '\0';
75 strncat (cgiHeaderString, value, vp-value);
76 cgiHeaderString[cp-name+2+vp-value] = '\r';
77 cgiHeaderString[cp-name+2+vp-value+1] = '\n';
82 int cgiSetType (char *type)
87 if (!type || !strlen (type))
92 for (cp=type;*cp && *cp!='\r' && *cp!='\n';cp++);
94 len = (cp-type+1) * sizeof (char);
95 if ((cgiType = (char *)malloc (len+20)) == NULL)
97 memset (cgiType, 0, len);
98 strncpy (cgiType, type, cp-type);
106 printf ("Content-type: %s\r\n", cgiType);
108 printf ("Content-type: text/html\r\n");
110 printf ("%s", cgiHeaderString);
114 void cgiDebug (int level, int where)
117 cgiDebugLevel = level;
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);
150 /* cgiReadVariables()
152 * Read from stdin if no string is provided via CGI. Variables that
153 * doesn't have a value associated with it doesn't get stored.
155 s_var **cgiReadVariables ()
160 char *cp, *ip, *esp, *sptr;
165 cp = getenv("REQUEST_METHOD");
166 ip = getenv("CONTENT_LENGTH");
168 if (cp && !strcmp(cp, "POST")) {
171 if ((line = (char *)malloc (length+2)) == NULL)
173 fgets(line, length+1, stdin);
176 } else if (cp && !strcmp(cp, "GET")) {
177 esp = getenv("QUERY_STRING");
178 if (esp && strlen(esp)) {
179 if ((line = (char *)malloc (strlen(esp)+2)) == NULL)
181 sprintf (line, "%s", esp);
186 printf ("(offline mode: enter name=value pairs on standard input)\n");
187 memset (tmp, 0, sizeof(tmp));
188 while((cp = fgets (tmp, 100, stdin)) != NULL) {
190 if (tmp[strlen(tmp)-1] == '\n')
191 tmp[strlen(tmp)-1] = '&';
193 length += strlen(tmp);
194 len = (length+1) * sizeof(char);
195 if ((line = (char *)realloc (line, len)) == NULL)
199 length = strlen(tmp);
200 len = (length+1) * sizeof(char);
201 if ((line = (char *)malloc (len)) == NULL)
203 memset (line, 0, len);
207 memset (tmp, 0, sizeof(tmp));
211 if (line[strlen(line)-1] == '&')
212 line[strlen(line)-1] = '\0';
216 * From now on all cgi variables are stored in the variable line
217 * and look like foo=bar&foobar=barfoo&foofoo=
220 if (cgiDebugLevel > 0) {
222 fprintf (stderr, "Received cgi input: %s\n", line);
224 printf ("<b>Received cgi input</b><br>\n<pre>\n--\n%s\n--\n</pre>\n\n", line);
227 for (cp=line; *cp; cp++)
232 for (numargs=1,cp=line; *cp; cp++)
233 if (*cp == '&' || *cp == ';' ) numargs++;
236 if (cgiDebugLevel > 0) {
238 fprintf (stderr, "%d cgi variables found.\n", numargs);
240 printf ("%d cgi variables found.<br>\n", numargs);
243 len = (numargs+1) * sizeof(s_var *);
244 if ((result = (s_var **)malloc (len)) == NULL)
246 memset (result, 0, len);
251 if ((ip = (char *)strchr(cp, '&')) != NULL) {
253 } else if ((ip = (char *)strchr(cp, ';')) != NULL) {
256 ip = cp + strlen(cp);
258 if ((esp=(char *)strchr(cp, '=')) == NULL) {
270 /* try to find out if there's already such a variable */
271 for (k=0; k<i && (strncmp (result[k]->name,cp, esp-cp) || !(strlen (result[k]->name) == esp-cp)); k++);
273 if (k == i) { /* No such variable yet */
274 if ((result[i] = (s_var *)malloc(sizeof(s_var))) == NULL)
276 if ((result[i]->name = (char *)malloc((esp-cp+1) * sizeof(char))) == NULL)
278 memset (result[i]->name, 0, esp-cp+1);
279 strncpy(result[i]->name, cp, esp-cp);
281 if ((result[i]->value = (char *)malloc((ip-esp+1) * sizeof(char))) == NULL)
283 memset (result[i]->value, 0, ip-esp+1);
284 strncpy(result[i]->value, cp, ip-esp);
285 result[i]->value = cgiDecodeString(result[i]->value);
288 fprintf (stderr, "%s: %s\n", result[i]->name, result[i]->value);
290 printf ("<h3>Variable %s</h3>\n<pre>\n%s\n</pre>\n\n", result[i]->name, result[i]->value);
293 } else { /* There is already such a name, suppose a mutiple field */
295 len = (strlen(result[k]->value)+(ip-esp)+2) * sizeof (char);
296 if ((sptr = (char *)malloc(len)) == NULL)
298 memset (sptr, 0, len);
299 sprintf (sptr, "%s\n", result[k]->value);
300 strncat(sptr, cp, ip-esp);
301 free(result[k]->value);
302 result[k]->value = cgiDecodeString (sptr);
312 * Read from stdin if no string is provided via CGI. Variables that
313 * doesn't have a value associated with it doesn't get stored.
321 vars = cgiReadVariables ();
322 cookies = cgiReadCookies ();
324 if (!vars && !cookies)
327 if ((res = (s_cgi *)malloc (sizeof (s_cgi))) == NULL)
330 res->cookies = cookies;
335 char *cgiGetValue (s_cgi *parms, const char *name)
339 if (!parms || !parms->vars)
341 for (i=0;parms->vars[i]; i++)
342 if (!strcmp(name,parms->vars[i]->name)) {
343 if (cgiDebugLevel > 0) {
345 fprintf (stderr, "%s found as %s\n", name, parms->vars[i]->value);
347 printf ("%s found as %s<br>\n", name, parms->vars[i]->value);
349 if (strlen(parms->vars[i]->value) > 0)
350 return parms->vars[i]->value;
356 fprintf (stderr, "%s not found\n", name);
358 printf ("%s not found<br>\n", name);
363 char **cgiGetVariables (s_cgi *parms)
369 if (!parms || !parms->vars)
371 for (i=0;parms->vars[i]; i++);
372 len = sizeof (char *) * ++i;
373 if ((res = (char **)malloc (len)) == NULL)
375 memset (res, 0, len);
376 for (i=0;parms->vars[i]; i++) {
377 len = strlen (parms->vars[i]->name) +1;
378 if ((res[i] = (char *)malloc (len)) == NULL)
380 memset (res[i], 0, len);
381 strcpy (res[i], parms->vars[i]->name);
386 void cgiRedirect (const char *url)
388 if (url && strlen(url)) {
389 printf ("Content-type: text/html\r\nContent-length: %d\r\n", 77+(strlen(url)*2));
390 printf ("Status: 302 Temporal Relocation\r\n");
391 printf ("Location: %s\r\n\r\n", url);
392 printf ("<html>\n<body>\nThe page has been moved to <a href=\"%s\">%s</a>\n</body>\n</html>\n", url, url);
396 void cgiFreeList (char **list)
400 for (i=0; list[i] != NULL; i++)
405 void cgiFree (s_cgi *parms)
412 for (i=0;parms->vars[i]; i++) {
413 if (parms->vars[i]->name)
414 free (parms->vars[i]->name);
415 if (parms->vars[i]->value)
416 free (parms->vars[i]->value);
417 free (parms->vars[i]);
421 if (parms->cookies) {
422 if (parms->cookies[0]->version)
423 free (parms->cookies[0]->version);
424 for (i=0;parms->cookies[i]; i++) {
425 if (parms->cookies[i]->name)
426 free (parms->cookies[i]->name);
427 if (parms->cookies[i]->value)
428 free (parms->cookies[i]->value);
429 if (parms->cookies[i]->path)
430 free (parms->cookies[i]->path);
431 if (parms->cookies[i]->domain)
432 free (parms->cookies[i]->domain);
433 free (parms->cookies[i]);
435 free (parms->cookies);
439 if (cgiHeaderString) {
440 free (cgiHeaderString);
441 cgiHeaderString = NULL;