Re: Getting environment variables

"Michael Hartley" <michael.hartley-jEoFCcbHCOQze0waTqBjZBy+ShKaMYb+@public.gmane.org>
Newsgroups gmane.comp.web.fastcgi.devel
Organization Openfield Solutions LTD
Message-ID <[email protected]>
Hi

In an FCGI environment, you have two sets of Environment variables, the
first set of environment variables are those of the application runtime
environment.  The second set are the variables passed from the WebServer
to the fcgi application via the fcgi FCGX_Request structure.

I've included a few lines or source code from one of the own c++ classes


This gets called/passed when the fcgi request accepts.

fcgi_data::fcgi_data(FCGX_Request* request)
//#UBLK-BEG-METHOD mm:bba182b5-ed57-11d9-9d59-931a4068ecc7
{
	m_request = request;
}


This is a generic function that collects the fcgi environment variable
  from the request.

// Method : see header file for documentation.
std::string fcgi_data::getenv(const std::string& name, FCGX_ParamArray
envp)
//#UBLK-BEG-METHOD mm:b1198456-f3b5-11d9-8fd5-8fac2d850fff
{
	std::string result;
	char * t;

	t = FCGX_GetParam( name.c_str(), envp );

	//if result==NULL, check the environ
	if ( t )
		result = t;

	return ( result );

}

And this would be how I wrap the SERVER_NAME environment getter method.

std::string fcgi_data::get_server_name()
//#UBLK-BEG-METHOD mm:cfef73e5-ed6b-11d9-9d59-931a4068ecc7
{
	return ( getenv("SERVER_NAME", m_request->envp) ) ;

}


Kind regards

Michael Hartley


On Fri, 18 Nov 2005 14:35:14 -0000, Sebastian Beyer <[email protected]>
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???
> Hope this mail is not formatted too bad. ;-)
> Greetings
> Sebastian Beyer
>
>



-- 

Michael Hartley
Openfield Solutions Ltd
www.openfieldsolutions.co.uk

___________________________________
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.