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 file is distributed under the GPL, see file COPYING for details
17 #define ishex(x) (((x) >= '0' && (x) <= '9') || ((x) >= 'a' && (x) <= 'f') || ((x) >= 'A' && (x) <= 'F'))
21 static int swf_htoi(unsigned char * s)
26 if (isupper(c)) c = tolower(c);
27 value = (c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10) * 16;
30 if (isupper(c)) c = tolower(c);
31 value += c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10;
36 static void swf_url_unescape(unsigned char * s)
37 { unsigned char *dest = s;
40 { if (s[0] == '+') dest[0] = ' ';
42 { if (s[0] == '%' && ishex(s[1]) && ishex(s[2]))
43 { dest[0] = (unsigned char) swf_htoi(s + 1);
53 static void swf_cgienv(unsigned char * var)
54 { unsigned char *buf, *c, *s, *t, *oldval = NULL, *newval;
55 int despace = 0, got_cr = 0;
57 // fprintf(stderr,"%s\n",var);
58 swf_url_unescape(var);
59 // fprintf(stderr,"%s\n",var);
62 buf = (unsigned char*)malloc(strlen(var) + sizeof(PREFIX) + 2);
67 { strcpy(&buf[sizeof(PREFIX)-1], &var[1]);
70 else strcpy(&buf[sizeof(PREFIX)-1], var);
72 for (c = buf; c[0] ; c++)
73 { if (c[0] == '.') c[0] = '_';
74 if (c[0] == '=') break;
80 { for (s = c+1; s[0] && isspace(s[0]); s++);
95 while (t > c && isspace(*--t));
99 if ((oldval = getenv(buf)))
100 { newval = (unsigned char*)malloc(strlen(oldval) + strlen(buf) + strlen(&c[1]) + 3);
104 sprintf(newval, "%s#%s", buf, oldval);
107 oldval -= strlen(buf) + 1; // skip past VAR=
122 static void swf_scanquery(char * q)
127 { next = strchr(q, '&');
128 if (next) next[0] = 0;
137 char * swf_postread()
139 int size = 0, sofar = 0, got;
141 buf = getenv("CONTENT_TYPE");
142 if ((!buf) || strcmp(buf, "application/x-www-form-urlencoded")) return NULL;
144 buf = getenv("CONTENT_LENGTH");
145 if (!buf) return NULL;
148 buf = (unsigned char*)malloc(size + 1);
151 { got = fread(buf + sofar, 1, size - sofar, stdin);
153 } while (got && sofar < size);
161 { char *query, *dupquery, *method;
163 query = getenv("QUERY_STRING");
164 if ((query) && strlen(query))
165 { dupquery = strdup(query);
166 swf_scanquery(dupquery);
170 method = getenv("REQUEST_METHOD");
171 if ((method) && ! strcmp(method, "POST"))
172 { query = swf_postread();
173 if ((query)&&(query[0]!=0)) swf_scanquery(query);