Sambar Server Documentation
|
CGI & WinCGI Scripts |
$| = 1;
The Sambar Server allows CGI and WinCGI scripts to be executed from anywhere in the Documents Directory by using the CGI Extensions and WinCGI Extensions. CGI's are typically restricted to the cgi-bin or cgi-win directories because UNIX machines tend not to use the document's MIME type to identify executable scripts. The CGI directory provides the "hint" needed by the server. The other reason for restricting CGIs to a particular directory is for security reasons (i.e. only "trusted" users are allowed to post CGIs, but anyone can post content). If neither of these apply to your environment, CGI's can be executed anywhere in the Documents Directory.
CGI applications with the extension .bat, .exe and .pl are recognized and properly executed by the Sambar Server. Files with other extensions require either a interpreter indicator at the top of the file (#!) or a file system association with the appropriate executable (see Using tcl, sh etc. below).
The cgi-bin and cgi-win directories are designed specifically for CGI applications. HTML and text documents must be placed in the docs directory.
Note: WinCGI is primarily used for Visual Basic and Delphi 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. This is particularly important for WinCGI Visual Basic applications because the Sambar Server automatically puts quotes around input parameters if the parameters have spaces; Visual Basic improperly interprets these file name parameters.
CGI Directory = /docs/cgi-bin/
For a comprehensive listing of Perl executables and more recent releases the Comprehensive Perl Archive Network is the best source. The CPAN contains the collected wisdom of the Perl community.
To use a newer version or perl, simple copy the perl.exe and associated DLLs to the perl subdirectory of the Sambar Server Installtion. Optionally, you can modify the Perl Executable parameter in the config.ini file to point to the location where you have installed the perl.exe. Many Perl installations (ActiveState and CPAN) install perl in /perl/bin by default, so the appropriate modification would be to set:
Perl Executable = c:\perl\bin\perl.exe
Note: Many Perl scripts written for UNIX do not run properly with the Sambar Server. Typically, one or more of the following is the issue:
#!
interpreter directive.
If found, this directive is used to execute the CGI request.
If an interpreter directive is not found, the FindExecutable() windows
API is used to find the file association.
The following is an example of how this directive is used
(typically on UNIX systems):
#!/sambar/distrib/perl/perl.exe
print "SERVER_PORT: ", $ENV{'SERVER_PORT'};
print "REMOTE_HOST: ", $ENV{'REMOTE_HOST'};
Open With...
Select this menu item and when Windows
asks which program to open, select from the list or browse to the correct
interpreter, making sure to check always use this program to open
this type of file
.
CGI
Environment Variable | Description |
---|---|
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). |
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 any of the following:
the Sambar Server will append the appropriate HTTP response status (200 or 302) followed by the headers and 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.
CGI Failure... CGI command: C:\Program Files\sambar/cgi-bin/dmailweb.exe Failure status: 7858720 No output was written to stderr...
The CGI exit test is configurable using the configuration parameter CGI Exit Test = true. You can set this to false if your CGI programs do not return an exit status of zero (0) on success.
© 1998 Sambar Technologies. All rights reserved. Terms of Use.