applied MSVC compatibility patch from Dwight Kelly
[swftools.git] / lib / modules / swfcgi.c
1 /* swfcgi.c
2
3    Parse CGI parameters
4    
5    Partly adopted from Steven Grimm's uncgi tool and library.
6
7    Extension module for the rfxswf library.
8    Part of the swftools package.
9
10    Copyright (c) 2001 Rainer Böhme <rfxswf@reflex-studio.de>
11  
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.
16
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.
21
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 */
25
26
27 #define ishex(x) (((x) >= '0' && (x) <= '9') || ((x) >= 'a' && (x) <= 'f') || ((x) >= 'A' && (x) <= 'F'))
28
29 #define PREFIX "WWW_"
30
31 static int swf_htoi(unsigned char * s)
32 { int     value;
33   char    c;
34
35   c = s[0];
36   if (isupper(c)) c = tolower(c);
37   value = (c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10) * 16;
38
39   c = s[1];
40   if (isupper(c)) c = tolower(c);
41   value += c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10;
42
43   return (value);
44 }
45
46 static void swf_url_unescape(unsigned char * s)
47 { unsigned char  *dest = s;
48
49   while (s[0])
50   { if (s[0] == '+') dest[0] = ' ';
51     else
52     { if (s[0] == '%' && ishex(s[1]) && ishex(s[2]))
53       { dest[0] = (unsigned char) swf_htoi(s + 1);
54         s += 2;
55       }
56       else dest[0] = s[0];
57     }
58     s++;dest++;
59   }
60   dest[0] = 0;
61 }
62
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;
66
67   // fprintf(stderr,"%s\n",var);
68   swf_url_unescape(var);
69   // fprintf(stderr,"%s\n",var);
70
71   
72   buf = (unsigned char*)rfx_alloc(strlen((const char*)var) + sizeof(PREFIX) + 2);
73   if (!buf) return;
74
75   strcpy((char*)buf, (const char*)PREFIX);
76   if (var[0] == '_')
77   { strcpy((char*)&buf[sizeof(PREFIX)-1], (const char*)&var[1]);
78     despace = 1;
79   }
80   else strcpy((char*)&buf[sizeof(PREFIX)-1], (const char*)var);
81
82   for (c = buf; c[0] ; c++)
83   { if (c[0] == '.') c[0] = '_';
84     if (c[0] == '=') break;
85   }
86   if (!c[0]) c[1] = 0;
87   c[0] = 0;
88
89   if (despace && c[1])
90   { for (s = c+1; s[0] && isspace(s[0]); s++);
91     t = c + 1;
92     while (s[0])
93     { if (s[0] == '\r')
94       { got_cr = 1;
95         s++;
96         continue;
97       }
98       if (got_cr)
99       { if (s[0] != '\n')
100         *t++ = '\n';
101         got_cr = 0;
102       }
103       *t++ = *s++;
104     }
105     while (t > c && isspace(*--t));
106     t[1] = 0;
107   }
108
109   if ((oldval = (unsigned char*)getenv((const char*)buf)))
110   { newval = (unsigned char*)rfx_alloc(strlen((const char*)oldval) + strlen((const char *)buf) + strlen((const char*)&c[1]) + 3);
111     if (!newval) return;
112
113     c[0] = '=';
114     sprintf((char*)newval, "%s#%s", buf, oldval);
115     c[0] = 0;
116
117     oldval -= strlen((const char*)buf) + 1; // skip past VAR= 
118   }
119   else 
120   { c[0] = '=';
121     newval = buf;
122   }
123   
124   putenv((const char *)newval);
125         
126   if (oldval)
127   { rfx_free(oldval);
128     rfx_free(buf);
129   }
130 }
131
132 static void swf_scanquery(char * q)
133 { char *next = q;
134   if (!q) return;
135
136   while (next)
137   { next = strchr(q, '&');
138     if (next) next[0] = 0;
139     swf_cgienv((unsigned char*)q);
140     if (next)
141     { next[0] = '&';
142       q = next+1;
143     }
144   } 
145 }
146
147 char * swf_postread()
148 { char * buf = NULL;
149   int size = 0, sofar = 0, got;
150
151   buf = getenv("CONTENT_TYPE");
152   if ((!buf) || strcmp(buf, "application/x-www-form-urlencoded")) return NULL;
153
154   buf = getenv("CONTENT_LENGTH");
155   if (!buf) return NULL;
156         
157   size = atoi(buf);
158   buf = (char*)rfx_alloc(size + 1);
159   if (buf)
160   { do
161     { got = fread(buf + sofar, 1, size - sofar, stdin);
162       sofar += got;
163     } while (got && sofar < size);
164     buf[sofar] = 0;
165   }
166
167   return buf;
168 }
169
170 void swf_uncgi()
171 { char *query, *dupquery, *method;
172
173   query = getenv("QUERY_STRING");
174   if ((query) && strlen(query))
175   { dupquery = strdup(query);
176     swf_scanquery(dupquery);
177     rfx_free(dupquery);
178   }
179
180   method = getenv("REQUEST_METHOD");
181   if ((method) && ! strcmp(method, "POST"))
182   { query = swf_postread();
183     if ((query)&&(query[0]!=0)) swf_scanquery(query);
184     rfx_free(query);
185   }
186   
187 }
188       
189 #undef ishex
190 #undef PREFIX