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

I am currently struggling with a small problem:
I want to create a FastCGI / CGI binding for nekoVM . This is done by writing C / C ++ help code, which is loaded by the virtual machine. I want my binding behavior to be as compatible as possible with the neko native API (mod_neko, mod_tora). With mod_neko, you can get all the HTTP headers sent by the client.
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 the headers.

My question is: is it possible to get all the headers sent by the client?

+6
c ++ c fastcgi
source share
2 answers

On most systems, you can use an environment variable defined externally with zero completion to get an array of all the environment variables that you could iterate over to capture the headers you need (if FastCGI sets the environment variables in a reasonable 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 .

+3
source share

Apache mod_fcgi puts all the HTTP client headers in the "FCGX_ParamArray" that you passed to FCGX_Accept (the main server application loop). This type is just char **, with a common pattern "name, value, name, ..." for 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 use Apache and want to access all the static configuration settings ("httpd.conf"), they are passed in the "arge" main () environment block.

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

Remember that not all clients will send all possible headers. For example, CURL does not send an accept header.

+4
source share

All Articles