CGI programming with GSL

Now that you have seen how to output HTML from a GSL program, the next step is to use GSL as a Common Gateway Interface and generate the HTML dynamically from the browser. In the following example, an HTML form is used to request input which is then passed to a GSL program which functions as a CGI. The GSL will generate a web page based on form input.

First a form is needed. Here is the HTML for a simple form which prompts for a single parameter, your name:

<form method="post" action="/Gsharp-bin/gs_hello1.gsw">
<b>Please type your name: </b><input type="text" size=20 name="name">
<input type="submit" value="Enter">
</form>
The action attribute to the HTML form element points to the CGI that handles the form, in this case the GSL script gs_hello1.gsw.

The GSL program created in the previous example can, with a few minor modifications, function as a CGI. First, a comment must be added at the beginning of the script so that UNIX knows how to execute it:


  #!/bin/GsharpWE
On the Windows NT platform, this is not necessary as the .gsw extension determines which program is used to run the script. However, it does no harm to include the comment.

Previously, the GSL program only output HTML. When run as a CGI, it must also output a HyperText Transfer Protocol (HTTP) header. This is done with these additional statements added as the first fwrite output:


  fwrite(id, "Content-type: text/html");
  fwrite(id, "");
Next, the output changed from going to a file, to going directly to stdout. The fopen and fclose statements are no longer needed:


  id = 1;

And finally, make a change to the greeting text so that the form input is used:

  fwrite(id, "Hello ", FORM_name);
You may wonder where the GSL variable FORM_name comes from. GsharpWE will automatically parse form input, either from the environment variable QUERY_STRING when the form method "GET" is used, or from stdin when the method "POST" is used. GsharpWE will append the input name defined by the form to the prefix "FORM_" and create a string variable which is set to the corresponding form value string. Recall the HTML statement from the form:


</b><input type="text" size=20 name="name">
This statement defines a form parameter named "name". GsharpWE parses this parameter from stdin and creates the variable FORM_name which is then used in the GSL fwrite statement.

A complete listing of the modified GSL program follows.

Check Point


#!/bin/GsharpWE
/*
 * Open file, write out HTML, close file
 */
id = 1;
fwrite(id, "Content-type: text/html");
fwrite(id, "");
fwrite(id, "<HTML><BODY BGCOLOR=#FFFFFF>");
fwrite(id, "<TITLE>GsharpWE Greeting</TITLE>");
fwrite(id, "<FONT SIZE=+4><CENTER><B>");
fwrite(id, "Hello ", FORM_name);
fwrite(id, "</B></CENTER></FONT><P>");
fwrite(id, "</BODY></HTML>");
To run any cgi script, the script must be recognised as a script by the web werver. The GsharpWE installation has added the Gsharp cgi-bin examples including gs_hello1.gsw as /Gsharp-bin/. You can try running it using the form below:


Please type your name:

Carry on to Graph on Demand