X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=examples%2Fecho-cpp.cpp;fp=examples%2Fecho-cpp.cpp;h=697e529339d1beadd82d780d9d9367336718ce4c;hb=8b4c497b9e4f0865ec156bac9665754f7c83ef7e;hp=ea21e86f0723f59a409d3aa4486946ae53ee41aa;hpb=81c34d58fe30fb34a4c657e882c59cd0e0e5567c;p=catagits%2Ffcgi2.git diff --git a/examples/echo-cpp.cpp b/examples/echo-cpp.cpp index ea21e86..697e529 100644 --- a/examples/echo-cpp.cpp +++ b/examples/echo-cpp.cpp @@ -1,15 +1,15 @@ /* * A simple FastCGI application example in C++. - * - * $Id: echo-cpp.cpp,v 1.7 2001/12/07 02:28:16 robs Exp $ - * + * + * $Id: echo-cpp.cpp,v 1.8 2002/02/01 19:43:27 robs Exp $ + * * Copyright (c) 2001 Rob Saccoccio and Chelsea Networks * All rights reserved. - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: - * + * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright @@ -17,7 +17,7 @@ * documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. @@ -46,7 +46,7 @@ static const unsigned long STDIN_MAX = 1000000; static void penv(const char * const * envp) { cout << "
\n"; - for ( ; *envp; ++envp) + for ( ; *envp; ++envp) { cout << *envp << "\n"; } @@ -63,27 +63,30 @@ static long gstdin(FCGX_Request * request, char ** content) clen = strtol(clenstr, &clenstr, 10); if (*clenstr) { - cerr << "can't parse \"CONTENT_LENGTH=" - << FCGX_GetParam("CONTENT_LENGTH", request->envp) + cerr << "can't parse \"CONTENT_LENGTH=" + << FCGX_GetParam("CONTENT_LENGTH", request->envp) << "\"\n"; clen = STDIN_MAX; } - } - - // Note that *you* should not read stdin when CONTENT_LENGTH - // is missing or unparsable (this is a demo/test program). - // *always* put a cap on the amount of data that will be read - if (clen > STDIN_MAX) clen = STDIN_MAX; + // *always* put a cap on the amount of data that will be read + if (clen > STDIN_MAX) clen = STDIN_MAX; - *content = new char[clen]; + *content = new char[clen]; - cin.read(*content, clen); - clen = cin.gcount(); + cin.read(*content, clen); + clen = cin.gcount(); + } + else + { + // *never* read stdin when CONTENT_LENGTH is missing or unparsable + *content = 0; + clen = 0; + } // Chew up any remaining stdin - this shouldn't be necessary // but is because mod_fastcgi doesn't handle it correctly. - // + // ignore() doesn't set the eof bit in some versions of glibc++ // so use gcount() instead of eof()... do cin.ignore(1024); while (cin.gcount() == 1024); @@ -96,32 +99,36 @@ int main (void) int count = 0; long pid = getpid(); + streambuf * cin_streambuf = cin.rdbuf(); + streambuf * cout_streambuf = cout.rdbuf(); + streambuf * cerr_streambuf = cerr.rdbuf(); + FCGX_Request request; - + FCGX_Init(); FCGX_InitRequest(&request, 0, 0); - while (FCGX_Accept_r(&request) == 0) + while (FCGX_Accept_r(&request) == 0) { // Note that the default bufsize (0) will cause the use of iostream - // methods that require positioning (such as peek(), seek(), + // methods that require positioning (such as peek(), seek(), // unget() and putback()) to fail (in favour of more efficient IO). - fcgi_streambuf fin(request.in); - fcgi_streambuf fout(request.out); - fcgi_streambuf ferr(request.err); + fcgi_streambuf cin_fcgi_streambuf(request.in); + fcgi_streambuf cout_fcgi_streambuf(request.out); + fcgi_streambuf cerr_fcgi_streambuf(request.err); #ifdef HAVE_IOSTREAM_WITHASSIGN_STREAMBUF - cin = &fin; - cout = &fout; - cerr = &ferr; + cin = &cin_fcgi_streambuf; + cout = &cout_fcgi_streambuf; + cerr = &cerr_fcgi_streambuf; #else - cin.rdbuf(&fin); - cout.rdbuf(&fout); - cerr.rdbuf(&ferr); + cin.rdbuf(&cin_fcgi_streambuf); + cout.rdbuf(&cout_fcgi_streambuf); + cerr.rdbuf(&cerr_fcgi_streambuf); #endif // Although FastCGI supports writing before reading, - // many http clients (browsers) don't support it (so + // many http clients (browsers) don't support it (so // the connection deadlocks until a timeout expires!). char * content; unsigned long clen = gstdin(&request, &content); @@ -152,5 +159,15 @@ int main (void) // have to be flushed here. } +#ifdef HAVE_IOSTREAM_WITHASSIGN_STREAMBUF + cin = cin_streambuf; + cout = cout_streambuf; + cerr = cerr_streambuf; +#else + cin.rdbuf(cin_streambuf); + cout.rdbuf(cout_streambuf); + cerr.rdbuf(cerr_streambuf); +#endif + return 0; }