CGI Program

CGI, The Common Gateway Interface, is a set of standards that define how information is exchanged between the web server and a custom script. 
Silk can run as CGI script program on a Web Server which supports CGI and it is configured to handle CGI Programs.
Here is an example how to configure Apache for CGI:

Apache CGI
On Windows, we extract the Apache2 package into C:\apache2, and edit the following settings in C:\apache2\conf:
ScriptAlias /cgi-bin/ "c:/apache2/cgi-bin/"

<Directory "c:/apache2/cgi-bin">
AllowOverride None
Options ExecCGI
Order allow,deny
Allow from all

AddHandler cgi-script .exe .pl .cgi .si

Save the conf and run httpd.exe in C:/apache2/bin to start the Apache.

CGI program
Here we create the first CGI program named, and copy it into c:/apache2/cgi-bin, the source code of is as follows:
//The top line is the path of Silk Interpreter, which begins with #!
//It will tell Apache the location of the Silk interpreter file
//Please replace it with Linux Silk Interpreter if the CGI is in Linux

    printf("This is a Silk CGI program");

Type in browser and we will get the following result:
This is a Silk CGI program

CGI Environment
CGI gets the data from the web server&browser through environments, and most of the environments are as follows:

  • CONTENT_TYPE    The data type of the content. Used when the client is sending attached content to the server. 
  • CONTENT_LENGTH    The length of the content. It is available only for POST requests.
  • HTTP_COOKIE    Returns the set cookies in the form of key & value pair.
  • HTTP_USER_AGENT    Contains information about the user agent. It is name of the web browser.
  • QUERY_STRING    The URL-encoded information that is sent with GET method request.
  • REMOTE_ADDR    The IP address of the remote host making the request. 
  • REQUEST_METHOD    The method used to make the request. The most common methods are GET and POST.
  • SCRIPT_FILENAME    The full path to the CGI script.
  • SCRIPT_NAME    The name of the CGI script.
  • SERVER_NAME    The server's hostname or IP Address
  • SERVER_SOFTWARE    The name and version of the software the server is running.
Silk will get environment and POST data with the following built-in functions:
 _fun("getenv", env_name)
getenv is the function name, env_name is the environment name. getenv can get all the environments.

_fun("getstdin", length)
getstdin is the function name, length is the size of the POST data. getstdin can get the POST data, which can be binary data.

_fun("putstdin", data)
putstdin is function name, data is the data which will send to the browser, it can be binary data. putstdin can be used to send binary data such as file data to browser.

The following example shows how to get environments and POST data by using  getenv:

//The top line is the path of Silk Interpreter, which begins with #!
//It will tell Apache the location of the Silk interpreter file

func getenvs()
        len= _int(env["CONTENT_LENGTH"]);
        env["POST"]=_fun("getstdin",len);//get the POST data
    return env;
    printf("<h2>CGI Env: </h2>");

Silk can also be used as an HTML-embedded server-side scripting language like PHP/ASP, so we can integrated Silk code into HTML to generate dynamic web page.
Please refer to Server Scripts and the CGI sample in Silk installer.