Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c

http

fastcgi

currently I am struggeling with a little problem:
I want create FastCGI/CGI binding for the nekoVM. This is done 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?

like image 975
TheHippo Avatar asked Nov 05 '10 13:11

TheHippo


2 Answers

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&ltstd::string, std::string&gt  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.

like image 138
Zack Yezek Avatar answered Sep 19 '22 08:09

Zack Yezek


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.

like image 30
Wyatt Anderson Avatar answered Sep 20 '22 08:09

Wyatt Anderson