2 cgi.c - Some simple routines for cgi programming
3 Copyright (c) 1996-8 Martin Schulze <joey@infodrom.north.de>
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;
32 printf ("Content-type: text/html\n\n");
35 void cgiDebug (int level, int where)
38 cgiDebugLevel = level;
47 char *cgiDecodeString (char *text)
51 for (cp=text,xp=text; *cp; cp++) {
53 if (strchr("0123456789ABCDEFabcdef", *(cp+1))
54 && strchr("0123456789ABCDEFabcdef", *(cp+2))) {
56 *(cp+1) = toupper(*(cp+1));
58 *(cp+2) = toupper(*(cp+2));
59 *(xp) = (*(cp+1) >= 'A' ? *(cp+1) - 'A' + 10 : *(cp+1) - '0' ) * 16
60 + (*(cp+2) >= 'A' ? *(cp+2) - 'A' + 10 : *(cp+2) - '0');
73 * Read from stdin if no string is provided via CGI. Variables that
74 * doesn't have a value associated with it doesn't get stored.
86 line = getenv("CONTENT_LENGTH");
90 if ((line = (char *)malloc (length+2)) == NULL)
92 fgets(line, length+1, stdin);
94 if (!getenv("REQUEST_METHOD")) {
96 printf ("(offline mode: enter name=value pairs on standard input)\n");
97 for (cp = fgets(tmp, 100, stdin); cp != NULL;
98 cp = fgets(tmp, 100, stdin) ) {
100 length += strlen(tmp);
101 if ((ip = (char *)malloc (length * sizeof(char))) == NULL)
105 if (line[strlen(line)-1] == '\n')
106 line[strlen(line)-1] = '&';
109 ip = strcat(ip, tmp);
115 if (line[strlen(line)-1] == '\n')
116 line[strlen(line)-1] = '\0';
120 /* line now contains all values stored like foo=bar&foobar=barfoo&foofoo= */
122 if (cgiDebugLevel > 0)
124 fprintf (stderr, "Received cgi input: %s\n", line);
126 printf ("<b>Received cgi input</b><br>\n<pre>\n--\n%s\n--\n</pre>\n\n", line);
128 for (cp=line; *cp; cp++)
133 for (numargs=1,cp=line; *cp; cp++)
134 if (*cp == '&') numargs++;
137 if (cgiDebugLevel > 0)
139 fprintf (stderr, "%d cgi variables found.\n", numargs);
141 printf ("%d cgi variables found.<br>\n", numargs);
143 if ((result = (s_cgi **)malloc((numargs+1) * sizeof(s_cgi *))) == NULL)
145 bzero (result, (numargs+1) * sizeof(s_cgi *));
150 if ((ip = (char *)index(cp, '&')) != NULL) {
153 ip = cp + strlen(cp);
155 if ((esp=(char *)index(cp, '=')) == NULL) {
166 if ((result[i] = (s_cgi *)malloc(sizeof(s_cgi))) == NULL)
168 if ((result[i]->name = (char *)malloc((esp-cp+1) * sizeof(char))) == NULL)
170 bzero (result[i]->name, esp-cp+1);
171 strncpy(result[i]->name, cp, esp-cp);
173 if ((result[i]->value = (char *)malloc((ip-esp+1) * sizeof(char))) == NULL)
175 bzero (result[i]->value, ip-esp+1);
176 strncpy(result[i]->value, cp, ip-esp);
177 result[i]->value = cgiDecodeString(result[i]->value);
180 fprintf (stderr, "%s: %s\n", result[i]->name, result[i]->value);
182 printf ("<h3>Variable %s</h3>\n<pre>\n%s\n</pre>\n\n", result[i]->name, result[i]->value);
191 char *cgiGetValue(s_cgi **parms, const char *var)
196 for (i=0;parms[i]; i++)
197 if (!strcmp(var,parms[i]->name))
198 return parms[i]->value;