2

我并尝试将 mongo c++ 驱动程序代码与我在此站点http://www.nongnu.org/fastcgipp/上找到的 fcgi c++ 代码连接,我的代码如下。

/***************************************************************************
* Copyright (C) 2007 Eddie Carle [eddie@erctech.org]                       *
*                                                                          *
* This file is part of fastcgi++.                                          *
*                                                                          *
* fastcgi++ is free software: you can redistribute it and/or modify it     *
* under the terms of the GNU Lesser General Public License as  published   *
* by the Free Software Foundation, either version 3 of the License, or (at *
* your option) any later version.                                          *
*                                                                          *
* fastcgi++ is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or    *
* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public     *
* License for more details.                                                *
*                                                                          *
* You should have received a copy of the GNU Lesser General Public License *
* along with fastcgi++.  If not, see <http://www.gnu.org/licenses/>.       *
****************************************************************************/
#include <iostream>
#include "mongo/client/dbclient.h"

#include <fstream>
#include <boost/date_time/posix_time/posix_time.hpp>

#include <fastcgi++/request.hpp>
#include <fastcgi++/manager.hpp>
using namespace bson;
using namespace mongo;

///////////////////////////////////////////bson////////////////////////////

void iter(bo o) {
    /* iterator example */
    cout << "\niter()\n";
    for( bo::iterator i(o); i.more(); ) {
        cout << ' ' << i.next().toString() << '\n';
    }
}
////////////////////////////Mongo code /////////////////////////////////
void printIfAge(DBClientConnection& c, int age) {
    auto_ptr<DBClientCursor> cursor = c.query("tutorial.persons", QUERY( "age" << age ).sort("name") );
    while( cursor->more() ) {
        BSONObj p = cursor->next();
        cout << p.getStringField("name") << endl;
    }
}

void run() {

}

///////////////////////////////mongo code above////////////////////////////
// I like to have an independent error log file to keep track of exceptions while debugging.
// You might want a different filename. I just picked this because everything has access there.
void error_log(const char* msg)
{
    using namespace std;
    using namespace boost;
    static ofstream error;
    if(!error.is_open())
    {
        error.open("/tmp/errlog", ios_base::out | ios_base::app);
        error.imbue(locale(error.getloc(), new posix_time::time_facet()));
    }

    error << '[' << posix_time::second_clock::local_time() << "] " << msg << endl;
}

// Let's make our request handling class. It must do the following:
// 1) Be derived from Fastcgipp::Request
// 2) Define the virtual response() member function from Fastcgipp::Request()

// First things first let's decide on what kind of character set we will use.
// Since we want to be able to echo all languages we will use unicode. The way this
// library handles unicode might be different than some are used to but it is done
// the way it is supposed to be. All internal characters are wide. In this case UTF-32.
// This way we don't have to mess around with variable size characters in our program.
// A string with 10 wchar_ts is ten characters long. Not up in the air as it is with UTF-8.
// Anyway, moving right along, the streams will code convert all the UTF-32 data to UTF-8
// before it is sent out to the client. This way we get the best of both worlds.
//
// So, whenever we are going to use UTF-8, our template parameter for Fastcgipp::Request<charT>
// should be wchar_t. Keep in mind that this suddendly makes
// everything wide character and utf compatible. Including HTTP header data (cookies, urls, yada-yada).

class Echo: public Fastcgipp::Request<wchar_t>
{
    bool response()
    {
        using namespace Fastcgipp;
        wchar_t cookieString[] = { '<', '"', 0x0440, 0x0443, 0x0441, 0x0441, 0x043a, 0x0438, 0x0439, '"', '>', ';', 0x0000 };

        // Let's make our header, note the charset=utf-8. Remember that HTTP headers
        // must be terminated with \r\n\r\n. NOT just \n\n.
        // Let's set a cookie just for fun too, in UTF-8.
        out << "Set-Cookie: echoCookie=" << encoding(URL) << cookieString << encoding(NONE) << "; path=/\n";
        out << "Content-Type: text/html; charset=utf-8\r\n\r\n";

        // Now it's all stuff you should be familiar with
        out << "<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
        out << "<title>fastcgi++: Echo in UTF-8</title></head><body>";

        // This environment().data structure is defined in fastcgi++/http.hpp
        out << "<h1>Environment Parameters</h1>";
        out << "<p><b>FastCGI Version:</b> " << Protocol::version << "<br />";
        out << "<b>fastcgi++ Version:</b> " << version << "<br />";
        out << "<b>Hostname:</b> " << encoding(HTML) << environment().host << encoding(NONE) << "<br />";
        out << "<b>User Agent:</b> " << encoding(HTML) << environment().userAgent << encoding(NONE) << "<br />";
        out << "<b>Accepted Content Types:</b> " << encoding(HTML) << environment().acceptContentTypes << encoding(NONE) << "<br />";
        out << "<b>Accepted Languages:</b> " << encoding(HTML) << environment().acceptLanguages << encoding(NONE) << "<br />";
        out << "<b>Accepted Characters Sets:</b> " << encoding(HTML) << environment().acceptCharsets << encoding(NONE) << "<br />";
        out << "<b>Referer:</b> " << encoding(HTML) << environment().referer << encoding(NONE) << "<br />";
        out << "<b>Content Type:</b> " << encoding(HTML) << environment().contentType << encoding(NONE) << "<br />";
        out << "<b>Root:</b> " << encoding(HTML) << environment().root << encoding(NONE) << "<br />";
        out << "<b>Script Name:</b> " << encoding(HTML) << environment().scriptName << encoding(NONE) << "<br />";
        out << "<b>Request URI:</b> " << encoding(HTML) << environment().requestUri << encoding(NONE) << "<br />";
        out << "<b>Request Method:</b> " << encoding(HTML) << environment().requestMethod << encoding(NONE) << "<br />";
        out << "<b>Content Length:</b> " << encoding(HTML) << environment().contentLength << encoding(NONE) << "<br />";
        out << "<b>Keep Alive Time:</b> " << encoding(HTML) << environment().keepAlive << encoding(NONE) << "<br />";
        out << "<b>Server Address:</b> " << encoding(HTML) << environment().serverAddress << encoding(NONE) << "<br />";
        out << "<b>Server Port:</b> " << encoding(HTML) << environment().serverPort << encoding(NONE) << "<br />";
        out << "<b>Client Address:</b> " << encoding(HTML) << environment().remoteAddress << encoding(NONE) << "<br />";
        out << "<b>Client Port:</b> " << encoding(HTML) << environment().remotePort << encoding(NONE) << "<br />";
        out << "<b>If Modified Since:</b> " << encoding(HTML) << environment().ifModifiedSince << encoding(NONE) << "</p>";
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 DBClientConnection c;
    c.connect("localhost"); //"192.168.58.1");
    cout << "connected ok" << endl;
    BSONObj p = BSON( "name" << "Joe" << "age" << 33 );
    c.insert("tutorial.persons", p);
    p = BSON( "name" << "Jane" << "age" << 40 );
    c.insert("tutorial.persons", p);
    p = BSON( "name" << "Abe" << "age" << 33 );
    c.insert("tutorial.persons", p);
    p = BSON( "name" << "Methuselah" << "age" << BSONNULL);
    c.insert("tutorial.persons", p);
    p = BSON( "name" << "Samantha" << "age" << 21 << "city" << "Los Angeles" << "state" << "CA" );
    c.insert("tutorial.persons", p);

    c.ensureIndex("tutorial.persons", fromjson("{age:1}"));

    cout << "count:" << c.count("tutorial.persons") << endl;
    out << "<h1>count:</h1>";
    out << "<b>" << c.count("tutorial.persons")  << ":</b> " << "<br />";

     cout << "\nprintifage:\n";
    auto_ptr<DBClientCursor> cursor = c.query("tutorial.persons", BSONObj());



    while( cursor->more() ) {
         cout << cursor->next().toString() << endl;
    }


    printIfAge(c, 33);


 bo empty;
    cout << "empty: " << empty << endl;

    /* make a simple { name : 'joe', age : 33.7 } object */
    {
        bob b;
        b.append("name", "joe");
        b.append("age", 33.7);
        b.obj();
    }

    /* make { name : 'joe', age : 33.7 } with a more compact notation. */
    bo x = bob().append("name", "joe").append("age", 33.7).obj();

    /* convert from bson to json */
    string json = x.toString();
     out << "<b>" << encoding(HTML) << json << encoding(NONE)  << ":</b> " << "<br />";
    cout << "json for x:" << json << endl;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // Let's see the Path Info
        out << "<h1>Path Data</h1>";
        if(environment().pathInfo.size())
        {
            std::wstring preTab;
            for(Http::Environment<wchar_t>::PathInfo::const_iterator it=environment().pathInfo.begin(); it!=environment().pathInfo.end(); ++it)
            {
                out << preTab << encoding(HTML) << *it << encoding(NONE) << "<br />";
                preTab += L"&nbsp;&nbsp;&nbsp;";
            }
        }
        else
            out << "<p>No Path Info</p>";

        // Let's see the GET data
        out << "<h1>GET Data</h1>";
        if(environment().gets.size())
            for(Http::Environment<wchar_t>::Gets::const_iterator it=environment().gets.begin(); it!=environment().gets.end(); ++it)
                out << "<b>" << encoding(HTML) << it->first << encoding(NONE) << ":</b> " << encoding(HTML) << it->second << encoding(NONE) << "<br />";
        else
            out << "<p>No GET data</p>";

        // Let's see the cookie data
        out << "<h1>Cookie Data</h1>";
        if(environment().cookies.size())
            for(Http::Environment<wchar_t>::Cookies::const_iterator it=environment().cookies.begin(); it!=environment().cookies.end(); ++it)
                out << "<b>" << encoding(HTML) << it->first << encoding(NONE) << ":</b> " << encoding(HTML) << it->second << encoding(NONE) << "<br />";
        else
            out << "<p>No Cookie data</p>";

        //Fastcgipp::Http::Post is defined in fastcgi++/http.hpp
        out << "<h1>POST Data</h1>";
        if(environment().posts.size())
        {
            for(Http::Environment<wchar_t>::Posts::const_iterator it=environment().posts.begin(); it!=environment().posts.end(); ++it)
            {
                out << "<h2>" << encoding(HTML) << it->first << encoding(NONE) << "</h2>";
                if(it->second.type==Http::Post<wchar_t>::form)
                {
                    out << "<p><b>Type:</b> form data<br />";
                    out << "<b>Value:</b> " << encoding(HTML) << it->second.value << encoding(NONE) << "</p>";
                }

                else
                {
                    out << "<p><b>Type:</b> file<br />";
                    // When the post type is a file, we have some additional information
                    out << "<b>Filename:</b> " << encoding(HTML) << it->second.filename << encoding(NONE) << "<br />";
                    out << "<b>Content Type:</b> " << encoding(HTML) << it->second.contentType << encoding(NONE) << "<br />";
                    out << "<b>Size:</b> " << it->second.size() << "<br />";
                    out << "<b>Data:</b></p><pre>";
                    // We will use dump to send the raw data directly to the client
                    out.dump(it->second.data(), it->second.size());
                    out << "</pre>";
                }
            }
        }
        else
            out << "<p>No POST data</p>";

        out << "</body></html>";

        // Always return true if you are done. This will let apache know we are done
        // and the manager will destroy the request and free it's resources.
        // Return false if you are not finished but want to relinquish control and
        // allow other requests to operate. You might do this after an SQL query
        // while waiting for a reply. Passing messages to requests through the
        // manager is possible but beyond the scope of this example.
        return true;
    }
};

// The main function is easy to set up
int main()
{
    try
    {
        // First we make a Fastcgipp::Manager object, with our request handling class
        // as a template parameter.
        Fastcgipp::Manager<Echo> fcgi;
        // Now just call the object handler function. It will sleep quietly when there
        // are no requests and efficiently manage them when there are many.
        fcgi.handler();
    }
    catch(std::exception& e)
    {
        error_log(e.what());
    }

try {
        run();
    }
    catch( DBException &e ) {
        cout << "caught " << e.what() << endl;
    }
}

我使用这个命令编译了代码

 g++ -o echo.fcgi echo.cpp -I../include -L../src -lfastcgipp -lboost_thread-mt -pthread -lboost_date_time-mt -g -O2 -pthread -lmongoclient -lboost_thread-mt -lboost_filesystem -lboost_program_options -lboost_system

现在一切都编译好了

但是当我尝试在我的 html 页面中显示 Mongodb 数据时

out << "<b>" << c.query("tutorial.persons", BSONObj()) << ":</b> " << "<br />";

我得到错误

    In file included from /usr/local/include/mongo/base/error_codes.h:20:0,
                     from /usr/local/include/mongo/base/status.h:21,
                     from /usr/local/include/mongo/util/assert_util.h:24,
                     from /usr/local/include/mongo/pch.h:76,
                     from /usr/local/include/mongo/client/dbclient.h:30,
                     from echo.cpp:20:
    /usr/local/include/mongo/base/string_data.h:162:19: note: std::ostream& mongo::operator<<(std::ostream&, const mongo::StringData&)
    /usr/local/include/mongo/base/string_data.h:162:19: note:   no known conversion for argument 1 from ‘std::basic_ostream<wchar_t>’ to ‘std::ostream& {aka std::b

asic_ostream<char>&}’

我想在 fcgi 代码中显示 mongodb 数据(例如以 HTML 的形式)我该怎么做请帮忙。

所以我找到了自己问题的答案

在 fcgi 代码(例如 HTML 的形式)中显示 show mongodb 数据的方式如下所示

while( cursor->more() ) {
         cout << cursor->next().toString() << endl;
string msg1 = cursor->next().toString();
         char *s2;

s2 = (char*)malloc(msg1.length() + 1); // don't forget to free!!!!
//s2 = msg1; // I forget if the string class can implicitly be converted to char*


strcpy(s2, msg1.c_str());


     out << "<b>" << s2<< ":</b> " << "<br />";
   free(s2);
    }

但是现在当我使用 localhost/echo.fcgi 打开网页时,客户端页面上出现错误

我收到以下错误

Status: 500 Internal Server Error Content-Type: text/html; charset=ISO-8859-1
500 Internal Server Error

有人可以帮助我。

4

0 回答 0