5 Partly adopted from Steven Grimm's uncgi tool and library.
7 Extension module for the rfxswf library.
8 Part of the swftools package.
10 Copyright (c) 2001 Rainer Böhme <rfxswf@reflex-studio.de>
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
27 #define ishex(x) (((x) >= '0' && (x) <= '9') || ((x) >= 'a' && (x) <= 'f') || ((x) >= 'A' && (x) <= 'F'))
31 static int swf_htoi(unsigned char * s)
36 if (isupper(c)) c = tolower(c);
37 value = (c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10) * 16;
40 if (isupper(c)) c = tolower(c);
41 value += c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10;
46 static void swf_url_unescape(unsigned char * s)
47 { unsigned char *dest = s;
50 { if (s[0] == '+') dest[0] = ' ';
52 { if (s[0] == '%' && ishex(s[1]) && ishex(s[2]))
53 { dest[0] = (unsigned char) swf_htoi(s + 1);
63 static void swf_cgienv(unsigned char * var)
64 { unsigned char *buf, *c, *s, *t, *oldval = NULL, *newval;
65 int despace = 0, got_cr = 0;
67 // fprintf(stderr,"%s\n",var);
68 swf_url_unescape(var);
69 // fprintf(stderr,"%s\n",var);
72 buf = (unsigned char*)malloc(strlen(var) + sizeof(PREFIX) + 2);
77 { strcpy(&buf[sizeof(PREFIX)-1], &var[1]);
80 else strcpy(&buf[sizeof(PREFIX)-1], var);
82 for (c = buf; c[0] ; c++)
83 { if (c[0] == '.') c[0] = '_';
84 if (c[0] == '=') break;
90 { for (s = c+1; s[0] && isspace(s[0]); s++);
105 while (t > c && isspace(*--t));
109 if ((oldval = getenv(buf)))
110 { newval = (unsigned char*)malloc(strlen(oldval) + strlen(buf) + strlen(&c[1]) + 3);
114 sprintf(newval, "%s#%s", buf, oldval);
117 oldval -= strlen(buf) + 1; // skip past VAR=
132 static void swf_scanquery(char * q)
137 { next = strchr(q, '&');
138 if (next) next[0] = 0;
147 char * swf_postread()
149 int size = 0, sofar = 0, got;
151 buf = getenv("CONTENT_TYPE");
152 if ((!buf) || strcmp(buf, "application/x-www-form-urlencoded")) return NULL;
154 buf = getenv("CONTENT_LENGTH");
155 if (!buf) return NULL;
158 buf = (unsigned char*)malloc(size + 1);
161 { got = fread(buf + sofar, 1, size - sofar, stdin);
163 } while (got && sofar < size);
171 { char *query, *dupquery, *method;
173 query = getenv("QUERY_STRING");
174 if ((query) && strlen(query))
175 { dupquery = strdup(query);
176 swf_scanquery(dupquery);
180 method = getenv("REQUEST_METHOD");
181 if ((method) && ! strcmp(method, "POST"))
182 { query = swf_postread();
183 if ((query)&&(query[0]!=0)) swf_scanquery(query);