Getting environment variables
"Sebastian Beyer" <[email protected]>
| Newsgroups | gmane.comp.web.fastcgi.devel |
|---|---|
| Message-ID | <[email protected]> |
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
___________________________________
fastcgi-developers mailing list
http://fastcgi.com/fastcgi-developers/