I am new to CGI. I use Apache v2.2 and GNU cgicc v3.2.9. I want to use CGI for generating a HTML web page with a simple form. Aim of this work is to let an user send data by hiting a button on this CGI directly back to the "same" CGI. The CGI evaluates the data and changes the web page:
CGI --> CGI --> CGI --> ...
I can not use a simple HTML page and sending the requests to a cgi because after that I have created a CGI page were the user can send again a request to this cgi which leads to the same situatiuon as introduced:
HTML --> CGI --> CGI --> CGI --> ...
Using form method POST leads into a server error message while GET works:
access.log
[16/Mar/2011:15:00:50] "GET /cgi-bin/cgiHandleEvents.cgi HTTP/1.1" 200 973
[16/Mar/2011:15:00:52] "GET /cgi-bin/cgiHandleEvents.cgi?value1=&value2=option1&value3=button1&value4=data4 HTTP/1.1" 200 973
[16/Mar/2011:15:01:34] "GET /cgi-bin/cgiHandleEvents.cgi HTTP/1.1" 200 974
[16/Mar/2011:15:01:37] "POST /cgi-bin/cgiHandleEvents.cgi HTTP/1.1" 500 538
error.log (I reduced the http:// to http// because of spam warning regarding my stackoverflow reputation)
[Wed Mar 16 15:01:37 2011] [error] [client 127.0.0.1] Premature end of script headers: cgiHandleEvents.cgi, referer: http//localhost/cgi-bin/cgiHandleEvents.cgi
[Wed Mar 16 15:01:37 2011] [error] [client 127.0.0.1] \r, referer: http//localhost/cgi-bin/cgiHandleEvents.cgi
[Wed Mar 16 15:01:37 2011] [error] [client 127.0.0.1] This application has requested the Runtime to terminate it in an unusual way., referer: http//localhost/cgi-bin/cgiHandleEvents.cgi
[Wed Mar 16 15:01:37 2011] [error] [client 127.0.0.1] Please contact the application's support team for more information.\r, referer: http//localhost/cgi-bin/cgiHandleEvents.cgi
[Wed Mar 16 15:01:37 2011] [error] [client 127.0.0.1] terminate called after throwing an instance of 'std::runtime_error'\r, referer: http//localhost/cgi-bin/cgiHandleEvents.cgi
[Wed Mar 16 15:01:37 2011] [error] [client 127.0.0.1] what(): I/O error\r, referer: http//localhost/cgi-bin/cgiHandleEvents.cgi
The current code contains no message handler. In my opinion on POST the page should simply reload. For checking the code you can exchange the lines containing the different form method.
#include "cgicc/CgiDefs.h"
#include "cgicc/Cgicc.h"
#include "cgicc/HTTPHTMLHeader.h"
#include "cgicc/HTMLClasses.h"
using namespace std;
using namespace cgicc;
int main()
{
Cgicc formData;
Cgicc cgi;
cout<< HTTPHTMLHeader() << endl;
cout<< HTMLDoctype(HTMLDoctype::eStrict) << endl;
cout<< html().set("lang", "EN").set("dir", "LTR") << endl;
// Set up the HTML document
cout<< html() << head() << title("Cgicc example") << head() << endl;
cout<< body().set("bgcolor","#cccccc").set("text","#000000").set("link","#0000ff").set("vlink","#000080") << endl;
try
{
cout<< h2("Test CGIcc form") << endl;
cout<< "GNU cgicc v" << cgi.getVersion() << "<BR>" << endl;
//cout<< "<form method=\"post\" action=\"cgiHandleEvents.cgi\">"<< endl;
cout<< "<form method=\"get\" action=\"cgiHandleEvents.cgi\">"<< endl;
cout<< " Value 1:" << endl;
cout<< " <input type=\"text\" name=\"value1\">" << endl;
cout<< " <p></p>" << endl;
cout<< " Value 2:" << endl;
cout<< " <select name=\"value2\">" << endl;
cout<< " <option value=\"option1\">Option 1" << endl;
cout<< " <option value=\"option2\">Option 2" << endl;
cout<< " <option value=\"option3\">Option 3" << endl;
cout<< " </select>" << endl;
cout<< " <p></p>" << endl;
cout<< " Value 3:" << endl;
cout<< " <input type=\"radio\" name=\"value3\" value=\"button1\" checked=\"checked\">Button1" << endl;
cout<< " <input type=\"radio\" name=\"value3\" value=\"button2\">Button2" << endl;
cout<< " <input type=\"hidden\" name=\"value4\" value=\"data4\">" << endl;
cout<< " <p></p>" << endl;
cout<< " <input type=\"submit\" value=\"Submit\">" << endl;
cout<< "</form>" << endl;
}
catch(exception& e)
{
// handle any errors here.
cout<< h2("ERROR!!!") << endl;
}
// Close the HTML document
cout << body() << html();
return 0; // To avoid Apache errors.
}
Does anybody have an idea what the problem is?
I found the errors. First error is malformed HTML. Look out for opening and closing tag for html not enclosing the complete html page!
cout<< html().set("lang", "EN").set("dir", "LTR") << endl;
// Set up the HTML document
cout<< html() << head() << title("Cgicc example") << head() << endl;
Second error seems to be
Cgicc formData;
I do not know why this leads to an error. But If I do not use it the POST request works fine. If I think twice it makes no sense to have 2 instances of Cgicc. This is my fault. m(
But I think this is a bug inside cgicc. I will file a bug on savanna...