1 | /**********************************************************************
|
---|
2 | *
|
---|
3 | * cgiwrapper.cpp -- output pages using the cgi protocol
|
---|
4 | * Copyright (C) 1999 The New Zealand Digital Library Project
|
---|
5 | *
|
---|
6 | * A component of the Greenstone digital library software
|
---|
7 | * from the New Zealand Digital Library Project at the
|
---|
8 | * University of Waikato, New Zealand.
|
---|
9 | *
|
---|
10 | * This program is free software; you can redistribute it and/or modify
|
---|
11 | * it under the terms of the GNU General Public License as published by
|
---|
12 | * the Free Software Foundation; either version 2 of the License, or
|
---|
13 | * (at your option) any later version.
|
---|
14 | *
|
---|
15 | * This program is distributed in the hope that it will be useful,
|
---|
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
18 | * GNU General Public License for more details.
|
---|
19 | *
|
---|
20 | * You should have received a copy of the GNU General Public License
|
---|
21 | * along with this program; if not, write to the Free Software
|
---|
22 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
23 | *
|
---|
24 | *********************************************************************/
|
---|
25 |
|
---|
26 | #include <stdio.h>
|
---|
27 | #ifdef __WIN32__
|
---|
28 | #include <fcntl.h>
|
---|
29 | #endif
|
---|
30 |
|
---|
31 | #include "gsdlconf.h"
|
---|
32 | #include "cgiwrapper.h"
|
---|
33 | #include "gsdlsitecfg.h"
|
---|
34 | #include "maincfg.h"
|
---|
35 | #include "fileutil.h"
|
---|
36 | #include "cgiutils.h"
|
---|
37 | #include <stdlib.h>
|
---|
38 | #include <assert.h>
|
---|
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 |
|
---|
55 |
|
---|
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();
|
---|
88 | for (int i=0;i<out_waiting();++i) (*other_ostream).put(thepbase[i]);
|
---|
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 |
|
---|
104 | static void format_error_string (text_t &errorpage, const text_t &errortext, bool debug) {
|
---|
105 |
|
---|
106 | errorpage.clear();
|
---|
107 |
|
---|
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 |
|
---|
146 | if (mode == 0) {
|
---|
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 |
|
---|
151 | } else if (mode == 1) {
|
---|
152 | errortext += "The gsdlsite.cfg configuration file does not contain a valid\n";
|
---|
153 | errortext += "gsdlhome entry.\n";
|
---|
154 | }
|
---|
155 |
|
---|
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 |
|
---|
164 | format_error_string (errorpage, errortext, debug);
|
---|
165 | }
|
---|
166 |
|
---|
167 |
|
---|
168 | static void page_errormaincfg (const text_t &gsdlhome, const text_t &collection,
|
---|
169 | bool debug, text_t &errorpage) {
|
---|
170 |
|
---|
171 | text_t errortext;
|
---|
172 |
|
---|
173 | if (collection.empty()) {
|
---|
174 | text_t main_cfg_file = filename_cat (gsdlhome, "etc", "main.cfg");
|
---|
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";
|
---|
180 | } else {
|
---|
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");
|
---|
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";
|
---|
190 | }
|
---|
191 |
|
---|
192 | format_error_string (errorpage, errortext, debug);
|
---|
193 | }
|
---|
194 |
|
---|
195 |
|
---|
196 | static void page_errorinit (const text_t &gsdlhome, bool debug, text_t &errorpage) {
|
---|
197 |
|
---|
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";
|
---|
201 |
|
---|
202 | text_t error_file = filename_cat (gsdlhome, "etc", "error.txt");
|
---|
203 | char *efile = error_file.getcstr();
|
---|
204 | ifstream errin (efile);
|
---|
205 | delete []efile;
|
---|
206 | if (errin) {
|
---|
207 | errortext += "The error log, " + error_file + ", contains the\n";
|
---|
208 | errortext += "following information:\n\n";
|
---|
209 | if (!debug) errortext += "<pre>\n";
|
---|
210 |
|
---|
211 | char c;
|
---|
212 | errin.get(c);
|
---|
213 | while (!errin.eof ()) {
|
---|
214 | errortext.push_back(c);
|
---|
215 | errin.get(c);
|
---|
216 | }
|
---|
217 |
|
---|
218 | if (!debug) errortext += "</pre>\n";
|
---|
219 |
|
---|
220 | errin.close();
|
---|
221 |
|
---|
222 | } else {
|
---|
223 | errortext += "Please consult " + error_file + " for more information.\n";
|
---|
224 | }
|
---|
225 |
|
---|
226 | format_error_string (errorpage, errortext, debug);
|
---|
227 | }
|
---|
228 |
|
---|
229 | static void page_errorparseargs (const text_t &gsdlhome, bool debug, text_t &errorpage) {
|
---|
230 |
|
---|
231 | text_t errortext = "An error occurred during the parsing of the cgi arguments.\n";
|
---|
232 |
|
---|
233 | text_t error_file = filename_cat (gsdlhome, "etc", "error.txt");
|
---|
234 | char *efile = error_file.getcstr();
|
---|
235 | ifstream errin (efile);
|
---|
236 | delete []efile;
|
---|
237 | if (errin) {
|
---|
238 | errortext += "The error log, " + error_file + ", contains the\n";
|
---|
239 | errortext += "following information:\n\n";
|
---|
240 | if (!debug) errortext += "<pre>\n";
|
---|
241 |
|
---|
242 | char c;
|
---|
243 | errin.get(c);
|
---|
244 | while (!errin.eof ()) {
|
---|
245 | errortext.push_back(c);
|
---|
246 | errin.get(c);
|
---|
247 | }
|
---|
248 | if (!debug) errortext += "</pre>\n";
|
---|
249 | errin.close();
|
---|
250 |
|
---|
251 | } else {
|
---|
252 | errortext += "Please consult " + error_file + " for more information.\n";
|
---|
253 | }
|
---|
254 |
|
---|
255 | format_error_string (errorpage, errortext, debug);
|
---|
256 | }
|
---|
257 |
|
---|
258 | static void page_errorcgipage (const text_t &gsdlhome, bool debug, text_t &errorpage) {
|
---|
259 |
|
---|
260 | text_t errortext = "An error occurred during the construction of the cgi page.\n";
|
---|
261 |
|
---|
262 | text_t error_file = filename_cat (gsdlhome, "etc", "error.txt");
|
---|
263 | char *efile = error_file.getcstr();
|
---|
264 | ifstream errin (efile);
|
---|
265 | delete []efile;
|
---|
266 | if (errin) {
|
---|
267 | errortext += "The error log, " + error_file + ", contains the\n";
|
---|
268 | errortext += "following information:\n\n";
|
---|
269 | if (!debug) errortext += "<pre>\n";
|
---|
270 |
|
---|
271 | char c;
|
---|
272 | errin.get(c);
|
---|
273 | while (!errin.eof ()) {
|
---|
274 | errortext.push_back(c);
|
---|
275 | errin.get(c);
|
---|
276 | }
|
---|
277 | if (!debug) errortext += "</pre>\n";
|
---|
278 | errin.close();
|
---|
279 |
|
---|
280 | } else {
|
---|
281 | errortext += "Please consult " + error_file + " for more information.\n";
|
---|
282 | }
|
---|
283 |
|
---|
284 | format_error_string (errorpage, errortext, debug);
|
---|
285 | }
|
---|
286 |
|
---|
287 | static void print_debug_info (receptionist &recpt) {
|
---|
288 |
|
---|
289 | outconvertclass text_t2ascii;
|
---|
290 | const recptconf &configinfo = recpt.get_configinfo ();
|
---|
291 | text_t etc_dir = filename_cat (configinfo.gsdlhome, "etc");
|
---|
292 |
|
---|
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"
|
---|
298 | << " initialization and error log error.txt in " << etc_dir << ".\n"
|
---|
299 | << "------------------------------------------------------------\n\n";
|
---|
300 |
|
---|
301 | bool colspec = false;
|
---|
302 | if (configinfo.collection.empty()) {
|
---|
303 | cout << "Receptionist is running in \"general\" (i.e. not \"collection\n"
|
---|
304 | << "specific\") mode.\n";
|
---|
305 | } else {
|
---|
306 | cout << text_t2ascii
|
---|
307 | << "Receptionist is running in \"collection specific\" mode.\n"
|
---|
308 | << " collection=" << configinfo.collection << "\n"
|
---|
309 | << " collection directory=" << configinfo.collectdir << "\n";
|
---|
310 | colspec = true;
|
---|
311 | }
|
---|
312 |
|
---|
313 | cout << text_t2ascii << "gsdlhome=" << configinfo.gsdlhome << "\n";
|
---|
314 | if (!configinfo.dbhome.empty())
|
---|
315 | cout << text_t2ascii << "dbhome=" << configinfo.dbhome << "\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"
|
---|
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"
|
---|
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";
|
---|
333 |
|
---|
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;
|
---|
345 | for (int i = 0; i < spaces; ++i) outspaces.push_back (' ');
|
---|
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 | }
|
---|
356 | }
|
---|
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 |
|
---|
370 | ++this_mfile;
|
---|
371 | }
|
---|
372 |
|
---|
373 | cout << "------------------------------------------------------------\n\n"
|
---|
374 | << "Collections:\n"
|
---|
375 | << "------------\n"
|
---|
376 | << " Note that collections will only appear as \"running\" if\n"
|
---|
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();
|
---|
384 |
|
---|
385 | bool is_z3950 = false;
|
---|
386 | bool found_valid_col = false;
|
---|
387 |
|
---|
388 |
|
---|
389 | while (rprotolist_here != rprotolist_end) {
|
---|
390 | comerror_t err;
|
---|
391 | if ((*rprotolist_here).p == NULL) continue;
|
---|
392 | else if (is_z3950==false &&
|
---|
393 | (*rprotolist_here).p->get_protocol_name(err) == "z3950proto") {
|
---|
394 | cout << "\nZ39.50 Servers: (always public)\n"
|
---|
395 | << "---------------\n";
|
---|
396 | is_z3950=true;
|
---|
397 | }
|
---|
398 |
|
---|
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;
|
---|
412 | for (int i = 0; i < spaces; ++i) outspaces.push_back (' ');
|
---|
413 | cout << text_t2ascii << outspaces;
|
---|
414 |
|
---|
415 | ColInfoResponse_t *cinfo = recpt.get_collectinfo_ptr ((*rprotolist_here).p, *collist_here, cerr);
|
---|
416 | if (cinfo != NULL) {
|
---|
417 | if (cinfo->isPublic) cout << "public ";
|
---|
418 | else cout << "private";
|
---|
419 |
|
---|
420 | if (cinfo->buildDate > 0) {
|
---|
421 | cout << " running ";
|
---|
422 | found_valid_col = true;
|
---|
423 | } else {
|
---|
424 | cout << " not running";
|
---|
425 | }
|
---|
426 | }
|
---|
427 |
|
---|
428 | cout << "\n";
|
---|
429 |
|
---|
430 | ++collist_here;
|
---|
431 | }
|
---|
432 | }
|
---|
433 | is_z3950=false;
|
---|
434 | ++rprotolist_here;
|
---|
435 | } // end of while loop
|
---|
436 |
|
---|
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";
|
---|
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;
|
---|
456 | const recptconf &configinfo = recpt.get_configinfo ();
|
---|
457 |
|
---|
458 | // find out whether this is being run as a cgi-script
|
---|
459 | // or a fastcgi script
|
---|
460 | #ifdef USE_FASTCGI
|
---|
461 | fcgistreambuf outbuf;
|
---|
462 | int isfastcgi = !FCGX_IsCGI();
|
---|
463 | FCGX_Stream *fcgiin, *fcgiout, *fcgierr;
|
---|
464 | FCGX_ParamArray fcgienvp;
|
---|
465 | #else
|
---|
466 | int isfastcgi = 0;
|
---|
467 | #endif
|
---|
468 |
|
---|
469 | // we need gsdlhome to do fileupload stuff, so moved this configure stuff before the get argstr stuff
|
---|
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);
|
---|
481 | else recpt.configure("gwcgi", "/gsdl");
|
---|
482 |
|
---|
483 | // read in the configuration files.
|
---|
484 | text_t gsdlhome;
|
---|
485 | configurator gsdlconfigurator(&recpt);
|
---|
486 | if (!site_cfg_read (gsdlconfigurator, gsdlhome, maxrequests)) {
|
---|
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);
|
---|
492 | } else if (!directory_exists(gsdlhome)) {
|
---|
493 | // gsdlhome not a valid directory
|
---|
494 | page_errorsitecfg (errorpage, debug, 1);
|
---|
495 | } else if (!main_cfg_read (recpt, gsdlhome, collection)) {
|
---|
496 | // couldn't find the main configuration file
|
---|
497 | page_errormaincfg (gsdlhome, collection, debug, errorpage);
|
---|
498 | } else if (configinfo.collectinfo.empty() && false) { // commented out for corba
|
---|
499 | // don't have any collections
|
---|
500 | page_errorcollect (gsdlhome, errorpage, debug);
|
---|
501 | }
|
---|
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) {
|
---|
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 |
|
---|
528 | long length = content_length;
|
---|
529 | unsigned char * buffer = new unsigned char[content_length];
|
---|
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 |
|
---|
537 | argstr.setcarr((char *)buffer, content_length);
|
---|
538 |
|
---|
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 | }
|
---|
556 |
|
---|
557 | if (debug) {
|
---|
558 | cout << "Configuring Greenstone...\n";
|
---|
559 | cout << flush;
|
---|
560 | }
|
---|
561 |
|
---|
562 |
|
---|
563 | if (errorpage.empty()) {
|
---|
564 |
|
---|
565 | // initialise the library software
|
---|
566 | if (debug) {
|
---|
567 | cout << "Initializing...\n";
|
---|
568 | cout << flush;
|
---|
569 | }
|
---|
570 |
|
---|
571 | text_t error_file = filename_cat (gsdlhome, "etc", "error.txt");
|
---|
572 | char *eout = error_file.getcstr();
|
---|
573 | ofstream errout (eout, ios::app);
|
---|
574 | delete []eout;
|
---|
575 | if (!recpt.init(errout)) {
|
---|
576 | // an error occurred during the initialisation
|
---|
577 | errout.close();
|
---|
578 | page_errorinit(gsdlhome, debug, errorpage);
|
---|
579 | }
|
---|
580 | errout.close();
|
---|
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 |
|
---|
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;
|
---|
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);
|
---|
616 | --content_length;
|
---|
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;
|
---|
624 | else argstr = g_EmptyText;
|
---|
625 | }
|
---|
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
|
---|
638 |
|
---|
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) {
|
---|
646 | for(; *fcgienvp != NULL; ++fcgienvp) {
|
---|
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 |
|
---|
674 | // if there has been no error so far, perform the production of the
|
---|
675 | // output page
|
---|
676 | if (errorpage.empty()) {
|
---|
677 | text_t error_file = filename_cat (gsdlhome, "etc", "error.txt");
|
---|
678 | char *eout = error_file.getcstr();
|
---|
679 | ofstream errout (eout, ios::app);
|
---|
680 | delete []eout;
|
---|
681 |
|
---|
682 | #if defined(__WIN32__) && defined(GSDL_USE_IOS_H)
|
---|
683 | // old Windows compilers (VC++4.2)
|
---|
684 | cerr = errout;
|
---|
685 | #else
|
---|
686 | // can't do this anymore according to c++ standard...
|
---|
687 | // cerr = errout;
|
---|
688 | // ... but can do this instead
|
---|
689 | streambuf* errbuf = cerr.rdbuf(errout.rdbuf());
|
---|
690 | #endif
|
---|
691 |
|
---|
692 | // parse the cgi arguments and produce the resulting page if there
|
---|
693 | // has been no errors so far
|
---|
694 | if (!recpt.parse_cgi_args (argstr, fileuploads, args, errout, fastcgienv)) {
|
---|
695 | errout.close ();
|
---|
696 | page_errorparseargs(gsdlhome, debug, errorpage);
|
---|
697 | } else {
|
---|
698 | // produce the output page
|
---|
699 |
|
---|
700 | if (!recpt.produce_cgi_page (args, pageout, errout, fastcgienv)) {
|
---|
701 | errout.close ();
|
---|
702 | page_errorcgipage(gsdlhome, debug, errorpage);
|
---|
703 | }
|
---|
704 | recpt.log_cgi_args (args, errout, fastcgienv);
|
---|
705 | errout.close ();
|
---|
706 | }
|
---|
707 |
|
---|
708 | #if !defined(__WIN32__) || !defined(GSDL_USE_IOS_H)
|
---|
709 | // restore the cerr buffer
|
---|
710 | cerr.rdbuf(errbuf);
|
---|
711 | #endif
|
---|
712 | }
|
---|
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 |
|
---|
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;
|
---|
734 |
|
---|
735 | // finish with the output streams
|
---|
736 | #ifdef USE_FASTCGI
|
---|
737 | if (isfastcgi) FCGX_Finish();
|
---|
738 | #endif
|
---|
739 |
|
---|
740 | ++numrequests;
|
---|
741 | }
|
---|
742 |
|
---|
743 | return;
|
---|
744 | }
|
---|