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
Makefile
simpleEgi.c
index.html Web Page
<HTML>
<HEAD>
<TITLE>Embedded Gateway Interface (EGI) Sample</TITLE>
</HEAD>
<BODY>
<h1>Embedded Gateway Interface (EGI) Sample</h1>
<FORM action=/myEgi.egi method=POST>
<TABLE>
<TR>
<TD>Name:</TD><TD><input type=text name=name size=50 value=""></TD>
</TR>
<TR>
<TD>Address:</TD><TD><input type=text name=address size=50 value=""></TD>
</TR>
<TR>
<TD ALIGN="CENTER">
<input type=submit name=ok value="OK">
<input type=submit name=ok value="Cancel">
</TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML>
Configuration File
simpleEgi.conf
DocumentRoot "."
Listen 8888
ThreadLimit 4
AddHandler egiHandler .egi
AddHandler copyHandler
The simpleEgi.conf file is an AppWeb configuration
file. It is configured to run single-threaded and assumes
that the sample is being run from the current directory.
You should modify the DocumentRoot and Listen directives
to suit your application's needs.
Makefile
The Makefile will build on Windows or Linux. A Windows VS 6.0 project
file is also supplied.
Typical output from the Makefile build is listed below. This is the
output on a Windows system:
cl -o selectEventLoop.exe selectEventLoop.c -Zi -Od -D_NDEBUG -W3 -nologo -MDd -FD -DWIN -D_DLL \
-D_MT -D_WINDOWS -DWIN32 -D_WIN32_WINNT=0x500 -D_X86_=1 -D_CRT_SECURE_NO_DEPRECATE -D_USRDLL \
-I../../../include ../../../bin/libappwebStatic.lib ws2_32.lib advapi32.lib user32.lib
Source Code
simpleEgi.c
/*
* @file simpleEgi.c
* @brief Demonstrate the use of the Embedded Gateway Interface (EGI)
* in a simple multi-threaded application.
* Copyright (c) Mbedthis Software LLC, 2003-2007. All Rights Reserved.
*/
/******************************* Includes *****************************/
#define UNSAFE_FUNCTIONS_OK 1
#include "appweb/appweb.h"
/********************************* Code *******************************/
#if BLD_FEATURE_C_API_MODULE
/*
* This method 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.
*/
static void myEgi(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) {
maCreateEnvVars(rq, postData, postLen);
}
maWriteStr(rq, "simpleEgi\r\n");
maWriteFmt(rq, "Name: %s
\n",
maGetVar(rq, MA_FORM_OBJ, "name", "-"));
maWriteFmt(rq, "Address: %s
\n",
maGetVar(rq, MA_FORM_OBJ, "address", "-"));
maWriteFmt(rq, "\r\n");
#if POSSIBLE
//
// Useful things to do in egi forms
//
maSetResponseCode(rq, 200);
maSetContentType(rq, "text/html");
maSetHeaderFlags(rq, MPR_HTTP_DONT_CACHE);
maRedirect(rq, "/myURl");
maRequestError(rq, 409, "My message : %d", 5);
#endif
}
/**********************************************************************/
int main(int argc, char** argv)
{
MaHttp *http; /* For the http service inside our app */
MaServer *server; /* For a HTTP server */
/*
* Initialize the run-time and give our app a name "simpleEgi"
*/
mprCreateMpr("simpleEgi");
/*
* Do the following two statements only if you want debug trace
*/
mprAddLogFileListener();
mprSetLogSpec("stdout:4");
/*
* Start run-time services
*/
mprStartMpr(0);
/*
* Create the HTTP and server objects. Give the server a name
* "default" and define "." as the default serverRoot, ie. the
* directory with the server configuration files.
*/
http = maCreateHttp();
server = maCreateServer(http, "default", ".");
/*
* Activate the handlers. Only needed when linking statically.
*/
mprEgiInit(0);
mprCopyInit(0);
/*
* Configure the server based on the directives in
* simpleEgi.conf.
*/
if (maConfigureServer(server, "simpleEgi.conf") < 0) {
fprintf(stderr,
"Can't configure the server. Error on line %d\n",
maGetConfigErrorLine(server));
exit(2);
}
/*
* Define our EGI form
*/
maDefineEgiForm("/myEgi.egi", myEgi);
/*
* Start serving pages. After this we are live.
*/
if (maStartServers(http) < 0) {
fprintf(stderr, "Can't start the server\n");
exit(2);
}
/*
* Service events. This call will block until the server is exited
* Call mprTerminate() at any time to instruct the server to exit.
* The -1 is a timeout on the block. Useful if you use
* MPR_LOOP_ONCE and have a polling event loop.
*/
mprServiceEvents(MPR_LOOP_FOREVER, -1);
/*
* Stop all HTTP services
*/
maStopServers(http);
/*
* Delete the server and http objects
*/
maDeleteServer(server);
maDeleteHttp(http);
/*
* Stop and delete the run-time services
*/
mprStopMpr();
mprDeleteMpr();
return 0;
}
/**********************************************************************/
#else /* BLD_FEATURE_C_API_MODULE */
int main()
{
fprintf(stderr, "BLD_FEATURE_C_API_MODULE is not defined in config.h\n");
exit(2);
}
#endif /* BLD_FEATURE_C_API_MODULE */