[code]
/*
* A simple FastCGI application example in C++.
*
* $Id: echo-cpp.cpp,v 1.10 2002/02/25 00:46:17 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
* notice, this list of conditions and the following disclaimer in the
* 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.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma warning( disable : 4251)
#define UNICODE
#include <stdlib.h>
#ifdef _WIN32
#include <process.h>
#else
#include <unistd.h>
extern char ** environ;
#endif
#include "fcgio.h"
#include "fcgi_config.h" // HAVE_IOSTREAM_WITHASSIGN_STREAMBUF
#include "c2plib_string.h"
#include "mysql++.h"
using namespace std;
// Maximum number of bytes allowed to be read from stdin
static const unsigned long STDIN_MAX = 1000000;
int main (void)
{
int count = 0;
streambuf * cin_streambuf = cin.rdbuf();
streambuf * cout_streambuf = cout.rdbuf();
streambuf * cerr_streambuf = cerr.rdbuf();
// Connect to the sample database.
mysqlpp::Connection con(false);
if (!con.connect("test", "localhost", "root", "11111", 3306)) {
return 1;
}
tchar_stuff::string_t s(_T("фываыафваафвафываыафваафва"));
tchar_stuff::string_t s1(_T("фываыафваафвафываыафваафва"));
s.reserve(10000);
s1.reserve(10000);
mysqlpp::Query query = con.query();
mysqlpp::ResUse res;
mysqlpp::Row row;
FCGX_Request request;
FCGX_Init();
FCGX_InitRequest(&request, 0, 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(),
// unget() and putback()) to fail (in favour of more efficient IO).
fcgi_streambuf cin_fcgi_streambuf(request.in);
fcgi_streambuf cout_fcgi_streambuf(request.out);
fcgi_streambuf cerr_fcgi_streambuf(request.err);
#if HAVE_IOSTREAM_WITHASSIGN_STREAMBUF
cin = &cin_fcgi_streambuf;
cout = &cout_fcgi_streambuf;
cerr = &cerr_fcgi_streambuf;
#else
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
// the connection deadlocks until a timeout expires!).
//tchar_stuff::string_t s(_T("фываыафваафва"));
cout << _T("Content-type: text/html; charset=UTF-8\r\n\r\n");
for (int i=0;i<100;i++) {
query << "select * from testtable where uid>5000 and uid<5210";
res = query.use();
// Retreive result rows one by one, and display them.
if (res) {
// Display header
cout.setf(ios::left);
cout << setw(21) << "uid" << setw(10) << "desc" << endl << endl;
// Get each row in result set, and print its contents
while (row = res.fetch_row()) {
cout << s.c_str() << "<br/>";
cout << row["uid"] << ' ' << row["desc"] << ' ' << "<br/>" << endl;
}
}
}
s = _T("фываыафваафва");
s1= _T("фываыафваафва");
for (int i=0; i<4500; i++) {
s += s1;
}
//cout << s.c_str();
}
#if 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;
}
[code]
|