[144] | 1 | /**********************************************************************
|
---|
| 2 | *
|
---|
| 3 | * cgiwrapper.cpp -- output pages using the cgi protocol
|
---|
| 4 | * Copyright (C) 1999 The New Zealand Digital Library Project
|
---|
| 5 | *
|
---|
[533] | 6 | * A component of the Greenstone digital library software
|
---|
| 7 | * from the New Zealand Digital Library Project at the
|
---|
| 8 | * University of Waikato, New Zealand.
|
---|
[144] | 9 | *
|
---|
[533] | 10 | * This program is free software; you can redistribute it and/or modify
|
---|
| 11 | * it under the terms of the GNU General Public License as published by
|
---|
| 12 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 13 | * (at your option) any later version.
|
---|
| 14 | *
|
---|
| 15 | * This program is distributed in the hope that it will be useful,
|
---|
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 18 | * GNU General Public License for more details.
|
---|
| 19 | *
|
---|
| 20 | * You should have received a copy of the GNU General Public License
|
---|
| 21 | * along with this program; if not, write to the Free Software
|
---|
| 22 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 23 | *
|
---|
[144] | 24 | *********************************************************************/
|
---|
| 25 |
|
---|
[12794] | 26 | #include <stdio.h>
|
---|
| 27 | #ifdef __WIN32__
|
---|
| 28 | #include <fcntl.h>
|
---|
| 29 | #endif
|
---|
| 30 |
|
---|
[144] | 31 | #include "gsdlconf.h"
|
---|
| 32 | #include "cgiwrapper.h"
|
---|
[15402] | 33 | #include "gsdlsitecfg.h"
|
---|
| 34 | #include "maincfg.h"
|
---|
[963] | 35 | #include "fileutil.h"
|
---|
[12514] | 36 | #include "cgiutils.h"
|
---|
[144] | 37 | #include <stdlib.h>
|
---|
[872] | 38 | #include <assert.h>
|
---|
[144] | 39 |
|
---|
| 40 | #if defined(GSDL_USE_OBJECTSPACE)
|
---|
| 41 | # include <ospace/std/iostream>
|
---|
| 42 | # include <ospace/std/fstream>
|
---|
| 43 | #elif defined(GSDL_USE_IOS_H)
|
---|
| 44 | # include <iostream.h>
|
---|
| 45 | # include <fstream.h>
|
---|
| 46 | #else
|
---|
| 47 | # include <iostream>
|
---|
| 48 | # include <fstream>
|
---|
| 49 | #endif
|
---|
| 50 |
|
---|
| 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";
|
---|
[15589] | 314 | if (!configinfo.dbhome.empty())
|
---|
| 315 | cout << text_t2ascii << "dbhome=" << configinfo.dbhome << "\n";
|
---|
[1089] | 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 | int numrequests = 0;
|
---|
| 455 | bool debug = false;
|
---|
[1864] | 456 | const recptconf &configinfo = recpt.get_configinfo ();
|
---|
[1089] | 457 |
|
---|
[144] | 458 | // find out whether this is being run as a cgi-script
|
---|
| 459 | // or a fastcgi script
|
---|
| 460 | #ifdef USE_FASTCGI
|
---|
[1089] | 461 | fcgistreambuf outbuf;
|
---|
[144] | 462 | int isfastcgi = !FCGX_IsCGI();
|
---|
| 463 | FCGX_Stream *fcgiin, *fcgiout, *fcgierr;
|
---|
| 464 | FCGX_ParamArray fcgienvp;
|
---|
| 465 | #else
|
---|
| 466 | int isfastcgi = 0;
|
---|
| 467 | #endif
|
---|
| 468 |
|
---|
[12514] | 469 | // we need gsdlhome to do fileupload stuff, so moved this configure stuff before the get argstr stuff
|
---|
[1089] | 470 | // init stuff - we can't output error pages directly with
|
---|
| 471 | // fastcgi so the pages are stored until we can output them
|
---|
| 472 | text_t errorpage;
|
---|
| 473 | outconvertclass text_t2ascii;
|
---|
| 474 |
|
---|
| 475 | // set defaults
|
---|
| 476 | int maxrequests = 10000;
|
---|
| 477 | recpt.configure ("collection", collection);
|
---|
| 478 | recpt.configure ("httpimg", "/gsdl/images");
|
---|
| 479 | char *script_name = getenv("SCRIPT_NAME");
|
---|
| 480 | if (script_name != NULL) recpt.configure("gwcgi", script_name);
|
---|
[2344] | 481 | else recpt.configure("gwcgi", "/gsdl");
|
---|
[1089] | 482 |
|
---|
| 483 | // read in the configuration files.
|
---|
| 484 | text_t gsdlhome;
|
---|
[15402] | 485 | configurator gsdlconfigurator(&recpt);
|
---|
| 486 | if (!site_cfg_read (gsdlconfigurator, gsdlhome, maxrequests)) {
|
---|
[1089] | 487 | // couldn't find the site configuration file
|
---|
| 488 | page_errorsitecfg (errorpage, debug, 0);
|
---|
| 489 | } else if (gsdlhome.empty()) {
|
---|
| 490 | // no gsdlhome in gsdlsite.cfg
|
---|
| 491 | page_errorsitecfg (errorpage, debug, 1);
|
---|
[2344] | 492 | } else if (!directory_exists(gsdlhome)) {
|
---|
| 493 | // gsdlhome not a valid directory
|
---|
| 494 | page_errorsitecfg (errorpage, debug, 1);
|
---|
[1089] | 495 | } else if (!main_cfg_read (recpt, gsdlhome, collection)) {
|
---|
| 496 | // couldn't find the main configuration file
|
---|
| 497 | page_errormaincfg (gsdlhome, collection, debug, errorpage);
|
---|
[2344] | 498 | } else if (configinfo.collectinfo.empty() && false) { // commented out for corba
|
---|
[1089] | 499 | // don't have any collections
|
---|
| 500 | page_errorcollect (gsdlhome, errorpage, debug);
|
---|
[144] | 501 | }
|
---|
[12514] | 502 | // get the query string if it is not being run as a fastcgi
|
---|
| 503 | // script
|
---|
| 504 | text_t argstr = g_EmptyText;
|
---|
| 505 | fileupload_tmap fileuploads;
|
---|
| 506 | cgiargsclass args;
|
---|
| 507 | char *aURIStr;
|
---|
| 508 | if (!isfastcgi) {
|
---|
| 509 | char *request_method_str = getenv("REQUEST_METHOD");
|
---|
| 510 | char *content_length_str = getenv("CONTENT_LENGTH");
|
---|
| 511 | if (request_method_str != NULL && strcmp(request_method_str, "POST") == 0 &&
|
---|
| 512 | content_length_str != NULL) {
|
---|
| 513 | // POST form data
|
---|
| 514 | long content_length = (content_length_str ? atoi(content_length_str) : 0);
|
---|
| 515 | if (content_length > 0) {
|
---|
[12794] | 516 | #ifdef __WIN32__
|
---|
| 517 | // On Windows it is important that standard input be read in binary
|
---|
| 518 | // mode, otherwise end of line "<CR><LF>" is turned into <LF> only
|
---|
| 519 | // which breaks the MIME standard (and our parsing code!)
|
---|
| 520 |
|
---|
| 521 | int result = _setmode( _fileno( stdin ), _O_BINARY );
|
---|
| 522 | if( result == -1 ) {
|
---|
| 523 | cerr << "Warning: Failed to set standard input to binary mode." << endl;
|
---|
| 524 | cerr << " Parsing of multi-part MIME will most likely fail" << endl;
|
---|
| 525 | }
|
---|
| 526 | #endif
|
---|
| 527 |
|
---|
[12514] | 528 | long length = content_length;
|
---|
| 529 | unsigned char * buffer = new unsigned char[content_length];
|
---|
[12794] | 530 |
|
---|
| 531 | int chars_read = fread(buffer,1,content_length,stdin);
|
---|
| 532 |
|
---|
| 533 | if (chars_read != content_length) {
|
---|
| 534 | cerr << "Warning: mismatch between CONTENT_LENGTH and data read from standard in" << endl;
|
---|
| 535 | }
|
---|
| 536 |
|
---|
[12514] | 537 | argstr.setcarr((char *)buffer, content_length);
|
---|
[12794] | 538 |
|
---|
[12514] | 539 | text_t content_type;
|
---|
| 540 | char *content_type_str = getenv("CONTENT_TYPE");
|
---|
| 541 | if (content_type_str) content_type = content_type_str;
|
---|
| 542 | argstr = parse_post_data(content_type, argstr, fileuploads, gsdlhome);
|
---|
| 543 | }
|
---|
| 544 | } else {
|
---|
| 545 | aURIStr = getenv("QUERY_STRING");
|
---|
| 546 | if ((request_method_str != NULL && strcmp(request_method_str, "GET") == 0)
|
---|
| 547 | || aURIStr != NULL) {
|
---|
| 548 | // GET form data
|
---|
| 549 | if (aURIStr != NULL) argstr = aURIStr;
|
---|
| 550 | } else {
|
---|
| 551 | // debugging from command line
|
---|
| 552 | debug = true;
|
---|
| 553 | }
|
---|
| 554 | }
|
---|
| 555 | }
|
---|
[144] | 556 |
|
---|
[12514] | 557 | if (debug) {
|
---|
| 558 | cout << "Configuring Greenstone...\n";
|
---|
| 559 | cout << flush;
|
---|
| 560 | }
|
---|
| 561 |
|
---|
| 562 |
|
---|
[1089] | 563 | if (errorpage.empty()) {
|
---|
| 564 |
|
---|
| 565 | // initialise the library software
|
---|
[1097] | 566 | if (debug) {
|
---|
| 567 | cout << "Initializing...\n";
|
---|
| 568 | cout << flush;
|
---|
| 569 | }
|
---|
[1089] | 570 |
|
---|
[2939] | 571 | text_t error_file = filename_cat (gsdlhome, "etc", "error.txt");
|
---|
| 572 | char *eout = error_file.getcstr();
|
---|
| 573 | ofstream errout (eout, ios::app);
|
---|
[7438] | 574 | delete []eout;
|
---|
[2939] | 575 | if (!recpt.init(errout)) {
|
---|
[1089] | 576 | // an error occurred during the initialisation
|
---|
[2939] | 577 | errout.close();
|
---|
[1089] | 578 | page_errorinit(gsdlhome, debug, errorpage);
|
---|
| 579 | }
|
---|
[2939] | 580 | errout.close();
|
---|
[1089] | 581 | }
|
---|
| 582 |
|
---|
| 583 | if (debug && errorpage.empty()) {
|
---|
| 584 | // get query string from command line
|
---|
| 585 | print_debug_info (recpt);
|
---|
| 586 | char cinURIStr[1024];
|
---|
| 587 | cin.get(cinURIStr, 1024);
|
---|
| 588 | argstr = cinURIStr;
|
---|
| 589 | }
|
---|
| 590 |
|
---|
| 591 | // cgi scripts only deal with one request
|
---|
| 592 | if (!isfastcgi) maxrequests = 1;
|
---|
| 593 |
|
---|
[144] | 594 | // Page-request loop. If this is not being run as a fastcgi
|
---|
| 595 | // process then only one request will be processed and then
|
---|
| 596 | // the process will exit.
|
---|
| 597 | while (numrequests < maxrequests) {
|
---|
| 598 | #ifdef USE_FASTCGI
|
---|
| 599 | if (isfastcgi) {
|
---|
| 600 | if (FCGX_Accept(&fcgiin, &fcgiout, &fcgierr, &fcgienvp) < 0) break;
|
---|
[1248] | 601 |
|
---|
| 602 | char *request_method_str = FCGX_GetParam ("REQUEST_METHOD", fcgienvp);
|
---|
| 603 | char *content_length_str = FCGX_GetParam ("CONTENT_LENGTH", fcgienvp);
|
---|
| 604 |
|
---|
| 605 | if (request_method_str != NULL && strcmp(request_method_str, "POST") == 0 &&
|
---|
| 606 | content_length_str != NULL) {
|
---|
| 607 | // POST form data
|
---|
| 608 | int content_length = text_t(content_length_str).getint();
|
---|
| 609 | if (content_length > 0) {
|
---|
| 610 | argstr.clear();
|
---|
| 611 | int c;
|
---|
| 612 | do {
|
---|
| 613 | c = FCGX_GetChar (fcgiin);
|
---|
| 614 | if (c < 0) break;
|
---|
| 615 | argstr.push_back (c);
|
---|
[9620] | 616 | --content_length;
|
---|
[1248] | 617 | } while (content_length > 0);
|
---|
| 618 | }
|
---|
| 619 |
|
---|
| 620 | } else {
|
---|
| 621 | // GET form data
|
---|
| 622 | aURIStr = FCGX_GetParam("QUERY_STRING", fcgienvp);
|
---|
| 623 | if (aURIStr != NULL) argstr = aURIStr;
|
---|
[7438] | 624 | else argstr = g_EmptyText;
|
---|
[1248] | 625 | }
|
---|
[144] | 626 | }
|
---|
| 627 | #endif
|
---|
| 628 |
|
---|
| 629 | // get output streams ready
|
---|
| 630 | #ifdef USE_FASTCGI
|
---|
| 631 | outbuf.fcgisbreset ();
|
---|
| 632 | if (isfastcgi) outbuf.set_fcgx_stream (fcgiout);
|
---|
| 633 | else outbuf.set_other_ostream (&cout);
|
---|
| 634 | ostream pageout (&outbuf);
|
---|
| 635 | #else
|
---|
| 636 | #define pageout cout
|
---|
| 637 | #endif
|
---|
[155] | 638 |
|
---|
[872] | 639 | // if using fastcgi we'll load environment into a map,
|
---|
| 640 | // otherwise simply pass empty map (can't get environment
|
---|
| 641 | // variables using getenv() while using FCGX versions
|
---|
| 642 | // of fastcgi - at least I can't ;-) - Stefan)
|
---|
| 643 | text_tmap fastcgienv;
|
---|
| 644 | #ifdef USE_FASTCGI
|
---|
| 645 | if (isfastcgi) {
|
---|
[9620] | 646 | for(; *fcgienvp != NULL; ++fcgienvp) {
|
---|
[872] | 647 | text_t fvalue = *fcgienvp;
|
---|
| 648 | text_t::const_iterator begin = fvalue.begin();
|
---|
| 649 | text_t::const_iterator end = fvalue.end();
|
---|
| 650 | text_t::const_iterator equals_sign = findchar (begin, end, '=');
|
---|
| 651 | if (equals_sign != end)
|
---|
| 652 | fastcgienv[substr(begin, equals_sign)] = substr(equals_sign+1, end);
|
---|
| 653 | }
|
---|
| 654 | }
|
---|
| 655 | #endif
|
---|
| 656 |
|
---|
| 657 | // temporarily need to configure gwcgi here when using fastcgi as I can't
|
---|
| 658 | // get it to pass the SCRIPT_NAME environment variable to the initial
|
---|
| 659 | // environment (if anyone can work out how to do this using the apache
|
---|
| 660 | // server, let me know). Note that this overrides the gwcgi field in
|
---|
| 661 | // site.cfg (which it shouldn't do) but I can't at present set gwcgi
|
---|
| 662 | // from site.cfg as I have old receptionists laying around that wouldn't
|
---|
| 663 | // appreciate it. The following 5 lines of code should be deleted once
|
---|
| 664 | // I either a: get the server to pass SCRIPT_NAME at initialization
|
---|
| 665 | // time or b: convert all the collections using old receptionists over
|
---|
| 666 | // to this version and uncomment gwcgi in the site.cfg file -- Stefan.
|
---|
| 667 | #ifdef USE_FASTCGI
|
---|
| 668 | if (isfastcgi) {
|
---|
| 669 | recpt.configure("gwcgi", fastcgienv["SCRIPT_NAME"]);
|
---|
| 670 | }
|
---|
| 671 | #endif
|
---|
| 672 |
|
---|
| 673 |
|
---|
[1860] | 674 | // if there has been no error so far, perform the production of the
|
---|
| 675 | // output page
|
---|
[155] | 676 | if (errorpage.empty()) {
|
---|
[2939] | 677 | text_t error_file = filename_cat (gsdlhome, "etc", "error.txt");
|
---|
[963] | 678 | char *eout = error_file.getcstr();
|
---|
[1255] | 679 | ofstream errout (eout, ios::app);
|
---|
[7438] | 680 | delete []eout;
|
---|
[3006] | 681 |
|
---|
[3026] | 682 | #if defined(__WIN32__) && defined(GSDL_USE_IOS_H)
|
---|
| 683 | // old Windows compilers (VC++4.2)
|
---|
| 684 | cerr = errout;
|
---|
| 685 | #else
|
---|
[3006] | 686 | // can't do this anymore according to c++ standard...
|
---|
[3381] | 687 | // cerr = errout;
|
---|
[3006] | 688 | // ... but can do this instead
|
---|
[3396] | 689 | streambuf* errbuf = cerr.rdbuf(errout.rdbuf());
|
---|
[3026] | 690 | #endif
|
---|
[3006] | 691 |
|
---|
[155] | 692 | // parse the cgi arguments and produce the resulting page if there
|
---|
| 693 | // has been no errors so far
|
---|
[12514] | 694 | if (!recpt.parse_cgi_args (argstr, fileuploads, args, errout, fastcgienv)) {
|
---|
[155] | 695 | errout.close ();
|
---|
[1089] | 696 | page_errorparseargs(gsdlhome, debug, errorpage);
|
---|
[155] | 697 | } else {
|
---|
[1860] | 698 | // produce the output page
|
---|
| 699 |
|
---|
[872] | 700 | if (!recpt.produce_cgi_page (args, pageout, errout, fastcgienv)) {
|
---|
[155] | 701 | errout.close ();
|
---|
[1089] | 702 | page_errorcgipage(gsdlhome, debug, errorpage);
|
---|
[155] | 703 | }
|
---|
[872] | 704 | recpt.log_cgi_args (args, errout, fastcgienv);
|
---|
[1089] | 705 | errout.close ();
|
---|
[155] | 706 | }
|
---|
[3381] | 707 |
|
---|
| 708 | #if !defined(__WIN32__) || !defined(GSDL_USE_IOS_H)
|
---|
| 709 | // restore the cerr buffer
|
---|
| 710 | cerr.rdbuf(errbuf);
|
---|
| 711 | #endif
|
---|
[155] | 712 | }
|
---|
[12514] | 713 | // clean up any files that were uploaded
|
---|
| 714 | fileupload_tmap::const_iterator this_file = fileuploads.begin();
|
---|
| 715 | fileupload_tmap::const_iterator end_file = fileuploads.end();
|
---|
| 716 | while (this_file != end_file)
|
---|
| 717 | {
|
---|
| 718 | if (file_exists((*this_file).second.tmp_name))
|
---|
| 719 | {
|
---|
| 720 | char *thefile = (*this_file).second.tmp_name.getcstr();
|
---|
| 721 | unlink(thefile);
|
---|
| 722 | delete [] thefile;
|
---|
| 723 | }
|
---|
| 724 | ++this_file;
|
---|
| 725 | }
|
---|
| 726 |
|
---|
[155] | 727 | // there was an error, output the error page
|
---|
| 728 | if (!errorpage.empty()) {
|
---|
| 729 | pageout << text_t2ascii << errorpage;
|
---|
| 730 | errorpage.clear();
|
---|
| 731 | numrequests = maxrequests; // make this the last page
|
---|
| 732 | }
|
---|
| 733 | pageout << flush;
|
---|
[144] | 734 |
|
---|
| 735 | // finish with the output streams
|
---|
| 736 | #ifdef USE_FASTCGI
|
---|
| 737 | if (isfastcgi) FCGX_Finish();
|
---|
| 738 | #endif
|
---|
| 739 |
|
---|
[9620] | 740 | ++numrequests;
|
---|
[144] | 741 | }
|
---|
| 742 |
|
---|
| 743 | return;
|
---|
| 744 | }
|
---|