Writing an RCGI Application


In addition to using LCGI, BASIC, or Perl to create a dynamic web page, you can use ANSI C to write an RCGI application that generates a dynamic web page. This chapter explains how to write an RCGI application and describes the associated header and source files.

Guidelines for Writing an RCGI Application

To write an RCGI application, you must be familiar with the ANSI C programming language. If the web page will be executed from the NetWare platform only, you must write the application to be an NLM. RCGI applications use sockets to communicate with the server. The Web Server and its extensions use the SRM.CFG configuration file to map sockets to data-communication ports.

The \WEB\SAMPLES\CGIAPP directory contains an RCGI application template called CGIAPP.C. This file contains code that performs the socket calls required by RCGI applications and NLMs. If you decide not to use the template, you will need to understand socket programming.

The \WEB\SAMPLES\CGIAPP directory also contains the RCGI header file, RCGI.H, and the RCGI source file, RCGI.C.

The RCGI Header File

The RCGI.H file defines a number of useful functions and variables for use with RCGI applications. The file contains the following statements:

#include <stdio.h>
#include <process.h>

/****************************************************************/
/************ VARIABLE PROVIDED BY THE RCGI INTERFACE *********/
/**************************************************************/
extern char *env[];** Contains the environment string that **/
                  /** was received from the web server **/
extern char *input; /** Contains the stdin that was received **/
                    /** from the web server **/
extern char *cmd_line; /** Contains the command line that **/
                       /** was received from the web server **/
int server_port = 8003; /*** Set this to the default port **/
                        /** that you need **/

/*****************************************************/
/****** FUNCTIONS PROVIDED BY THE RCGI INTERFACE *********/
/************************************************************/
extern int rcgi_main(char *port, int (*fptr)()); 
               /** Main RCGI processor **/
               /** "port" specifies the port on which this **/
               /** server should listen **/
               /** "fptr" is a pointer to a function that does
               /** all the extension work including **/
               /** sending back formatted data       **/

extern int rcgi_cleanup(); /** Use this to clean up the RCGI **/
                           /** part of the code when you **/
                           /** code when you are done **/

extern int rcgi_sendreply(char *buf); /** Use this to send **/
                           /** the NULL terminated buf to the **/
                           /** web server **/

extern int rcgi_sendheader();/** Use this to send the generic **/
                             /** plain text header **/

The RCGI Template

The RCGI template file, CGIAPP.C, contains a skeleton program that you can modify by adding statements to complete the processing necessary for your unique web page. The source code in CGIAPP.C performs the following

The RCGI Source File

The RCGI source file, RCGI.C, contains source code that performs several necessary tasks. Make sure you include RCGI.C when you build your program. You won't need to modify the code in this file, but you should understand the functions it provides.

Click here to see a table of the functions defined in RCGI.C that are called directly by CGIAPP.C.

Modifying the Template

The CGIAPP.C file contains the skeleton of a C program that creates an program. As delivered, CGIAPP.C contains a simple process_request procedure that sends the file server name and the values of the environment variables. You'll need to replace this procedure with code that performs the processing needed to generate your web pages.

Click here to view the CGIAPP.C template file.

In CGIAPP.C, the calls to rcgi_cleanup function are specific to the NetWare operating system. You may need to replace them with functions specific to the platform on which the web page will be executed.

Compiling and Building an RCGI Application

After you have finished writing your RCGI application, you must compile and build it. This procedure is dependent upon the platform on which the application will be executed.

If your RCGI application is for the NetWare operating system, you can use a makefile like the one below to create an NLM. You can modify the makefile where appropriate for use on other platforms. This particular makefile creates an RCGI NLM named CGIAPP.NLM.

CC = i386-novell-netware-gcc
 
LD = i386-novell-netware-gcc
NLMCONV = i386-novell-netware-nlmconv
 
cgiapp.nlm : cgiapp.0
      $(NLMCONV) -T cgiapp.0 cgiapp.nlm
 
cgiapp.0 : cgiapp.o rcgi.o
 
cgiapp.o : cgiapp.c rcgi.h
      $(CC) cgiapp.c
 
rcgi.o : rcgi.c
      $(CC) rcgi.c

If you created an NLM, you can store it in any directory on the NetWare server.

Editing the SRM.CFG File

To define the RCGI application to the server, regardless of whether it is an NLM, add a RemoteScriptAlias directive to the SRM.CFG file. For example, to define CGIAPP to the server, add the following directive to the SRM.CFG file:

RemoteScriptAlias /cgiproc/ localhost:8003/sys:web samples/cgiapp/

Defining the RCGI Application to the Server

To restart the NetWare Web Server, type these commands at the console prompt.

unload http
load http

Starting the RCGI Application

The method for starting your RCGI application depends on the platform on which it will execute. An NLM has to be running at all times in order to accept commands from the server. To start execution of the NLM, use the LOAD command. The syntax of the LOAD command is

load yournlm -p portnumber

yournlm is the name of the NLM to be started.

portnumber is the number of the port on which the NLM runs. Make sure you load the NLM on the port you specified in the RemoteScriptAlias directive in the SRM.CFG file (see Editing the SRM.CFG File).

Testing the RCGI Application

To test the RCGI application, type its URL in a browser and check the web page that is returned. For example, type

http://www.webserver/cgiproc/extra/path/info?a=b

Examine the resulting web page to be sure that all functions perform properly.