File: swfcgi.c

package info (click to toggle)
swftools 0.9.2%2Bds1-3
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 9,488 kB
  • sloc: ansic: 122,576; sh: 8,494; cpp: 8,020; yacc: 2,260; lisp: 904; makefile: 581; python: 304
file content (193 lines) | stat: -rw-r--r-- 4,499 bytes parent folder | download | duplicates (3)
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
/* swfcgi.c

   Parse CGI parameters
   
   Partly adopted from Steven Grimm's uncgi tool and library.

   Extension module for the rfxswf library.
   Part of the swftools package.

   Copyright (c) 2001 Rainer Bhme <rfxswf@reflex-studio.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-1307  USA */

#include <stdlib.h>
#include <stdio.h>
#include "../rfxswf.h"

#define ishex(x) (((x) >= '0' && (x) <= '9') || ((x) >= 'a' && (x) <= 'f') || ((x) >= 'A' && (x) <= 'F'))

#define PREFIX "WWW_"

static int swf_htoi(unsigned char * s)
{ int     value;
  char    c;

  c = s[0];
  if (isupper(c)) c = tolower(c);
  value = (c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10) * 16;

  c = s[1];
  if (isupper(c)) c = tolower(c);
  value += c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10;

  return (value);
}

static void swf_url_unescape(unsigned char * s)
{ unsigned char  *dest = s;

  while (s[0])
  { if (s[0] == '+') dest[0] = ' ';
    else
    { if (s[0] == '%' && ishex(s[1]) && ishex(s[2]))
      { dest[0] = (unsigned char) swf_htoi(s + 1);
        s += 2;
      }
      else dest[0] = s[0];
    }
    s++;dest++;
  }
  dest[0] = 0;
}

static void swf_cgienv(unsigned char * var)
{ unsigned char *buf, *c, *s, *t, *oldval = NULL, *newval;
  int despace = 0, got_cr = 0;

  // fprintf(stderr,"%s\n",var);
  swf_url_unescape(var);
  // fprintf(stderr,"%s\n",var);

  
  buf = (unsigned char*)rfx_alloc(strlen((const char*)var) + sizeof(PREFIX) + 2);
  if (!buf) return;

  strcpy((char*)buf, (const char*)PREFIX);
  if (var[0] == '_')
  { strcpy((char*)&buf[sizeof(PREFIX)-1], (const char*)&var[1]);
    despace = 1;
  }
  else strcpy((char*)&buf[sizeof(PREFIX)-1], (const char*)var);

  for (c = buf; c[0] ; c++)
  { if (c[0] == '.') c[0] = '_';
    if (c[0] == '=') break;
  }
  if (!c[0]) c[1] = 0;
  c[0] = 0;

  if (despace && c[1])
  { for (s = c+1; s[0] && isspace(s[0]); s++);
    t = c + 1;
    while (s[0])
    { if (s[0] == '\r')
      { got_cr = 1;
        s++;
        continue;
      }
      if (got_cr)
      { if (s[0] != '\n')
        *t++ = '\n';
        got_cr = 0;
      }
      *t++ = *s++;
    }
    while (t > c && isspace(*--t));
    t[1] = 0;
  }

  if ((oldval = (unsigned char*)getenv((const char*)buf)))
  { newval = (unsigned char*)rfx_alloc(strlen((const char*)oldval) + strlen((const char *)buf) + strlen((const char*)&c[1]) + 3);
    if (!newval) return;

    c[0] = '=';
    sprintf((char*)newval, "%s#%s", buf, oldval);
    c[0] = 0;

    oldval -= strlen((const char*)buf) + 1; // skip past VAR= 
  }
  else 
  { c[0] = '=';
    newval = buf;
  }
  
  putenv((char *)newval);
        
  if (oldval)
  { rfx_free(oldval);
    rfx_free(buf);
  }
}

static void swf_scanquery(char * q)
{ char *next = q;
  if (!q) return;

  while (next)
  { next = strchr(q, '&');
    if (next) next[0] = 0;
    swf_cgienv((unsigned char*)q);
    if (next)
    { next[0] = '&';
      q = next+1;
    }
  } 
}

char * swf_postread()
{ char * buf = NULL;
  int size = 0, sofar = 0, got;

  buf = getenv("CONTENT_TYPE");
  if ((!buf) || strcmp(buf, "application/x-www-form-urlencoded")) return NULL;

  buf = getenv("CONTENT_LENGTH");
  if (!buf) return NULL;
        
  size = atoi(buf);
  buf = (char*)rfx_alloc(size + 1);
  if (buf)
  { do
    { got = fread(buf + sofar, 1, size - sofar, stdin);
      sofar += got;
    } while (got && sofar < size);
    buf[sofar] = 0;
  }

  return buf;
}

void swf_uncgi()
{ char *query, *dupquery, *method;

  query = getenv("QUERY_STRING");
  if ((query) && strlen(query))
  { dupquery = strdup(query);
    swf_scanquery(dupquery);
    rfx_free(dupquery);
  }

  method = getenv("REQUEST_METHOD");
  if ((method) && ! strcmp(method, "POST"))
  { query = swf_postread();
    if ((query)&&(query[0]!=0)) swf_scanquery(query);
    rfx_free(query);
  }
  
}
      
#undef ishex
#undef PREFIX