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.
27 int cgiDebugLevel = 0;
28 int cgiDebugStderr = 1;
29 char *cgiHeaderString = NULL;
32 extern s_cookie **cgiReadCookies();
34 int cgiSetHeader (char *name, char *value)
36 char *cp, *vp, *pivot;
39 if (!name || !strlen (name) || !value || !strlen (value))
42 for (cp=name;*cp && *cp!=' ' && *cp!='\r' && *cp!='\n' && *cp!=':';cp++);
43 for (vp=value;*vp && *vp!='\r' && *vp!='\n';vp++);
45 if (cgiHeaderString) {
46 len = (strlen (cgiHeaderString) + cp-name + vp-value + 5) * sizeof (char);
47 if ((pivot = (char *)realloc (cgiHeaderString,len)) == NULL)
49 cgiHeaderString = pivot;
50 pivot += strlen (cgiHeaderString);
52 len = (cp-name + vp-value + 5) * sizeof (char);
53 if ((cgiHeaderString = (char *)malloc (len)) == NULL)
55 pivot = cgiHeaderString;
57 strncpy (pivot, name, cp-name);
58 strncat (pivot, ": ", 2);
59 strncat (pivot, value, vp-value);
60 strncat (pivot, "\r\n", 2);
65 int cgiSetType (char *type)
70 if (!type || !strlen (type))
75 for (cp=type;*cp && *cp!='\r' && *cp!='\n';cp++);
77 len = (cp-type+1) * sizeof (char);
78 if ((cgiType = (char *)malloc (len+20)) == NULL)
80 memset (cgiType, 0, len);
81 strncpy (cgiType, type, cp-type);
89 printf ("Content-type: %s\r\n", cgiType);
91 printf ("Content-type: text/html\r\n");
93 printf ("%s", cgiHeaderString);
97 void cgiDebug (int level, int where)
100 cgiDebugLevel = level;
109 char *cgiDecodeString (char *text)
113 for (cp=text,xp=text; *cp; cp++) {
115 if (strchr("0123456789ABCDEFabcdef", *(cp+1))
116 && strchr("0123456789ABCDEFabcdef", *(cp+2))) {
117 if (islower(*(cp+1)))
118 *(cp+1) = toupper(*(cp+1));
119 if (islower(*(cp+2)))
120 *(cp+2) = toupper(*(cp+2));
121 *(xp) = (*(cp+1) >= 'A' ? *(cp+1) - 'A' + 10 : *(cp+1) - '0' ) * 16
122 + (*(cp+2) >= 'A' ? *(cp+2) - 'A' + 10 : *(cp+2) - '0');
129 memset(xp, 0, cp-xp);
133 /* cgiReadVariables()
135 * Read from stdin if no string is provided via CGI. Variables that
136 * doesn't have a value associated with it doesn't get stored.
138 s_var **cgiReadVariables ()
143 char *cp, *ip, *esp, *sptr;
148 cp = getenv("REQUEST_METHOD");
149 ip = getenv("CONTENT_LENGTH");
151 if (cp && !strcmp(cp, "POST")) {
154 if ((line = (char *)malloc (length+2)) == NULL)
156 fgets(line, length+1, stdin);
159 } else if (cp && !strcmp(cp, "GET")) {
160 esp = getenv("QUERY_STRING");
161 if (esp && strlen(esp)) {
162 if ((line = (char *)malloc (strlen(esp)+2)) == NULL)
164 sprintf (line, "%s", esp);
169 printf ("(offline mode: enter name=value pairs on standard input)\n");
170 memset (tmp, 0, sizeof(tmp));
171 while((cp = fgets (tmp, 100, stdin)) != NULL) {
173 if (tmp[strlen(tmp)-1] == '\n')
174 tmp[strlen(tmp)-1] = '&';
176 length += strlen(tmp);
177 len = (length+1) * sizeof(char);
178 if ((line = (char *)realloc (line, len)) == NULL)
182 length = strlen(tmp);
183 len = (length+1) * sizeof(char);
184 if ((line = (char *)malloc (len)) == NULL)
186 memset (line, 0, len);
190 memset (tmp, 0, sizeof(tmp));
194 if (line[strlen(line)-1] == '&')
195 line[strlen(line)-1] = '\0';
199 * From now on all cgi variables are stored in the variable line
200 * and look like foo=bar&foobar=barfoo&foofoo=
203 if (cgiDebugLevel > 0) {
205 fprintf (stderr, "Received cgi input: %s\n", line);
207 printf ("<b>Received cgi input</b><br>\n<pre>\n--\n%s\n--\n</pre>\n\n", line);
210 for (cp=line; *cp; cp++)
215 for (numargs=1,cp=line; *cp; cp++)
216 if (*cp == '&' || *cp == ';' ) numargs++;
219 if (cgiDebugLevel > 0) {
221 fprintf (stderr, "%d cgi variables found.\n", numargs);
223 printf ("%d cgi variables found.<br>\n", numargs);
226 len = (numargs+1) * sizeof(s_var *);
227 if ((result = (s_var **)malloc (len)) == NULL)
229 memset (result, 0, len);
234 if ((ip = (char *)strchr(cp, '&')) != NULL) {
236 } else if ((ip = (char *)strchr(cp, ';')) != NULL) {
239 ip = cp + strlen(cp);
241 if ((esp=(char *)strchr(cp, '=')) == NULL) {
253 /* try to find out if there's already such a variable */
254 for (k=0; k<i && (strncmp (result[k]->name,cp, esp-cp) || !(strlen (result[k]->name) == esp-cp)); k++);
256 if (k == i) { /* No such variable yet */
257 if ((result[i] = (s_var *)malloc(sizeof(s_var))) == NULL)
259 if ((result[i]->name = (char *)malloc((esp-cp+1) * sizeof(char))) == NULL)
261 memset (result[i]->name, 0, esp-cp+1);
262 strncpy(result[i]->name, cp, esp-cp);
264 if ((result[i]->value = (char *)malloc((ip-esp+1) * sizeof(char))) == NULL)
266 memset (result[i]->value, 0, ip-esp+1);
267 strncpy(result[i]->value, cp, ip-esp);
268 result[i]->value = cgiDecodeString(result[i]->value);
271 fprintf (stderr, "%s: %s\n", result[i]->name, result[i]->value);
273 printf ("<h3>Variable %s</h3>\n<pre>\n%s\n</pre>\n\n", result[i]->name, result[i]->value);
276 } else { /* There is already such a name, suppose a mutiple field */
278 len = (strlen(result[k]->value)+(ip-esp)+2) * sizeof (char);
279 if ((sptr = (char *)malloc(len)) == NULL)
281 memset (sptr, 0, len);
282 sprintf (sptr, "%s\n", result[k]->value);
283 strncat(sptr, cp, ip-esp);
284 free(result[k]->value);
285 result[k]->value = cgiDecodeString (sptr);
295 * Read from stdin if no string is provided via CGI. Variables that
296 * doesn't have a value associated with it doesn't get stored.
304 vars = cgiReadVariables ();
305 cookies = cgiReadCookies ();
307 if (!vars && !cookies)
310 if ((res = (s_cgi *)malloc (sizeof (s_cgi))) == NULL)
313 res->cookies = cookies;
318 char *cgiGetValue (s_cgi *parms, const char *name)
322 if (!parms || !parms->vars)
324 for (i=0;parms->vars[i]; i++)
325 if (!strcmp(name,parms->vars[i]->name)) {
326 if (cgiDebugLevel > 0) {
328 fprintf (stderr, "%s found as %s\n", name, parms->vars[i]->value);
330 printf ("%s found as %s<br>\n", name, parms->vars[i]->value);
332 if (strlen(parms->vars[i]->value) > 0)
333 return parms->vars[i]->value;
339 fprintf (stderr, "%s not found\n", name);
341 printf ("%s not found<br>\n", name);
346 char **cgiGetVariables (s_cgi *parms)
352 if (!parms || !parms->vars)
354 for (i=0;parms->vars[i]; i++);
355 len = sizeof (char *) * ++i;
356 if ((res = (char **)malloc (len)) == NULL)
358 memset (res, 0, len);
359 for (i=0;parms->vars[i]; i++) {
360 len = strlen (parms->vars[i]->name) +1;
361 if ((res[i] = (char *)malloc (len)) == NULL)
363 memset (res[i], 0, len);
364 strcpy (res[i], parms->vars[i]->name);
369 void cgiRedirect (const char *url)
371 if (url && strlen(url)) {
372 printf ("Content-type: text/html\r\nContent-length: %d\r\n", 77+(strlen(url)*2));
373 printf ("Status: 302 Temporal Relocation\r\n");
374 printf ("Location: %s\r\n\r\n", url);
375 printf ("<html>\n<body>\nThe page has been moved to <a href=\"%s\">%s</a>\n</body>\n</html>\n", url, url);
379 void cgiFreeList (char **list)
383 for (i=0; list[i] != NULL; i++)
388 void cgiFree (s_cgi *parms)
395 for (i=0;parms->vars[i]; i++) {
396 if (parms->vars[i]->name)
397 free (parms->vars[i]->name);
398 if (parms->vars[i]->value)
399 free (parms->vars[i]->value);
400 free (parms->vars[i]);
404 if (parms->cookies) {
405 if (parms->cookies[0]->version)
406 free (parms->cookies[0]->version);
407 for (i=0;parms->cookies[i]; i++) {
408 if (parms->cookies[i]->name)
409 free (parms->cookies[i]->name);
410 if (parms->cookies[i]->value)
411 free (parms->cookies[i]->value);
412 if (parms->cookies[i]->path)
413 free (parms->cookies[i]->path);
414 if (parms->cookies[i]->domain)
415 free (parms->cookies[i]->domain);
416 free (parms->cookies[i]);
418 free (parms->cookies);
422 if (cgiHeaderString) {
423 free (cgiHeaderString);
424 cgiHeaderString = NULL;