FastCGI: retrieve the request headers

2019-04-09 18:22发布

I’m currently working on a Web C++ application using FastCGI with Apache and mod_fcgid.

I’m trying to retrieve the headers of a request, but I didn’t find how to do so. After some researches, I thought the headers were in the attribute “envp” of “FCGX_Request”, but it contains environment variables such as:

REMOTE_ADDR: 192.168.0.50
SERVER_SOFTWARE: Apache/2.2.21 (Unix) mod_ssl/2.2.21 OpenSSL/1.0.0f DAV/2 mod_fcgid/2.3.6
REDIRECT_UNIQUE_ID: TxytP38AAAEAABpcDskAAAAE
FCGI_ROLE: RESPONDER
HTTP_ACCEPT_LANGUAGE: fr
SERVER_SIGNATURE: <address>Apache/2.2.21 [etc.]

These variables offer me useful informations, but I need the real HTTP headers, and especially “Cookie”. I tried to read on the stream “in” of the “FCGX_Request” but it seems to be for the request body (POST datas). As my application is intended to be multi-threaded, I use “FCGX_Accept_r()”, like this:

while(true)
{
    FCGX_Init();
    FCGX_Request* fcgiRequest = new FCGX_Request;
    FCGX_InitRequest(fcgiRequest, 0, 0);

    if(FCGX_Accept_r(fcgiRequest) < 0)
        break;

    Request* request = new Request(fcgiRequest);
    request->process();
}

But actually, I don’t use threads. Requests are executed one after the other.

How can I get the request headers?

Thank you.

标签: c++ c fastcgi
1条回答
放荡不羁爱自由
2楼-- · 2019-04-09 18:52

Try the following code. It should print out the entire environment so you can find the variable you are looking for.

while(true)
{
    FCGX_Init();
    FCGX_Request* fcgiRequest = new FCGX_Request;
    FCGX_InitRequest(fcgiRequest, 0, 0);

    if(FCGX_Accept_r(fcgiRequest) < 0)
        break;

    char **env = fcgiRequest->envp;
    while (*(++env))
        puts(*env);


    Request* request = new Request(fcgiRequest);
    request->process();
}
查看更多
登录 后发表回答