
CGI Function Example
E-8
E.3.2
Create the Base WEBPAGE Source File
Once the HTML pages are ready in source form, the main WEBPAGE.C
source file is created. This file will perform all the necessary WEB processing
in the example. The basic source code declares the HTML pages as ”files” to
the EFS file system. To do this, it exports two functions called from the main
network initialization routine, AddWebFiles() and RemoveWebFiles(). Note
that a CGI function is also declared to handle processing of the CGI form con-
tained on the WEB page, called
sample.cgi
.
The source code as defined so far is shown below.
#include <netmain.h>
#include ”cgiparse.h”
\\ Our Private CGI Parser. (See section E.2.3)
#pragma DATA_SECTION(DEFAULT, ”OBJMEM”);
#include ”webdata\default.c”
static const char htm404[]=
”<html><body><h1>HTTP/1.0 404 Object Not Found</h1></body></html>”;
static int cgiSample(int htmlSock, int ContentLength );
\\ Our CGI Function
void AddWebFiles(void)
{
efs_createfile(”index.html”, DEFAULT_SIZE, DEFAULT);
efs_createfile(”sample.cgi”, 0, (UINT8*)cgiSample);
efs_createfile(”404.htm”, strlen(htm404), (UINT8 *)htm404);
}
void RemoveWebFiles(void)}
{
efs_destroyfile(”404.htm”);
efs_destroyfile(”sample.cgi”);
efs_destroyfile(”index.html”);
}
E.3.3
Create CGI Function
All that is left is to create the CGI function. The CGI function performs the fol-
lowing steps:
1) Read in request data
2) Parse request using cgiParseVars(), or a similar function.
3) Process request in some meaningful way. Make sure the inputs are valid
before processing them to avoid crashes or security violations. This may
involve querying or updating the configuration, making a network request,
or updating a file.
4) Send a response. Keep in mind the first line of the response should indi-
cate whether the request was successful or not.