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.
28 int cgiDebugLevel = 0;
29 int cgiDebugStderr = 1;
33 printf ("Content-type: text/html\n\n");
36 void cgiDebug (int level, int where)
39 cgiDebugLevel = level;
48 char *cgiDecodeString (char *text)
52 for (cp=text,xp=text; *cp; cp++) {
54 if (strchr("0123456789ABCDEFabcdef", *(cp+1))
55 && strchr("0123456789ABCDEFabcdef", *(cp+2))) {
57 *(cp+1) = toupper(*(cp+1));
59 *(cp+2) = toupper(*(cp+2));
60 *(xp) = (*(cp+1) >= 'A' ? *(cp+1) - 'A' + 10 : *(cp+1) - '0' ) * 16
61 + (*(cp+2) >= 'A' ? *(cp+2) - 'A' + 10 : *(cp+2) - '0');
74 * Read from stdin if no string is provided via CGI. Variables that
75 * doesn't have a value associated with it doesn't get stored.
82 char *cp, *ip, *esp, *sptr;
87 cp = getenv("REQUEST_METHOD");
88 ip = getenv("CONTENT_LENGTH");
90 if (cp && !strcmp(cp, "POST")) {
93 if ((line = (char *)malloc (length+2)) == NULL)
95 fgets(line, length+1, stdin);
98 } else if (cp && !strcmp(cp, "GET")) {
99 esp = getenv("QUERY_STRING");
100 if (esp && strlen(esp)) {
101 if ((line = (char *)malloc (strlen(esp)+2)) == NULL)
103 sprintf (line, "%s", esp);
108 printf ("(offline mode: enter name=value pairs on standard input)\n");
109 for (cp = fgets(tmp, 100, stdin); cp != NULL;
110 cp = fgets(tmp, 100, stdin) ) {
112 length += strlen(tmp);
113 if ((ip = (char *)malloc (length * sizeof(char))) == NULL)
115 memset(ip, 0, length);
117 if (line[strlen(line)-1] == '\n')
118 line[strlen(line)-1] = '&';
121 ip = strcat(ip, tmp);
129 if (line[strlen(line)-1] == '\n')
130 line[strlen(line)-1] = '\0';
134 * From now on all cgi variables are stored in the variable line
135 * and look like foo=bar&foobar=barfoo&foofoo=
138 if (cgiDebugLevel > 0)
140 fprintf (stderr, "Received cgi input: %s\n", line);
142 printf ("<b>Received cgi input</b><br>\n<pre>\n--\n%s\n--\n</pre>\n\n", line);
144 for (cp=line; *cp; cp++)
149 for (numargs=1,cp=line; *cp; cp++)
150 if (*cp == '&') numargs++;
153 if (cgiDebugLevel > 0)
155 fprintf (stderr, "%d cgi variables found.\n", numargs);
157 printf ("%d cgi variables found.<br>\n", numargs);
159 if ((result = (s_cgi **)malloc((numargs+1) * sizeof(s_cgi *))) == NULL)
161 memset (result, 0, (numargs+1) * sizeof(s_cgi *));
166 if ((ip = (char *)index(cp, '&')) != NULL) {
169 ip = cp + strlen(cp);
171 if ((esp=(char *)index(cp, '=')) == NULL) {
183 for (k=0; k<i && (strncmp(result[k]->name,cp, esp-cp)); k++);
184 /* try to find out if there's already such a variable */
185 if (k == i) { /* No such variable yet */
186 if ((result[i] = (s_cgi *)malloc(sizeof(s_cgi))) == NULL)
188 if ((result[i]->name = (char *)malloc((esp-cp+1) * sizeof(char))) == NULL)
190 memset (result[i]->name, 0, esp-cp+1);
191 strncpy(result[i]->name, cp, esp-cp);
193 if ((result[i]->value = (char *)malloc((ip-esp+1) * sizeof(char))) == NULL)
195 memset (result[i]->value, 0, ip-esp+1);
196 strncpy(result[i]->value, cp, ip-esp);
197 result[i]->value = cgiDecodeString(result[i]->value);
200 fprintf (stderr, "%s: %s\n", result[i]->name, result[i]->value);
202 printf ("<h3>Variable %s</h3>\n<pre>\n%s\n</pre>\n\n", result[i]->name, result[i]->value);
205 } else { /* There is already such a name, suppose a mutiple field */
206 if ((sptr = (char *)malloc((strlen(result[k]->value)+(ip-esp)+2)* sizeof(char))) == NULL)
208 memset (sptr, 0, strlen(result[k]->value)+(ip-esp)+2);
209 sprintf (sptr, "%s\n", result[k]->value);
211 strncat(sptr, cp, ip-esp);
212 free(result[k]->value);
213 result[k]->value = sptr;
221 char *cgiGetValue(s_cgi **parms, const char *var)
226 for (i=0;parms[i]; i++)
227 if (!strcmp(var,parms[i]->name)) {
228 if (cgiDebugLevel > 0)
230 fprintf (stderr, "%s found as %s\n", var, parms[i]->value);
232 printf ("%s found as %s<br>\n", var, parms[i]->value);
233 return parms[i]->value;
237 fprintf (stderr, "%s not found\n", var);
239 printf ("%s not found<br>\n", var);
244 void cgiRedirect (const char *url)
246 if (url && strlen(url)) {
247 printf ("Content-type: text/html\nContent-length: %d\n", 77+(strlen(url)*2));
248 printf ("Status: 302 Temporal Relocation\n");
249 printf ("Location: %s\n\n", url);
250 printf ("<html>\n<body>\nThe page has been moved to <a href=\"%s\">%s</a>\n</body>\n</html>\n", url, url);