Simple Embedded Server Pages (ESP) Sample in C++

The simple HTTP server sample demonstrates how to use Embedded Server Pages within your application to create dynamically generated web pages. This sample adds HTTP server functionality and ESP support to a main program.

The sample is a multithreaded main program that listens on port 8888 for HTTP requests. By changing the value of the ThreadLimit directive in the configuration file to zero you can run single-threaded.

See also the equivalent C simpleEsp sample.

Files

index.esp
simpleEsp.conf
simpleEsp.cpp

index.esp Web Page



Simple ESP Test Page




Calling the ESP procedure helloWorld



<% result = helloWorld("1", 2, 3, 4, "5"); %>

Result is: @@result



Some Test ESP calls



<% i=2;%>
<% write("i is " + i);%>

<% write("REMOTE_ADDR: " + REMOTE_ADDR);%>

<% write("QUERY_STRING: " + QUERY_STRING);%>

<% write("AUTH_TYPE: " + AUTH_TYPE);%>

<% write("CONTENT_LENGTH: " + CONTENT_LENGTH);%>

<% write("CONTENT_TYPE: " + CONTENT_TYPE);%>

<% write("GATEWAY_INTERFACE: " + GATEWAY_INTERFACE);%>

<% write("PATH_INFO: " + PATH_INFO);%>

<% write("PATH_TRANSLATED: " + PATH_TRANSLATED);%>

<% write("QUERY_STRING: " + QUERY_STRING);%>

<% write("REMOTE_ADDR: " + REMOTE_ADDR);%>

<% write("REMOTE_USER: " + REMOTE_USER);%>

<% write("REQUEST_METHOD: " + REQUEST_METHOD);%>

<% write("SERVER_NAME: " + SERVER_NAME);%>

<% write("SERVER_PORT: " + SERVER_PORT);%>

<% write("SERVER_PROTOCOL: " + SERVER_PROTOCOL);%>

<% write("SERVER_SOFTWARE: " + SERVER_SOFTWARE);%>

<% write("HTTP_HOST: " + HTTP_HOST);%>

<% write("HTTP_USER_AGENT: " + HTTP_USER_AGENT);%>

<% write("HTTP_CONNECTION: " + HTTP_CONNECTION);%>


<% for (i = 0;i < 3; i++) {
write("

Line " + i + "

");
}
%>




Configuration File

simpleEsp.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/simpleEsp 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

simpleEsp.cpp

//
// Copyright (c) Mbedthis Software LLC, 2003-2004. All Rights Reserved.
//
/// @file simpleEsp.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 ESP object to be called when the web page is
// processed by the ESP handler.
//
class MyEsp : public MaEspProc {
public:
MyEsp(char *name);
~MyEsp();
int run(MaRequest *rq, int argc, char **argv);
};

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

int main(int argc, char** argv)
{
MaHttp *http; // Http service inside our app
MaServer *server; // For the HTTP server
Mpr mpr("simpleEsp"); // 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();

//
// 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 simpleEsp.conf.
//
http = new MaHttp();
server = new MaServer(http, "default", ".");

//
// Configure the server with the configuration directive file
//
if (server->configure("simpleEsp.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 MyEsp("helloWorld");

//
// 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;
}

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

MyEsp::MyEsp(char *name) : MaEspProc(name)
{
// Put required initialization (if any) here
}

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

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

////////////////////////////////////////////////////////////////////////
//
// Method that is run when the ESP procedure is called from the web
// page. Rq is the request context. Argv is the parameters to the
// ESP procedure defined in the web page.
//

int MyEsp::run(MaRequest *rq, int argc, char **argv)
{
char *s;
int i;

//
// There are a suite of write calls available. This just writes a
// string.
//
rq->write("

Hello World

Args: ");

mprAssert(argv);
for (i = 0; i < argc; ) {
s = argv[i];
rq->write(s);
if (++i < argc) {
rq->write(" ");
}
}
rq->write("

");

//
// Procedures can return a result
//
scriptEngine->setResult("sunny day");
return 0;
}

////////////////////////////////////////////////////////////////////////
#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.