代码改变世界

另一个官方fcgi实例echo-cpp.cpp

2013-02-27 23:04  好动的树懒  阅读(1007)  评论(0编辑  收藏  举报

http://www.fastcgi.com/devkit/examples/echo-cpp.cpp

开发环境:freebsd or linux
开发语言:c++ 
 
代码:
#include <stdlib.h>
#include <string>
#include <iostream>
#ifdef _WIN32
#include <process.h>
#else
#include <unistd.h>
extern char ** environ;
#endif
#include <fcgio.h>
#include <fcgi_config.h>  // HAVE_IOSTREAM_WITHASSIGN_STREAMBUF


using namespace std;


// Maximum number of bytes allowed to be read from stdin
static const unsigned long STDIN_MAX = 1000000;


static void penv(const char * const * envp)
{
    cout << "<PRE>\n";
    for ( ; *envp; ++envp)
    {
        cout << *envp << "\n";
    }
    cout << "</PRE>\n";
}


static long gstdin(FCGX_Request * request, char ** content)
{
    char * clenstr = FCGX_GetParam("CONTENT_LENGTH", request->envp);
    unsigned long clen = STDIN_MAX;


    if (clenstr)
    {
        clen = strtol(clenstr, &clenstr, 10);
        if (*clenstr)
        {
            cerr << "can't parse \"CONTENT_LENGTH="
                 << FCGX_GetParam("CONTENT_LENGTH", request->envp)
                 << "\"\n";
            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];


        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);


    return clen;
}


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)
    {
        // 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!).
        char * content;
        unsigned long clen = gstdin(&request, &content);


        cout << "Content-type: text/html\r\n"
                "\r\n"
                "<TITLE>echo-cpp</TITLE>\n"
                "<H1>echo-cpp</H1>\n"
                "<H4>PID: " << pid << "</H4>\n"
                "<H4>Request Number: " << ++count << "</H4>\n";


        cout << "<H4>Request Environment</H4>\n";
        penv(request.envp);


        cout << "<H4>Process/Initial Environment</H4>\n";
        penv(environ);


        cout << "<H4>Standard Input - " << clen;
        if (clen == STDIN_MAX) cout << " (STDIN_MAX)";
        cout << " bytes</H4>\n";
        if (clen) cout.write(content, clen);


        if (content) delete []content;


        // If the output streambufs had non-zero bufsizes and
        // were constructed outside of the accept loop (i.e.
        // their destructor won't be called here), they would
        // have to be flushed here.
    }


#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;
}

编译:

root@mypc % g++47 -o echo.fcgi echo.cpp -I /usr/local/include -L /usr/local/lib -lfcgi -lstdc++ -lfcgi++

http://127.0.0.1/echo.fcgi

 

输出:

 

Request Number: 0

Request Environment

FCGI_ROLE=RESPONDER
UNIQUE_ID=ULd5VnkKKJoAAAl0Aa0AAAAB
HTTP_HOST=127.0.0.1
HTTP_CONNECTION=close
HTTP_CACHE_CONTROL=max-age=0
HTTP_USER_AGENT=Mozilla/5.0 (X11; FreeBSD i386) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2
HTTP_ACCEPT=text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
HTTP_ACCEPT_ENCODING=gzip,deflate,sdch
HTTP_ACCEPT_LANGUAGE=en-US,en;q=0.8
HTTP_ACCEPT_CHARSET=UTF-8,*;q=0.5
PATH=/sbin:/bin:/usr/sbin:/usr/bin
SERVER_SIGNATURE=
SERVER_SOFTWARE=Apache/2.2.21 (FreeBSD) mod_ssl/2.2.21 OpenSSL/0.9.8q DAV/2 mod_fcgid/2.3.6
SERVER_NAME=127.0.0.1
SERVER_ADDR=127.0.0.1
SERVER_PORT=80
REMOTE_ADDR=127.0.0.1
DOCUMENT_ROOT=/usr/www/lovely/htdocs
SERVER_ADMIN=webmaster@dummy-host.example.com
SCRIPT_FILENAME=/usr/www/lovely/htdocs/echo.fcgi
REMOTE_PORT=11449
GATEWAY_INTERFACE=CGI/1.1
SERVER_PROTOCOL=HTTP/1.1
REQUEST_METHOD=GET
QUERY_STRING=
REQUEST_URI=/echo.fcgi
SCRIPT_NAME=/echo.fcgi

Process/Initial Environment

PATH=/sbin:/bin:/usr/sbin:/usr/bin
LD_LIBRARY_PATH=/usr/local/lib:

Standard Input - 0 bytes

问题总结:

 

编译时要加上 -lfcgi++这个库,否则会出现: undefined reference to `fcgi_streambuf::~fcgi_streambuf()'

错误。