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
libappwebStatic library. The sample is a main program
that listens on port 8888 for HTTP requests.
Files
goaheadCompat.conf
Makefile
goaheadCompat.c
Configuration File
goaheadCompat.conf
DocumentRoot "."
Listen 8888
ThreadLimit 0
AddHandler egiHandler .egi
AddHandler espHandler .esp .asp
AddHandler copyHandler
<Location /goform>
SetHandler egiHandler
</Location>
DirectoryIndex index.asp
The goaheadCompat.conf file is an Appweb configuration file that
activates the embedded server pages and embedded gateway
interface, and static copy handlers. The GoAhead compatability
API and Appweb C APIs are linked directly from the appwebStatic
library.
The sample must run single-threaded as the GoAhead WebServer API
is not thread safe.
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 goaheadCompat.exe goaheadCompat.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
goaheadCompat.c
/*
* @file goaheadCompat.c
* @brief Demonstrate the GoAhead WebServer API compatibility
* Copyright (c) Embedthis Software LLC, 2003-2009. All Rights Reserved.
* Portions Copyright (c) GoAhead Software, 1998-2000.
*/
/******************************* Includes *****************************/
#define UNSAFE_FUNCTIONS_OK 1
#include "appweb/appweb.h"
#if BLD_FEATURE_COMPAT_MODULE
/************************** 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(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.
*/
mprEjsInit(0);
mprEspInit(0);
mprEgiInit(0);
mprCopyInit(0);
/*
* Configure the server based on the directives in goaheadCompat.conf.
*/
if (maConfigureServer(server, "goaheadCompat.conf") < 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, "BLD_FEATURE_COMPAT_MODULE is not defined in config.h\n");
exit(2);
}
#endif /* BLD_FEATURE_COMPAT_MODULE */