2 * @brief functions to deal with CGI parameters
4 /* Copyright 1999,2000,2001 BrightStation PLC
5 * Copyright 2001 James Aylett
6 * Copyright 2001 Ananova Ltd
7 * Copyright 2002,2003,2009,2011,2015,2017 Olly Betts
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
29 #include "urldecode.h"
30 #include "urlencode.h"
41 multimap
<string
, string
> cgi_params
;
44 add_param(string name
, string val
)
46 size_t i
= name
.length();
47 if (i
> 2 && name
[i
- 2] == '.') {
48 // An image button called B gives B.x and B.y parameters with the
49 // coordinates of the click. We throw away the ".y" one and trim
50 // ".x" from the other.
51 if (name
[i
- 1] == 'y') return;
52 if (name
[i
- 1] == 'x') {
54 // For an image button, the value of the CGI parameter is the
55 // coordinate of the click within the image - this is meaningless
56 // to us, so instead we turn "[ 2 ].x=NNN" into "[ 2 ]=2 ]", then
57 // below that gets turned into "[=2 ]". The trailing non-numeric
58 // characters are ignored by atoi().
59 i
= name
.find_first_of(" \t");
60 if (i
!= string::npos
)
61 val
.assign(name
, i
+ 1, string::npos
);
63 i
= name
.find_first_not_of("0123456789");
64 if (i
== string::npos
) {
65 // For image buttons with entirely numeric names, make the
66 // value the name, and the name "#" - e.g. "2.x=NNN" becomes
71 // Otherwise we just copy the name into the value, so
72 // ">.x=NNN" becomes ">=>".
78 // Truncate at first space or tab - convert '[ page two ]=2'
80 i
= name
.find_first_of(" \t");
81 if (i
!= string::npos
) name
.resize(i
);
82 cgi_params
.insert(multimap
<string
, string
>::value_type(name
, val
));
86 CGIParameterHandler::operator()(const string
& var
, const string
& val
) const
92 decode_argv(char **argv
)
96 char *p
= strchr(*argv
, '=');
98 add_param(string(*argv
, p
), p
+ 1);
100 add_param(*argv
, "");
110 while (!feof(stdin
)) {
112 bool had_equals
= false;
115 if (ch
== EOF
|| ch
== '\n') {
116 if (name
.empty()) return; // end on blank line
117 add_param(name
, val
);
122 } else if (ch
== '=') {
134 char *content_length
;
137 content_length
= getenv("CONTENT_LENGTH");
138 /* Netscape Fasttrack server for NT doesn't give CONTENT_LENGTH */
139 if (content_length
) cl
= atoi(content_length
);
142 url_decode(CGIParameterHandler(), StdinItor(cl
), StdinItor());
149 const char *q_str
= getenv("QUERY_STRING");
150 // If QUERY_STRING isn't set, that's pretty broken, but don't segfault.
152 url_decode(CGIParameterHandler(), CStringItor(q_str
), CStringItor());