Changeset 90


Ignore:
Timestamp:
1998-12-23T16:58:42+13:00 (25 years ago)
Author:
rjmcnab
Message:

Added support for the FastCGI protocol.

Location:
trunk/gsdl
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl/etc/site.cfg.example

    r57 r90  
     1# You should save this file as site.cfg and edit it
     2# to suit your site's configuration. This file may
     3# be placed in the GSDLHOME directory, the
     4# GSDLHOME/etc directory or in a particular collection's
     5# directory.
     6
    17gsdlhome    /home/rjmcnab/gsdl/collect/gberg
    28httpprefix  http://162.105.138.14/gberg
    39gwcgi   http://162.105.138.14/cgi-bin/gbergmain
     10
     11# maxrequests is the most requests a fastcgi process
     12# will serve before it exits. This can be set to a
     13# low figure (like 1) while debugging and then set
     14# to a high figure (like 10000) when everything is
     15# working ok.
     16maxrequests 1
  • trunk/gsdl/src/library/Makefile

    r89 r90  
    2626CC = gcc
    2727CCFLAGS = -O2
    28 DEFS = -DNZDL -DQUIET -DSHORT_SUFFIX -DPARADOCNUM
     28DEFS = -DNZDL -DQUIET -DSHORT_SUFFIX -DPARADOCNUM -DUSE_FASTCGI
    2929RANLIB = ranlib
    3030INCLUDES = -I../../lib -I../../packages/mg-1.3d -I../../packages/mg-1.3d/lib \
    31            -I../../packages/mg-1.3d/src/text
     31           -I../../packages/mg-1.3d/src/text -I../../packages/fcgi/include
    3232LDFLAGS = -lg++ -lgdbm
    3333LIBS = ../../lib/gsdllib.a
  • trunk/gsdl/src/library/cgiwrap.cpp

    r89 r90  
    1818#include "fileutil.h"
    1919
     20#ifdef USE_FASTCGI
     21#include "fcgiapp.h"
     22#endif
     23
    2024// Note: site.h would not be needed if we could
    2125// dynamically find out gsdlhome.
     
    2327
    2428
    25 static void site_cfg_read (libinterface *lib, const text_t &filename) {
     29#ifdef USE_FASTCGI
     30// used to output the text from libinterface
     31class fcgistreambuf : public streambuf {
     32public:
     33  fcgistreambuf ();
     34  int sync ();
     35  int overflow (int ch);
     36  int underflow () {return EOF;}
     37 
     38  void fcgisbreset() {fcgx_stream = NULL; other_ostream = NULL;};
     39  void set_fcgx_stream(FCGX_Stream *newone) {fcgx_stream=newone;};
     40  void set_other_ostream(ostream *newone) {other_ostream=newone;};
     41 
     42private:
     43  FCGX_Stream *fcgx_stream;
     44  ostream *other_ostream;
     45};
     46
     47fcgistreambuf::fcgistreambuf() {
     48  fcgisbreset();
     49  if (base() == ebuf()) allocate();
     50  setp (base(), ebuf());
     51};
     52
     53int fcgistreambuf::sync () {
     54  if ((fcgx_stream != NULL) &&
     55      (FCGX_PutStr (pbase(), out_waiting(), fcgx_stream) < 0)) {
     56    fcgx_stream = NULL;
     57  }
     58
     59  if (other_ostream != NULL) {
     60    char *thepbase=pbase();
     61    for (int i=0;i<out_waiting();i++) (*other_ostream).put(thepbase[i]);
     62  }
     63 
     64  setp (pbase(), epptr());
     65 
     66  return 0;
     67}
     68
     69int fcgistreambuf::overflow (int ch) {
     70  if (sync () == EOF) return EOF;
     71  if (ch != EOF) sputc (ch);
     72  return 0;
     73}
     74
     75#endif
     76
     77
     78
     79static int site_cfg_read (libinterface *lib, const text_t &filename,
     80              int &maxrequests) {
    2681  text_tarray cfgline;
    2782  char *cstr = filename.getcstr();
    2883  ifstream confin (cstr);
    2984  delete cstr;
     85
     86  maxrequests = 10000;
    3087
    3188  if (confin) {
     
    3895    else if (cfgline[0] == "httpprefix") lib->sethttpprefix(cfgline[1]);
    3996    else if (cfgline[0] == "gwcgi") lib->setgwcgi(cfgline[1]);
     97    else if (cfgline[0] == "maxrequests") {
     98      maxrequests = cfgline[1].getint();
     99      if (maxrequests < 1) maxrequests = 1;
     100    }
    40101      }
    41102    }
    42103    confin.close ();
    43   }
     104    return 1;
     105  }
     106  return 0;
    44107}
    45108
    46109// cgiwrap does everything necessary to output a page
    47 // using the cgi protocol.
     110// using the cgi (or fastcgi) protocol.
    48111int cgiwrap (libinterface *lib) {
     112#ifdef USE_FASTCGI
     113  fcgistreambuf outbuf;
     114#endif
     115
     116  // init stuff
     117
    49118  // set defaults
     119  int maxrequests = 10000;
    50120  lib->setgsdlhome (GSDLHOME);
    51121  lib->sethttpprefix("http://127.0.0.1");
    52122  lib->setgwcgi("http://127.0.0.1/cgi-bin/gw");
    53123
    54   // read in the main configuration file
    55   text_t filename = filename_cat (GSDLHOME, "etc/site.cfg");
    56   site_cfg_read (lib, filename);
    57 
    58   // read in the collection configuration file (overriding the
    59   // main configuration file)
    60   filename = filename_cat (GSDLHOME, "collect");
     124  // read in the site configuration file
     125  // try in the collection directory, then GSDLHOME/etc,
     126  // then in GSDLHOME.
     127  text_t filename = filename_cat (GSDLHOME, "collect");
    61128  filename = filename_cat (filename, lib->get_collection_name());
    62129  filename = filename_cat (filename, "site.cfg");
    63   site_cfg_read (lib, filename);
     130  if (!site_cfg_read (lib, filename, maxrequests)) {
     131    filename = filename_cat (GSDLHOME, "etc/site.cfg");
     132    if (!site_cfg_read (lib, filename, maxrequests)) {
     133      filename = filename_cat (GSDLHOME, "site.cfg");
     134      site_cfg_read (lib, filename, maxrequests);
     135    }
     136  }
    64137
    65138  // initialise the library software
    66139  lib->init();
    67140 
    68   // get the query arguments from the environment
    69   // or from standard input
     141  // find out whether this is being run as a cgi-script
     142  // or a fastcgi script
     143  int numrequests = 0;
     144#ifdef USE_FASTCGI
     145  int isfastcgi = !FCGX_IsCGI();
     146  FCGX_Stream *fcgiin, *fcgiout, *fcgierr;
     147  FCGX_ParamArray fcgienvp;
     148#else
     149  int isfastcgi = 0;
     150#endif
     151
     152  // get the query string if it is not being run as a fastcgi
     153  // script
    70154  text_t argstr = "";
    71   char *aURIStr = getenv("QUERY_STRING");
    72   if (aURIStr != NULL) {
    73     // the arguments were available in the environment
    74     argstr = aURIStr;
    75   } else {
    76     // get the arguments from standard input
    77     char cinURIStr[1024];
    78     cin >> cinURIStr;
    79     argstr = cinURIStr;
    80   }
    81  
    82   // output the cgi header
    83   cout << "content-type: text/html\n\n";
    84 
    85   // redirect stderr to a file
    86   ofstream errout (GSDLHOME "/etc/errout.txt");
    87   cerr = errout;
    88 
    89   // get the query page
    90   lib->getpage (argstr, cout, errout);
    91 
    92   // close the file containing the redirected standard output
    93   errout.close();
    94 
    95   // make sure we finish with a return on an empty line
    96   cout << "\n";
     155  char *aURIStr;
     156  if (!isfastcgi) {
     157    aURIStr = getenv("QUERY_STRING");
     158    if (aURIStr != NULL) argstr = aURIStr;
     159    else {
     160      char cinURIStr[1024];
     161      cin >> cinURIStr;
     162      argstr = cinURIStr;
     163    }
     164    maxrequests = 1;
     165  }
     166
     167  // Page-request loop. If this is not being run as a fastcgi
     168  // process then only one request will be processed and then
     169  // the process will exit.
     170  while (numrequests < maxrequests) {
     171#ifdef USE_FASTCGI
     172    if (isfastcgi) {
     173      if (FCGX_Accept(&fcgiin, &fcgiout, &fcgierr, &fcgienvp) < 0) break;
     174      aURIStr = FCGX_GetParam("QUERY_STRING", fcgienvp);
     175      if (aURIStr != NULL) argstr = aURIStr;
     176      else argstr = "";
     177    }
     178#endif
     179
     180    // get output streams ready
     181#ifdef USE_FASTCGI
     182    outbuf.fcgisbreset ();
     183    if (isfastcgi) outbuf.set_fcgx_stream (fcgiout);
     184    else outbuf.set_other_ostream (&cout);
     185    ostream pageout (&outbuf);
     186#else
     187#define pageout cout
     188#endif
     189    pageout << "content-type: text/html\n\n";
     190    ofstream errout (GSDLHOME "/etc/errout.txt");
     191    cerr = errout;
     192   
     193    // get the query page
     194    lib->getpage (argstr, pageout, errout);
     195   
     196    // finish with the output streams
     197    pageout << "\n";
     198    pageout << flush;
     199    errout.close();
     200#ifdef USE_FASTCGI
     201    if (isfastcgi) FCGX_Finish();
     202#endif
     203
     204    numrequests++;
     205  }
    97206
    98207  return 1;
Note: See TracChangeset for help on using the changeset viewer.