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