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.c
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
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.c
/*
* Copyright (c) Mbedthis Software LLC, 2003-2004. All Rights Reserved.
*/
/*!
* @file simpleEsp.c
* @brief Demonstrate the use of Embedded Server Pages (ESP) in a
* simple multi-threaded application.
*/
/******************************* Includes *****************************/
#include "appWeb/appWeb.h"
/********************************* Code *******************************/
#if MPR_FEATURE_C_API
/**********************************************************************/
/*
* Function 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.
*/
static int myEsp(MaRequest *rq, int argc, char **argv)
{
char *s;
int i;
/*
* There are a suite of write calls available. This just writes a
* string.
*/
maWriteStr(rq, "<h1>Hello World</h1><p>Args: ");
mprAssert(argv);
for (i = 0; i < argc; ) {
s = argv[i];
maWriteStr(rq, s);
if (++i < argc) {
maWriteStr(rq, " ");
}
}
maWriteStr(rq, "</p>");
/*
* Procedures can return a result
*/
maSetResult(rq, "sunny day");
return 0;
}
/**********************************************************************/
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 "simpleEsp"
*/
mprCreateMpr("simpleEsp");
/*
* 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", ".");
/*
* Configure the server based on the directives in
* simpleEsp.conf.
*/
if (maConfigureServer(server, "simpleEsp.conf", 0) < 0) {
fprintf(stderr,
"Can't configure the server. Error on line %d\n",
maGetConfigErrorLine(server));
exit(2);
}
/*
* Define our ESP procedures
*/
maDefineEsp("helloWorld", myEsp);
/*
* 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 /* MPR_FEATURE_C_API */
int main()
{
fprintf(stderr, "MPR_FEATURE_C_API is not defined in config.h\n");
exit(2);
}
#endif /* MPR_FEATURE_C_API */