Testing CGI

Here is a simple shell script which I always use when I first set up a new web server. It prints out some of the CGI environment variables and gives you a warm feeling that everything is working.
 
#!/bin/sh

# disable filename globbing
set -f

echo Content-type: text/plain
echo

echo CGI/1.0 test script report:
echo

echo argc is $#. argv is "$*".
echo

echo SERVER_SOFTWARE = $SERVER_SOFTWARE
echo SERVER_NAME = $SERVER_NAME
echo GATEWAY_INTERFACE = $GATEWAY_INTERFACE
echo SERVER_PROTOCOL = $SERVER_PROTOCOL
echo SERVER_PORT = $SERVER_PORT
echo REQUEST_METHOD = $REQUEST_METHOD
echo HTTP_ACCEPT = "$HTTP_ACCEPT"
echo PATH_INFO = "$PATH_INFO"
echo PATH_TRANSLATED = "$PATH_TRANSLATED"
echo SCRIPT_NAME = "$SCRIPT_NAME"
echo QUERY_STRING = "$QUERY_STRING"
echo REMOTE_HOST = $REMOTE_HOST
echo REMOTE_ADDR = $REMOTE_ADDR
echo REMOTE_USER = $REMOTE_USER
echo AUTH_TYPE = $AUTH_TYPE
echo CONTENT_TYPE = $CONTENT_TYPE
echo CONTENT_LENGTH = $CONTENT_LENGTH
  Cut and past this file into your favorite editor and save it in your CGI directory as testcgi. Make sure to set the execute permissions on it (Unix).  
chmod +x testcgi
 

And an NT version which is not quit as good, but give you most of the information. I would appreciate an e-mail on how to get the argument count and all the arguments (like **argv in 'C') in a dos batch file.

 
@echo off

echo Content-type: text/plain 
echo.

echo CGI/1.0 test script report:
echo.

rem echo argc is %0
echo argv is %0
echo.

echo SERVER_SOFTWARE = %SERVER_SOFTWARE%
echo SERVER_NAME = %SERVER_NAME%
echo GATEWAY_INTERFACE = %GATEWAY_INTERFACE%
echo SERVER_PROTOCOL = %SERVER_PROTOCOL%
echo SERVER_PORT = %SERVER_PORT%
echo REQUEST_METHOD = %REQUEST_METHOD%
echo HTTP_ACCEPT = "%HTTP_ACCEPT%"
echo PATH_INFO = "%PATH_INFO%"
echo PATH_TRANSLATED = "%PATH_TRANSLATED%"
echo SCRIPT_NAME = "%SCRIPT_NAME%"
echo QUERY_STRING = "%QUERY_STRING%"
echo REMOTE_HOST = %REMOTE_HOST%
echo REMOTE_ADDR = %REMOTE_ADDR%
echo REMOTE_USER = %REMOTE_USER%
echo AUTH_TYPE = %AUTH_TYPE%
echo CONTENT_TYPE = %CONTENT_TYPE%
echo CONTENT_LENGTH = %CONTENT_LENGTH%
  NT users should save this file as testcgi.bat. Be sure to use the .bat extension on NT so that NT can make the association with the file extension and run it correctly.

Accessing the script

 
http://your_domain/cgi-bin/testcgi.bat

or

http://your_ip_address/cgi-bin/testcgi.bat
 

What you should see

If all goes well, your script should display something like.
 
CGI/1.0 test script report:

argc is 0. argv is .

SERVER_SOFTWARE = Apache/1.2.4
SERVER_NAME = www.jlk.net
GATEWAY_INTERFACE = CGI/1.1
SERVER_PROTOCOL = HTTP/1.0
SERVER_PORT = 80
REQUEST_METHOD = GET
HTTP_ACCEPT = image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*
PATH_INFO = 
PATH_TRANSLATED = 
SCRIPT_NAME = /cgi-bin/test-cgi
QUERY_STRING = 
REMOTE_HOST = 158.76.200.102
REMOTE_ADDR = 158.76.200.102
REMOTE_USER =
AUTH_TYPE =
CONTENT_TYPE =
CONTENT_LENGTH =
 

What can go wrong, and
what to do about it

 
Not Found

The requested URL /cgi-bin/testcgi was not found on this server.
  You have either:
  • typed in the incorrect URL
  • saved the file with a different name

    Check you typing and/or the file names in you CGI directory.

  • specified a different directory for CGIs
  • or CGIs are not configured

    Check your srm.conf configuration for ScriptAliases (see Getting Started with CGIs)
 
Internal Server Error

The server encountered an internal error or mis-configuration and 
was unable to complete your request.

Please contact the server administrator, webmaster@jlk.net and 
inform them of the time the error occurred, and anything you 
might have done that may have caused the error.
  It means, that someway, somehow, you have screwed up the above scripts. Every CGI script must return:
  1. One or more Header lines
    (i.e. Content-type: text/html)
  2. A blank line
  3. the body of the document (optional but recommended)
and when this error occurs, either 1 or 2 or both are missing.

Check you scripts!

 
Forbidden

You don't have permission to access /cgi-bin/test-cgi on this server.
  You have forgotten to set the execute bit on the script (Unix though you may be able to mess with the permissions on NT and get this error).  
# cd /var/apache/cgi-bin
# ls -l test-cgi
-rw-r--r--   1 103      20           757 Apr  8 14:12 test-cgi
# chmod +x test-cgi
# ls -l test-cgi
-rwxr-xr-x   1 103      20           757 Apr  8 14:12 test-cgi
#
  Once you have basic CGIs working, there are some more Apache configuration options which you can apply that may make your life easier while developing CGI scripts.  


Getting Started with CGI   RTFM Index   Debugging CGI
Copyright © 2006 Open Source Migrations ltd. All Rights Reserved.
Apache RTFM last updated Saturday October 21 2006