1 | /**********************************************************************
|
---|
2 | *
|
---|
3 | * cgiwrapper.cpp -- output pages using the cgi protocol
|
---|
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 | *********************************************************************/
|
---|
25 |
|
---|
26 | #include <stdio.h>
|
---|
27 | #include <cstring>
|
---|
28 | #ifdef __WIN32__
|
---|
29 | #include <fcntl.h>
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | #include "gsdlconf.h"
|
---|
33 | #include "cgiwrapper.h"
|
---|
34 | #include "gsdlsitecfg.h"
|
---|
35 | #include "maincfg.h"
|
---|
36 | #include "fileutil.h"
|
---|
37 | #include "cgiutils.h"
|
---|
38 | #include <stdlib.h>
|
---|
39 | #include <assert.h>
|
---|
40 |
|
---|
41 | #if defined(GSDL_USE_OBJECTSPACE)
|
---|
42 | # include <ospace/std/iostream>
|
---|
43 | # include <ospace/std/fstream>
|
---|
44 | #elif defined(GSDL_USE_IOS_H)
|
---|
45 | # include <iostream.h>
|
---|
46 | # include <fstream.h>
|
---|
47 | #else
|
---|
48 | # include <iostream>
|
---|
49 | # include <fstream>
|
---|
50 | #endif
|
---|
51 |
|
---|
52 | #ifdef USE_FASTCGI
|
---|
53 | #include "fcgiapp.h"
|
---|
54 | #endif
|
---|
55 |
|
---|
56 |
|
---|
57 | #ifdef USE_FASTCGI
|
---|
58 | // used to output the text from receptionist
|
---|
59 | class fcgistreambuf : public streambuf {
|
---|
60 | public:
|
---|
61 | fcgistreambuf ();
|
---|
62 | int sync ();
|
---|
63 | int overflow (int ch);
|
---|
64 | int underflow () {return EOF;}
|
---|
65 |
|
---|
66 | void fcgisbreset() {fcgx_stream = NULL; other_ostream = NULL;};
|
---|
67 | void set_fcgx_stream(FCGX_Stream *newone) {fcgx_stream=newone;};
|
---|
68 | void set_other_ostream(ostream *newone) {other_ostream=newone;};
|
---|
69 |
|
---|
70 | private:
|
---|
71 | FCGX_Stream *fcgx_stream;
|
---|
72 | ostream *other_ostream;
|
---|
73 | };
|
---|
74 |
|
---|
75 | fcgistreambuf::fcgistreambuf() {
|
---|
76 | fcgisbreset();
|
---|
77 | if (base() == ebuf()) allocate();
|
---|
78 | setp (base(), ebuf());
|
---|
79 | };
|
---|
80 |
|
---|
81 | int fcgistreambuf::sync () {
|
---|
82 | if ((fcgx_stream != NULL) &&
|
---|
83 | (FCGX_PutStr (pbase(), out_waiting(), fcgx_stream) < 0)) {
|
---|
84 | fcgx_stream = NULL;
|
---|
85 | }
|
---|
86 |
|
---|
87 | if (other_ostream != NULL) {
|
---|
88 | char *thepbase=pbase();
|
---|
89 | for (int i=0;i<out_waiting();++i) (*other_ostream).put(thepbase[i]);
|
---|
90 | }
|
---|
91 |
|
---|
92 | setp (pbase(), epptr());
|
---|
93 |
|
---|
94 | return 0;
|
---|
95 | }
|
---|
96 |
|
---|
97 | int fcgistreambuf::overflow (int ch) {
|
---|
98 | if (sync () == EOF) return EOF;
|
---|
99 | if (ch != EOF) sputc (ch);
|
---|
100 | return 0;
|
---|
101 | }
|
---|
102 |
|
---|
103 | #endif
|
---|
104 |
|
---|
105 | static void format_error_string (text_t &errorpage, const text_t &errortext, bool debug) {
|
---|
106 |
|
---|
107 | errorpage.clear();
|
---|
108 |
|
---|
109 | if (debug) {
|
---|
110 | errorpage += "\n";
|
---|
111 | errorpage += "ERROR: " + errortext;
|
---|
112 | errorpage += "\n";
|
---|
113 |
|
---|
114 | } else {
|
---|
115 |
|
---|
116 | errorpage += "Content-type: text/html\n\n";
|
---|
117 |
|
---|
118 | errorpage += "<html>\n";
|
---|
119 | errorpage += "<head>\n";
|
---|
120 | errorpage += "<title>Error</title>\n";
|
---|
121 | errorpage += "</head>\n";
|
---|
122 | errorpage += "<body>\n";
|
---|
123 | errorpage += "<h2>Oops!</h2>\n";
|
---|
124 | errorpage += errortext;
|
---|
125 | errorpage += "</body>\n";
|
---|
126 | errorpage += "</html>\n";
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | static void page_errorcollect (const text_t &gsdlhome, text_t &errorpage, bool debug) {
|
---|
131 |
|
---|
132 | text_t collecthome = filename_cat (gsdlhome, "collect");
|
---|
133 |
|
---|
134 | text_t errortext = "No valid collections were found: Check that your collect directory\n";
|
---|
135 | errortext += "(" + collecthome + ") is readable and contains at least one valid collection.\n";
|
---|
136 | errortext += "Note that modelcol is NOT a valid collection.\n";
|
---|
137 | errortext += "If the path to your collect directory is wrong edit the 'gsdlhome' field\n";
|
---|
138 | errortext += "in your gsdlsite.cfg configuration file.\n";
|
---|
139 |
|
---|
140 | format_error_string (errorpage, errortext, debug);
|
---|
141 | }
|
---|
142 |
|
---|
143 | static void page_errorsitecfg (text_t &errorpage, bool debug, int mode) {
|
---|
144 |
|
---|
145 | text_t errortext;
|
---|
146 |
|
---|
147 | if (mode == 0) {
|
---|
148 | errortext += "The gsdlsite.cfg configuration file could not be found. This\n";
|
---|
149 | errortext += "file should contain configuration information relating to this\n";
|
---|
150 | errortext += "site's setup.\n";
|
---|
151 |
|
---|
152 | } else if (mode == 1) {
|
---|
153 | errortext += "The gsdlsite.cfg configuration file does not contain a valid\n";
|
---|
154 | errortext += "gsdlhome entry.\n";
|
---|
155 | }
|
---|
156 |
|
---|
157 | if (debug) {
|
---|
158 | errortext += "gsdlsite.cfg should reside in the directory from which the\n";
|
---|
159 | errortext += "library executable was run.\n";
|
---|
160 | } else {
|
---|
161 | errortext += "gsdlsite.cfg should reside in the same directory as the library\n";
|
---|
162 | errortext += "executable file.\n";
|
---|
163 | }
|
---|
164 |
|
---|
165 | format_error_string (errorpage, errortext, debug);
|
---|
166 | }
|
---|
167 |
|
---|
168 |
|
---|
169 | static void page_errormaincfg (const text_t &gsdlhome, const text_t &collection,
|
---|
170 | bool debug, text_t &errorpage) {
|
---|
171 |
|
---|
172 | text_t errortext;
|
---|
173 |
|
---|
174 | if (collection.empty()) {
|
---|
175 | text_t main_cfg_file = filename_cat (gsdlhome, "etc", "main.cfg");
|
---|
176 | errortext += "The main.cfg configuration file could not be found. This file\n";
|
---|
177 | errortext += "should contain configuration information relating to the\n";
|
---|
178 | errortext += "setup of the interface. As this receptionist is not being run\n";
|
---|
179 | errortext += "in collection specific mode the file should reside at\n";
|
---|
180 | errortext += main_cfg_file + ".\n";
|
---|
181 | } else {
|
---|
182 | text_t collect_cfg_file = filename_cat (gsdlhome, "collect", collection, "etc", "collect.cfg");
|
---|
183 | text_t main_collect_cfg_file = filename_cat (gsdlhome, "etc", "collect.cfg");
|
---|
184 | text_t main_cfg_file = filename_cat (gsdlhome, "etc", "main.cfg");
|
---|
185 | errortext += "Either the collect.cfg or main.cfg configuration file could\n";
|
---|
186 | errortext += "not be found. This file should contain configuration information\n";
|
---|
187 | errortext += "relating to the setup of the interface. As this receptionist is\n";
|
---|
188 | errortext += "being run in collection specific mode the file should reside\n";
|
---|
189 | errortext += "at either " + collect_cfg_file + ",\n";
|
---|
190 | errortext += main_collect_cfg_file + " or " + main_cfg_file + ".\n";
|
---|
191 | }
|
---|
192 |
|
---|
193 | format_error_string (errorpage, errortext, debug);
|
---|
194 | }
|
---|
195 |
|
---|
196 |
|
---|
197 | static void page_errorinit (const text_t &gsdlhome, bool debug, text_t &errorpage) {
|
---|
198 |
|
---|
199 | text_t errortext = "An error occurred during the initialisation of the Greenstone Digital\n";
|
---|
200 | errortext += "Library software. It is likely that the software has not been setup\n";
|
---|
201 | errortext += "correctly.\n";
|
---|
202 |
|
---|
203 | text_t error_file = filename_cat (gsdlhome, "etc", "error.txt");
|
---|
204 | char *efile = error_file.getcstr();
|
---|
205 | ifstream errin (efile);
|
---|
206 | delete []efile;
|
---|
207 | if (errin) {
|
---|
208 | errortext += "The error log, " + error_file + ", contains the\n";
|
---|
209 | errortext += "following information:\n\n";
|
---|
210 | if (!debug) errortext += "<pre>\n";
|
---|
211 |
|
---|
212 | char c;
|
---|
213 | errin.get(c);
|
---|
214 | while (!errin.eof ()) {
|
---|
215 | errortext.push_back(c);
|
---|
216 | errin.get(c);
|
---|
217 | }
|
---|
218 |
|
---|
219 | if (!debug) errortext += "</pre>\n";
|
---|
220 |
|
---|
221 | errin.close();
|
---|
222 |
|
---|
223 | } else {
|
---|
224 | errortext += "Please consult " + error_file + " for more information.\n";
|
---|
225 | }
|
---|
226 |
|
---|
227 | format_error_string (errorpage, errortext, debug);
|
---|
228 | }
|
---|
229 |
|
---|
230 | static void page_errorparseargs (const text_t &gsdlhome, bool debug, text_t &errorpage) {
|
---|
231 |
|
---|
232 | text_t errortext = "An error occurred during the parsing of the cgi arguments.\n";
|
---|
233 |
|
---|
234 | text_t error_file = filename_cat (gsdlhome, "etc", "error.txt");
|
---|
235 | char *efile = error_file.getcstr();
|
---|
236 | ifstream errin (efile);
|
---|
237 | delete []efile;
|
---|
238 | if (errin) {
|
---|
239 | errortext += "The error log, " + error_file + ", contains the\n";
|
---|
240 | errortext += "following information:\n\n";
|
---|
241 | if (!debug) errortext += "<pre>\n";
|
---|
242 |
|
---|
243 | char c;
|
---|
244 | errin.get(c);
|
---|
245 | while (!errin.eof ()) {
|
---|
246 | errortext.push_back(c);
|
---|
247 | errin.get(c);
|
---|
248 | }
|
---|
249 | if (!debug) errortext += "</pre>\n";
|
---|
250 | errin.close();
|
---|
251 |
|
---|
252 | } else {
|
---|
253 | errortext += "Please consult " + error_file + " for more information.\n";
|
---|
254 | }
|
---|
255 |
|
---|
256 | format_error_string (errorpage, errortext, debug);
|
---|
257 | }
|
---|
258 |
|
---|
259 | static void page_errorcgipage (const text_t &gsdlhome, bool debug, text_t &errorpage) {
|
---|
260 |
|
---|
261 | text_t errortext = "An error occurred during the construction of the cgi page.\n";
|
---|
262 |
|
---|
263 | text_t error_file = filename_cat (gsdlhome, "etc", "error.txt");
|
---|
264 | char *efile = error_file.getcstr();
|
---|
265 | ifstream errin (efile);
|
---|
266 | delete []efile;
|
---|
267 | if (errin) {
|
---|
268 | errortext += "The error log, " + error_file + ", contains the\n";
|
---|
269 | errortext += "following information:\n\n";
|
---|
270 | if (!debug) errortext += "<pre>\n";
|
---|
271 |
|
---|
272 | char c;
|
---|
273 | errin.get(c);
|
---|
274 | while (!errin.eof ()) {
|
---|
275 | errortext.push_back(c);
|
---|
276 | errin.get(c);
|
---|
277 | }
|
---|
278 | if (!debug) errortext += "</pre>\n";
|
---|
279 | errin.close();
|
---|
280 |
|
---|
281 | } else {
|
---|
282 | errortext += "Please consult " + error_file + " for more information.\n";
|
---|
283 | }
|
---|
284 |
|
---|
285 | format_error_string (errorpage, errortext, debug);
|
---|
286 | }
|
---|
287 |
|
---|
288 | static void print_debug_info (receptionist &recpt) {
|
---|
289 |
|
---|
290 | outconvertclass text_t2ascii;
|
---|
291 | const recptconf &configinfo = recpt.get_configinfo ();
|
---|
292 | text_t etc_dir = filename_cat (configinfo.gsdlhome, "etc");
|
---|
293 |
|
---|
294 | cout << "\n";
|
---|
295 | cout << text_t2ascii
|
---|
296 | << "------------------------------------------------------------\n"
|
---|
297 | << "Configuration and initialization completed successfully.\n"
|
---|
298 | << " Note that more debug information may be available in the\n"
|
---|
299 | << " initialization and error log error.txt in " << etc_dir << ".\n"
|
---|
300 | << "------------------------------------------------------------\n\n";
|
---|
301 |
|
---|
302 | bool colspec = false;
|
---|
303 | if (configinfo.collection.empty()) {
|
---|
304 | cout << "Receptionist is running in \"general\" (i.e. not \"collection "
|
---|
305 | << "specific\") mode.\n";
|
---|
306 | } else {
|
---|
307 | cout << text_t2ascii
|
---|
308 | << "Receptionist is running in \"collection specific\" mode.\n"
|
---|
309 | << " collection=" << configinfo.collection << "\n"
|
---|
310 | << " collection directory=" << configinfo.collectdir << "\n";
|
---|
311 | colspec = true;
|
---|
312 | }
|
---|
313 |
|
---|
314 | cout << text_t2ascii << " gsdlhome=" << configinfo.gsdlhome << "\n";
|
---|
315 | if (!configinfo.collecthome.empty())
|
---|
316 | cout << text_t2ascii << " collecthome=" << configinfo.collecthome << "\n";
|
---|
317 | if (!configinfo.dbhome.empty())
|
---|
318 | cout << text_t2ascii << " dbhome=" << configinfo.dbhome << "\n";
|
---|
319 | cout << text_t2ascii << " httpprefix=" << configinfo.httpprefix << "\n";
|
---|
320 | cout << text_t2ascii << " httpimg=" << configinfo.httpimg << "\n";
|
---|
321 | cout << text_t2ascii << " gwcgi=" << configinfo.gwcgi << "\n\n"
|
---|
322 | << " Note that unless gwcgi has been set from a configuration\n"
|
---|
323 | << " file it is dependent on environment variables set by your\n"
|
---|
324 | << " webserver. Therefore it may not have the same value when run\n"
|
---|
325 | << " from the command line as it would be when run from your\n"
|
---|
326 | << " web server.\n";
|
---|
327 | if (configinfo.usecookies)
|
---|
328 | cout << "cookies are enabled\n";
|
---|
329 | else
|
---|
330 | cout << "cookies are disabled\n";
|
---|
331 | if (configinfo.logcgiargs)
|
---|
332 | cout << "logging is enabled\n";
|
---|
333 | else
|
---|
334 | cout << "logging is disabled\n";
|
---|
335 | cout << "------------------------------------------------------------\n\n";
|
---|
336 |
|
---|
337 | text_tset::const_iterator this_mfile = configinfo.macrofiles.begin();
|
---|
338 | text_tset::const_iterator end_mfile = configinfo.macrofiles.end();
|
---|
339 | cout << "Macro Files:\n"
|
---|
340 | << "------------\n";
|
---|
341 | text_t mfile;
|
---|
342 | bool found;
|
---|
343 | while (this_mfile != end_mfile) {
|
---|
344 | cout << text_t2ascii << *this_mfile;
|
---|
345 | int spaces = (22 - (*this_mfile).size());
|
---|
346 | if (spaces < 2) spaces = 2;
|
---|
347 | text_t outspaces;
|
---|
348 | for (int i = 0; i < spaces; ++i) outspaces.push_back (' ');
|
---|
349 | cout << text_t2ascii << outspaces;
|
---|
350 |
|
---|
351 | found = false;
|
---|
352 | if (colspec) {
|
---|
353 | // collection specific - try collectdir/macros first
|
---|
354 | mfile = filename_cat (configinfo.collectdir, "macros", *this_mfile);
|
---|
355 | if (file_exists (mfile)) {
|
---|
356 | cout << text_t2ascii << "found (" << mfile << ")\n";
|
---|
357 | found = true;
|
---|
358 | }
|
---|
359 | }
|
---|
360 |
|
---|
361 | if (!found) {
|
---|
362 | // try main macro directory
|
---|
363 | mfile = filename_cat (configinfo.gsdlhome, "macros", *this_mfile);
|
---|
364 | if (file_exists (mfile)) {
|
---|
365 | cout << text_t2ascii << "found (" << mfile << ")\n";
|
---|
366 | found = true;
|
---|
367 | }
|
---|
368 | }
|
---|
369 |
|
---|
370 | if (!found)
|
---|
371 | cout << text_t2ascii << "NOT FOUND\n";
|
---|
372 |
|
---|
373 | ++this_mfile;
|
---|
374 | }
|
---|
375 |
|
---|
376 | cout << "------------------------------------------------------------\n\n"
|
---|
377 | << "Collections:\n"
|
---|
378 | << "------------\n"
|
---|
379 | << " Note that collections will only appear as \"running\" if\n"
|
---|
380 | << " their build.cfg files exist, are readable, contain a valid\n"
|
---|
381 | << " builddate field (i.e. > 0), and are in the collection's\n"
|
---|
382 | << " index directory (i.e. NOT the building directory)\n\n";
|
---|
383 |
|
---|
384 | recptprotolistclass *protos = recpt.get_recptprotolist_ptr();
|
---|
385 | recptprotolistclass::iterator rprotolist_here = protos->begin();
|
---|
386 | recptprotolistclass::iterator rprotolist_end = protos->end();
|
---|
387 |
|
---|
388 | bool is_z3950 = false;
|
---|
389 | bool found_valid_col = false;
|
---|
390 |
|
---|
391 |
|
---|
392 | while (rprotolist_here != rprotolist_end) {
|
---|
393 | comerror_t err;
|
---|
394 | if ((*rprotolist_here).p == NULL) continue;
|
---|
395 | else if (is_z3950==false &&
|
---|
396 | (*rprotolist_here).p->get_protocol_name(err) == "z3950proto") {
|
---|
397 | cout << "\nZ39.50 Servers: (always public)\n"
|
---|
398 | << "---------------\n";
|
---|
399 | is_z3950=true;
|
---|
400 | }
|
---|
401 |
|
---|
402 | text_tarray collist;
|
---|
403 | (*rprotolist_here).p->get_collection_list (collist, err, cerr);
|
---|
404 | if (err == noError) {
|
---|
405 | text_tarray::iterator collist_here = collist.begin();
|
---|
406 | text_tarray::iterator collist_end = collist.end();
|
---|
407 |
|
---|
408 | while (collist_here != collist_end) {
|
---|
409 |
|
---|
410 | cout << text_t2ascii << *collist_here;
|
---|
411 |
|
---|
412 | int spaces = (22 - (*collist_here).size());
|
---|
413 | if (spaces < 2) spaces = 2;
|
---|
414 | text_t outspaces;
|
---|
415 | for (int i = 0; i < spaces; ++i) outspaces.push_back (' ');
|
---|
416 | cout << text_t2ascii << outspaces;
|
---|
417 |
|
---|
418 | ColInfoResponse_t *cinfo = recpt.get_collectinfo_ptr ((*rprotolist_here).p, *collist_here, cerr);
|
---|
419 | if (cinfo != NULL) {
|
---|
420 | if (cinfo->isPublic) cout << "public ";
|
---|
421 | else cout << "private";
|
---|
422 |
|
---|
423 | if (cinfo->buildDate > 0) {
|
---|
424 | cout << " running ";
|
---|
425 | found_valid_col = true;
|
---|
426 | } else {
|
---|
427 | cout << " not running";
|
---|
428 | }
|
---|
429 | }
|
---|
430 |
|
---|
431 | cout << "\n";
|
---|
432 |
|
---|
433 | ++collist_here;
|
---|
434 | }
|
---|
435 | }
|
---|
436 | is_z3950=false;
|
---|
437 | ++rprotolist_here;
|
---|
438 | } // end of while loop
|
---|
439 |
|
---|
440 | if (!found_valid_col) {
|
---|
441 | cout << "WARNING: No \"running\" collections were found. You need to\n";
|
---|
442 | cout << " build one of the above collections\n";
|
---|
443 | }
|
---|
444 |
|
---|
445 | cout << "\n------------------------------------------------------------\n";
|
---|
446 | cout << "------------------------------------------------------------\n\n";
|
---|
447 | cout << "receptionist running in command line debug mode\n";
|
---|
448 | cout << "enter cgi arguments as name=value pairs (e.g. 'a=p&p=home'):\n";
|
---|
449 |
|
---|
450 | }
|
---|
451 |
|
---|
452 | // cgiwrapper does everything necessary to output a page
|
---|
453 | // using the cgi protocol. If this is being run for a particular
|
---|
454 | // collection then "collection" should be set, otherwise it
|
---|
455 | // should equal "".
|
---|
456 | void cgiwrapper (receptionist &recpt, text_t collection) {
|
---|
457 | int numrequests = 0;
|
---|
458 | bool debug = false;
|
---|
459 | const recptconf &configinfo = recpt.get_configinfo ();
|
---|
460 |
|
---|
461 | // find out whether this is being run as a cgi-script
|
---|
462 | // or a fastcgi script
|
---|
463 | #ifdef USE_FASTCGI
|
---|
464 | fcgistreambuf outbuf;
|
---|
465 | int isfastcgi = !FCGX_IsCGI();
|
---|
466 | FCGX_Stream *fcgiin, *fcgiout, *fcgierr;
|
---|
467 | FCGX_ParamArray fcgienvp;
|
---|
468 | #else
|
---|
469 | int isfastcgi = 0;
|
---|
470 | #endif
|
---|
471 |
|
---|
472 | // we need gsdlhome to do fileupload stuff, so moved this configure stuff before the get argstr stuff
|
---|
473 | // init stuff - we can't output error pages directly with
|
---|
474 | // fastcgi so the pages are stored until we can output them
|
---|
475 | text_t errorpage;
|
---|
476 | outconvertclass text_t2ascii;
|
---|
477 |
|
---|
478 | // set defaults
|
---|
479 | int maxrequests = 10000;
|
---|
480 | recpt.configure ("collection", collection);
|
---|
481 | recpt.configure ("httpimg", "/gsdl/images");
|
---|
482 | char *script_name = getenv("SCRIPT_NAME");
|
---|
483 | if (script_name != NULL) recpt.configure("gwcgi", script_name);
|
---|
484 | else recpt.configure("gwcgi", "/gsdl");
|
---|
485 |
|
---|
486 | // read in the configuration files.
|
---|
487 | text_t gsdlhome;
|
---|
488 | text_t collecthome;
|
---|
489 | configurator gsdlconfigurator(&recpt);
|
---|
490 | if (!site_cfg_read (gsdlconfigurator, gsdlhome, collecthome, maxrequests)) {
|
---|
491 | // couldn't find the site configuration file
|
---|
492 | page_errorsitecfg (errorpage, debug, 0);
|
---|
493 | } else if (gsdlhome.empty()) {
|
---|
494 | // no gsdlhome in gsdlsite.cfg
|
---|
495 | page_errorsitecfg (errorpage, debug, 1);
|
---|
496 | } else if (!directory_exists(gsdlhome)) {
|
---|
497 | // gsdlhome not a valid directory
|
---|
498 | page_errorsitecfg (errorpage, debug, 1);
|
---|
499 | } else if (!main_cfg_read (recpt, gsdlhome, collecthome, collection)) {
|
---|
500 | // couldn't find the main configuration file
|
---|
501 | page_errormaincfg (gsdlhome, collection, debug, errorpage);
|
---|
502 | } else if (configinfo.collectinfo.empty() && false) { // commented out for corba
|
---|
503 | // don't have any collections
|
---|
504 | page_errorcollect (gsdlhome, errorpage, debug);
|
---|
505 | }
|
---|
506 | // get the query string if it is not being run as a fastcgi
|
---|
507 | // script
|
---|
508 | text_t argstr = g_EmptyText;
|
---|
509 | fileupload_tmap fileuploads;
|
---|
510 | cgiargsclass args;
|
---|
511 | char *aURIStr;
|
---|
512 | if (!isfastcgi) {
|
---|
513 | char *request_method_str = getenv("REQUEST_METHOD");
|
---|
514 | char *content_length_str = getenv("CONTENT_LENGTH");
|
---|
515 | if (request_method_str != NULL && strcmp(request_method_str, "POST") == 0 &&
|
---|
516 | content_length_str != NULL) {
|
---|
517 | // POST form data
|
---|
518 | long content_length = (content_length_str ? atoi(content_length_str) : 0);
|
---|
519 | if (content_length > 0) {
|
---|
520 | #ifdef __WIN32__
|
---|
521 | // On Windows it is important that standard input be read in binary
|
---|
522 | // mode, otherwise end of line "<CR><LF>" is turned into <LF> only
|
---|
523 | // which breaks the MIME standard (and our parsing code!)
|
---|
524 |
|
---|
525 | int result = _setmode( _fileno( stdin ), _O_BINARY );
|
---|
526 | if( result == -1 ) {
|
---|
527 | cerr << "Warning: Failed to set standard input to binary mode." << endl;
|
---|
528 | cerr << " Parsing of multi-part MIME will most likely fail" << endl;
|
---|
529 | }
|
---|
530 | #endif
|
---|
531 |
|
---|
532 | long length = content_length;
|
---|
533 | unsigned char * buffer = new unsigned char[content_length];
|
---|
534 |
|
---|
535 | int chars_read = fread(buffer,1,content_length,stdin);
|
---|
536 |
|
---|
537 | if (chars_read != content_length) {
|
---|
538 | cerr << "Warning: mismatch between CONTENT_LENGTH and data read from standard in" << endl;
|
---|
539 | }
|
---|
540 |
|
---|
541 | argstr.setcarr((char *)buffer, content_length);
|
---|
542 |
|
---|
543 | text_t content_type;
|
---|
544 | char *content_type_str = getenv("CONTENT_TYPE");
|
---|
545 | if (content_type_str) content_type = content_type_str;
|
---|
546 | argstr = parse_post_data(content_type, argstr, fileuploads, gsdlhome);
|
---|
547 | }
|
---|
548 | } else {
|
---|
549 | aURIStr = getenv("QUERY_STRING");
|
---|
550 | if ((request_method_str != NULL && strcmp(request_method_str, "GET") == 0)
|
---|
551 | || aURIStr != NULL) {
|
---|
552 | // GET form data
|
---|
553 | if (aURIStr != NULL) argstr = aURIStr;
|
---|
554 | } else {
|
---|
555 | // debugging from command line
|
---|
556 | debug = true;
|
---|
557 | }
|
---|
558 | }
|
---|
559 | }
|
---|
560 |
|
---|
561 | if (debug) {
|
---|
562 | cout << "Configuring Greenstone...\n";
|
---|
563 | cout << flush;
|
---|
564 | }
|
---|
565 |
|
---|
566 |
|
---|
567 | if (errorpage.empty()) {
|
---|
568 |
|
---|
569 | // initialise the library software
|
---|
570 | if (debug) {
|
---|
571 | cout << "Initializing...\n";
|
---|
572 | cout << flush;
|
---|
573 | }
|
---|
574 |
|
---|
575 | text_t error_file = filename_cat (gsdlhome, "etc", "error.txt");
|
---|
576 | char *eout = error_file.getcstr();
|
---|
577 | ofstream errout (eout, ios::app);
|
---|
578 | delete []eout;
|
---|
579 | if (!recpt.init(errout)) {
|
---|
580 | // an error occurred during the initialisation
|
---|
581 | errout.close();
|
---|
582 | page_errorinit(gsdlhome, debug, errorpage);
|
---|
583 | }
|
---|
584 | errout.close();
|
---|
585 | }
|
---|
586 |
|
---|
587 | if (debug && errorpage.empty()) {
|
---|
588 | // get query string from command line
|
---|
589 | print_debug_info (recpt);
|
---|
590 | char cinURIStr[1024];
|
---|
591 | cin.get(cinURIStr, 1024);
|
---|
592 | argstr = cinURIStr;
|
---|
593 | }
|
---|
594 |
|
---|
595 | // cgi scripts only deal with one request
|
---|
596 | if (!isfastcgi) maxrequests = 1;
|
---|
597 |
|
---|
598 | // Page-request loop. If this is not being run as a fastcgi
|
---|
599 | // process then only one request will be processed and then
|
---|
600 | // the process will exit.
|
---|
601 | while (numrequests < maxrequests) {
|
---|
602 | #ifdef USE_FASTCGI
|
---|
603 | if (isfastcgi) {
|
---|
604 | if (FCGX_Accept(&fcgiin, &fcgiout, &fcgierr, &fcgienvp) < 0) break;
|
---|
605 |
|
---|
606 | char *request_method_str = FCGX_GetParam ("REQUEST_METHOD", fcgienvp);
|
---|
607 | char *content_length_str = FCGX_GetParam ("CONTENT_LENGTH", fcgienvp);
|
---|
608 |
|
---|
609 | if (request_method_str != NULL && strcmp(request_method_str, "POST") == 0 &&
|
---|
610 | content_length_str != NULL) {
|
---|
611 | // POST form data
|
---|
612 | int content_length = text_t(content_length_str).getint();
|
---|
613 | if (content_length > 0) {
|
---|
614 | argstr.clear();
|
---|
615 | int c;
|
---|
616 | do {
|
---|
617 | c = FCGX_GetChar (fcgiin);
|
---|
618 | if (c < 0) break;
|
---|
619 | argstr.push_back (c);
|
---|
620 | --content_length;
|
---|
621 | } while (content_length > 0);
|
---|
622 | }
|
---|
623 |
|
---|
624 | } else {
|
---|
625 | // GET form data
|
---|
626 | aURIStr = FCGX_GetParam("QUERY_STRING", fcgienvp);
|
---|
627 | if (aURIStr != NULL) argstr = aURIStr;
|
---|
628 | else argstr = g_EmptyText;
|
---|
629 | }
|
---|
630 | }
|
---|
631 | #endif
|
---|
632 |
|
---|
633 | // get output streams ready
|
---|
634 | #ifdef USE_FASTCGI
|
---|
635 | outbuf.fcgisbreset ();
|
---|
636 | if (isfastcgi) outbuf.set_fcgx_stream (fcgiout);
|
---|
637 | else outbuf.set_other_ostream (&cout);
|
---|
638 | ostream pageout (&outbuf);
|
---|
639 | #else
|
---|
640 | #define pageout cout
|
---|
641 | #endif
|
---|
642 |
|
---|
643 | // if using fastcgi we'll load environment into a map,
|
---|
644 | // otherwise simply pass empty map (can't get environment
|
---|
645 | // variables using getenv() while using FCGX versions
|
---|
646 | // of fastcgi - at least I can't ;-) - Stefan)
|
---|
647 | text_tmap fastcgienv;
|
---|
648 | #ifdef USE_FASTCGI
|
---|
649 | if (isfastcgi) {
|
---|
650 | for(; *fcgienvp != NULL; ++fcgienvp) {
|
---|
651 | text_t fvalue = *fcgienvp;
|
---|
652 | text_t::const_iterator begin = fvalue.begin();
|
---|
653 | text_t::const_iterator end = fvalue.end();
|
---|
654 | text_t::const_iterator equals_sign = findchar (begin, end, '=');
|
---|
655 | if (equals_sign != end)
|
---|
656 | fastcgienv[substr(begin, equals_sign)] = substr(equals_sign+1, end);
|
---|
657 | }
|
---|
658 | }
|
---|
659 | #endif
|
---|
660 |
|
---|
661 | // temporarily need to configure gwcgi here when using fastcgi as I can't
|
---|
662 | // get it to pass the SCRIPT_NAME environment variable to the initial
|
---|
663 | // environment (if anyone can work out how to do this using the apache
|
---|
664 | // server, let me know). Note that this overrides the gwcgi field in
|
---|
665 | // site.cfg (which it shouldn't do) but I can't at present set gwcgi
|
---|
666 | // from site.cfg as I have old receptionists laying around that wouldn't
|
---|
667 | // appreciate it. The following 5 lines of code should be deleted once
|
---|
668 | // I either a: get the server to pass SCRIPT_NAME at initialization
|
---|
669 | // time or b: convert all the collections using old receptionists over
|
---|
670 | // to this version and uncomment gwcgi in the site.cfg file -- Stefan.
|
---|
671 | #ifdef USE_FASTCGI
|
---|
672 | if (isfastcgi) {
|
---|
673 | recpt.configure("gwcgi", fastcgienv["SCRIPT_NAME"]);
|
---|
674 | }
|
---|
675 | #endif
|
---|
676 |
|
---|
677 |
|
---|
678 | // if there has been no error so far, perform the production of the
|
---|
679 | // output page
|
---|
680 | if (errorpage.empty()) {
|
---|
681 | text_t error_file = filename_cat (gsdlhome, "etc", "error.txt");
|
---|
682 | char *eout = error_file.getcstr();
|
---|
683 | ofstream errout (eout, ios::app);
|
---|
684 | delete []eout;
|
---|
685 |
|
---|
686 | #if defined(__WIN32__) && defined(GSDL_USE_IOS_H)
|
---|
687 | // old Windows compilers (VC++4.2)
|
---|
688 | cerr = errout;
|
---|
689 | #else
|
---|
690 | // can't do this anymore according to c++ standard...
|
---|
691 | // cerr = errout;
|
---|
692 | // ... but can do this instead
|
---|
693 | streambuf* errbuf = cerr.rdbuf(errout.rdbuf());
|
---|
694 | #endif
|
---|
695 |
|
---|
696 | // parse the cgi arguments and produce the resulting page if there
|
---|
697 | // has been no errors so far
|
---|
698 | if (!recpt.parse_cgi_args (argstr, fileuploads, args, errout, fastcgienv)) {
|
---|
699 | errout.close ();
|
---|
700 | page_errorparseargs(gsdlhome, debug, errorpage);
|
---|
701 | } else {
|
---|
702 | // produce the output page
|
---|
703 |
|
---|
704 | if (!recpt.produce_cgi_page (args, pageout, errout, fastcgienv)) {
|
---|
705 | errout.close ();
|
---|
706 | page_errorcgipage(gsdlhome, debug, errorpage);
|
---|
707 | }
|
---|
708 | recpt.log_cgi_args (args, errout, fastcgienv);
|
---|
709 | errout.close ();
|
---|
710 | }
|
---|
711 |
|
---|
712 | #if !defined(__WIN32__) || !defined(GSDL_USE_IOS_H)
|
---|
713 | // restore the cerr buffer
|
---|
714 | cerr.rdbuf(errbuf);
|
---|
715 | #endif
|
---|
716 | }
|
---|
717 | // clean up any files that were uploaded
|
---|
718 | fileupload_tmap::const_iterator this_file = fileuploads.begin();
|
---|
719 | fileupload_tmap::const_iterator end_file = fileuploads.end();
|
---|
720 | while (this_file != end_file)
|
---|
721 | {
|
---|
722 | if (file_exists((*this_file).second.tmp_name))
|
---|
723 | {
|
---|
724 | char *thefile = (*this_file).second.tmp_name.getcstr();
|
---|
725 | unlink(thefile);
|
---|
726 | delete [] thefile;
|
---|
727 | }
|
---|
728 | ++this_file;
|
---|
729 | }
|
---|
730 |
|
---|
731 | // there was an error, output the error page
|
---|
732 | if (!errorpage.empty()) {
|
---|
733 | pageout << text_t2ascii << errorpage;
|
---|
734 | errorpage.clear();
|
---|
735 | numrequests = maxrequests; // make this the last page
|
---|
736 | }
|
---|
737 | pageout << flush;
|
---|
738 |
|
---|
739 | // finish with the output streams
|
---|
740 | #ifdef USE_FASTCGI
|
---|
741 | if (isfastcgi) FCGX_Finish();
|
---|
742 | #endif
|
---|
743 |
|
---|
744 | ++numrequests;
|
---|
745 | }
|
---|
746 |
|
---|
747 | return;
|
---|
748 | }
|
---|