Embedded Gateway Interface (EGI) Sample in C++

The simpleEgi sample demonstrates how to create and use the Embedded Gateway Interface (EGI) in your application to respond to posted HTTP forms.

The sample is a main program that listens on port 8888 for HTTP requests and responds to the "/myEgi" URL when used with the POST method. The supplied index.esp page displays a form and prompts for user input. This is sent to the form when the user presses OK. The EGI handler echos back the input data values.

The sample is multithreaded and is configured to use a pool of 4 threads. By changing the value of the ThreadLimit directive in the configuration file to zero you can run single-threaded.

See also the equivalent C simpleEgi sample.

Files

index.esp
simpleEgi.conf
simpleEgi.cpp

index.esp Web Page



Embedded Gateway Interface (EGI) Sample



Embedded Gateway Interface (EGI) Sample


action=/myEgi.egi method=POST>











Name:
Address:








Configuration File

simpleEgi.conf

DocumentRoot "."
Listen 8888
ThreadLimit 4

LoadModule ejs ../../../lib/libejsModule
LoadModule egi ../../../lib/libegiHandler
LoadModule esp ../../../lib/libespHandler
LoadModule static ../../../lib/libcopyHandler

AddHandler egiHandler .egi
AddHandler espHandler .esp
AddHandler copyHandler

This configuration file loads the embedded javascript module and the embedded server pages and embedded gateway interface, and static content handlers. It is configured to run with 4 pool threads. It assumes that the sample is being run from the samples C/simpleEgi directory and so the module paths are relative to the lib directory in the samples source tree. Modify these module paths to suit your installation.

You should modify the DocumentRoot and Listen directives to suit your application's needs.

Source Code

simpleEgi.cpp

//
// Copyright (c) Mbedthis Software LLC, 2003-2004. All Rights Reserved.
//
/// @file simpleEgi.cpp
/// @brief Demonstrate the use of Embedded Server Pages (ESP) in a
/// simple multi-threaded application.
///

/////////////////////////////// Includes ///////////////////////////////

#include "appWeb/appWeb.h"

//////////////////////////////// Defines ///////////////////////////////
#if MPR_FEATURE_ESP
//
// Define the our EGI object to be called when the web form is posted.
//
class MyEgi : public MaEgiForm {
public:
MyEgi(char *egiName);
~MyEgi();
void run(MaRequest *rq, char *script, char *path, char *query,
char *postData, int postLen);
};

/////////////////////////////////// Code ///////////////////////////////

int main(int argc, char** argv)
{
MaHttp *http; // Http service inside our app
MaServer *server; // For the HTTP server
Mpr mpr("simpleEgi"); // Initialize the run time

#if MPR_FEATURE_LOG
//
// Do the following two statements only if you want debug trace
//
mpr.addListener(new MprLogToFile());
mpr.setLogSpec("stdout:4");
#endif

//
// Start the Mbedthis Portable Runtime
//
mpr.start(0);

//
// Create Http and Server objects for this application. We set the
// ServerName to be "default" and the initial serverRoot to be ".".
// This will be overridden in simpleEgi.conf.
//
http = new MaHttp();
server = new MaServer(http, "default", ".");

//
// Configure the server with the configuration directive file
//
if (server->configure("simpleEgi.conf", 0) < 0) {
mprFprintf(MPR_STDERR,
"Can't configure the server. Error on line %d\n",
server->getLine());
exit(2);
}

//
// Define our ESP procedures
//
new MyEgi("/myEgi.egi");

//
// Start the server
//
if (http->start() < 0) {
mprFprintf(MPR_STDERR, "Can't start the server\n");
exit(2);
}

//
// Tell the MPR to loop servicing incoming requests. We can
// replace this call with a variety of event servicing
// mechanisms offered by AppWeb.
//
mpr.serviceEvents(0, -1);

//
// Orderly shutdown
//
http->stop();
delete server;
delete http;

//
// MPR run-time will automatically stop and be cleaned up
//
return 0;
}

////////////////////////////////////////////////////////////////////////

MyEgi::MyEgi(char *name) : MaEgiForm(name)
{
// Put required initialization (if any) here
}

////////////////////////////////////////////////////////////////////////

MyEgi::~MyEgi()
{
// Put cleanup herre
}

////////////////////////////////////////////////////////////////////////
//
// Method that is run when the EGI form is called from the web
// page. Rq is the request context. URI is the bare URL minus query.
// Query is the string after a "?" in the URL. Post data is posted
// HTTP form data.
//

void MyEgi::run(MaRequest *rq, char *script, char *uri, char *query,
char *postData, int postLen)
{
//
// For convenience, decode and convert each post data variable
// into the hashed environment
//
if (postLen > 0) {
rq->createEnvVars(postData, postLen);
}
rq->write("simpleEgi\r\n");
rq->writeFmt("

Name: %s

\n", rq->getVar("name", "-"));
rq->writeFmt("

Address: %s

\n", rq->getVar("address", "-"));
rq->write("\r\n");

#if UNUSED
//
// Possible useful things to do in egi forms
//
rq->setResponseCode(200);
rq->setContentType("text/html");
rq->setHeaderFlags(MPR_HTTP_DONT_CACHE);
rq->redirect(302, "/myURl");
rq->requestError(409, "My message : %d", 5);
#endif
}

////////////////////////////////////////////////////////////////////////
#else
int main()
{
fprintf(stderr, "MPR_FEATURE_ESP is not defined in config.h\n");
exit(2);
}
#endif /* MPR_FEATURE_ESP */



© Mbedthis Software LLC, 2003-2204. All rights reserved. Mbedthis is a trademark of Mbedthis Software LLC.