source: main/trunk/greenstone2/runtime-src/src/recpt/cgiwrapper.cpp

Last change on this file was 39000, checked in by kjdon, 6 weeks ago

the usecookies option has been renamed to usecookiesForUID to better reflect what it means. A new option usecookiesForE is added. If this is set to true (the default), then the e arg will be saved as a cookie, instead of being set into the various compressedoptions macros (which will now be empty). nzdl.org is getting hammered by bots, and one theory is that the e arg changes everytime so looks like a new page when its not.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 32.2 KB
Line 
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#include "authenaction.h"
57#include "browseaction.h"
58#include "collectoraction.h"
59#include "depositoraction.h"
60#include "documentaction.h"
61#include "dynamicclassifieraction.h"
62#include "extlinkaction.h"
63#include "pageaction.h"
64#ifdef ENABLE_MGPP
65#include "phindaction.h"
66#endif
67#include "pingaction.h"
68#include "queryaction.h"
69
70#if defined(USE_SQLITE)
71#include "sqlqueryaction.h"
72#endif
73
74#if defined(GSDL_USE_GTI_ACTION)
75#include "gtiaction.h"
76#endif
77
78#if defined(USE_RSS)
79#include "rssaction.h"
80#endif
81
82#include "tipaction.h"
83#include "statusaction.h"
84#include "usersaction.h"
85#include "configaction.h"
86
87#include "vlistbrowserclass.h"
88#include "hlistbrowserclass.h"
89#include "datelistbrowserclass.h"
90#include "invbrowserclass.h"
91#include "pagedbrowserclass.h"
92#include "htmlbrowserclass.h"
93#include "phindbrowserclass.h"
94
95
96#ifdef USE_FASTCGI
97// used to output the text from receptionist
98class fcgistreambuf : public streambuf {
99public:
100 fcgistreambuf ();
101 int sync ();
102 int overflow (int ch);
103 int underflow () {return EOF;}
104
105 void fcgisbreset() {fcgx_stream = NULL; other_ostream = NULL;};
106 void set_fcgx_stream(FCGX_Stream *newone) {fcgx_stream=newone;};
107 void set_other_ostream(ostream *newone) {other_ostream=newone;};
108
109private:
110 FCGX_Stream *fcgx_stream;
111 ostream *other_ostream;
112};
113
114fcgistreambuf::fcgistreambuf() {
115 fcgisbreset();
116 if (base() == ebuf()) allocate();
117 setp (base(), ebuf());
118};
119
120int fcgistreambuf::sync () {
121 if ((fcgx_stream != NULL) &&
122 (FCGX_PutStr (pbase(), out_waiting(), fcgx_stream) < 0)) {
123 fcgx_stream = NULL;
124 }
125
126 if (other_ostream != NULL) {
127 char *thepbase=pbase();
128 for (int i=0;i<out_waiting();++i) (*other_ostream).put(thepbase[i]);
129 }
130
131 setp (pbase(), epptr());
132
133 return 0;
134}
135
136int fcgistreambuf::overflow (int ch) {
137 if (sync () == EOF) return EOF;
138 if (ch != EOF) sputc (ch);
139 return 0;
140}
141
142#endif
143
144static void format_error_string (text_t &errorpage, const text_t &errortext, bool debug) {
145
146 errorpage.clear();
147
148 if (debug) {
149 errorpage += "\n";
150 errorpage += "ERROR: " + errortext;
151 errorpage += "\n";
152
153 } else {
154
155 errorpage += "Content-type: text/html\n\n";
156
157 errorpage += "<html>\n";
158 errorpage += "<head>\n";
159 errorpage += "<title>Error</title>\n";
160 errorpage += "</head>\n";
161 errorpage += "<body>\n";
162 errorpage += "<h2>Oops!</h2>\n";
163 errorpage += errortext;
164 errorpage += "</body>\n";
165 errorpage += "</html>\n";
166 }
167}
168
169static void page_errorcollect (const text_t &gsdlhome, text_t &errorpage, bool debug) {
170
171 text_t collecthome = filename_cat (gsdlhome, "collect");
172
173 text_t errortext = "No valid collections were found: Check that your collect directory\n";
174 errortext += "(" + collecthome + ") is readable and contains at least one valid collection.\n";
175 errortext += "Note that modelcol is NOT a valid collection.\n";
176 errortext += "If the path to your collect directory is wrong edit the 'gsdlhome' field\n";
177 errortext += "in your gsdlsite.cfg configuration file.\n";
178
179 format_error_string (errorpage, errortext, debug);
180}
181
182static void page_errorsitecfg (text_t &errorpage, bool debug, int mode) {
183
184 text_t errortext;
185
186 if (mode == 0) {
187 errortext += "The gsdlsite.cfg configuration file could not be found. This\n";
188 errortext += "file should contain configuration information relating to this\n";
189 errortext += "site's setup.\n";
190
191 } else if (mode == 1) {
192 errortext += "The gsdlsite.cfg configuration file does not contain a valid\n";
193 errortext += "gsdlhome entry.\n";
194 }
195
196 if (debug) {
197 errortext += "gsdlsite.cfg should reside in the directory from which the\n";
198 errortext += "library executable was run.\n";
199 } else {
200 errortext += "gsdlsite.cfg should reside in the same directory as the library\n";
201 errortext += "executable file.\n";
202 }
203
204 format_error_string (errorpage, errortext, debug);
205}
206
207
208static void page_errormaincfg (const text_t &gsdlhome, const text_t &collection,
209 bool debug, text_t &errorpage) {
210
211 text_t errortext;
212
213 if (collection.empty()) {
214 text_t main_cfg_file = filename_cat (gsdlhome, "etc", "main.cfg");
215 errortext += "The main.cfg configuration file could not be found. This file\n";
216 errortext += "should contain configuration information relating to the\n";
217 errortext += "setup of the interface. As this receptionist is not being run\n";
218 errortext += "in collection specific mode the file should reside at\n";
219 errortext += main_cfg_file + ".\n";
220 } else {
221 text_t collect_cfg_file = filename_cat (gsdlhome, "collect", collection, "etc", "collect.cfg");
222 text_t main_collect_cfg_file = filename_cat (gsdlhome, "etc", "collect.cfg");
223 text_t main_cfg_file = filename_cat (gsdlhome, "etc", "main.cfg");
224 errortext += "Either the collect.cfg or main.cfg configuration file could\n";
225 errortext += "not be found. This file should contain configuration information\n";
226 errortext += "relating to the setup of the interface. As this receptionist is\n";
227 errortext += "being run in collection specific mode the file should reside\n";
228 errortext += "at either " + collect_cfg_file + ",\n";
229 errortext += main_collect_cfg_file + " or " + main_cfg_file + ".\n";
230 }
231
232 format_error_string (errorpage, errortext, debug);
233}
234
235
236static void page_errorinit (const text_t &gsdlhome, bool debug, text_t &errorpage) {
237
238 text_t errortext = "An error occurred during the initialisation of the Greenstone Digital\n";
239 errortext += "Library software. It is likely that the software has not been setup\n";
240 errortext += "correctly.\n";
241
242 text_t error_file = filename_cat (gsdlhome, "etc", "error.txt");
243 // This is all commented out because I think it's a really bad idea
244 // The error.txt file may be very large, causing out of memory problems and even crashing the machine in extreme
245 // cases where multiple processes are causing this type of error (e.g. automated processes that try to "hack"
246 // the Greenstone site by supplying values such as site URLs for the CGI arguments -- this has happened)
247 // Also, the error.txt may contain information that shouldn't be exposed (such as usage or query information)
248 // Maybe this should be configurable through a main.cfg configuration setting, but I don't think it's worth it
249 // The only people who should need the contents of this file should have access to it through the file system
250 // I think you can also view the contents of this file through the statusaction if you have a suitable login
251// char *efile = error_file.getcstr();
252// ifstream errin (efile);
253// delete []efile;
254// if (errin) {
255// errortext += "The error log, " + error_file + ", contains the\n";
256// errortext += "following information:\n\n";
257// if (!debug) errortext += "<pre>\n";
258
259// char c;
260// errin.get(c);
261// while (!errin.eof ()) {
262// errortext.push_back(c);
263// errin.get(c);
264// }
265
266// if (!debug) errortext += "</pre>\n";
267
268// errin.close();
269
270// } else {
271 errortext += "Please consult " + error_file + " for more information.\n";
272// }
273
274 format_error_string (errorpage, errortext, debug);
275}
276
277static void page_errorparseargs (const text_t &gsdlhome, bool debug, text_t &errorpage) {
278
279 text_t errortext = "An error occurred during the parsing of the cgi arguments.\n";
280
281 text_t error_file = filename_cat (gsdlhome, "etc", "error.txt");
282 // This is all commented out because I think it's a really bad idea
283 // The error.txt file may be very large, causing out of memory problems and even crashing the machine in extreme
284 // cases where multiple processes are causing this type of error (e.g. automated processes that try to "hack"
285 // the Greenstone site by supplying values such as site URLs for the CGI arguments -- this has happened)
286 // Also, the error.txt may contain information that shouldn't be exposed (such as usage or query information)
287 // Maybe this should be configurable through a main.cfg configuration setting, but I don't think it's worth it
288 // The only people who should need the contents of this file should have access to it through the file system
289 // I think you can also view the contents of this file through the statusaction if you have a suitable login
290// char *efile = error_file.getcstr();
291// ifstream errin (efile);
292// delete []efile;
293// if (errin) {
294// errortext += "The error log, " + error_file + ", contains the\n";
295// errortext += "following information:\n\n";
296// if (!debug) errortext += "<pre>\n";
297
298// char c;
299// errin.get(c);
300// while (!errin.eof ()) {
301// errortext.push_back(c);
302// errin.get(c);
303// }
304// if (!debug) errortext += "</pre>\n";
305// errin.close();
306
307// } else {
308 errortext += "Please consult " + error_file + " for more information.\n";
309// }
310
311 format_error_string (errorpage, errortext, debug);
312}
313
314static void page_errorcgipage (const text_t &gsdlhome, bool debug, text_t &errorpage) {
315
316 text_t errortext = "An error occurred during the construction of the cgi page.\n";
317
318 text_t error_file = filename_cat (gsdlhome, "etc", "error.txt");
319 // This is all commented out because I think it's a really bad idea
320 // The error.txt file may be very large, causing out of memory problems and even crashing the machine in extreme
321 // cases where multiple processes are causing this type of error (e.g. automated processes that try to "hack"
322 // the Greenstone site by supplying values such as site URLs for the CGI arguments -- this has happened)
323 // Also, the error.txt may contain information that shouldn't be exposed (such as usage or query information)
324 // Maybe this should be configurable through a main.cfg configuration setting, but I don't think it's worth it
325 // The only people who should need the contents of this file should have access to it through the file system
326 // I think you can also view the contents of this file through the statusaction if you have a suitable login
327// char *efile = error_file.getcstr();
328// ifstream errin (efile);
329// delete []efile;
330// if (errin) {
331// errortext += "The error log, " + error_file + ", contains the\n";
332// errortext += "following information:\n\n";
333// if (!debug) errortext += "<pre>\n";
334
335// char c;
336// errin.get(c);
337// while (!errin.eof ()) {
338// errortext.push_back(c);
339// errin.get(c);
340// }
341// if (!debug) errortext += "</pre>\n";
342// errin.close();
343
344// } else {
345 errortext += "Please consult " + error_file + " for more information.\n";
346// }
347
348 format_error_string (errorpage, errortext, debug);
349}
350
351static void print_debug_info (receptionist &recpt) {
352
353 outconvertclass text_t2ascii;
354 const recptconf &configinfo = recpt.get_configinfo ();
355 text_t etc_dir = filename_cat (configinfo.gsdlhome, "etc");
356
357 cout << "\n";
358 cout << text_t2ascii
359 << "------------------------------------------------------------\n"
360 << "Configuration and initialization completed successfully.\n"
361 << " Note that more debug information may be available in the\n"
362 << " initialization and error log error.txt in " << etc_dir << ".\n"
363 << "------------------------------------------------------------\n\n";
364
365 bool colspec = false;
366 if (configinfo.collection.empty()) {
367 cout << "Receptionist is running in \"general\" (i.e. not \"collection "
368 << "specific\") mode.\n";
369 } else {
370 cout << text_t2ascii
371 << "Receptionist is running in \"collection specific\" mode.\n"
372 << " collection=" << configinfo.collection << "\n"
373 << " collection directory=" << configinfo.collectdir << "\n";
374 colspec = true;
375 }
376
377 cout << text_t2ascii << " gsdlhome=" << configinfo.gsdlhome << "\n";
378 if (!configinfo.collecthome.empty())
379 cout << text_t2ascii << " collecthome=" << configinfo.collecthome << "\n";
380 if (!configinfo.dbhome.empty())
381 cout << text_t2ascii << " dbhome=" << configinfo.dbhome << "\n";
382 cout << text_t2ascii << " httpprefix=" << configinfo.httpprefix << "\n";
383 cout << text_t2ascii << " httpweb=" << configinfo.httpweb << "\n";
384 cout << text_t2ascii << " gwcgi=" << configinfo.gwcgi << "\n\n"
385 << " Note that unless gwcgi has been set from a configuration\n"
386 << " file it is dependent on environment variables set by your\n"
387 << " webserver. Therefore it may not have the same value when run\n"
388 << " from the command line as it would be when run from your\n"
389 << " web server.\n";
390 if (configinfo.usecookiesForUID)
391 cout << "UID will be saved as a cookie\n";
392 else
393 cout << "UID not saved as a cookie\n";
394 if (configinfo.usecookiesForE)
395 cout << "The e arg will be saved as a cookie\n";
396 else
397 cout << "The e arg is not saved as a cookie - set usecookiesForE to true to use cookies for e \n";
398 if (configinfo.logcgiargs)
399 cout << "logging is enabled\n";
400 else
401 cout << "logging is disabled\n";
402 cout << "------------------------------------------------------------\n\n";
403
404 text_tset::const_iterator this_mfile = configinfo.macrofiles.begin();
405 text_tset::const_iterator end_mfile = configinfo.macrofiles.end();
406 cout << "Macro Files:\n"
407 << "------------\n";
408 text_t mfile;
409 bool found;
410 while (this_mfile != end_mfile) {
411 cout << text_t2ascii << *this_mfile;
412 int spaces = (22 - (*this_mfile).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 found = false;
419 if (colspec) {
420 // collection specific - try collectdir/macros first
421 mfile = filename_cat (configinfo.collectdir, "macros", *this_mfile);
422 if (file_exists (mfile)) {
423 cout << text_t2ascii << "found (" << mfile << ")\n";
424 found = true;
425 }
426 }
427
428 if (!found) {
429 // try main macro directory
430 mfile = filename_cat (configinfo.gsdlhome, "macros", *this_mfile);
431 if (file_exists (mfile)) {
432 cout << text_t2ascii << "found (" << mfile << ")\n";
433 found = true;
434 }
435 }
436
437 if (!found)
438 cout << text_t2ascii << "NOT FOUND\n";
439
440 ++this_mfile;
441 }
442
443 cout << "------------------------------------------------------------\n\n"
444 << "Collections:\n"
445 << "------------\n"
446 << " Note that collections will only appear as \"running\" if\n"
447 << " their build.cfg files exist, are readable, contain a valid\n"
448 << " builddate field (i.e. > 0), and are in the collection's\n"
449 << " index directory (i.e. NOT the building directory)\n\n";
450
451 recptprotolistclass *protos = recpt.get_recptprotolist_ptr();
452 recptprotolistclass::iterator rprotolist_here = protos->begin();
453 recptprotolistclass::iterator rprotolist_end = protos->end();
454
455 bool is_z3950 = false;
456 bool found_valid_col = false;
457
458
459 while (rprotolist_here != rprotolist_end) {
460 comerror_t err;
461 if ((*rprotolist_here).p == NULL) continue;
462 else if (is_z3950==false &&
463 (*rprotolist_here).p->get_protocol_name(err) == "z3950proto") {
464 cout << "\nZ39.50 Servers: (always public)\n"
465 << "---------------\n";
466 is_z3950=true;
467 }
468
469 text_tarray collist;
470 (*rprotolist_here).p->get_collection_list (collist, err, cerr);
471 if (err == noError) {
472 text_tarray::iterator collist_here = collist.begin();
473 text_tarray::iterator collist_end = collist.end();
474
475 while (collist_here != collist_end) {
476
477 cout << text_t2ascii << *collist_here;
478
479 int spaces = (22 - (*collist_here).size());
480 if (spaces < 2) spaces = 2;
481 text_t outspaces;
482 for (int i = 0; i < spaces; ++i) outspaces.push_back (' ');
483 cout << text_t2ascii << outspaces;
484
485 ColInfoResponse_t *cinfo = recpt.get_collectinfo_ptr ((*rprotolist_here).p, *collist_here, cerr);
486 if (cinfo != NULL) {
487 if (cinfo->isPublic) cout << "public ";
488 else cout << "private";
489
490 if (cinfo->buildDate > 0) {
491 cout << " running ";
492 found_valid_col = true;
493 } else {
494 cout << " not running";
495 }
496 }
497
498 cout << "\n";
499
500 ++collist_here;
501 }
502 }
503 is_z3950=false;
504 ++rprotolist_here;
505 } // end of while loop
506
507 if (!found_valid_col) {
508 cout << "WARNING: No \"running\" collections were found. You need to\n";
509 cout << " build one of the above collections\n";
510 }
511
512 cout << "\n------------------------------------------------------------\n";
513 cout << "------------------------------------------------------------\n\n";
514 cout << "receptionist running in command line debug mode\n";
515 cout << "enter cgi arguments as name=value pairs (e.g. 'a=p&p=home'):\n";
516
517}
518
519
520
521
522void add_all_actions(receptionist& recpt, userdbclass* udb, keydbclass* kdb, isPersistentEnum isPersistentVal)
523{
524 // the list of actions.
525
526#ifdef GSDL_USE_TIP_ACTION
527 tipaction* atipaction = new tipaction();
528 recpt.add_action (atipaction);
529#endif
530
531#ifdef GSDL_USE_STATUS_ACTION
532 statusaction *astatusaction = new statusaction();
533 astatusaction->set_receptionist (&recpt);
534 recpt.add_action (astatusaction);
535#endif
536
537 pageaction *apageaction = new pageaction();
538 apageaction->set_receptionist (&recpt);
539 recpt.add_action (apageaction);
540
541#ifdef GSDL_USE_PING_ACTION
542 recpt.add_action (new pingaction());
543#endif
544
545 ispersistentaction *aIsPersistentAction = new ispersistentaction(isPersistentVal);
546 recpt.add_action (aIsPersistentAction);
547
548#if defined(USE_RSS)
549 rssaction *arssaction = new rssaction();
550 recpt.add_action (arssaction);
551#endif
552
553 queryaction *aqueryaction = new queryaction();
554 aqueryaction->set_userdb(udb);
555 aqueryaction->set_receptionist (&recpt);
556 recpt.add_action (aqueryaction);
557
558#if defined(USE_SQLITE)
559 sqlqueryaction *asqlqueryaction = new sqlqueryaction();
560 asqlqueryaction->set_receptionist (&recpt);
561 recpt.add_action (asqlqueryaction);
562#endif
563
564 documentaction *adocumentaction = new documentaction();
565 adocumentaction->set_receptionist (&recpt);
566 recpt.add_action (adocumentaction);
567
568#ifdef GSDL_USE_USERS_ACTION
569 usersaction *ausersaction = new usersaction();
570 ausersaction->set_userdb(udb);
571 recpt.add_action (ausersaction);
572#endif
573
574#ifdef GSDL_USE_EXTLINK_ACTION
575 extlinkaction *aextlinkaction = new extlinkaction();
576 aextlinkaction->set_receptionist(&recpt);
577 recpt.add_action (aextlinkaction);
578#endif
579
580#ifdef GSDL_USE_AUTHEN_ACTION
581 authenaction *aauthenaction = new authenaction();
582 aauthenaction->set_userdb(udb);
583 aauthenaction->set_keydb(kdb);
584 aauthenaction->set_receptionist(&recpt);
585 recpt.add_action (aauthenaction);
586#endif
587
588#ifdef GSDL_USE_COLLECTOR_ACTION
589 collectoraction *acollectoraction = new collectoraction();
590 acollectoraction->set_receptionist (&recpt);
591 recpt.add_action(acollectoraction);
592#endif
593
594#ifdef GSDL_USE_DEPOSITOR_ACTION
595 depositoraction *adepositoraction = new depositoraction();
596 adepositoraction->set_receptionist (&recpt);
597 recpt.add_action(adepositoraction);
598#endif
599
600#ifdef GSDL_USE_BROWSE_ACTION
601 browseaction *abrowseaction = new browseaction();
602 abrowseaction->set_receptionist (&recpt);
603 recpt.add_action(abrowseaction);
604#endif
605
606#ifdef GSDL_USE_PHIND_ACTION
607 // Phind uses MPPP,do we also need to check if ENABLE_MGPP is set??
608 phindaction *aphindaction = new phindaction();
609 recpt.add_action(aphindaction);
610#endif
611
612#ifdef GSDL_USE_GTI_ACTION
613 gtiaction *agtiaction = new gtiaction();
614 agtiaction->set_receptionist(&recpt);
615 recpt.add_action(agtiaction);
616#endif
617
618 dynamicclassifieraction *adynamicclassifieraction = new dynamicclassifieraction();
619 adynamicclassifieraction->set_receptionist(&recpt);
620 recpt.add_action(adynamicclassifieraction);
621
622#if defined(USE_MYSQL) || defined(USE_ACCESS)
623 orderaction *aorderaction = new orderaction();
624 aorderaction->set_receptionist(&recpt);
625 recpt.add_action(aorderaction);
626#endif
627
628 // action that allows collections to be added, released etc. when server
629 // is persistent (e.g. fastcgi or when Greenstone is configured as an
630 // Apache module). Presumably this includes Windows server.exe as well
631
632 // Want to always include it in list of actions even if compiling
633 // Greenstone to be used in a non-persistent way (e.g. library.cgi).
634 // This is so the e-variable that is formed is consistent between the
635 // persisent executable and the non-persistent executable
636 //
637
638 configaction *aconfigaction = new configaction();
639 aconfigaction->set_receptionist(&recpt);
640 recpt.add_action(aconfigaction);
641}
642
643
644
645void add_all_browsers(receptionist& recpt)
646{
647 // list of browsers
648 vlistbrowserclass *avlistbrowserclass = new vlistbrowserclass();
649 avlistbrowserclass->set_receptionist(&recpt);
650 recpt.add_browser (avlistbrowserclass);
651 recpt.setdefaultbrowser ("VList");
652
653 hlistbrowserclass *ahlistbrowserclass = new hlistbrowserclass();
654 ahlistbrowserclass->set_receptionist(&recpt);
655 recpt.add_browser (ahlistbrowserclass);
656
657#ifdef GSDL_USE_DATELIST_BROWSER
658 datelistbrowserclass *adatelistbrowserclass = new datelistbrowserclass();
659 recpt.add_browser (adatelistbrowserclass);
660#endif
661
662 invbrowserclass *ainvbrowserclass = new invbrowserclass();
663 recpt.add_browser (ainvbrowserclass);
664
665#ifdef GSDL_USE_PAGED_BROWSER
666 pagedbrowserclass *apagedbrowserclass = new pagedbrowserclass();
667 recpt.add_browser (apagedbrowserclass);
668#endif
669
670#ifdef GSDL_USE_HTML_BROWSER
671 htmlbrowserclass *ahtmlbrowserclass = new htmlbrowserclass();
672 recpt.add_browser (ahtmlbrowserclass);
673#endif
674
675#ifdef GSDL_USE_PHIND_BROWSER
676 phindbrowserclass *aphindbrowserclass = new phindbrowserclass();;
677 recpt.add_browser (aphindbrowserclass);
678#endif
679}
680
681
682// cgiwrapper does everything necessary to output a page
683// using the cgi protocol. If this is being run for a particular
684// collection then "collection" should be set, otherwise it
685// should equal "".
686void cgiwrapper (receptionist &recpt, text_t collection) {
687 int numrequests = 0;
688 bool debug = false;
689 const recptconf &configinfo = recpt.get_configinfo ();
690
691 // find out whether this is being run as a cgi-script
692 // or a fastcgi script
693#ifdef USE_FASTCGI
694 fcgistreambuf outbuf;
695 int isfastcgi = !FCGX_IsCGI();
696 FCGX_Stream *fcgiin, *fcgiout, *fcgierr;
697 FCGX_ParamArray fcgienvp;
698#else
699 int isfastcgi = 0;
700#endif
701
702 // we need gsdlhome to do fileupload stuff, so moved this configure stuff before the get argstr stuff
703 // init stuff - we can't output error pages directly with
704 // fastcgi so the pages are stored until we can output them
705 text_t errorpage;
706 outconvertclass text_t2ascii;
707
708 // set defaults
709 int maxrequests = 10000;
710 recpt.configure ("collection", collection);
711 char *script_name = getenv("SCRIPT_NAME");
712 if (script_name != NULL) recpt.configure("gwcgi", script_name);
713 else recpt.configure("gwcgi", "/gsdl");
714
715 // read in the configuration files.
716 text_t gsdlhome;
717 text_t collecthome;
718 configurator gsdlconfigurator(&recpt);
719 if (!site_cfg_read (gsdlconfigurator, gsdlhome, collecthome, maxrequests)) {
720 // couldn't find the site configuration file
721 page_errorsitecfg (errorpage, debug, 0);
722 } else if (gsdlhome.empty()) {
723 // no gsdlhome in gsdlsite.cfg
724 page_errorsitecfg (errorpage, debug, 1);
725 } else if (!directory_exists(gsdlhome)) {
726 // gsdlhome not a valid directory
727 page_errorsitecfg (errorpage, debug, 1);
728 } else if (!main_cfg_read (recpt, gsdlhome, collecthome, collection)) {
729 // couldn't find the main configuration file
730 page_errormaincfg (gsdlhome, collection, debug, errorpage);
731 } else if (configinfo.collectinfo.empty() && false) { // commented out for corba
732 // don't have any collections
733 page_errorcollect (gsdlhome, errorpage, debug);
734 }
735
736 // set up the httpweb variable if it hasn't been defined yet
737 if (configinfo.httpweb.empty()) {
738 recpt.configure("httpweb", configinfo.httpprefix+"/web");
739 }
740
741 // get the query string if it is not being run as a fastcgi
742 // script
743 text_t argstr = g_EmptyText;
744 fileupload_tmap fileuploads;
745 cgiargsclass args;
746 char *aURIStr;
747 if (!isfastcgi) {
748 char *request_method_str = getenv("REQUEST_METHOD");
749 char *content_length_str = getenv("CONTENT_LENGTH");
750 if (request_method_str != NULL && strcmp(request_method_str, "POST") == 0 &&
751 content_length_str != NULL) {
752 // POST form data
753 long content_length = (content_length_str ? atoi(content_length_str) : 0);
754 if (content_length > 0) {
755#ifdef __WIN32__
756 // On Windows it is important that standard input be read in binary
757 // mode, otherwise end of line "<CR><LF>" is turned into <LF> only
758 // which breaks the MIME standard (and our parsing code!)
759
760 int result = _setmode( _fileno( stdin ), _O_BINARY );
761 if( result == -1 ) {
762 cerr << "Warning: Failed to set standard input to binary mode." << endl;
763 cerr << " Parsing of multi-part MIME will most likely fail" << endl;
764 }
765#endif
766
767 long length = content_length;
768 unsigned char * buffer = new unsigned char[content_length];
769
770 int chars_read = fread(buffer,1,content_length,stdin);
771
772 if (chars_read != content_length) {
773 cerr << "Warning: mismatch between CONTENT_LENGTH and data read from standard in" << endl;
774 }
775
776 argstr.setcarr((char *)buffer, content_length);
777
778 text_t content_type;
779 char *content_type_str = getenv("CONTENT_TYPE");
780 if (content_type_str) content_type = content_type_str;
781 argstr = parse_post_data(content_type, argstr, fileuploads, gsdlhome);
782 }
783 } else {
784 aURIStr = getenv("QUERY_STRING");
785 if ((request_method_str != NULL && strcmp(request_method_str, "GET") == 0)
786 || aURIStr != NULL) {
787 // GET form data
788 if (aURIStr != NULL) argstr = aURIStr;
789 //kjdon a get form is not unicode
790 argstr.setencoding(1);
791 } else {
792 // debugging from command line
793 debug = true;
794 }
795 }
796 }
797
798 if (debug) {
799 cout << "Configuring Greenstone...\n";
800 cout << flush;
801 }
802
803
804 if (errorpage.empty()) {
805
806 // initialise the library software
807 if (debug) {
808 cout << "Initializing...\n";
809 cout << flush;
810 }
811
812 text_t error_file = filename_cat (gsdlhome, "etc", "error.txt");
813 char *eout = error_file.getcstr();
814 ofstream errout (eout, ios::app);
815 delete []eout;
816 if (!recpt.init(errout)) {
817 // an error occurred during the initialisation
818 errout.close();
819 page_errorinit(gsdlhome, debug, errorpage);
820 }
821 errout.close();
822 }
823
824 if (debug && errorpage.empty()) {
825 // get query string from command line
826 print_debug_info (recpt);
827 char cinURIStr[1024];
828 cin.get(cinURIStr, 1024);
829 argstr = cinURIStr;
830 }
831
832 // cgi scripts only deal with one request
833 if (!isfastcgi) maxrequests = 1;
834
835 // Page-request loop. If this is not being run as a fastcgi
836 // process then only one request will be processed and then
837 // the process will exit.
838 while (numrequests < maxrequests) {
839#ifdef USE_FASTCGI
840 if (isfastcgi) {
841 if (FCGX_Accept(&fcgiin, &fcgiout, &fcgierr, &fcgienvp) < 0) break;
842
843 char *request_method_str = FCGX_GetParam ("REQUEST_METHOD", fcgienvp);
844 char *content_length_str = FCGX_GetParam ("CONTENT_LENGTH", fcgienvp);
845
846 if (request_method_str != NULL && strcmp(request_method_str, "POST") == 0 &&
847 content_length_str != NULL) {
848 // POST form data
849 int content_length = text_t(content_length_str).getint();
850 if (content_length > 0) {
851 argstr.clear();
852 int c;
853 do {
854 c = FCGX_GetChar (fcgiin);
855 if (c < 0) break;
856 argstr.push_back (c);
857 --content_length;
858 } while (content_length > 0);
859 }
860
861 } else {
862 // GET form data
863 aURIStr = FCGX_GetParam("QUERY_STRING", fcgienvp);
864 if (aURIStr != NULL) argstr = aURIStr;
865 else argstr = g_EmptyText;
866 }
867 }
868#endif
869
870 // get output streams ready
871#ifdef USE_FASTCGI
872 outbuf.fcgisbreset ();
873 if (isfastcgi) outbuf.set_fcgx_stream (fcgiout);
874 else outbuf.set_other_ostream (&cout);
875 ostream pageout (&outbuf);
876#else
877#define pageout cout
878#endif
879
880 // if using fastcgi we'll load environment into a map,
881 // otherwise simply pass empty map (can't get environment
882 // variables using getenv() while using FCGX versions
883 // of fastcgi - at least I can't ;-) - Stefan)
884 text_tmap fastcgienv;
885#ifdef USE_FASTCGI
886 if (isfastcgi) {
887 for(; *fcgienvp != NULL; ++fcgienvp) {
888 text_t fvalue = *fcgienvp;
889 text_t::const_iterator begin = fvalue.begin();
890 text_t::const_iterator end = fvalue.end();
891 text_t::const_iterator equals_sign = findchar (begin, end, '=');
892 if (equals_sign != end)
893 fastcgienv[substr(begin, equals_sign)] = substr(equals_sign+1, end);
894 }
895 }
896#endif
897
898 // temporarily need to configure gwcgi here when using fastcgi as I can't
899 // get it to pass the SCRIPT_NAME environment variable to the initial
900 // environment (if anyone can work out how to do this using the apache
901 // server, let me know). Note that this overrides the gwcgi field in
902 // site.cfg (which it shouldn't do) but I can't at present set gwcgi
903 // from site.cfg as I have old receptionists laying around that wouldn't
904 // appreciate it. The following 5 lines of code should be deleted once
905 // I either a: get the server to pass SCRIPT_NAME at initialization
906 // time or b: convert all the collections using old receptionists over
907 // to this version and uncomment gwcgi in the site.cfg file -- Stefan.
908#ifdef USE_FASTCGI
909 if (isfastcgi) {
910 recpt.configure("gwcgi", fastcgienv["SCRIPT_NAME"]);
911 }
912#endif
913
914
915 // if there has been no error so far, perform the production of the
916 // output page
917 if (errorpage.empty()) {
918 text_t error_file = filename_cat (gsdlhome, "etc", "error.txt");
919 char *eout = error_file.getcstr();
920 ofstream errout (eout, ios::app);
921 delete []eout;
922
923#if defined(__WIN32__) && defined(GSDL_USE_IOS_H)
924 // old Windows compilers (VC++4.2)
925 cerr = errout;
926#else
927 // can't do this anymore according to c++ standard...
928 // cerr = errout;
929 // ... but can do this instead
930 streambuf* errbuf = cerr.rdbuf(errout.rdbuf());
931#endif
932
933 // parse the cgi arguments and produce the resulting page if there
934 // has been no errors so far
935 if (!recpt.parse_cgi_args (argstr, fileuploads, args, errout, fastcgienv)) {
936 errout.close ();
937 page_errorparseargs(gsdlhome, debug, errorpage);
938 } else {
939 // produce the output page
940
941 if (!recpt.produce_cgi_page (args, pageout, errout, fastcgienv)) {
942 errout.close ();
943 page_errorcgipage(gsdlhome, debug, errorpage);
944 }
945 recpt.log_cgi_args (args, errout, fastcgienv);
946 errout.close ();
947 }
948
949#if !defined(__WIN32__) || !defined(GSDL_USE_IOS_H)
950 // restore the cerr buffer
951 cerr.rdbuf(errbuf);
952#endif
953 }
954 // clean up any files that were uploaded
955 fileupload_tmap::const_iterator this_file = fileuploads.begin();
956 fileupload_tmap::const_iterator end_file = fileuploads.end();
957 while (this_file != end_file)
958 {
959 if (file_exists((*this_file).second.tmp_name))
960 {
961 char *thefile = (*this_file).second.tmp_name.getcstr();
962 unlink(thefile);
963 delete [] thefile;
964 }
965 ++this_file;
966 }
967
968 // there was an error, output the error page
969 if (!errorpage.empty()) {
970 pageout << text_t2ascii << errorpage;
971 errorpage.clear();
972 numrequests = maxrequests; // make this the last page
973 }
974 pageout << flush;
975
976 // finish with the output streams
977#ifdef USE_FASTCGI
978 if (isfastcgi) FCGX_Finish();
979#endif
980
981 ++numrequests;
982 }
983
984 return;
985}
Note: See TracBrowser for help on using the repository browser.