The goaheadCompat sample demonstrates use of the GoAhead WebServer compatibility API module. For customers with existing GoAhead WebServer code, AppWeb supports the WebServer API so that previous WebServer applications can be easily migrated to AppWeb. The AppWeb compatibility API supports the WebServer ASP, GoForm and utility APIs.
To compile a program that uses the AppWeb compatibility API, you must include the
appWeb/compatApi.h header and then link with the
libcompatModule library. The sample is a main program that listens on port 8888 for HTTP requests.
Files
goaheadCompat.conf
goaheadCompat.c
Configuration File
goaheadCompat.conf
DocumentRoot "."
Listen 8888
ThreadLimit 0
LoadModule ejs ../../../lib/libejsModule
LoadModule egi ../../../lib/libegiHandler
LoadModule esp ../../../lib/libespHandler
LoadModule static ../../../lib/libcopyHandler
#
# Load the C API module, then the GoAhead compatibility module
#
LoadModule capi ../../../lib/libcapiModule
LoadModule compat ../../../lib/libcompatModule
AddHandler egiHandler .egi
AddHandler espHandler .esp .asp
AddHandler copyHandler
SetHandler egiHandler
DirectoryIndex index.asp
This configuration file loads the embedded JavaScript module and the embedded server pages and embedded gateway interface, and static content handlers. It then loads the AppWeb C API module and the GoAhead compatibility module.
The sample must run single-threaded as the GoAhead WebServer API is not thread safe. It assumes that the sample is being run from the
C/goaheadCompat directory under samples. Modify these module paths to suit your installation.
You should modify the
DocumentRoot and
Listen directives to suit your application's needs.
Source Code
goaheadCompat.c
/*
* Copyright (c) Mbedthis Software LLC, 2003-2004. All Rights Reserved.
* Portions Copyright (c) GoAhead Software, 1998-2000.
*/
/*!
* @file goaheadCompat.c
* @brief Demonstrate the GoAhead WebServer API compatibility
*/
/******************************* Includes *****************************/
#include "appWeb/appWeb.h"
#if MPR_FEATURE_COMPAT
/************************** Forward Declarations **********************/
static int addMyExtensions();
static int aspTest(int eid, webs_t wp, int argc, char_t **argv);
static void formTest(webs_t wp, char_t *path, char_t *query);
static void formWithError(webs_t wp, char_t *path, char_t *query);
static int websHomePageHandler(webs_t wp, char_t *urlPrefix,
char_t *webDir, int arg, char_t *url, char_t *path,
char_t *query);
/********************************* Code *******************************/
/*
* See the addMyExtensions routine for the use of GoAhead APIs
*/
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 "goaheadCompat"
*/
mprCreateMpr("goaheadCompat");
/*
* Do the following two statements only if you want debug trace
*/
mprAddLogFileListener();
mprSetLogSpec("stdout:2");
/*
* Start run-time services
*/
mprStartMpr(1);
/*
* 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 goaheadCompat.conf.
*/
if (maConfigureServer(server, "goaheadCompat.conf", 0) < 0) {
fprintf(stderr,
"Can't configure the server. Error on line %d\n",
maGetConfigErrorLine(server));
exit(2);
}
/*
* Routine to demonstrate the GA Compatibility
*/
addMyExtensions();
/*
* 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;
}
/**********************************************************************/
static int addMyExtensions()
{
void *mp;
char *cp;
sym_t *sp;
value_t v;
int sd, rc;
/*
* Define ASP and goForm functions
*/
websAspDefine(T("aspTest"), aspTest);
websFormDefine(T("formTest"), formTest);
websFormDefine(T("formWithError"), formWithError);
/*
* URL handler for the home page
*/
websUrlHandlerDefine(T("/"), NULL, 0, websHomePageHandler, 0);
/*
* Test other miscellaneous routines. This is just to test the
* syntax and to demonstrate basic operation. For full usage
* details -- consult the GoAhead WebServer documentation.
*/
mp = balloc(B_L, 5);
brealloc(B_L, mp, 50);
bfree(B_L, mp);
mp = 0;
bfreeSafe(B_L, mp);
fmtAlloc(&cp, 256, "Hello %s", "World");
bfree(B_L, cp);
sd = symOpen(59);
a_assert(sd >= 0);
v.type = string;
v.value.string = "444 Lake City Way";
symEnter(sd, "Peter Smith", v, 0);
sp = symLookup(sd, "Peter Smith");
a_assert(sp);
rc = symDelete(sd, "Peter Smith");
a_assert(rc == 0);
symClose(sd);
return 0;
}
/********************************* ASP ********************************/
/*
* Typcial asp function. Usage "aspTest name address"
*/
static int aspTest(int eid, webs_t wp, int argc, char_t **argv)
{
char_t *name, *address;
a_assert(websValid(wp));
if (ejArgs(argc, argv, T("%s %s"), &name, &address) < 2) {
websError(wp, 400, T("Insufficient args\n"));
return -1;
}
return websWrite(wp, T("Name: %s, Address %s"), name, address);
}
/******************************* Goforms ******************************/
/*
* Typcial GoForm function. Parameters name address
*/
static void formTest(webs_t wp, char_t *path, char_t *query)
{
char_t *name, *address;
/*
* The second parameter is an optional default
*/
name = websGetVar(wp, T("name"), T("Joe Smith"));
address = websGetVar(wp, T("address"), T("1212 Milky Way Ave."));
websHeader(wp);
websWrite(wp, T("Name: %s, Address: %s
\n"),
name, address);
websFooter(wp);
websDone(wp, 200);
}
/**********************************************************************/
/*
* GoForm returning an error to the browser
*/
static void formWithError(webs_t wp, char_t *path, char_t *query)
{
websError(wp, 400, "Intentional error testing websError");
}
/**************************** URL Handlers ****************************/
/*
* URL handler for the home page. Called when "/" is requested.
*/
static int websHomePageHandler(webs_t wp, char_t *urlPrefix,
char_t *webDir, int arg, char_t *url, char_t *path, char_t *query)
{
if (*url == '\0' || gstrcmp(url, T("/")) == 0) {
websRedirect(wp, T("home.asp"));
return 1;
}
return 0;
}
/**********************************************************************/
#else
int main()
{
fprintf(stderr, "MPR_FEATURE_COMPAT is not defined in config.h\n");
exit(2);
}
#endif /* MPR_FEATURE_COMPAT */