source: trunk/gsdl/src/recpt/cgiutils.cpp@ 533

Last change on this file since 533 was 533, checked in by sjboddie, 25 years ago

added GPL notice

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 13.2 KB
Line 
1/**********************************************************************
2 *
3 * cgiutils.cpp -- general cgi utilities
4 * Copyright (C) 1999 The New Zealand Digital Library Project
5 *
6 * A component of the Greenstone digital library software
7 * from the New Zealand Digital Library Project at the
8 * University of Waikato, New Zealand.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 * $Id: cgiutils.cpp 533 1999-09-07 04:57:01Z sjboddie $
25 *
26 *********************************************************************/
27
28/*
29 $Log$
30 Revision 1.8 1999/09/07 04:56:53 sjboddie
31 added GPL notice
32
33 Revision 1.7 1999/08/25 22:27:13 sjboddie
34 prevented cgi_safe from converting '+' and '-'. It was causing problems
35 with query strings containing spaces. The space was being converted
36 to '+', then %2b, then %xx2b over time when saved in compressed args.
37 I hope this won't cause problems elsewhere...
38
39 Revision 1.6 1999/07/11 01:05:19 rjmcnab
40 Stored origin of cgiarg with argument.
41
42 Revision 1.5 1999/06/26 01:08:36 rjmcnab
43 Added encoding and decoding of multibyte compresesd arguments.
44
45 Revision 1.4 1999/06/08 22:03:43 sjboddie
46 query string is now made cgi safe before being added to compressed args
47
48 Revision 1.3 1999/02/08 01:28:00 rjmcnab
49
50 Got the receptionist producing something using the statusaction.
51
52 Revision 1.2 1999/02/05 10:42:43 rjmcnab
53
54 Continued working on receptionist
55
56 Revision 1.1 1999/01/08 08:40:56 rjmcnab
57
58 Moved from lib directory.
59
60 Revision 1.1 1999/01/08 03:57:45 rjmcnab
61
62 Initial revision
63
64 */
65
66
67#include "cgiutils.h"
68
69
70static unsigned short hexdigit (unsigned short c) {
71 if (c >= '0' && c <= '9') return (c-'0');
72 if (c >= 'a' && c <= 'f') return (c-'a'+10);
73 if (c >= 'A' && c <= 'F') return (c-'A'+10);
74 return c;
75}
76
77
78static void c2hex (unsigned short c, text_t &t) {
79 t.clear();
80
81 if (c >= 256) {
82 t = "20"; // ' '
83 return;
84 }
85
86 unsigned short o1, o2;
87
88 o1 = (c/16) % 16;
89 o2 = c % 16;
90 if (o1 >= 10) o1 += 'a' - 10;
91 else o1 += '0';
92 if (o2 >= 10) o2 += 'a' - 10;
93 else o2 += '0';
94
95 t.push_back(o1);
96 t.push_back(o2);
97}
98
99// convert %xx and + to their appropriate equivalents
100void decode_cgi_arg (text_t &argstr) {
101 text_t::iterator in = argstr.begin();
102 text_t::iterator out = in;
103 text_t::iterator end = argstr.end();
104
105 while (in != end) {
106 if (*in == '+') *out = ' ';
107
108 else if (*in == '%') {
109 unsigned short c = '%';
110 in++;
111 if (in != end) {
112 c = hexdigit (*in);
113 in++;
114 }
115 if (in != end && c < 16) { // sanity check on the previous character
116 c = c*16 + hexdigit (*in);
117 }
118
119 *out = c;
120 } else *out = *in;
121
122 if (in != end) in++;
123 out++;
124 }
125
126 // remove the excess characters
127 argstr.erase (out, end);
128}
129
130
131// split up the cgi arguments
132void split_cgi_args (text_t argstr, cgiargsclass &args) {
133 args.clear();
134
135 text_t::iterator here = argstr.begin();
136 text_t::iterator end = argstr.end();
137 text_t key, value;
138
139 // extract out the key=value pairs
140 while (here != end) {
141 // get the next key and value pair
142 here = getdelimitstr (here, end, '=', key);
143 here = getdelimitstr (here, end, '&', value);
144
145 // convert %xx and + to their appropriate equivalents
146 decode_cgi_arg (value);
147 value.setencoding(1); // other encoding
148 // store this key=value pair
149 if (!key.empty()) args.setarg (key, value, cgiarg_t::cgi_arg);
150 }
151}
152
153text_t cgi_safe (const text_t &intext) {
154 text_t outtext;
155
156 text_t::const_iterator here = intext.begin ();
157 text_t::const_iterator end = intext.end ();
158 unsigned short c;
159 text_t ttmp;
160
161 while (here != end) {
162 c = *here;
163 if (((c >= 'a') && (c <= 'z')) ||
164 ((c >= 'A') && (c <= 'Z')) ||
165 ((c >= '0') && (c <= '9')) ||
166 (c == '-') || (c == '+')) {
167 // alphanumeric character
168 outtext.push_back(c);
169 } else if (c == ' ') {
170 // space
171 outtext.push_back('+');
172 } else {
173 // everything else
174 outtext.push_back('%');
175 c2hex(c, ttmp);
176 outtext += ttmp;
177 }
178
179 here++;
180 }
181
182 return outtext;
183}
184
185
186
187
188static text_t::const_iterator get_next_save_arg (text_t::const_iterator first,
189 text_t::const_iterator last,
190 text_t &argname) {
191 first = getdelimitstr (first, last, '-', argname);
192 return first;
193}
194
195
196// check_save_conf_str checks the configuration string for
197// the saved args and makes sure it does not conflict with
198// the information about the arguments. If an error is encountered
199// it will return false and the program should not produce any
200// output.
201bool check_save_conf_str (const text_t &saveconf,
202 const cgiargsinfoclass &argsinfo,
203 ostream &logout) {
204 outconvertclass text_t2ascii;
205
206 text_tset argsset;
207 text_t::const_iterator saveconfhere = saveconf.begin ();
208 text_t::const_iterator saveconfend = saveconf.end ();
209 text_t argname;
210 const cgiarginfo *info;
211
212 // first check to make sure all saved arguments can be saved
213
214 while (saveconfhere != saveconfend) {
215 saveconfhere = get_next_save_arg (saveconfhere, saveconfend, argname);
216
217 if (!argname.empty()) {
218 // save the argument name for later
219 argsset.insert (argname);
220
221 // check the argument
222 info = argsinfo.getarginfo (argname);
223 if (info == NULL) {
224 logout << text_t2ascii << "Error: the cgi argument \"" << argname
225 << "\" is used in the configuration string for the\n"
226 << "saved arguments but does not exist as a valid argument.\n\n";
227 return false;
228 }
229 if (info->savedarginfo == cgiarginfo::mustnot) {
230 logout << text_t2ascii << "Error: the cgi argument \"" << argname
231 << "\" is used in the configuration string for the\n"
232 << "saved arguments but has been specified as an argument whose\n"
233 << "state must not be saved.\n\n";
234 return false;
235 }
236 }
237 }
238
239
240 // next check that all saved arguments that should be saved
241 // are saved
242 cgiargsinfoclass::const_iterator argsinfohere = argsinfo.begin ();
243 cgiargsinfoclass::const_iterator argsinfoend = argsinfo.end ();
244
245 while (argsinfohere != argsinfoend) {
246 if (((*argsinfohere).second.savedarginfo == cgiarginfo::must) &&
247 (argsset.find((*argsinfohere).second.shortname) == argsset.end())) {
248 logout << text_t2ascii << "Error: the cgi argument \""
249 << (*argsinfohere).second.shortname << "\" was specified as needing to\n"
250 << "be save but was not listed in the saved arguments.\n\n";
251 return false;
252 }
253
254 argsinfohere++;
255 }
256
257 return true; // made it, no clashes
258}
259
260
261// create_save_conf_str will create a configuration string
262// based on the information in argsinfo. This method of configuration
263// is not recomended as small changes can produce large changes in
264// the resulting configuration string (for instance a totally different
265// ordering). Only arguments which "must" be saved are included in
266// the resulting string.
267text_t create_save_conf_str (const cgiargsinfoclass &argsinfo,
268 ostream &/*logout*/) {
269 cgiargsinfoclass::const_iterator argsinfohere = argsinfo.begin ();
270 cgiargsinfoclass::const_iterator argsinfoend = argsinfo.end ();
271 text_t saveconf;
272 bool first = true;
273
274 while (argsinfohere != argsinfoend) {
275 // save this argument if it must be saved
276 if ((*argsinfohere).second.savedarginfo == cgiarginfo::must) {
277 if (!first) saveconf.push_back ('-');
278 else first = false;
279 saveconf += (*argsinfohere).second.shortname;
280 }
281
282 argsinfohere++;
283 }
284
285 return saveconf;
286}
287
288
289// expand_save_args will expand the saved arguments based
290// on saveconf placing the results in args if they are not
291// already defined. If it encounters an error it will return false
292// and output more information to logout.
293bool expand_save_args (const cgiargsinfoclass &argsinfo,
294 const text_t &saveconf,
295 cgiargsclass &args,
296 ostream &logout) {
297 outconvertclass text_t2ascii;
298
299 text_t *arg_e = args.getarg("e");
300 if (arg_e == NULL) return true; // no compressed arguments
301 if (arg_e->empty()) return true; // no compressed arguments
302
303 text_t argname, argvalue;
304 const cgiarginfo *argnameinfo;
305
306 text_t::const_iterator saveconfhere = saveconf.begin();
307 text_t::const_iterator saveconfend = saveconf.end();
308
309 text_t::iterator arg_ehere = arg_e->begin();
310 text_t::iterator arg_eend = arg_e->end();
311 while (saveconfhere != saveconfend && arg_ehere != arg_eend) {
312 saveconfhere = get_next_save_arg (saveconfhere, saveconfend, argname);
313
314 if (!argname.empty()) {
315 // found another entry
316 argnameinfo = argsinfo.getarginfo (argname);
317
318 if (argnameinfo == NULL) {
319 // no information about the argument could be found
320 // we can't keep going because we don't know whether
321 // this argument is a single or multiple character value
322 logout << text_t2ascii << "Error: the cgi argument \"" << argname
323 << "\" was specified as being a compressed argument\n"
324 << "but no information about it could be found within the "
325 << "cgiargsinfoclass.\n";
326 return false;
327
328 } else {
329
330 // found the argument information
331 if (argnameinfo->multiplechar) {
332 arg_ehere = getdelimitstr (arg_ehere, arg_eend, '-', argvalue);
333 argvalue.setencoding(1); // other encoding
334 if (!argvalue.empty()) args.setdefaultarg (argname, argvalue, cgiarg_t::compressed_arg);
335 } else {
336 args.setdefaultcarg (argname,*arg_ehere, cgiarg_t::compressed_arg);
337 arg_ehere++;
338 }
339 }
340 }
341 }
342
343 return true;
344}
345
346
347// adds the default values for those arguments which have not
348// been specified
349void add_default_args (const cgiargsinfoclass &argsinfo,
350 cgiargsclass &args,
351 ostream &/*logout*/) {
352 cgiargsinfoclass::const_iterator argsinfohere = argsinfo.begin ();
353 cgiargsinfoclass::const_iterator argsinfoend = argsinfo.end ();
354
355 while (argsinfohere != argsinfoend) {
356 if ((*argsinfohere).second.defaultstatus != cgiarginfo::none) {
357 args.setdefaultarg ((*argsinfohere).second.shortname,
358 (*argsinfohere).second.argdefault, cgiarg_t::default_arg);
359 }
360 argsinfohere++;
361 }
362}
363
364
365// compress_save_args will compress the arguments and return
366// them in compressed_args. If an error was encountered
367// compressed_args will be set to to "", an error will be
368// written to logout, and the function will return false.
369bool compress_save_args (const cgiargsinfoclass &argsinfo,
370 const text_t &saveconf,
371 cgiargsclass &args,
372 text_t &compressed_args,
373 outconvertclass &outconvert,
374 ostream &logout) {
375 outconvertclass text_t2ascii;
376
377 compressed_args.clear();
378
379 text_t argname, argvalue;
380 const cgiarginfo *argnameinfo;
381
382 text_t::const_iterator saveconfhere = saveconf.begin();
383 text_t::const_iterator saveconfend = saveconf.end();
384
385 while (saveconfhere != saveconfend) {
386 saveconfhere = get_next_save_arg (saveconfhere, saveconfend, argname);
387
388 if (!argname.empty()) {
389 // found another entry
390 argnameinfo = argsinfo.getarginfo (argname);
391
392 if (argnameinfo == NULL) {
393 // no information about the argument could be found
394 // we can't keep going because we don't know whether
395 // this argument is a single or multiple character value
396 logout << text_t2ascii << "Error: the cgi argument \"" << argname
397 << "\" was specified as being a compressed argument\n"
398 << "but no information about it could be found within the "
399 << "cgiargsinfoclass.\n";
400 compressed_args.clear();
401 return false;
402
403 } else {
404 // found the argument information
405 if (argnameinfo->multiplechar) {
406 // multiple character argument -- make sure it is cgi safe
407 compressed_args += cgi_safe (outconvert.convert(args[argname]));
408 if (saveconfhere != saveconfend) compressed_args.push_back ('-');
409
410 } else {
411 // single character argument
412 if (args[argname].size() == 0) {
413 logout << text_t2ascii << "Error: the cgi argument \"" << argname
414 << "\" was specified as being a compressed argument which\n"
415 << "should have a one character value but it was empty.\n\n";
416 compressed_args.clear ();
417 return false;
418
419 } else if (args[argname].size() > 1) {
420 logout << text_t2ascii << "Error: the cgi argument \"" << argname
421 << "\" was specified as being a compressed argument which\n"
422 << "should have a one character value but it had multiple characters.\n\n";
423 compressed_args.clear ();
424 return false;
425 }
426
427 // everything is ok
428 compressed_args += args[argname];
429 }
430 }
431 }
432 }
433
434 return true;
435}
436
437
438// args_tounicode converts any arguments which are not in unicode
439// to unicode using inconvert
440void args_tounicode (cgiargsclass &args, inconvertclass &inconvert) {
441 cgiargsclass::iterator here = args.begin();
442 cgiargsclass::iterator end = args.end();
443
444 while (here != end) {
445 if ((*here).second.value.getencoding() > 0) {
446 (*here).second.value = inconvert.convert((*here).second.value);
447 }
448
449 here++;
450 }
451}
Note: See TracBrowser for help on using the repository browser.