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 */
28 #include "../rfxswf.h"
30 #define ishex(x) (((x) >= '0' && (x) <= '9') || ((x) >= 'a' && (x) <= 'f') || ((x) >= 'A' && (x) <= 'F'))
34 static int swf_htoi(unsigned char * s)
39 if (isupper(c)) c = tolower(c);
40 value = (c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10) * 16;
43 if (isupper(c)) c = tolower(c);
44 value += c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10;
49 static void swf_url_unescape(unsigned char * s)
50 { unsigned char *dest = s;
53 { if (s[0] == '+') dest[0] = ' ';
55 { if (s[0] == '%' && ishex(s[1]) && ishex(s[2]))
56 { dest[0] = (unsigned char) swf_htoi(s + 1);
66 static void swf_cgienv(unsigned char * var)
67 { unsigned char *buf, *c, *s, *t, *oldval = NULL, *newval;
68 int despace = 0, got_cr = 0;
70 // fprintf(stderr,"%s\n",var);
71 swf_url_unescape(var);
72 // fprintf(stderr,"%s\n",var);
75 buf = (unsigned char*)rfx_alloc(strlen((const char*)var) + sizeof(PREFIX) + 2);
78 strcpy((char*)buf, (const char*)PREFIX);
80 { strcpy((char*)&buf[sizeof(PREFIX)-1], (const char*)&var[1]);
83 else strcpy((char*)&buf[sizeof(PREFIX)-1], (const char*)var);
85 for (c = buf; c[0] ; c++)
86 { if (c[0] == '.') c[0] = '_';
87 if (c[0] == '=') break;
93 { for (s = c+1; s[0] && isspace(s[0]); s++);
108 while (t > c && isspace(*--t));
112 if ((oldval = (unsigned char*)getenv((const char*)buf)))
113 { newval = (unsigned char*)rfx_alloc(strlen((const char*)oldval) + strlen((const char *)buf) + strlen((const char*)&c[1]) + 3);
117 sprintf((char*)newval, "%s#%s", buf, oldval);
120 oldval -= strlen((const char*)buf) + 1; // skip past VAR=
127 putenv((char *)newval);
135 static void swf_scanquery(char * q)
140 { next = strchr(q, '&');
141 if (next) next[0] = 0;
142 swf_cgienv((unsigned char*)q);
150 char * swf_postread()
152 int size = 0, sofar = 0, got;
154 buf = getenv("CONTENT_TYPE");
155 if ((!buf) || strcmp(buf, "application/x-www-form-urlencoded")) return NULL;
157 buf = getenv("CONTENT_LENGTH");
158 if (!buf) return NULL;
161 buf = (char*)rfx_alloc(size + 1);
164 { got = fread(buf + sofar, 1, size - sofar, stdin);
166 } while (got && sofar < size);
174 { char *query, *dupquery, *method;
176 query = getenv("QUERY_STRING");
177 if ((query) && strlen(query))
178 { dupquery = strdup(query);
179 swf_scanquery(dupquery);
183 method = getenv("REQUEST_METHOD");
184 if ((method) && ! strcmp(method, "POST"))
185 { query = swf_postread();
186 if ((query)&&(query[0]!=0)) swf_scanquery(query);