Re: Getting environment variables

Marc Prewitt <[email protected]>
Newsgroups gmane.comp.web.fastcgi.devel
Message-ID <[email protected]>
Sebastian Beyer wrote:
> Hi,
> 
> I wrote a programm, I call from a HTML Page with a Formular, that gives
> me environment variables. It works perfect with CGI.  
> 
> #include <iostream>
> 
> #include <string>
> 
> using namespace std;
> 
> class Environment {
> 
> public:
> 
>             Environment (char* envp[])
> 
>             : m_envp(envp)
> 
>             {}
> 
>             string get (string ENVname)
> 
>             {          ENVname += "=";
> 
>                         int i = 0;
> 
>                         while (m_envp[i]!=NULL) {
> 
>                                    string envParam (m_envp[i]);
> 
>                                    if (envParam.find (ENVname)==0)
> 
>                                                return envParam.erase(0,
> ENVname.length());
> 
>                                    i++;
> 
>                         }
> 
>                         throw "Environment variable " + ENVname + “not
> found";}
> 
> protected:
> 
>             char** m_envp;};
> 
> int main(int argc, char* argv[], char* envp[]) {
> 
> try {
> 
> cout << "  <h3>Environment variables</h3>" << endl;
> 
>             cout << "  <p>" << endl;
> 
>             int i = 0;
> 
>             while (envp[i]!=NULL) {
> 
>                         cout << "    " << envp[i] << "<br>" << endl;
> 
>                         i++;}
> 
>             cout << "  </p>" << endl;
> 
>             cout << "  <h3>Formular-Echo</h3>" << endl;
> 
>             Environment ENV (envp);
> 
> string RequestMethod = ENV.get("REQUEST_METHOD");
> 
> cout << "  <p>You have send the following formulardata with " <<      
> RequestMethod << „</p>" << endl;
> 
> cout << "  <p><strong>ParameterString:</strong><br>" << endl;
> 
>             string ParameterString;
> 
>             if (RequestMethod == "GET") {
> 
>                         ParameterString = ENV.get("QUERY_STRING");
> 
>                         cout << ParameterString << endl;}
> 
>             else if (RequestMethod == "POST") {
> 
>                         cout << "<pre
> style=\"background-color:#EEEEEE\">" << endl;
> 
>                         const int length = 1000;
> 
>                         char buffer [length];
> 
>                         cin.getline (buffer, length);
> 
>                         while (cin) {
> 
>                                     cout << Puffer << endl;
> 
>                                     cin.getline (Puffer, Laenge);}
> 
>                         cout << "</pre>" << endl;}
> 
>             else throw "Not valid REQUEST_METHOD: " +
> ENV.get("REQUEST_METHOD");
> 
>             cout << "  </p>" << endl;
> 
> }
> 
>             catch (string text) {
> 
>                         cout << endl;
> 
>                         cout << "<h3>FEHLER: " << text.c_str() <<
> ".</h3>" << endl;}
> 
>             cout << "</body>" << endl;
> 
>             cout << "</html>" << endl;
> 
> return 0;}
> 
> For fastCGI I thought I just include the fcgi_stdio.h, include the
> library and put a  while (FCGI_Accept() >= 0)   { } around the code in
> main. But try and catch doesn’t seem to work, because it only gives me a
> <html><body></body></html>
> 
> I also tried the example from http://www.fastcgi.com/devkit/doc/fcgi-devel-kit.htm#S1 but getenv(“SERVER_NAME”) returns nothing. The echo-cpp.cpp works fine but do I really have to do it that way to get the environment???
> 
>  
> 

The simple answer is, yes.

For a cgi process the web server that receivd the request runs it
directly so if you look at the env of the process it will be the env of
the web server as it inherited it.  However, with fcgi, the fcgi process
isn't neccessarily started by the web server that received the request.
 With external fcgi processes there may not even be a web server that
started it at all.  In order to retain some sort of backwards
compatilibility with cgi, you would want the environment from the web
server.  using the request.envp gives you that.

___________________________________
fastcgi-developers mailing list
http://fastcgi.com/fastcgi-developers/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.