A simple form test.This example shows how to handle form data and use cookies.
#include <ehs.h>
#include <iostream>
#include <sstream>
#include <string>
#include <cstdlib>
#include "common.h"
using namespace std;
class FormTester :
public EHS {
public:
FormTester ( ) : m_oNameList ( StringList ( ) ) { }
StringList m_oNameList;
};
{
ostringstream oss;
oss << "<html><head><title>StringList</title></head>" << endl << "<body>" << endl;
if ( request->
FormValues (
"user" ).m_sBody.length ( ) ||
request->
FormValues (
"existinguser" ).m_sBody.length ( ) ) {
string sName = request->
FormValues (
"existinguser" ).m_sBody;
if ( request->
FormValues (
"user" ).m_sBody.length() ) {
}
cerr << "Got name of " << sName << endl;
oss << "Hi " << sName << "<p><a href=\"/\">Back to login form</a></body></html>";
m_oNameList.push_back ( sName );
response->
SetBody( oss.str().c_str(), oss.str().length() );
return HTTPRESPONSECODE_200_OK;
} else {
cerr << "Got no form data" << endl;
oss << "<p>Please log in</p>" << endl << "<form action = \"/\" method=\"POST\">" << endl
<< "User name: <input type=\"text\" name=\"user\"><br />" << endl
<< "<select name=\"existinguser\" width=\"20\">" << endl;
for ( StringList::iterator i = m_oNameList.begin(); i != m_oNameList.end ( ); ++i ) {
oss << "<option>" << i->substr ( 0, 150 ) << endl;
}
oss << "</select> <input type=\"submit\">" << endl << "</form>" << endl;
}
oss << "</body>" << endl << "</html>";
response->
SetBody( oss.str().c_str(), oss.str().length() );
return HTTPRESPONSECODE_200_OK;
}
int main (int argc, char ** argv)
{
cout << getEHSconfig() << endl;
if (argc != 2 && argc != 4) {
cout << "usage: " << basename(argv[0]) << " <port> [<certificate_file> <certificate_passphrase>]" << endl;
cout << "\tIf you specify the last 2 parameters, it will run in https mode" << endl;
return 0;
}
FormTester srv;
EHSServerParameters oSP;
oSP["port"] = argv[1];
oSP["mode"] = "threadpool";
if (argc == 4) {
cout << "in https mode" << endl;
oSP["https"] = 1;
oSP["certificate"] = argv [ 2 ];
oSP["passphrase"] = argv [ 3 ];
}
try {
srv.StartServer(oSP);
kbdio kbd;
cout << "Press q to terminate ..." << endl;
while (!(srv.ShouldTerminate() || kbd.qpressed())) {
usleep(300000);
}
srv.StopServer ( );
} catch (exception &e) {
cerr << "ERROR: " << e.what() << endl;
}
return 0;
}