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