00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
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
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
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
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
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
00112 void
00113 FastCGIApplication::operator () ()
00114 {
00115 while (FCGI_Accept() >= 0)
00116 {
00117 try
00118 {
00119 CGI cgi (hdf_);
00120 *cgi_ = cgi;
00121 (*cgi_)();
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;
00145 *cgi_ = cgi0;
00146 }
00147 }
00148
00149
00150 CGIApplication::Ptr
00151 FastCGIApplication::cgi () const { return cgi_; }
00152
00153
00154 HDF
00155 FastCGIApplication::hdf () { return hdf_; }
00156
00157
00158 bool&
00159 FastCGIApplication::debug ()
00160 {
00161 static bool debug_ = false;
00162 return debug_;
00163 }
00164
00165 };
00166
00167
00168
00169
00170
00171 void
00172 register_cb ()
00173 {
00174 cgiwrap_init_emu
00175 (
00176 0,
00177 read_cb,
00178 writef_cb,
00179 write_cb,
00180 0,
00181 0,
00182 0
00183
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