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

Revision 22142, 28.9 KB (checked in by davidb, 10 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
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#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
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();
120    for (int i=0;i<out_waiting();++i) (*other_ostream).put(thepbase[i]);
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
136static void format_error_string (text_t &errorpage, const text_t &errortext, bool debug) {
137
138  errorpage.clear();
139
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
163  text_t collecthome = filename_cat (gsdlhome, "collect");
164
165  text_t errortext = "No valid collections were found: Check that your collect directory\n";
166  errortext += "(" + collecthome + ") is readable and contains at least one valid collection.\n";
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
178  if (mode == 0) {
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
183  } else if (mode == 1) {
184    errortext += "The gsdlsite.cfg configuration file does not contain a valid\n";
185    errortext += "gsdlhome entry.\n";
186  }
187
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
196  format_error_string (errorpage, errortext, debug);
197}
198
199
200static void page_errormaincfg (const text_t &gsdlhome, const text_t &collection,
201                   bool debug, text_t &errorpage) {
202
203  text_t errortext;
204
205  if (collection.empty()) {
206    text_t main_cfg_file = filename_cat (gsdlhome, "etc", "main.cfg");
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";
212  } else {
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");
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";
222  }
223
224  format_error_string (errorpage, errortext, debug);
225}
226
227
228static void page_errorinit (const text_t &gsdlhome, bool debug, text_t &errorpage) {
229
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";
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   
250    if (!debug) errortext += "</pre>\n";
251
252    errin.close();
253
254  } else {
255    errortext += "Please consult " + error_file + " for more information.\n";
256  }
257
258  format_error_string (errorpage, errortext, debug);
259}
260
261static void page_errorparseargs (const text_t &gsdlhome, bool debug, text_t &errorpage) {
262
263  text_t errortext = "An error occurred during the parsing of the cgi arguments.\n";
264
265  text_t error_file = filename_cat (gsdlhome, "etc", "error.txt");
266  char *efile = error_file.getcstr();
267  ifstream errin (efile);
268  delete []efile;
269  if (errin) {
270    errortext += "The error log, " + error_file + ", contains the\n";
271    errortext += "following information:\n\n";
272    if (!debug) errortext += "<pre>\n";
273
274    char c;
275    errin.get(c);
276    while (!errin.eof ()) {
277      errortext.push_back(c);
278      errin.get(c);
279    }
280    if (!debug) errortext += "</pre>\n";
281    errin.close();
282
283  } else {
284    errortext += "Please consult " + error_file + " for more information.\n";
285  }
286
287  format_error_string (errorpage, errortext, debug);
288}
289
290static void page_errorcgipage (const text_t &gsdlhome, bool debug, text_t &errorpage) {
291
292  text_t errortext = "An error occurred during the construction of the cgi page.\n";
293
294  text_t error_file = filename_cat (gsdlhome, "etc", "error.txt");
295  char *efile = error_file.getcstr();
296  ifstream errin (efile);
297  delete []efile;
298  if (errin) {
299    errortext += "The error log, " + error_file + ", contains the\n";
300    errortext += "following information:\n\n";
301    if (!debug) errortext += "<pre>\n";
302
303    char c;
304    errin.get(c);
305    while (!errin.eof ()) {
306      errortext.push_back(c);
307      errin.get(c);
308    }
309    if (!debug) errortext += "</pre>\n";
310    errin.close();
311
312  } else {
313    errortext += "Please consult " + error_file + " for more information.\n";
314  }
315
316  format_error_string (errorpage, errortext, debug);
317}
318
319static void print_debug_info (receptionist &recpt) {
320
321  outconvertclass text_t2ascii;
322  const recptconf &configinfo = recpt.get_configinfo ();
323  text_t etc_dir = filename_cat (configinfo.gsdlhome, "etc");
324
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"
330       << "  initialization and error log error.txt in " << etc_dir << ".\n"
331       << "------------------------------------------------------------\n\n";
332
333  bool colspec = false;
334  if (configinfo.collection.empty()) {
335    cout << "Receptionist is running in \"general\" (i.e. not \"collection "
336     << "specific\") mode.\n";
337  } else {
338    cout << text_t2ascii
339     << "Receptionist is running in \"collection specific\" mode.\n"
340     << "  collection=" << configinfo.collection << "\n"
341     << "  collection directory=" << configinfo.collectdir << "\n";
342    colspec = true;
343  }
344 
345  cout << text_t2ascii << "  gsdlhome=" << configinfo.gsdlhome << "\n";
346  if (!configinfo.collecthome.empty())
347    cout << text_t2ascii << "  collecthome=" << configinfo.collecthome << "\n";
348  if (!configinfo.dbhome.empty())
349    cout << text_t2ascii << "  dbhome=" << configinfo.dbhome << "\n";
350  cout << text_t2ascii << "  httpprefix=" << configinfo.httpprefix << "\n";
351  cout << text_t2ascii << "  httpweb=" << configinfo.httpweb << "\n";
352  cout << text_t2ascii << "  gwcgi=" << configinfo.gwcgi << "\n\n"
353       << "  Note that unless gwcgi has been set from a configuration\n"
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"
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";
367
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;
379    for (int i = 0; i < spaces; ++i) outspaces.push_back (' ');
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      }
390    }
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
404    ++this_mfile;
405  }
406
407  cout << "------------------------------------------------------------\n\n"
408       << "Collections:\n"
409       << "------------\n"
410       << "  Note that collections will only appear as \"running\" if\n"
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();
418
419  bool is_z3950 = false;
420  bool found_valid_col = false;
421
422
423  while (rprotolist_here != rprotolist_end) {
424    comerror_t err;   
425    if ((*rprotolist_here).p == NULL) continue;
426    else if (is_z3950==false &&
427         (*rprotolist_here).p->get_protocol_name(err) == "z3950proto") {
428      cout << "\nZ39.50 Servers:   (always public)\n"
429       << "---------------\n";
430      is_z3950=true;
431    }
432
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;
446    for (int i = 0; i < spaces; ++i) outspaces.push_back (' ');
447    cout << text_t2ascii << outspaces;
448   
449      ColInfoResponse_t *cinfo = recpt.get_collectinfo_ptr ((*rprotolist_here).p, *collist_here, cerr);
450      if (cinfo != NULL) {
451        if (cinfo->isPublic) cout << "public ";
452        else cout << "private";
453
454        if (cinfo->buildDate > 0) {
455          cout << "   running    ";
456          found_valid_col = true;
457        } else {
458          cout << "   not running";
459        }
460      }
461
462      cout << "\n";
463
464      ++collist_here;
465      }
466    }
467    is_z3950=false;
468    ++rprotolist_here;
469  } // end of while loop
470
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";
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
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
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;
644  const recptconf &configinfo = recpt.get_configinfo ();
645
646  // find out whether this is being run as a cgi-script
647  // or a fastcgi script
648#ifdef USE_FASTCGI
649  fcgistreambuf outbuf;
650  int isfastcgi = !FCGX_IsCGI();
651  FCGX_Stream *fcgiin, *fcgiout, *fcgierr;
652  FCGX_ParamArray fcgienvp;
653#else
654  int isfastcgi = 0;
655#endif
656
657  // we need gsdlhome to do fileupload stuff, so moved this configure stuff before the get argstr stuff
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);
668  else recpt.configure("gwcgi", "/gsdl");
669
670  // read in the configuration files.
671  text_t gsdlhome;
672  text_t collecthome;
673  configurator gsdlconfigurator(&recpt);
674  if (!site_cfg_read (gsdlconfigurator, gsdlhome, collecthome, maxrequests)) {
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);
680  } else if (!directory_exists(gsdlhome)) {
681    // gsdlhome not a valid directory
682    page_errorsitecfg (errorpage, debug, 1);
683  } else if (!main_cfg_read (recpt, gsdlhome, collecthome, collection)) {
684    // couldn't find the main configuration file
685    page_errormaincfg (gsdlhome, collection, debug, errorpage);
686  } else  if (configinfo.collectinfo.empty() && false) { // commented out for corba
687    // don't have any collections
688    page_errorcollect (gsdlhome, errorpage, debug);
689  }
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 
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) {
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
722    long length = content_length;
723    unsigned char * buffer = new unsigned char[content_length];
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
731    argstr.setcarr((char *)buffer, content_length);     
732
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  }
750
751  if (debug) {
752    cout << "Configuring Greenstone...\n";
753    cout << flush;
754  }
755
756
757  if (errorpage.empty()) {
758
759    // initialise the library software
760    if (debug) {
761      cout << "Initializing...\n";
762      cout << flush;
763    }
764
765    text_t error_file = filename_cat (gsdlhome, "etc", "error.txt");
766    char *eout = error_file.getcstr();
767    ofstream errout (eout, ios::app);
768    delete []eout;
769    if (!recpt.init(errout)) {
770      // an error occurred during the initialisation
771      errout.close();
772      page_errorinit(gsdlhome, debug, errorpage);
773    }
774    errout.close();
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
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;
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);
810        --content_length;
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;
818    else argstr = g_EmptyText;
819      }
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
832
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) {
840      for(; *fcgienvp != NULL; ++fcgienvp) {
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
868    // if there has been no error so far, perform the production of the
869    // output page
870    if (errorpage.empty()) {
871      text_t error_file = filename_cat (gsdlhome, "etc", "error.txt");
872      char *eout = error_file.getcstr();
873      ofstream errout (eout, ios::app);
874      delete []eout;
875
876#if defined(__WIN32__) && defined(GSDL_USE_IOS_H)
877      // old Windows compilers (VC++4.2)
878      cerr = errout;
879#else
880      // can't do this anymore according to c++ standard...
881      // cerr = errout;
882      // ... but can do this instead
883      streambuf* errbuf = cerr.rdbuf(errout.rdbuf());
884#endif
885
886      // parse the cgi arguments and produce the resulting page if there
887      // has been no errors so far
888      if (!recpt.parse_cgi_args (argstr, fileuploads, args, errout, fastcgienv)) {
889    errout.close ();
890    page_errorparseargs(gsdlhome, debug, errorpage);
891      } else {
892    // produce the output page
893
894    if (!recpt.produce_cgi_page (args, pageout, errout, fastcgienv)) {
895      errout.close ();
896      page_errorcgipage(gsdlhome, debug, errorpage);
897    }
898    recpt.log_cgi_args (args, errout, fastcgienv);
899    errout.close ();
900      }
901
902#if !defined(__WIN32__) || !defined(GSDL_USE_IOS_H)
903    // restore the cerr buffer
904    cerr.rdbuf(errbuf);
905#endif
906    }
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   
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;
928   
929    // finish with the output streams
930#ifdef USE_FASTCGI
931    if (isfastcgi) FCGX_Finish();
932#endif
933
934    ++numrequests;
935  }
936
937  return;
938}
Note: See TracBrowser for help on using the browser.