开发者

Get all client headers in FastCGI (C/C++)

currently I am struggeling with a little problem:

I want create FastCGI/CGI binding for the nekoVM. This is don开发者_运维问答e by writing some clue C/C++ code that is loaded by the VM. I want to make the behaviour of my binding as compatible as possible with neko native API (mod_neko, mod_tora). With mod_neko it is possible to get all HTTP headers the client send.

As far as I know you can get HTTP headers with FastCGI only by calling getenv('header_name'). To use this function you need to know the name of all headers.

My question: Is there any possibility to get all headers the client send?


Apache's mod_fcgi puts all client http headers in the "FCGX_ParamArray" that you passed into FCGX_Accept (the main loop of the server app). That type is just a char**, with the common pattern "name, value, name, ..." for the strings. So, you just need a loop like this to get them all:

std::map<std::string, std::string>  hdrs; 
std::string  name = 0;
char*        val  = 0;
int          i;

// "envp" is the FCGX_ParamArray you passed into FCGX_Accept(...) 
for(i=0; envp[i] != NULL; i+=2) {      
    name = envp[i];                    
    val  = envp[i+1];                                
    if(val != NULL) {                  
        hdrs[name] = string(val);      
    } 
    else {
        hdrs[name] = "";
    }                             
}                                     

If you're using Apache and want to access all the static config ("httpd.conf") settings as well, they're passed in the "arge" environment block of main().

int main(int argc, char** argv, char** arge) {
    ....
}

Be aware that not all clients will send all possible headers- CURL, for example, does not send an "accept" header.


You can use the externally-defined, null-terminated environ variable on most systems to get an array of all environment variables, which you could iterate to grab the headers you need (assuming FastCGI sets up the environment variables in a sensible way):

#include <stdio.h>

int main(int argc, char *argv[])
{
    extern char **environ;
    for (int i = 0; environ[i] != NULL; i++)
    {
        printf("%s\n", environ[i]);
    }
}

See man 7 environ.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜