Fabl can serve web pages (or perform other tasks invoked over the web) if configured as a handler for the Common Gateway Interface under the Apache web server. The following are sample directives for inclusion in the Apache httpd.conf configuration files:
ScriptAlias /fablbin /usr/local/fabl-2.0.11/bin Action fabl-script /fablbin/fabl AddHandler fabl-script .fsp
The convetional extension for Fabl CGI files is "fsp", which stands for "Fabl Server Page".
Then, viewing a URL such ashttp://www.myserver.com/examples/hello.fsp?var1=abc&var2=defwill cause Fabl to start up and load the designated hello.fsp file. As usual with CGI, the text written to standard output by hello.fsp is sent to the client.
Fabl loads the configuration file cgi.txt instead of config.txt when started in cgi mode. cgi.txt in turn specifies:
initFile cgi.fbl
Here are the contents of cgi.fbl.
stdlibPrefix = "file://../lib/";
stdNamespaces();
stdEquivalents();
load("{stdlibPrefix}discovery0.fb");
discovery0:namespaces();
discovery0:discovery();
// place some limits on memory and processing time
maxMemory(memoryAllocated() + 20000); // in kbytes
maxTempFileSize(500000); // in bytes
maxPmSteps(pmSteps() + 10000000);
cgiMode = true;
loadCgiFile(true); // safe mode
quit(); //shouldn't get this far
The resource limitation functions are described in the configuration section. The function
void loadCgiFile(boolean safe)
loads the file that invoked this cgi session (eg hello.fsp). If safe is true, the function safeMode(true) is called before interpreting the contents of the cgi file. This has the effect of prohibiting local file access during the cgi session.
Cgi mode is silent by default. The function
string getenv(id ev)
returns the value of the environment variable ev. The CGI specification details the environment variables passed by the server. Fabl also supplies a utility for accessing data from the http GET and POST methods:
void setCgiVars()
This sets the variable httpRequestMethod to "GET" or "POST" (no Fabl support is present as yet for other http methods such as PUT). If the method was a GET, then the variable httpQueryString is set to the query string, and if a POST, then httpContent is set to the content sent by the client. The function
void parseCgi()
first calls setGgiVars, and then parses httpQueryString or httpContent depending on the method, and places the result in the associative array httpGet for GET and httpPost for POST. For example, if the query string is
var1=abc&var2=def
then httpGet['var1'] will contain the string "abc", and httpGet['var2'] will contain the string "def". (See the section on associative arrays). Note that the type of the expression httpGet['var1'] is Resource, not string, so that the type cast httpGet['var1']~string may be required.
Fabl cgi files should always end with a call to "quit();". The function
void htmlHeader()
emits an HTTP header with Content-Type text/html. It also invokes silent(false).
Here is an example. If the contents of the file hello.fsp are:
parseCgi();
htmlHeader();
writeln("<html><head><title>TEST</title></head>");
writeln("<body>var1 = <b>{httpGet['var1']}</b></body></html>");
quit();
then access to the URL
http://www.myserver.com/examples/hello.fsp?var1=abc&var2=def
will yield
Hello var1 = abc