FastCGIApplication.cc

00001 /*
00002  * $Id: FastCGIApplication.cc,v 1.7 2006/04/03 15:11:18 brook Exp $
00003  */
00004 
00005 /*
00006  * ClearSilver++ Software License.
00007  *
00008  * Copyright (c) 2006 Brook Milligan <brook@nmsu.edu>
00009  * All rights reserved.
00010  * 
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions
00013  * are met:
00014  * 
00015  * 1. Redistributions of source code must retain the above copyright
00016  *    notice, this list of conditions and the following disclaimer.
00017  * 2. Redistributions in binary form must reproduce the above
00018  *    copyright notice, this list of conditions and the following
00019  *    disclaimer in the documentation and/or other materials provided
00020  *    with the distribution.
00021  * 3. The name of the author may not be used to endorse or promote
00022  *    products derived from this software without specific prior
00023  *    written permission.
00024  * 
00025  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
00026  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00027  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00028  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
00029  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00030  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00031  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00032  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00033  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036  */
00037 
00038 #include "FastCGIApplication.h"
00039 
00040 #include <stdexcept>
00041 #include <stdio.h>
00042 #include <syslog.h>
00043 #include <cgi/cgiwrap.h>
00044 #include <fcgi_stdio.h>
00045 #include "ClearSilverException.h"
00046 
00047 using namespace std;
00048 
00049 
00050                                 // I/O callback functions
00051 static void register_cb ();
00052 static int read_cb(void *data, char *buf, int buf_len);
00053 static int writef_cb(void *data, const char *fmt, va_list ap);
00054 static int write_cb(void *data, const char *buf, int buf_len);
00055 
00056 
00057 namespace ClearSilver
00058 {
00059 
00060                                 // constructors
00061   FastCGIApplication::FastCGIApplication ()
00062     : TraceObject(debug(), "FastCGIApplication"), cgi_(), hdf_()
00063   {
00064     register_cb();
00065   }
00066 
00067   FastCGIApplication::FastCGIApplication (CGIApplication::Ptr a)
00068     : TraceObject(debug(), "FastCGIApplication", "CGIApplication::Ptr"),
00069       cgi_(a), hdf_(a->hdf())
00070   {
00071     register_cb();
00072   }
00073 
00074   FastCGIApplication::FastCGIApplication (CGIApplication::Ptr a,
00075                                           const HDF& hdf)
00076     : TraceObject(debug(), "FastCGIApplication",
00077                   "CGIApplication::Ptr, const HDF&"),
00078       cgi_(a), hdf_(hdf)
00079   {
00080     register_cb();
00081   }
00082 
00083   FastCGIApplication::FastCGIApplication (const FastCGIApplication& a)
00084     : TraceObject(a), cgi_(a.cgi_), hdf_(a.hdf_)
00085   {
00086     register_cb();
00087   }
00088 
00089   FastCGIApplication::~FastCGIApplication () throw() {}
00090 
00091                                 // assignment
00092   FastCGIApplication&
00093   FastCGIApplication::operator = (const FastCGIApplication& a)
00094   {
00095     TraceObject::operator = (a);
00096     FastCGIApplication a_(a);
00097     swap (a_);
00098     return *this;
00099   }
00100 
00101                                 // swap contents
00102   void
00103   FastCGIApplication::swap (FastCGIApplication& a) throw()
00104   {
00105     TraceObject::swap (a);
00106     cgi_.swap (a.cgi_);
00107     hdf_.swap (a.hdf_);
00108   }
00109 
00110 
00111                                 // CGI application entry point
00112   void
00113   FastCGIApplication::operator () ()
00114   {
00115     while (FCGI_Accept() >= 0)
00116       {
00117         try
00118           {
00119             CGI cgi (hdf_);     // replace CGI environment in application
00120             *cgi_ = cgi;
00121             (*cgi_)();          // execute CGI application
00122           }
00123         catch (const ClearSilverException& e)
00124           {
00125             syslog(LOG_ERR, "ClearSilver exception:  %s", e.what());
00126             syslog(LOG_DEBUG, "ClearSilver exception:  %s", e.what());
00127           }
00128         catch (const invalid_argument& e)
00129           {
00130             syslog(LOG_ERR, "invalid_argument exception:  %s", e.what());
00131           }
00132         catch (const logic_error& e)
00133           {
00134             syslog(LOG_ERR, "logic_error exception:  %s", e.what());
00135           }
00136         catch (const exception& e)
00137           {
00138             syslog(LOG_ERR, "exception:  %s", e.what());
00139           }
00140         catch (...)
00141           {
00142             syslog(LOG_ERR, "%s", "Unknown exception caught.");
00143           }
00144         CGI cgi0;               // reset CGI environment
00145         *cgi_ = cgi0;
00146       }
00147   }
00148 
00149                                 // access the contained CGIApplication object
00150   CGIApplication::Ptr
00151   FastCGIApplication::cgi () const { return cgi_; }
00152 
00153                                 // access the contained HDF object
00154   HDF
00155   FastCGIApplication::hdf () { return hdf_; }
00156 
00157                                 // debug flag.
00158   bool&
00159   FastCGIApplication::debug ()
00160   {
00161     static bool debug_ = false;
00162     return debug_;
00163   }
00164 
00165 };                              // namespace ClearSilver
00166 
00167 
00168                                 // I/O callback functions
00169                                 // register all the callback functions
00170                                 // with the ClearSilver C library
00171 void
00172 register_cb ()
00173 {
00174   cgiwrap_init_emu
00175     (
00176      0,                 // data - user data passed to the callbacks
00177      read_cb,           // read_cb - replace fread(stdin)
00178      writef_cb,         // writef_cb - replace fprintf(stdout)
00179      write_cb,          // write_cb - replace fwrite(stdout)
00180      0,                 // getenv_cb - replace getenv()
00181      0,                 // putenv_cb - replace putenv()
00182      0                  // iterenv_cb - replace default environment iteration
00183                         //              function (i.e., walk the envp array)
00184      );
00185 }
00186 
00187 int
00188 read_cb(void *data, char *buf, int buf_len)
00189 {
00190   return fread(buf, sizeof(char), buf_len, stdin);
00191 }
00192 
00193 int
00194 writef_cb(void *data, const char *fmt, va_list ap)
00195 {
00196   vprintf (fmt, ap);
00197 
00198   return 0;
00199 }
00200 
00201 int
00202 write_cb(void *data, const char *buf, int buf_len)
00203 {
00204   return fwrite((char *)buf, sizeof(char), buf_len, stdout);
00205 }
00206 

Generated on Tue May 16 14:50:52 2006 for ClearSilver C++ Library by  doxygen 1.4.5