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
Makefile
simpleEsp.cpp
index.esp Web Page
<HTML>
<HEAD>
<TITLE>Simple ESP Test Page</TITLE>
</HEAD>
<BODY>
<p>Calling the ESP procedure <b>helloWorld</b></p>
<% result = helloWorld("1", 2, 3, 4, "5"); %>
<p>Result is: @@result</p>
<h2>Some Test ESP calls</h2>
<% i=2;%>
<% write("i is " + i);%>
<% write("REMOTE_ADDR: " + REMOTE_ADDR);%> <br/>
<% write("QUERY_STRING: " + QUERY_STRING);%> <Br/>
<% write("AUTH_TYPE: " + AUTH_TYPE);%> <Br/>
<% write("CONTENT_LENGTH: " + CONTENT_LENGTH);%> <Br/>
<% write("CONTENT_TYPE: " + CONTENT_TYPE);%> <Br/>
<% write("GATEWAY_INTERFACE: " + GATEWAY_INTERFACE);%> <Br/>
<% write("PATH_INFO: " + PATH_INFO);%> <Br/>
<% write("PATH_TRANSLATED: " + PATH_TRANSLATED);%> <Br/>
<% write("QUERY_STRING: " + QUERY_STRING);%> <Br/>
<% write("REMOTE_ADDR: " + REMOTE_ADDR);%> <Br/>
<% write("REMOTE_USER: " + REMOTE_USER);%> <br/>
<% write("REQUEST_METHOD: " + REQUEST_METHOD);%> <br/>
<% write("SERVER_NAME: " + SERVER_NAME);%> <br/>
<% write("SERVER_PORT: " + SERVER_PORT);%> <br/>
<% write("SERVER_PROTOCOL: " + SERVER_PROTOCOL);%> <br/>
<% write("SERVER_SOFTWARE: " + SERVER_SOFTWARE);%> <br/>
<% write("HTTP_HOST: " + HTTP_HOST);%> <br/>
<% write("HTTP_USER_AGENT: " + HTTP_USER_AGENT);%> <br/>
<% write("HTTP_CONNECTION: " + HTTP_CONNECTION);%> <br/>
<% for (i = 0;i < 3; i++) {
write("<p>Line " + i + "</p>");
}
%>
</BODY>
</HTML>
Configuration File
simpleEsp.conf
DocumentRoot "."
Listen 8888
ThreadLimit 4
AddHandler espHandler .esp
AddHandler copyHandler
This 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 simpleEsp.exe simpleEsp.cpp -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
simpleEsp.cpp
///
/// @file simpleEsp.cpp
/// @brief Demonstrate the use of Embedded Server Pages (ESP) in a
/// simple multi-threaded application.
//
// Copyright (c) Embedthis Software LLC, 2003-2009. All Rights Reserved.
//
/////////////////////////////// Includes ///////////////////////////////
#include "appweb/appweb.h"
////////////////////////// Forward Declarations ////////////////////////
#if BLD_FEATURE_ESP_MODULE
static int helloWorld(EspRequest *ep, 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 BLD_FEATURE_LOG
//
// Do the following two statements only if you want debug trace
//
mpr.addListener(new MprLogToFile());
mpr.setLogSpec("stdout:4");
#endif
//
// Start the Embedthis 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", ".");
//
// Activate the ESP module and handler
//
new MaEspModule(0);
new MaCopyModule(0);
//
// Configure the server with the configuration directive file
//
if (server->configure("simpleEsp.conf") < 0) {
mprFprintf(MPR_STDERR,
"Can't configure the server. Error on line %d\n",
server->getLine());
exit(2);
}
//
// Define our ESP procedures
//
espDefineStringCFunction(0, "helloWorld", helloWorld, 0);
//
// 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;
}
////////////////////////////////////////////////////////////////////////
//
// Function that is run when the ESP procedure is called from the web
// page. ep is the ESP context handle. argv contains the parameters to the
// ESP procedure defined in the web page.
//
static int helloWorld(EspRequest *ep, int argc, char **argv)
{
char *s;
int i;
//
// There are a suite of write calls available. This just writes a
// string.
//
espWriteString(ep, "<h1>Hello World</h1><p>Args: ");
mprAssert(argv);
for (i = 0; i < argc; ) {
s = argv[i];
espWriteString(ep, s);
if (++i < argc) {
espWriteString(ep, " ");
}
}
espWriteString(ep, "</p>");
//
// Procedures can return a result
//
espSetReturnString(ep, "sunny day");
return 0;
}
////////////////////////////////////////////////////////////////////////
#else
int main()
{
fprintf(stderr, "BLD_FEATURE_ESP_MODULE is not defined in config.h\n");
exit(2);
}
#endif /* BLD_FEATURE_ESP_MODULE */