1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
/*
cgi.c - Some simple routines for cgi programming
Copyright (c) 1996-8 Martin Schulze <joey@infodrom.north.de>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <malloc.h>
#include <cgi.h>
int cgiDebugLevel = 0;
int cgiDebugStderr = 1;
void cgiHeader ()
{
printf ("Content-type: text/html\n\n");
}
void cgiDebug (int level, int where)
{
if (level > 0)
cgiDebugLevel = level;
else
cgiDebugLevel = 0;
if (where)
cgiDebugStderr = 0;
else
cgiDebugStderr = 1;
}
char *cgiDecodeString (char *text)
{
char *cp, *xp;
for (cp=text,xp=text; *cp; cp++) {
if (*cp == '%') {
if (strchr("0123456789ABCDEFabcdef", *(cp+1))
&& strchr("0123456789ABCDEFabcdef", *(cp+2))) {
if (islower(*(cp+1)))
*(cp+1) = toupper(*(cp+1));
if (islower(*(cp+2)))
*(cp+2) = toupper(*(cp+2));
*(xp) = (*(cp+1) >= 'A' ? *(cp+1) - 'A' + 10 : *(cp+1) - '0' ) * 16
+ (*(cp+2) >= 'A' ? *(cp+2) - 'A' + 10 : *(cp+2) - '0');
xp++;cp+=2;
}
} else {
*(xp++) = *cp;
}
}
bzero(xp, cp-xp);
return text;
}
/* cgiInit()
*
* Read from stdin if no string is provided via CGI. Variables that
* doesn't have a value associated with it doesn't get stored.
*/
s_cgi **cgiInit ()
{
int length;
char *line = NULL;
int numargs;
char *cp, *ip, *esp, *sptr;
s_cgi **result;
int i, k;
char tmp[101];
cp = getenv("REQUEST_METHOD");
ip = getenv("CONTENT_LENGTH");
if (cp && !strcmp(cp, "POST")) {
if (ip) {
length = atoi(ip);
if ((line = (char *)malloc (length+2)) == NULL)
return NULL;
fgets(line, length+1, stdin);
} else
return NULL;
} else if (cp && !strcmp(cp, "GET")) {
esp = getenv("QUERY_STRING");
if (esp && strlen(esp)) {
if ((line = (char *)malloc (strlen(esp)+2)) == NULL)
return NULL;
sprintf (line, "%s", esp);
} else
return NULL;
} else {
length = 0;
printf ("(offline mode: enter name=value pairs on standard input)\n");
for (cp = fgets(tmp, 100, stdin); cp != NULL;
cp = fgets(tmp, 100, stdin) ) {
if (strlen(tmp)) {
length += strlen(tmp);
if ((ip = (char *)malloc (length * sizeof(char))) == NULL)
return NULL;
bzero(ip, length);
if (line) {
if (line[strlen(line)-1] == '\n')
line[strlen(line)-1] = '&';
strcpy(ip, line);
}
ip = strcat(ip, tmp);
if (line)
free (line);
line = ip;
}
}
if (!line)
return NULL;
if (line[strlen(line)-1] == '\n')
line[strlen(line)-1] = '\0';
}
/*
* From now on all cgi variables are stored in the variable line
* and look like foo=bar&foobar=barfoo&foofoo=
*/
if (cgiDebugLevel > 0)
if (cgiDebugStderr)
fprintf (stderr, "Received cgi input: %s\n", line);
else
printf ("<b>Received cgi input</b><br>\n<pre>\n--\n%s\n--\n</pre>\n\n", line);
for (cp=line; *cp; cp++)
if (*cp == '+')
*cp = ' ';
if (strlen(line)) {
for (numargs=1,cp=line; *cp; cp++)
if (*cp == '&') numargs++;
} else
numargs = 0;
if (cgiDebugLevel > 0)
if (cgiDebugStderr)
fprintf (stderr, "%d cgi variables found.\n", numargs);
else
printf ("%d cgi variables found.<br>\n", numargs);
if ((result = (s_cgi **)malloc((numargs+1) * sizeof(s_cgi *))) == NULL)
return NULL;
bzero (result, (numargs+1) * sizeof(s_cgi *));
cp = line;
i=0;
while (*cp) {
if ((ip = (char *)index(cp, '&')) != NULL) {
*ip = '\0';
}else
ip = cp + strlen(cp);
if ((esp=(char *)index(cp, '=')) == NULL) {
cp = ++ip;
continue;
}
if (!strlen(esp)) {
cp = ++ip;
continue;
}
if (i<numargs) {
for (k=0; k<i && (strncmp(result[k]->name,cp, esp-cp)); k++);
/* try to find out if there's already such a variable */
if (k == i) { /* No such variable yet */
if ((result[i] = (s_cgi *)malloc(sizeof(s_cgi))) == NULL)
return NULL;
if ((result[i]->name = (char *)malloc((esp-cp+1) * sizeof(char))) == NULL)
return NULL;
bzero (result[i]->name, esp-cp+1);
strncpy(result[i]->name, cp, esp-cp);
cp = ++esp;
if ((result[i]->value = (char *)malloc((ip-esp+1) * sizeof(char))) == NULL)
return NULL;
bzero (result[i]->value, ip-esp+1);
strncpy(result[i]->value, cp, ip-esp);
result[i]->value = cgiDecodeString(result[i]->value);
if (cgiDebugLevel) {
if (cgiDebugStderr)
fprintf (stderr, "%s: %s\n", result[i]->name, result[i]->value);
else
printf ("<h3>Variable %s</h3>\n<pre>\n%s\n</pre>\n\n", result[i]->name, result[i]->value);
}
i++;
} else { /* There is already such a name, suppose a mutiple field */
if ((sptr = (char *)malloc((strlen(result[k]->value)+(ip-esp)+2)* sizeof(char))) == NULL)
return NULL;
bzero (sptr, strlen(result[k]->value)+(ip-esp)+2);
sprintf (sptr, "%s\n", result[k]->value);
cp = ++esp;
strncat(sptr, cp, ip-esp);
free(result[k]->value);
result[k]->value = sptr;
}
}
cp = ++ip;
}
return result;
}
char *cgiGetValue(s_cgi **parms, const char *var)
{
int i;
if (parms)
for (i=0;parms[i]; i++)
if (!strcmp(var,parms[i]->name)) {
if (cgiDebugLevel > 0)
if (cgiDebugStderr)
fprintf (stderr, "%s found as %s\n", var, parms[i]->value);
else
printf ("%s found as %s<br>\n", var, parms[i]->value);
return parms[i]->value;
}
if (cgiDebugLevel)
if (cgiDebugStderr)
fprintf (stderr, "%s not found\n", var);
else
printf ("%s not found<br>\n", var);
return NULL;
}
void cgiRedirect (const char *url)
{
if (url && strlen(url)) {
printf ("Content-type: text/html\nContent-length: %d\n", 77+(strlen(url)*2));
printf ("Status: 302 Temporal Relocation\n");
printf ("Location: %s\n\n", url);
printf ("<html>\n<body>\nThe page has been moved to <a href=\"%s\">%s</a>\n</body>\n</html>\n", url, url);
}
}
/*
* Local variables:
* c-indent-level: 4
* c-basic-offset: 4
* tab-width: 8
* End:
*/
|