Class CgiHandler

  extended by sunlabs.brazil.handler.CgiHandler (view source)
All Implemented Interfaces:

public class CgiHandler
extends Object
implements Handler

Handler for implementing cgi/1.1 interface. This implementation allows either suffix matching (e.g. .cgi) to identify cgi scripts, or prefix matching (e.g. /bgi-bin). Defaults to "/". All output from the cgi script is buffered (e.g. chunked encoding is not supported).
NOTE: in versions of Java prior to release 1.3, the ability to set a working directory when running an external process is missing. This handler automatically checks for this ability and sets the proper working directory, but only if the underlying VM supports it.

The following request properties are used:

The document root for cgi files
The suffix for cgi files (defaults to .cgi)
The prefix for all cgi files (e.g. /cgi-bin)
"o(riginal)" or "c(urrent)". If an upstream handler has changed the URL, this specifes which url to look for the cgi script relative to. The default is to use the original url.
set to "true" to enable custom environment variables. If set, all server properties starting with this handler's prefix are placed into the environment with the name: CONFIG_name, where name is the property key, in upper case, with the prefix removed. This allows cgi scripts to be customized in the server's configuration file.
The command to use to run scripts. The absolute file path is added as the last parameter. If not specified, the file name is run as the command.
According to the CGI spec, cgi documents are to begin with properly formed http headers to specifie the type, return status and optionally other meta information about the request. if "noheaders" is specified, then the content is expected to *not* have any http headers, and the content type is as implied by the url suffix.
See example configuration in the samples included with the distribution.

Constructor Summary
Method Summary
 boolean init(Server server, String prefix)
          One time initialization.
 boolean respond(Request request)
          Dispatch and handle the CGI request.
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Detail


public CgiHandler()
Method Detail


public boolean init(Server server,
                    String prefix)
One time initialization. The handler configuration properties are extracted and set in respond(Request) to allow upstream handlers to modify the parameters.

Specified by:
init in interface Handler
server - The HTTP server that created this Handler. Typical Handlers will use Server.props to obtain run-time configuration information.
prefix - The handlers name. The string this Handler may prepend to all of the keys that it uses to extract configuration information from Server.props. This is set (by the Server and ChainHandler) to help avoid configuration parameter namespace collisions.
true if this Handler initialized successfully, false otherwise. If false is returned, this Handler should not be used.


public boolean respond(Request request)
Dispatch and handle the CGI request. Gets called on ALL requests. Set up the environment, exec the process, and deal appropriately with the input and output. In this implementation, all cgi script files must end with a standard suffix, although the suffix may omitted from the url. The url /main/do/me/too?a=b will look, starting in DocRoot, for main.cgi, main/do.cgi, etc until a matching file is found.

Input parameters examined in the request properties:

The suffix for all cgi scripts (defaults to .cgi)
The document root, for locating the script.

Specified by:
respond in interface Handler
request - The Request object that represents the HTTP request.
true if the request was handled. A request was handled if a response was supplied to the client, typically by calling Request.sendResponse() or Request.sendError.

Version Kenai-svn-r24, Generated 08/18/09
Copyright (c) 2001-2009, Sun Microsystems.