CGI & WinCGI Scripts

The Sambar Server CGI implementation is based on the CGI/1.1 specification. CGI enables HTTP server to be extended via server-side applications. Stdin, stdout and environment variables are used to communicate between the HTTP Server and the CGI application.

CGI & WinCGI Directories

The Sambar Server requires all CGI scripts to be placed in a CGI directory specified in the configuration file. While this directory may have sub-directories, the root for all CGI scripts must be fixed (cgi-bin by default). Similarly, all WinCGI files is also fixed (cgi-win by default).

CGI applications with the extension .bat, .exe and .pl are recognized and properly executed by the Sambar Server. Files with other extensions require file system association with the appropriate executable (see Using tcl, sh etc. below).

Note: WinCGI is primarily used for Visual Basic applications. The sample provided with this server is a Visual Basic executable. You must have the runtime Visual Basic libraries installed on your system to run this sample.

Important: The installation directory of the Sambar Server may contain spaces (i.e. c:\program files\sambar). May CGI/WinCGI applications especially 16-bit applications or Perl applications that perform file i/o based on the installation directory will not run if there are spaces in the directory path. To execute properly, the Sambar Server must be moved to a directory that has not spaces.

CGI Command line options

The command line is used in the case of an ISINDEX query. If the QUERY_STRING environment variable contains only a single parameter with no = character, the parameter is passed to the CGI command on the command line. For example, the following URL /cgi-bin/finger?httpd will execute "finger httpd" on the command line and output the results.

Using Perl

When the Sambar Server executes a Perl CGI application (*.pl files found in the CGI directory), the perl.exe executable found in the perl subdirectory of the Sambar Server installation is used. This perl executable is public domain software distributed by ActiveWare.

For a comprehensive listing of Perl executables and more recent releases the Options from the menu. A window will appear with a "File Types" tab from which you can add, remove or edit file types. To work with the Sambar Server, you must be able to run the program by double clicking it from the shell.

Stdout

Stdout is sent to the browser from the execution of the script. Should the script run longer than the maximum configured time (CGI Timeout), no output from the CGI script is returned to the client; a timeout error is returned.

Environment Variables

Environment VariableDescription
SERVER_SOFTWARE is the name and version of the Sambar Server answering the request.
SERVER_NAME is the server's hostname, DNS alias, or IP address as it would appear in self-referencing URLs.
GATEWAY_INTERFACE is the revision of the CGI sepcification to which the server complies.
SERVER_PROTOCOL is the name and revision of the protocol this request came in with.
SERVER_PORT specifies port to which the request was sent.
REQUEST_METHOD is the method with which the request was made: "GET", "POST" etc.
QUERY_STRING is defined as anything following the first '?' in the URL. Typically this data is the encoded results from your GET form. The string is encoded in the standard URL format changing spaces to +, and encoding special characters with %xx hexadecimal encoding.
PATH_INFO The extra path information, as given by the client.
PATH_TRANSLATED The server provides a translated version of PATH_INFO, which takes the path and does a virtual-to-physical maping to it.
SCRIPT_NAME is a virtual path to the script being executed.
REMOTE_HOST is the host name making the request. If DNS lookup is turned off, the REMOTE_ADDR is set and this variable is unset.
REMOTE_ADDR is IP address of the remote host making the request.
CONTENT_LENGTH is length of any attached information from an HTTP POST.
CONTENT_TYPE is the media type of the posted data (usually application/x-www-form-urlencoded).

Returning Data

CGI programs can return content in many different document types (i.e. text, images, audio). They can also return references to other documents. To tell the server what kind of document you are sending back, CGI requires you to place a short header on your output. This header is ASCII text, consisting of lines separated by either linefeeds or carriage returns (or both) followed by a single blank line. The output body then follows in whatever native format.

If you begin your script output with either "HTTP/" then the Sambar Server will send all output exactly as the script has written it to the client. Otherwise, the Sambar Server will send a default header back (text/html file type) with any data returned from the script. Important: If you do not choose to write the entire HTTP header, you should not provide any special headers, as they will appear as part of the body after Sambar Server processing.

If you begin your script with "Content-type:" or "Location:" the Sambar Server will append the appropriate HTTP response status (200 or 302) followed by the content of your script exactly as received.

For example, to send back HTML to the client, your output should read:

        Content-type: text/html

        <HTML><HEAD>
        <TITLE>output of HTML from CGI script</TITLE>
        </HEAD><BODY>
        <H1>Sample output</H1>
        Blah, blah, blah.
        </BODY></HTML>

In the above example, the response prepended is: HTTP/1.0 200 OK
To reference a file on another HTTP server, you would output something like this:

        Location: http://www.sambar.com/
        Content-type: text/html

        <HTML><HEAD>
        <TITLE>Whoops...it moved</TITLE>
        </HEAD><BODY>
        <H1>Content Moved!</H1>
        </BODY></HTML>

In the above example, the response prepended is: HTTP/1.0 302 MOVED Important: The Location: directive comes prior to the Content-type: directive.


Copyright 1995 to 1997 Sambar Technologies