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 CGIOn 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
</Directory>
AddHandler cgi-script .exe .pl .cgi .si
Save the conf and run httpd.exe in C:/apache2/bin to start the Apache.
CGI programHere
we create the first CGI program named test.si, and copy it
into c:/apache2/cgi-bin, the source code of test.si is as follows:
#!C:\Silk\Silk.exe
//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
main()
{
printf("Content-type:text/html\n\n");
printf("This is a Silk CGI program");
}
Type http://127.0.0.1/cgi-bin/test.si in browser and we will get the following result:
This is a Silk CGI program
CGI EnvironmentCGI 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:
#!C:\Silk\Silk.exe
//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()
{
env={};
env["REQUEST_METHOD"]=_fun("getenv","REQUEST_METHOD");
env["CONTENT_LENGTH"]=_fun("getenv","CONTENT_LENGTH");
env["QUERY_STRING"]=_fun("getenv","QUERY_STRING");
env["REQUEST_URI"]=_fun("getenv","REQUEST_URI");
env["REMOTE_ADDR"]=_fun("getenv","REMOTE_ADDR");
env["SERVER_ADDR"]=_fun("getenv","SERVER_ADDR");
env["DOCUMENT_ROOT"]=_fun("getenv","DOCUMENT_ROOT");
env["HTTP_HOST"]=_fun("getenv","HTTP_HOST");
env["HTTP_CONNECTION"]=_fun("getenv","HTTP_CONNECTION");
env["HTTP_USER_AGENT"]=_fun("getenv","HTTP_USER_AGENT");
env["SERVER_SOFTWARE"]=_fun("getenv","SERVER_SOFTWARE");
if(env["REQUEST_METHOD"]=="POST")
{
len= _int(env["CONTENT_LENGTH"]);
env["POST"]=_fun("getstdin",len);//get the POST data
}
return env;
}
main()
{
printf("Content-type:text/html\n\n");
env=getenvs();
printf("<html>");
printf("<h2>CGI Env: </h2>");
for(i=env.begin();!env.end(i);env.next(i))
{
print(env.get(i)[0],"=",env.get(i)[1],"<br/>");
}
printf("</html>");
}
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.