File: cgiparam.cc

package info (click to toggle)
xapian-omega 1.0.7-3+lenny2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,424 kB
  • ctags: 744
  • sloc: sh: 9,112; cpp: 7,954; makefile: 245; perl: 119
file content (207 lines) | stat: -rw-r--r-- 4,963 bytes parent folder | download
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* cgiparam.cc: Parse CGI parameters
 *
 * Copyright 1999,2000,2001 BrightStation PLC
 * Copyright 2001 James Aylett
 * Copyright 2001 Ananova Ltd
 * Copyright 2002,2003 Olly Betts
 *
 * 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 */

#include <config.h>

#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <map>
#include <string>

using std::string;
using std::map;
using std::multimap;

multimap<string, string> cgi_params;

static void
add_param(string name, string val)
{
    size_t i = name.length();
    if (i > 2 && name[i - 2] == '.') {
	// An image button called B gives B.x and B.y parameters with the
	// coordinates of the click.  We throw away the ".y" one and trim
	// ".x" from the other.
	if (name[i - 1] == 'y') return;
	if (name[i - 1] == 'x') {
	    name.resize(i - 2);
	    // For an image button, the value of the CGI parameter is the
	    // coordinate of the click within the image - this is meaningless
	    // to us, so instead we turn "[ 2 ].x=NNN" into "[ 2 ]=2 ]", then
	    // below that gets turned into "[=2 ]".  The trailing non-numeric
	    // characters are ignored by atoi().
	    i = name.find(' ');
	    if (i != string::npos)
		val = name.substr(i + 1);
	    else {
		i = name.find_first_not_of("0123456789");
		if (i == string::npos) {
		    // For image buttons with entirely numeric names, make the
		    // value the name, and the name "#" - e.g. "2.x=NNN" becomes
		    // "#=2".
		    val = name;
		    name = '#';
		} else {
		    // Otherwise we just copy the name into the value, so
		    // ">.x=NNN" becomes ">=>".
		    val = name;
		}
	    }
	}
    }
    // Truncate at first space - convert `[ page two ]=2'
    // into `[=2'
    i = name.find(' ');
    if (i != string::npos) name.resize(i);
    cgi_params.insert(multimap<string, string>::value_type(name, val));
}

void
decode_argv(char **argv)
{
    cgi_params.clear();
    while (*argv) {
	char *p = strchr(*argv, '=');
	if (p) {
	    add_param(string(*argv, p), p + 1);
	} else {
	    add_param(*argv, "");
	}
	++argv;
    }
}

void
decode_test()
{
    cgi_params.clear();
    while (!feof(stdin)) {
	string name, val;
	bool had_equals = false;
	while (1) {
	    int ch = getchar();
	    if (ch == EOF || ch == '\n') {
		if (name.empty()) return; // end on blank line
		add_param(name, val);
		break;
	    }
	    if (had_equals) {
		val += char(ch);
	    } else if (ch == '=') {
		had_equals = true;
	    } else {
		name += char(ch);
	    }
	}
    }
}

void
decode_post()
{
    char *content_length;
    unsigned int cl = INT_MAX;
    
    content_length = getenv("CONTENT_LENGTH");
    /* Netscape Fasttrack server for NT doesn't give CONTENT_LENGTH */
    if (content_length) cl = atoi(content_length);

    cgi_params.clear();
    while (cl && (!feof(stdin))) {
	string name, val;
	bool had_equals = false;
	while (1) {
	    int ch = EOF;
	    if (cl) {
		ch = getchar();
		cl--;
	    }
	    if (ch == EOF || ch == '&') {
		if (!name.empty()) add_param(name, val);
		break;
	    }
	    char orig_ch = ch;
	    if (ch == '+')
		ch = ' ';
	    else if (ch == '%') {
		if (cl >= 2) {
		    cl -= 2;
		    int c = getchar();
		    ch = (c & 0xf) + ((c & 64) ? 9 : 0);
		    if (c != EOF) c = getchar();
		    ch = ch << 4;
		    ch |= (c & 0xf) + ((c & 64) ? 9 : 0);
	        }
	    }
	    if (had_equals) {
		val += char(ch);
	    } else if (orig_ch == '=') {
		had_equals = true;
	    } else {
		name += char(ch);
	    }
	}
    }
}

void
decode_get()
{
    const char *q_str = getenv("QUERY_STRING");
    if (!q_str) q_str = ""; // Hmm, sounds like a broken web server

    cgi_params.clear();
    char ch;
    do {
	string name, val;
	bool had_equals = false;
	while (1) {
	    ch = *q_str++;
	    if (ch == '\0' || ch == '&') {
		if (name.empty()) return; // end on blank line
		add_param(name, val);
		break;
	    }
	    char orig_ch = ch;
	    if (ch == '+')
		ch = ' ';
	    else if (ch == '%') {
		int c = *q_str++;
		ch = (c & 0xf) + ((c & 64) ? 9 : 0);
		if (c) c = *q_str++;
		ch = ch << 4;
		ch |= (c & 0xf) + ((c & 64) ? 9 : 0);
		if (!c) return; // unfinished % code
	    }
	    if (had_equals) {
		val += char(ch);
	    } else if (orig_ch == '=') {
		had_equals = true;
	    } else {
		name += char(ch);
	    }
	}
    } while (ch);
}