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

Last change on this file since 22142 was 22142, checked in by davidb, 14 years ago

For the CGI 'e' variable to be inter-changable between mod_gsdl and library.cgi then they need to have exactly the same actions. The code has been refactored so they now use a shared function to do this, the ensure this is the case.

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